mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-12-03 19:44:22 +00:00
AppKit: Inherit view themes and resolve colours against the drawing view
Previously, a view took a copy of the active theme on initialization or decoding and kept it, while theme colours, system colours, and system images resolved against the active theme rather than the theme of the view being laid out or drawn. An explicit theme set on a view did not propagate to subviews and was overwritten on a theme switch. A view now stores an explicit theme when set and otherwise inherits its superview's theme, or the active theme when it has no superview; calling -setTheme: with nil restores inheritance. Adding or removing subviews and walking the view hierarchy on a theme switch apply the inherited theme, while explicit themes remain intact across switches. Views make their theme current along with their appearance during layout and drawing so colours and images resolve against it. CPComboBox and the convenience constructors +buttonWithTitle:, +textFieldWithStringValue:placeholder:width:, and +roundedTextFieldWithStringValue:placeholder:width: no longer assign the active theme explicitly, which would override inheritance.
This commit is contained in:
1 parent
d7c8f71744
commit
f549eb39ee
15 files changed
+361
-99
No files matched your search
+13
-2
@@ -149,16 +149,27 @@ CPButtonImageOffset = 3.0;
|
||||
return [super _binderClassForBinding:aBinding];
|
||||
}
|
||||
|
||||
/*
|
||||
2026-12-01: this method passed the active theme to +buttonWithTitle:theme:, which sets
|
||||
it on the button. A theme set on a view is kept, not inherited, so the button would keep
|
||||
the theme in force when it was made through every later theme switch. The button now
|
||||
inherits its theme.
|
||||
*/
|
||||
+ (id)buttonWithTitle:(CPString)aTitle
|
||||
{
|
||||
return [self buttonWithTitle:aTitle theme:[CPTheme defaultTheme]];
|
||||
return [self buttonWithTitle:aTitle theme:nil];
|
||||
}
|
||||
|
||||
/*
|
||||
Returns a button with aTitle, sized to fit. A non-nil aTheme is set on the button; with
|
||||
nil, the button inherits its theme.
|
||||
*/
|
||||
+ (id)buttonWithTitle:(CPString)aTitle theme:(CPTheme)aTheme
|
||||
{
|
||||
var button = [[self alloc] init];
|
||||
|
||||
[button setTheme:aTheme];
|
||||
if (aTheme)
|
||||
[button setTheme:aTheme];
|
||||
[button setTitle:aTitle];
|
||||
[button sizeToFit];
|
||||
|
||||
|
||||
+43
-28
@@ -311,8 +311,10 @@ function _CPThemeAttributeForSystemColorName(aName)
|
||||
|
||||
/*
|
||||
Returns the value of the system colour aName for anAppearance, in two steps. First the
|
||||
current theme's value: the value for the closest appearance, then the value without a state
|
||||
(see _CPThemeAttributeValueForAppearance). A value without a state is a value for every
|
||||
value of the current drawing theme (see _CPThemeCurrentDrawing()): the value for the
|
||||
closest appearance, then the value without a state (see
|
||||
_CPThemeAttributeValueForAppearance). A value without
|
||||
a state is a value for every
|
||||
appearance, as everywhere in CPTheme, so it comes before the built-in value. Then the
|
||||
built-in value (see _CPSystemColorBuiltInValue).
|
||||
|
||||
@@ -321,7 +323,7 @@ function _CPThemeAttributeForSystemColorName(aName)
|
||||
*/
|
||||
function _CPSystemColorValue(aName, anAppearance)
|
||||
{
|
||||
var attribute = [[CPTheme defaultTheme] attributeWithName:_CPThemeAttributeForSystemColorName(aName) forClass:[CPColor defaultThemeClass]],
|
||||
var attribute = [_CPThemeCurrentDrawing() attributeWithName:_CPThemeAttributeForSystemColorName(aName) forClass:[CPColor defaultThemeClass]],
|
||||
themeValue = _CPThemeAttributeValueForAppearance(attribute, anAppearance);
|
||||
|
||||
if (themeValue)
|
||||
@@ -332,35 +334,24 @@ function _CPSystemColorValue(aName, anAppearance)
|
||||
|
||||
/*
|
||||
Returns the value of the theme colour aName for anAppearance. The attribute "theme-colors"
|
||||
of the current theme holds, for each appearance state, a dictionary from colour name to
|
||||
colour. The lookup takes the dictionaries in the order of -bestMatchFromAppearancesWithNames:
|
||||
for anAppearance and uses the first one that holds aName, then the dictionary for every
|
||||
appearance. A name that no dictionary holds is a defect of the theme: it logs one warning
|
||||
and gives the clear colour.
|
||||
of a theme holds, for each appearance state, a dictionary from colour name to colour. The
|
||||
lookup asks the current drawing theme (see _CPThemeCurrentDrawing()). It takes the
|
||||
dictionaries in the order of -bestMatchFromAppearancesWithNames: for anAppearance and uses
|
||||
the first one that holds aName, then the dictionary for every appearance. A name that the
|
||||
theme does not hold is a defect of the theme: it logs one warning and gives the clear
|
||||
colour.
|
||||
|
||||
2026-11-30: the lookup asked only the default theme, so a view whose theme was set
|
||||
explicitly drew with the default theme's colours, or with the clear colour when the
|
||||
default theme lacked the name. The theme colours ignored the view's theme.
|
||||
*/
|
||||
function _CPThemeColorValue(aName, anAppearance)
|
||||
{
|
||||
var theme = [CPTheme defaultTheme],
|
||||
values = [[theme attributeWithName:@"theme-colors" forClass:[CPColor defaultThemeClass]] values];
|
||||
var theme = _CPThemeCurrentDrawing(),
|
||||
value = _CPThemeColorValueInTheme(theme, aName, anAppearance);
|
||||
|
||||
if (values)
|
||||
{
|
||||
var matchingNames = _CPAppearanceMatchingNames([anAppearance name]);
|
||||
|
||||
for (var i = 0, count = matchingNames.length; i < count; i++)
|
||||
{
|
||||
var state = _CPThemeStateForAppearanceName(matchingNames[i]),
|
||||
value = state ? [[values objectForKey:String(state)] objectForKey:aName] : nil;
|
||||
|
||||
if (value)
|
||||
return value;
|
||||
}
|
||||
|
||||
var normalValue = [[values objectForKey:String(CPThemeStateNormal)] objectForKey:aName];
|
||||
|
||||
if (normalValue)
|
||||
return normalValue;
|
||||
}
|
||||
if (value)
|
||||
return value;
|
||||
|
||||
if (!_CPThemeColorMissingNames.hasOwnProperty(aName))
|
||||
{
|
||||
@@ -371,6 +362,30 @@ function _CPThemeColorValue(aName, anAppearance)
|
||||
return [CPColor clearColor];
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the value that aTheme itself gives the theme colour aName for anAppearance, or nil.
|
||||
*/
|
||||
function _CPThemeColorValueInTheme(aTheme, aName, anAppearance)
|
||||
{
|
||||
var values = [[aTheme attributeWithName:@"theme-colors" forClass:[CPColor defaultThemeClass]] values];
|
||||
|
||||
if (!values)
|
||||
return nil;
|
||||
|
||||
var matchingNames = _CPAppearanceMatchingNames([anAppearance name]);
|
||||
|
||||
for (var i = 0, count = matchingNames.length; i < count; i++)
|
||||
{
|
||||
var state = _CPThemeStateForAppearanceName(matchingNames[i]),
|
||||
value = state ? [[values objectForKey:String(state)] objectForKey:aName] : nil;
|
||||
|
||||
if (value)
|
||||
return value;
|
||||
}
|
||||
|
||||
return [[values objectForKey:String(CPThemeStateNormal)] objectForKey:aName];
|
||||
}
|
||||
|
||||
/// @endcond
|
||||
|
||||
@class _CPCatalogColor
|
||||
|
||||
+6
-1
@@ -126,7 +126,12 @@ var CPComboBoxTextSubview = @"text",
|
||||
_selectedStringValue = @"";
|
||||
_popUpButtonCausedResign = NO;
|
||||
|
||||
[self setTheme:[CPTheme defaultTheme]];
|
||||
/*
|
||||
2026-12-01: this method set the active theme on the receiver, which already had it. A
|
||||
theme set on a view is now kept, not inherited, so the call would fix the combo box to
|
||||
the theme in force when it was made, through every later theme switch. The receiver
|
||||
inherits its theme.
|
||||
*/
|
||||
[self setBordered:YES];
|
||||
[self setBezeled:YES];
|
||||
[self setEditable:YES];
|
||||
|
||||
+5
-4
@@ -289,13 +289,14 @@ function _CPThemeAttributeForSystemImageName(aName)
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the image for the system image aName in anAppearance: the current theme's value for
|
||||
anAppearance (see _CPThemeAttributeValueForAppearance), then the default image in
|
||||
_CPSystemImageDefaults, or nil when the system image has no default.
|
||||
Returns the image for the system image aName in anAppearance: the value for anAppearance
|
||||
(see _CPThemeAttributeValueForAppearance) of the current drawing theme (see
|
||||
_CPThemeCurrentDrawing()); then the default image in _CPSystemImageDefaults, or nil when
|
||||
the system image has no default.
|
||||
*/
|
||||
function _CPSystemImageValue(aName, anAppearance)
|
||||
{
|
||||
var attribute = [[CPTheme defaultTheme] attributeWithName:_CPThemeAttributeForSystemImageName(aName) forClass:[CPImage defaultThemeClass]],
|
||||
var attribute = [_CPThemeCurrentDrawing() attributeWithName:_CPThemeAttributeForSystemImageName(aName) forClass:[CPImage defaultThemeClass]],
|
||||
themeValue = _CPThemeAttributeValueForAppearance(attribute, anAppearance);
|
||||
|
||||
if (themeValue)
|
||||
|
||||
@@ -220,7 +220,7 @@ function _CPImageViewRecordsAreSame(aRecord, anotherRecord)
|
||||
*/
|
||||
- (void)_applyImage
|
||||
{
|
||||
var previousAppearance = _CPAppearanceBeginDrawing([self effectiveAppearance]);
|
||||
var previousDrawingValues = _CPViewBeginDrawing(self);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -228,7 +228,7 @@ function _CPImageViewRecordsAreSame(aRecord, anotherRecord)
|
||||
}
|
||||
finally
|
||||
{
|
||||
_CPAppearanceEndDrawing(previousAppearance);
|
||||
_CPViewEndDrawing(previousDrawingValues);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -298,7 +298,7 @@ function _CPImageViewRecordsAreSame(aRecord, anotherRecord)
|
||||
- (void)imageDidLoad:(CPNotification)aNotification
|
||||
{
|
||||
var image = [self objectValue],
|
||||
previousAppearance = _CPAppearanceBeginDrawing([self effectiveAppearance]);
|
||||
previousDrawingValues = _CPViewBeginDrawing(self);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -315,7 +315,7 @@ function _CPImageViewRecordsAreSame(aRecord, anotherRecord)
|
||||
}
|
||||
finally
|
||||
{
|
||||
_CPAppearanceEndDrawing(previousAppearance);
|
||||
_CPViewEndDrawing(previousDrawingValues);
|
||||
}
|
||||
|
||||
[[CPNotificationCenter defaultCenter] removeObserver:self name:CPImageDidLoadNotification object:image];
|
||||
|
||||
@@ -693,11 +693,18 @@ _CPMenuWindowAttachedMenuBackgroundStyle = 2;
|
||||
|
||||
- (void)_setThemeIncludingDescendants:(CPTheme)aTheme
|
||||
{
|
||||
[self setTheme:aTheme];
|
||||
[_menuItemViews makeObjectsPerformSelector:@selector(_setThemeIncludingDescendants:) withObject:aTheme];
|
||||
[super _setThemeIncludingDescendants:aTheme];
|
||||
|
||||
/*
|
||||
The menu item views are not always subviews of the receiver, so they are visited here
|
||||
with the theme they would inherit from the receiver.
|
||||
*/
|
||||
var theme = [self theme];
|
||||
|
||||
[_menuItemViews makeObjectsPerformSelector:@selector(_setThemeIncludingDescendants:) withObject:theme];
|
||||
|
||||
for (var i = 0, items = [[self menu] itemArray], count = [items count]; i < count; i++)
|
||||
[[items[i] _menuItemView] _setThemeIncludingDescendants:aTheme];
|
||||
[[items[i] _menuItemView] _setThemeIncludingDescendants:theme];
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -438,20 +438,6 @@
|
||||
|
||||
// MARK: -
|
||||
|
||||
@implementation _CPMenuItemStandardView (CSSTheming)
|
||||
|
||||
// MARK: Override
|
||||
|
||||
- (void)_setThemeIncludingDescendants:(CPTheme)aTheme
|
||||
{
|
||||
[self setTheme:aTheme];
|
||||
[[self subviews] makeObjectsPerformSelector:@selector(_setThemeIncludingDescendants:) withObject:aTheme];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
// MARK: -
|
||||
|
||||
@implementation _CPMenuItemSubmenuIndicatorView : CPView
|
||||
{
|
||||
CPColor _color;
|
||||
|
||||
@@ -282,14 +282,18 @@
|
||||
|
||||
- (void)_setThemeIncludingDescendants:(CPTheme)aTheme
|
||||
{
|
||||
[self setTheme:aTheme];
|
||||
[_view _setThemeIncludingDescendants:aTheme];
|
||||
[super _setThemeIncludingDescendants:aTheme];
|
||||
|
||||
// Items must also perform this (without this, only the selected item does)
|
||||
/*
|
||||
These views are not always subviews of the receiver, so they are visited here with the
|
||||
theme they would inherit from the receiver.
|
||||
*/
|
||||
var theme = [self theme];
|
||||
|
||||
[_imageAndTextView _setThemeIncludingDescendants:aTheme];
|
||||
[_submenuView _setThemeIncludingDescendants:aTheme];
|
||||
[[_menuItem view] _setThemeIncludingDescendants:aTheme];
|
||||
[_view _setThemeIncludingDescendants:theme];
|
||||
[_imageAndTextView _setThemeIncludingDescendants:theme];
|
||||
[_submenuView _setThemeIncludingDescendants:theme];
|
||||
[[_menuItem view] _setThemeIncludingDescendants:theme];
|
||||
|
||||
if ([_view respondsToSelector:@selector(update)])
|
||||
[_view update];
|
||||
|
||||
@@ -965,7 +965,7 @@ var CPAutosavedRecentsChangedNotification = @"CPAutosavedRecentsChangedNotificat
|
||||
*/
|
||||
- (void)_layoutSubviews
|
||||
{
|
||||
var previousAppearance = _CPAppearanceBeginDrawing([self effectiveAppearance]);
|
||||
var previousDrawingValues = _CPViewBeginDrawing(self);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -996,7 +996,7 @@ var CPAutosavedRecentsChangedNotification = @"CPAutosavedRecentsChangedNotificat
|
||||
}
|
||||
finally
|
||||
{
|
||||
_CPAppearanceEndDrawing(previousAppearance);
|
||||
_CPViewEndDrawing(previousDrawingValues);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+7
-4
@@ -901,14 +901,17 @@ var CPTabViewItemsKey = "CPTabViewItemsKey",
|
||||
|
||||
- (void)_setThemeIncludingDescendants:(CPTheme)aTheme
|
||||
{
|
||||
[self setTheme:aTheme];
|
||||
[[self subviews] makeObjectsPerformSelector:@selector(_setThemeIncludingDescendants:) withObject:aTheme];
|
||||
[super _setThemeIncludingDescendants:aTheme];
|
||||
|
||||
// Items must also perform this (without this, only the selected item does)
|
||||
/*
|
||||
The views of the items that are not selected are outside the hierarchy, so they are
|
||||
visited here with the theme they would inherit from the receiver.
|
||||
*/
|
||||
var theme = [self theme];
|
||||
|
||||
for (var i = 0, allItems = [self items], count = allItems.length; i < count; i++)
|
||||
if (allItems[i] != _selectedTabViewItem)
|
||||
[[allItems[i] view] _setThemeIncludingDescendants:aTheme];
|
||||
[[allItems[i] view] _setThemeIncludingDescendants:theme];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
+19
-8
@@ -190,17 +190,26 @@ CPTextFieldStatePlaceholder = CPThemeState("placeholder");
|
||||
return [super _binderClassForBinding:aBinding];
|
||||
}
|
||||
|
||||
/*
|
||||
2026-12-01: the constructors without a theme passed the active theme to the ones with a
|
||||
theme, which set it on the field. A theme set on a view is kept, not inherited, so the
|
||||
field would keep the theme in force when it was made through every later theme switch.
|
||||
They now pass nil, and a field made without a theme inherits its theme. A non-nil theme is
|
||||
set on the field.
|
||||
*/
|
||||
+ (CPTextField)textFieldWithStringValue:(CPString)aStringValue placeholder:(CPString)aPlaceholder width:(float)aWidth
|
||||
{
|
||||
return [self textFieldWithStringValue:aStringValue placeholder:aPlaceholder width:aWidth theme:[CPTheme defaultTheme]];
|
||||
return [self textFieldWithStringValue:aStringValue placeholder:aPlaceholder width:aWidth theme:nil];
|
||||
}
|
||||
|
||||
+ (CPTextField)textFieldWithStringValue:(CPString)aStringValue placeholder:(CPString)aPlaceholder width:(float)aWidth theme:(CPTheme)aTheme
|
||||
{
|
||||
var minSize = aTheme ? [aTheme valueForAttributeWithName:@"min-size" forClass:CPTextField] : CGSizeMake(0,0),
|
||||
var theme = aTheme || [CPTheme defaultTheme],
|
||||
minSize = theme ? [theme valueForAttributeWithName:@"min-size" forClass:CPTextField] : CGSizeMake(0,0),
|
||||
textField = [[self alloc] initWithFrame:CGRectMake(0.0, 0.0, aWidth, minSize.height)];
|
||||
|
||||
[textField setTheme:aTheme];
|
||||
if (aTheme)
|
||||
[textField setTheme:aTheme];
|
||||
[textField setStringValue:aStringValue];
|
||||
[textField setPlaceholderString:aPlaceholder];
|
||||
[textField setBordered:YES];
|
||||
@@ -214,15 +223,17 @@ CPTextFieldStatePlaceholder = CPThemeState("placeholder");
|
||||
|
||||
+ (CPTextField)roundedTextFieldWithStringValue:(CPString)aStringValue placeholder:(CPString)aPlaceholder width:(float)aWidth
|
||||
{
|
||||
return [self roundedTextFieldWithStringValue:aStringValue placeholder:aPlaceholder width:aWidth theme:[CPTheme defaultTheme]];
|
||||
return [self roundedTextFieldWithStringValue:aStringValue placeholder:aPlaceholder width:aWidth theme:nil];
|
||||
}
|
||||
|
||||
+ (CPTextField)roundedTextFieldWithStringValue:(CPString)aStringValue placeholder:(CPString)aPlaceholder width:(float)aWidth theme:(CPTheme)aTheme
|
||||
{
|
||||
var minSize = aTheme ? [aTheme valueForAttributeWithName:@"min-size" forClass:CPTextField] : CGSizeMake(0,0),
|
||||
var theme = aTheme || [CPTheme defaultTheme],
|
||||
minSize = theme ? [theme valueForAttributeWithName:@"min-size" forClass:CPTextField] : CGSizeMake(0,0),
|
||||
textField = [[self alloc] initWithFrame:CGRectMake(0.0, 0.0, aWidth, minSize.height)];
|
||||
|
||||
[textField setTheme:aTheme];
|
||||
if (aTheme)
|
||||
[textField setTheme:aTheme];
|
||||
[textField setStringValue:aStringValue];
|
||||
[textField setPlaceholderString:aPlaceholder];
|
||||
[textField setBezelStyle:CPTextFieldRoundedBezel];
|
||||
@@ -820,7 +831,7 @@ CPTextFieldStatePlaceholder = CPThemeState("placeholder");
|
||||
*/
|
||||
- (CPString)_inputElementTextColorCSSString
|
||||
{
|
||||
var previousAppearance = _CPAppearanceBeginDrawing([self effectiveAppearance]);
|
||||
var previousDrawingValues = _CPViewBeginDrawing(self);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -828,7 +839,7 @@ CPTextFieldStatePlaceholder = CPThemeState("placeholder");
|
||||
}
|
||||
finally
|
||||
{
|
||||
_CPAppearanceEndDrawing(previousAppearance);
|
||||
_CPViewEndDrawing(previousDrawingValues);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+34
-1
@@ -37,7 +37,40 @@
|
||||
|
||||
var CPThemesByName = { },
|
||||
CPThemeDefaultTheme = nil,
|
||||
CPThemeDefaultHudTheme = nil;
|
||||
CPThemeDefaultHudTheme = nil,
|
||||
CPThemeCurrentDrawing = nil;
|
||||
|
||||
/*
|
||||
Returns the theme that resolves theme colours, system colours and system images: the
|
||||
theme of the view that lays out or draws or, with no such view, the default theme. A view makes its
|
||||
theme current together with its effective appearance (see _CPViewBeginDrawing()), as
|
||||
AppKit makes a view's appearance the current drawing appearance.
|
||||
*/
|
||||
function _CPThemeCurrentDrawing()
|
||||
{
|
||||
return CPThemeCurrentDrawing || CPThemeDefaultTheme;
|
||||
}
|
||||
|
||||
/*
|
||||
Makes aTheme the current drawing theme and returns the previous value of the slot, nil
|
||||
included, for _CPThemeEndDrawing(). Calls nest in any order.
|
||||
*/
|
||||
function _CPThemeBeginDrawing(aTheme)
|
||||
{
|
||||
var previous = CPThemeCurrentDrawing;
|
||||
|
||||
CPThemeCurrentDrawing = aTheme;
|
||||
|
||||
return previous;
|
||||
}
|
||||
|
||||
/*
|
||||
Restores the value that _CPThemeBeginDrawing() returned.
|
||||
*/
|
||||
function _CPThemeEndDrawing(aPreviousValue)
|
||||
{
|
||||
CPThemeCurrentDrawing = aPreviousValue;
|
||||
}
|
||||
|
||||
/*
|
||||
Posted by +setDefaultTheme:. The appearance manager observes it, because the default theme
|
||||
|
||||
+103
-17
@@ -137,6 +137,26 @@ var CPViewFlags = { },
|
||||
|
||||
var CPViewHighDPIDrawingEnabled = YES;
|
||||
|
||||
/*
|
||||
Makes aView's effective appearance the current drawing appearance and its theme the
|
||||
current drawing theme while it lays out, draws or resolves a colour, and returns the
|
||||
previous values for _CPViewEndDrawing(). Colours and images resolve under both: the
|
||||
appearance selects the variant, the theme gives the values (see _CPThemeCurrentDrawing()).
|
||||
*/
|
||||
function _CPViewBeginDrawing(aView)
|
||||
{
|
||||
return [_CPAppearanceBeginDrawing([aView effectiveAppearance]), _CPThemeBeginDrawing([aView theme])];
|
||||
}
|
||||
|
||||
/*
|
||||
Restores the values that _CPViewBeginDrawing() returned.
|
||||
*/
|
||||
function _CPViewEndDrawing(aPreviousValues)
|
||||
{
|
||||
_CPThemeEndDrawing(aPreviousValues[1]);
|
||||
_CPAppearanceEndDrawing(aPreviousValues[0]);
|
||||
}
|
||||
|
||||
/*
|
||||
Returns YES when the resolved colours aColor and anotherColor give the same output, so that
|
||||
a setter can skip its DOM write. -[CPColor isEqual:] compares components and pattern images
|
||||
@@ -272,6 +292,7 @@ function _CPViewResolvedColorsAreSame(aColor, anotherColor)
|
||||
BOOL _allowsVibrancy @accessors(property=allowsVibrancy);
|
||||
CPAppearance _appearance @accessors(getter=appearance);
|
||||
CPAppearance _appliedAppearance;
|
||||
CPTheme _explicitTheme;
|
||||
|
||||
CPMutableArray _trackingAreas @accessors(getter=trackingAreas, copy);
|
||||
|
||||
@@ -731,6 +752,7 @@ function _CPViewResolvedColorsAreSame(aColor, anotherColor)
|
||||
[aSubview _setWindow:nil];
|
||||
|
||||
[aSubview _recomputeAppearanceWithSuperviewEffectiveAppearance:[self effectiveAppearance]];
|
||||
[aSubview _recomputeThemeWithInheritedTheme:[self theme]];
|
||||
|
||||
if (isNewAddOrMove)
|
||||
[aSubview _postViewDidAppearNotification];
|
||||
@@ -777,6 +799,7 @@ function _CPViewResolvedColorsAreSame(aColor, anotherColor)
|
||||
{
|
||||
[self _setWindow:nil];
|
||||
[self _recomputeAppearanceWithSuperviewEffectiveAppearance:[self _inheritedEffectiveAppearance]];
|
||||
[self _recomputeThemeWithInheritedTheme:[self _inheritedTheme]];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2123,7 +2146,7 @@ function _CPViewResolvedColorsAreSame(aColor, anotherColor)
|
||||
if (![aResolvedColor isCSSBased] || !_CPCSSObjectIsAppearanceDependent(aResolvedColor))
|
||||
return nil;
|
||||
|
||||
var previousAppearance = _CPAppearanceBeginDrawing([self effectiveAppearance]);
|
||||
var previousDrawingValues = _CPViewBeginDrawing(self);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -2131,7 +2154,7 @@ function _CPViewResolvedColorsAreSame(aColor, anotherColor)
|
||||
}
|
||||
finally
|
||||
{
|
||||
_CPAppearanceEndDrawing(previousAppearance);
|
||||
_CPViewEndDrawing(previousDrawingValues);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2147,7 +2170,7 @@ function _CPViewResolvedColorsAreSame(aColor, anotherColor)
|
||||
if (![aColor isKindOfClass:_CPDynamicColor])
|
||||
return aColor;
|
||||
|
||||
var previousAppearance = _CPAppearanceBeginDrawing([self effectiveAppearance]);
|
||||
var previousDrawingValues = _CPViewBeginDrawing(self);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -2155,7 +2178,7 @@ function _CPViewResolvedColorsAreSame(aColor, anotherColor)
|
||||
}
|
||||
finally
|
||||
{
|
||||
_CPAppearanceEndDrawing(previousAppearance);
|
||||
_CPViewEndDrawing(previousDrawingValues);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2278,7 +2301,7 @@ function _CPViewResolvedColorsAreSame(aColor, anotherColor)
|
||||
named colour inside the CSS dictionaries then took the application's value. The
|
||||
write now runs in the receiver's effective appearance.
|
||||
*/
|
||||
var previousAppearance = _CPAppearanceBeginDrawing([self effectiveAppearance]);
|
||||
var previousDrawingValues = _CPViewBeginDrawing(self);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -2289,7 +2312,7 @@ function _CPViewResolvedColorsAreSame(aColor, anotherColor)
|
||||
}
|
||||
finally
|
||||
{
|
||||
_CPAppearanceEndDrawing(previousAppearance);
|
||||
_CPViewEndDrawing(previousDrawingValues);
|
||||
}
|
||||
}
|
||||
else if (_backgroundType === BackgroundTrivialColor || _backgroundType === BackgroundTransparentColor)
|
||||
@@ -2922,7 +2945,7 @@ setBoundsOrigin:
|
||||
A view draws with its effective appearance as the current drawing appearance, so that
|
||||
dynamic colours and images resolve for this view.
|
||||
*/
|
||||
var previousAppearance = _CPAppearanceBeginDrawing([self effectiveAppearance]);
|
||||
var previousDrawingValues = _CPViewBeginDrawing(self);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -2932,7 +2955,7 @@ setBoundsOrigin:
|
||||
}
|
||||
finally
|
||||
{
|
||||
_CPAppearanceEndDrawing(previousAppearance);
|
||||
_CPViewEndDrawing(previousDrawingValues);
|
||||
}
|
||||
|
||||
_dirtyRect = NULL;
|
||||
@@ -3050,7 +3073,7 @@ setBoundsOrigin:
|
||||
A view lays out with its effective appearance as the current drawing appearance, so
|
||||
that dynamic colours and images resolve for this view.
|
||||
*/
|
||||
var previousAppearance = _CPAppearanceBeginDrawing([self effectiveAppearance]);
|
||||
var previousDrawingValues = _CPViewBeginDrawing(self);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -3064,7 +3087,7 @@ setBoundsOrigin:
|
||||
}
|
||||
finally
|
||||
{
|
||||
_CPAppearanceEndDrawing(previousAppearance);
|
||||
_CPViewEndDrawing(previousDrawingValues);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3076,7 +3099,7 @@ setBoundsOrigin:
|
||||
*/
|
||||
- (void)_layoutSubviewsInEffectiveAppearance
|
||||
{
|
||||
var previousAppearance = _CPAppearanceBeginDrawing([self effectiveAppearance]);
|
||||
var previousDrawingValues = _CPViewBeginDrawing(self);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -3084,7 +3107,7 @@ setBoundsOrigin:
|
||||
}
|
||||
finally
|
||||
{
|
||||
_CPAppearanceEndDrawing(previousAppearance);
|
||||
_CPViewEndDrawing(previousDrawingValues);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3717,10 +3740,69 @@ setBoundsOrigin:
|
||||
|
||||
// MARK: Theme Attributes
|
||||
|
||||
/*
|
||||
Sets the receiver's theme. nil makes the receiver inherit its theme: a view's theme is the
|
||||
theme set on it, else its superview's theme, else the application's active theme. The
|
||||
receiver and every subview that inherits from it apply the new theme at once.
|
||||
|
||||
2026-12-01: a view took a copy of the active theme when it was made or decoded and kept
|
||||
it, so its theme depended on when it was created, not on where it sits. A theme set on a
|
||||
view did not reach its subviews, and colours that a view passes to its subviews, such as a
|
||||
split view's divider colour, resolved against the subviews' copies. The theme is now
|
||||
inherited down the hierarchy, as the effective appearance is.
|
||||
*/
|
||||
- (void)setTheme:(CPTheme)aTheme
|
||||
{
|
||||
if (_explicitTheme === aTheme)
|
||||
return;
|
||||
|
||||
_explicitTheme = aTheme;
|
||||
|
||||
[self _recomputeThemeWithInheritedTheme:[self _inheritedTheme]];
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the theme the receiver inherits: its superview's theme, else the application's
|
||||
active theme.
|
||||
*/
|
||||
- (CPTheme)_inheritedTheme
|
||||
{
|
||||
return _superview ? [_superview theme] : [CPTheme defaultTheme];
|
||||
}
|
||||
|
||||
/*
|
||||
Applies the theme set on the receiver, else anInheritedTheme, and then visits every
|
||||
subview. A view whose theme changes re-reads its theme attributes and is marked for
|
||||
layout and display (see -objectDidChangeTheme).
|
||||
*/
|
||||
- (void)_recomputeThemeWithInheritedTheme:(CPTheme)anInheritedTheme
|
||||
{
|
||||
var theme = _explicitTheme || anInheritedTheme;
|
||||
|
||||
if (_theme !== theme)
|
||||
{
|
||||
_theme = theme;
|
||||
|
||||
[self objectDidChangeTheme];
|
||||
}
|
||||
|
||||
var subviews = [_subviews copy];
|
||||
|
||||
for (var i = 0, count = subviews.length; i < count; i++)
|
||||
[subviews[i] _recomputeThemeWithInheritedTheme:theme];
|
||||
}
|
||||
|
||||
/*
|
||||
Applies aTheme as the theme the receiver inherits, and visits every subview. A theme
|
||||
switch calls this on each window's views with the new active theme. A view whose theme was
|
||||
set explicitly keeps it.
|
||||
|
||||
2026-12-01: this method set aTheme on every view as if it had been set explicitly, so a
|
||||
theme set on a view or a subtree was lost at the next switch.
|
||||
*/
|
||||
- (void)_setThemeIncludingDescendants:(CPTheme)aTheme
|
||||
{
|
||||
[self setTheme:aTheme];
|
||||
[[self subviews] makeObjectsPerformSelector:@selector(_setThemeIncludingDescendants:) withObject:aTheme];
|
||||
[self _recomputeThemeWithInheritedTheme:aTheme];
|
||||
}
|
||||
|
||||
- (void)objectDidChangeTheme
|
||||
@@ -3828,9 +3910,13 @@ setBoundsOrigin:
|
||||
_DOMElement.className = aClassName;
|
||||
}
|
||||
|
||||
/*
|
||||
2026-11-30: this method read the default theme, so a view whose theme was set explicitly
|
||||
answered for the default theme. It now asks the receiver's theme.
|
||||
*/
|
||||
- (BOOL)isCSSBased
|
||||
{
|
||||
return [[CPTheme defaultTheme] valueForAttributeWithName:@"css-based" forClass:CPView];
|
||||
return [[self actualTheme] isCSSBased];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -3969,7 +4055,7 @@ setBoundsOrigin:
|
||||
[self setNeedsLayout:YES];
|
||||
[self setNeedsDisplay:YES];
|
||||
|
||||
var previousAppearance = _CPAppearanceBeginDrawing(anAppearance);
|
||||
var previousDrawingValues = [_CPAppearanceBeginDrawing(anAppearance), _CPThemeBeginDrawing([self theme])];
|
||||
|
||||
try
|
||||
{
|
||||
@@ -3978,7 +4064,7 @@ setBoundsOrigin:
|
||||
}
|
||||
finally
|
||||
{
|
||||
_CPAppearanceEndDrawing(previousAppearance);
|
||||
_CPViewEndDrawing(previousDrawingValues);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,15 +20,13 @@
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
@import <AppKit/CPColor.j>
|
||||
@import <AppKit/CPAppearance.j>
|
||||
@import <AppKit/CPTheme.j>
|
||||
@import <AppKit/AppKit.j>
|
||||
|
||||
/*
|
||||
A theme's own colours are in the theme catalog "Theme". The theme gives their values in
|
||||
the attribute "theme-colors" of CPColor: for each appearance state, a dictionary from colour
|
||||
name to colour. A theme colour resolves at use against the default theme, as a System colour
|
||||
does.
|
||||
name to colour. A theme colour resolves at use against the theme of the view that draws it,
|
||||
or, with no such view, against the default theme, as a System colour does.
|
||||
*/
|
||||
@implementation CPColorThemeCatalogTest : OJTestCase
|
||||
{
|
||||
@@ -216,4 +214,23 @@
|
||||
[self assertTrue:[[self resolve:decodedColor in:CPAppearanceNameDarkAqua] isEqual:[[CPColor blueColor] colorWithAlphaComponent:0.25]]];
|
||||
}
|
||||
|
||||
/*
|
||||
A view that lays out or draws resolves a theme colour against its own theme, not the
|
||||
default theme.
|
||||
*/
|
||||
- (void)testAThemeColorResolvesAgainstTheThemeOfTheDrawingView
|
||||
{
|
||||
[[CPApplication alloc] init];
|
||||
|
||||
var otherTheme = [[CPTheme alloc] initWithName:@"CPColorThemeCatalogTestOtherTheme"],
|
||||
template = [CPColor colorWithWhite:0.0 alpha:1.0],
|
||||
view = [[CPView alloc] initWithFrame:CGRectMakeZero()];
|
||||
|
||||
[template setValue:@{ @"fillColor": [CPColor purpleColor] } forThemeAttribute:@"theme-colors"];
|
||||
[otherTheme takeThemeFromObject:template];
|
||||
[view setTheme:otherTheme];
|
||||
|
||||
[self assert:[CPColor purpleColor] equals:[view _resolvedBackgroundColor:[self themeColorNamed:@"fillColor"]]];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -202,6 +202,89 @@
|
||||
[self assertAccent:@"second" ofView:grandchild];
|
||||
}
|
||||
|
||||
/*
|
||||
A view's theme is inherited down the hierarchy, as its appearance is: the theme set on
|
||||
the view, else its superview's theme, else the application's active theme.
|
||||
*/
|
||||
- (void)testASubviewInheritsItsSuperviewsTheme
|
||||
{
|
||||
var child = [self newView];
|
||||
|
||||
[view addSubview:child];
|
||||
|
||||
[self assertAccent:@"first" ofView:child];
|
||||
}
|
||||
|
||||
- (void)testAThemeSetOnASuperviewReachesItsSubviews
|
||||
{
|
||||
var child = [self newView],
|
||||
grandchild = [self newView];
|
||||
|
||||
[view addSubview:child];
|
||||
[child addSubview:grandchild];
|
||||
[view setTheme:secondTheme];
|
||||
|
||||
[self assertAccent:@"second" ofView:child];
|
||||
[self assertAccent:@"second" ofView:grandchild];
|
||||
}
|
||||
|
||||
- (void)testAThemeSetOnAViewOverridesTheInheritedTheme
|
||||
{
|
||||
var child = [self newView];
|
||||
|
||||
[view addSubview:child];
|
||||
[child setTheme:secondTheme];
|
||||
|
||||
[self assertAccent:@"second" ofView:child];
|
||||
|
||||
[view setTheme:secondTheme];
|
||||
[view setTheme:firstTheme];
|
||||
|
||||
[self assertAccent:@"second" ofView:child];
|
||||
}
|
||||
|
||||
- (void)testSettingNoThemeRestoresTheInheritedTheme
|
||||
{
|
||||
var child = [self newView];
|
||||
|
||||
[view addSubview:child];
|
||||
[child setTheme:secondTheme];
|
||||
[child setTheme:nil];
|
||||
|
||||
[self assertAccent:@"first" ofView:child];
|
||||
}
|
||||
|
||||
- (void)testAViewRemovedFromItsSuperviewTakesTheActiveTheme
|
||||
{
|
||||
var child = [self newView];
|
||||
|
||||
[CPTheme setDefaultTheme:secondTheme];
|
||||
[view addSubview:child];
|
||||
[child removeFromSuperview];
|
||||
|
||||
[self assertAccent:@"second" ofView:child];
|
||||
}
|
||||
|
||||
/*
|
||||
A theme switch reaches every view that inherits its theme. A view whose theme was set
|
||||
explicitly keeps it, and so does the subtree that inherits from it.
|
||||
*/
|
||||
- (void)testAThemeSwitchKeepsAThemeSetOnASubtree
|
||||
{
|
||||
var parent = [self newView],
|
||||
child = [self newView],
|
||||
grandchild = [self newView];
|
||||
|
||||
[parent addSubview:child];
|
||||
[child addSubview:grandchild];
|
||||
[child setTheme:firstTheme];
|
||||
[parent _setThemeIncludingDescendants:secondTheme];
|
||||
|
||||
[self assertAccent:@"second" ofView:parent];
|
||||
[self assertAccent:@"first" ofView:child];
|
||||
[self assertAccent:@"first" ofView:grandchild];
|
||||
}
|
||||
|
||||
- (void)testAPerViewValueTakesPriorityOverTheTheme
|
||||
{
|
||||
[view setValue:@"own" forThemeAttribute:@"accent"];
|
||||
|
||||
Reference in new issue
Block a user