AppKit: Read CPTableView grid and row colours from the current theme

-initWithFrame: set the grid colour and the alternating row colours from
the theme, making them per-table values so a theme change did not replace
them. Encoding also wrote both colours for every table, and decoding set
them as per-table values.

Stop setting them in -initWithFrame:, and encode each colour only when
the table has a value of its own. CPOutlineView inherits this behaviour.
This commit is contained in:
David Richardson committed 2026-12-03 10:02:47 -06:00
1 parent ae075b03b8
commit bd51fb01f3
3 files changed
+71 -6

No files matched your search

+17 -4
View File
@@ -84,11 +84,20 @@ var CPTableViewDataSourceKey = @"CPTableViewDataSourceKey",
if (CGSizeEqualToSize(_intercellSpacing, CGSizeMakeZero()))
_intercellSpacing = CGSizeMake(3.0, 2.0);
[self setGridColor:[aCoder decodeObjectForKey:CPTableViewGridColorKey]];
/*
The archive holds the grid colour and the alternating row colours only when the
table had colours of its own. The encoder wrote the theme's colours for every
table, so a decoded table kept the colours of the theme it was archived under.
*/
if ([aCoder containsValueForKey:CPTableViewGridColorKey])
[self setGridColor:[aCoder decodeObjectForKey:CPTableViewGridColorKey]];
_gridStyleMask = [aCoder decodeIntForKey:CPTableViewGridStyleMaskKey];
_usesAlternatingRowBackgroundColors = [aCoder decodeObjectForKey:CPTableViewUsesAlternatingBackgroundKey];
[self setAlternatingRowBackgroundColors:[aCoder decodeObjectForKey:CPTableViewAlternatingRowColorsKey]];
if ([aCoder containsValueForKey:CPTableViewAlternatingRowColorsKey])
[self setAlternatingRowBackgroundColors:[aCoder decodeObjectForKey:CPTableViewAlternatingRowColorsKey]];
_headerView = [aCoder decodeObjectForKey:CPTableViewHeaderViewKey];
_cornerView = [aCoder decodeObjectForKey:CPTableViewCornerViewKey];
@@ -134,11 +143,15 @@ var CPTableViewDataSourceKey = @"CPTableViewDataSourceKey",
[aCoder encodeObject:_tableColumns forKey:CPTableViewTableColumnsKey];
[aCoder encodeObject:[self gridColor] forKey:CPTableViewGridColorKey];
if ([self _hasOwnValueForThemeAttribute:@"grid-color"])
[aCoder encodeObject:[self gridColor] forKey:CPTableViewGridColorKey];
[aCoder encodeInt:_gridStyleMask forKey:CPTableViewGridStyleMaskKey];
[aCoder encodeBool:_usesAlternatingRowBackgroundColors forKey:CPTableViewUsesAlternatingBackgroundKey];
[aCoder encodeObject:[self alternatingRowBackgroundColors] forKey:CPTableViewAlternatingRowColorsKey];
if ([self _hasOwnValueForThemeAttribute:@"alternating-row-colors"])
[aCoder encodeObject:[self alternatingRowBackgroundColors] forKey:CPTableViewAlternatingRowColorsKey];
[aCoder encodeObject:_cornerView forKey:CPTableViewCornerViewKey];
[aCoder encodeObject:_headerView forKey:CPTableViewHeaderViewKey];
+6 -2
View File
@@ -393,8 +393,13 @@ CPTableViewFirstColumnOnlyAutoresizingStyle = 5;
_selectionHighlightStyle = CPTableViewSelectionHighlightStyleRegular;
[self setBackgroundColor:nil];
/*
The alternating row colours and the grid colour are theme values. They were set
here from the theme, which made them values of the table's own, so after a theme
change the table kept the colours of the theme it was made under. They are read
from the current theme until the application sets them.
*/
[self setUsesAlternatingRowBackgroundColors:NO];
[self setAlternatingRowBackgroundColors:[self valueForThemeAttribute:@"alternating-row-colors"]];
_tableColumns = [];
_tableColumnRanges = [];
@@ -404,7 +409,6 @@ CPTableViewFirstColumnOnlyAutoresizingStyle = 5;
_intercellSpacing = CGSizeMake(3.0, 2.0);
_rowHeight = [self valueForThemeAttribute:@"default-row-height"];
[self setGridColor:[self valueForThemeAttribute:@"grid-color"]];
[self setGridStyleMask:CPTableViewGridNone];
[self setHeaderView:[[CPTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, [self bounds].size.width, [self valueForThemeAttribute:@"header-view-height"])]];
+48
View File
@@ -1,6 +1,7 @@
@import <AppKit/AppKit.j>
@import "CPNotificationCenterHelper.j"
@import "BuiltThemes.j"
@implementation CPTableViewTest : OJTestCase
{
@@ -742,6 +743,53 @@
[self assert:enumerateViewsInRowsCall equals:4]; // Only 4 views visible
}
/*
The grid colour and the alternating row colours are theme values. A table that has no
colours of its own takes them from its current theme, also after a theme change and
after archiving.
*/
- (void)testATableWithoutColoursOfItsOwnTakesThemFromItsTheme
{
var theme = BuiltThemeWithValue(@"Aristo3", CPTableView, @"grid-color", [CPColor redColor]);
[self assertNotNull:theme message:@"The built blends hold no Aristo3 theme. Build the themes first."];
[tableView setTheme:theme];
[self assert:[[CPColor redColor] cssString] equals:[[tableView gridColor] cssString] message:@"The grid colour follows the theme change"];
[self assertFalse:[tableView _hasOwnValueForThemeAttribute:@"grid-color"] message:@"A new table has no grid colour of its own"];
[self assertFalse:[tableView _hasOwnValueForThemeAttribute:@"alternating-row-colors"] message:@"A new table has no row colours of its own"];
var decoded = [CPKeyedUnarchiver unarchiveObjectWithData:[CPKeyedArchiver archivedDataWithRootObject:tableView]];
[self assertFalse:[decoded _hasOwnValueForThemeAttribute:@"grid-color"] message:@"A decoded table has no grid colour of its own"];
[self assertFalse:[decoded _hasOwnValueForThemeAttribute:@"alternating-row-colors"] message:@"A decoded table has no row colours of its own"];
}
/*
Colours that the application sets are the table's own. They stay through a theme change
and archiving.
*/
- (void)testColoursSetByTheApplicationStayThroughAThemeChangeAndArchiving
{
var theme = BuiltThemeWithValue(@"Aristo3", CPTableView, @"grid-color", [CPColor redColor]),
rowColors = [[CPColor whiteColor], [CPColor yellowColor]];
[self assertNotNull:theme message:@"The built blends hold no Aristo3 theme. Build the themes first."];
[tableView setGridColor:[CPColor greenColor]];
[tableView setAlternatingRowBackgroundColors:rowColors];
[tableView setTheme:theme];
[self assert:[[CPColor greenColor] cssString] equals:[[tableView gridColor] cssString] message:@"The grid colour stays through the theme change"];
var decoded = [CPKeyedUnarchiver unarchiveObjectWithData:[CPKeyedArchiver archivedDataWithRootObject:tableView]];
[self assert:[[CPColor greenColor] cssString] equals:[[decoded gridColor] cssString] message:@"The grid colour stays through archiving"];
[self assert:[[CPColor yellowColor] cssString] equals:[[decoded alternatingRowBackgroundColors][1] cssString] message:@"The row colours stay through archiving"];
}
@end
@implementation FirstResponderConfigurableTableView : CPTableView