mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-11-29 09:52:03 +00:00
CPAppearance: Match system appearance against theme capabilities
A theme now declares its supported appearances via the CPColor attribute "supported-appearances", defaulting to Aqua. If the list includes neither Aqua nor DarkAqua, a single warning is logged and it falls back to the default. System appearance resolution now maps the browser's traits to a theme-supported appearance: it uses the browser-derived appearance if the theme supports it, falls back to its opposite-lightness counterpart if available, and otherwise retains the derived appearance. Consequently, a theme lacking a dark variant remains light even in a dark browser environment. Setting CPRequiresAquaSystemAppearance to YES in Info.plist disables the dark trait altogether. +[CPTheme setDefaultTheme:] now posts a notification, prompting the appearance manager to recompute the system appearance whenever the active theme changes. Default popovers now adopt VibrantDark when supported by the theme and the positioning view has a dark appearance. Aristo3 declares support for both Aqua and DarkAqua.
This commit is contained in:
1 parent
d2d67042fd
commit
45286c9cc4
10 files changed
+663
-10
No files matched your search
@@ -26,6 +26,11 @@
|
||||
|
||||
@global CPApp
|
||||
|
||||
/*
|
||||
CPColor.j imports this file, so this file refers to CPColor only when its functions run.
|
||||
*/
|
||||
@class CPColor
|
||||
|
||||
/*
|
||||
Each name has AppKit's string with the CP prefix in place of NS, so that nib2cib maps an
|
||||
archived NSAppearance name by replacing the prefix. AppKit's high-contrast strings omit
|
||||
@@ -88,6 +93,113 @@ function _CPAppearanceMatchingNames(aName)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
The appearances that a theme supports when it gives no list: Aqua only. This is the class
|
||||
default of the attribute "supported-appearances" (see +[CPColor themeAttributes]).
|
||||
*/
|
||||
var _CPAppearanceDefaultSupportedNames = [CPAppearanceNameAqua],
|
||||
_CPAppearanceInvalidListThemeNames = {};
|
||||
|
||||
/*
|
||||
Returns the appearance of the other lightness with the same contrast and the same
|
||||
vibrancy, or nil for a name without one. The counterpart of light content is DarkAqua.
|
||||
*/
|
||||
function _CPAppearanceCounterpartName(aName)
|
||||
{
|
||||
switch (aName)
|
||||
{
|
||||
case CPAppearanceNameAqua:
|
||||
return CPAppearanceNameDarkAqua;
|
||||
|
||||
case CPAppearanceNameDarkAqua:
|
||||
return CPAppearanceNameAqua;
|
||||
|
||||
case CPAppearanceNameVibrantLight:
|
||||
return CPAppearanceNameVibrantDark;
|
||||
|
||||
case CPAppearanceNameVibrantDark:
|
||||
return CPAppearanceNameVibrantLight;
|
||||
|
||||
case CPAppearanceNameAccessibilityHighContrastAqua:
|
||||
return CPAppearanceNameAccessibilityHighContrastDarkAqua;
|
||||
|
||||
case CPAppearanceNameAccessibilityHighContrastDarkAqua:
|
||||
return CPAppearanceNameAccessibilityHighContrastAqua;
|
||||
|
||||
case CPAppearanceNameAccessibilityHighContrastVibrantLight:
|
||||
return CPAppearanceNameAccessibilityHighContrastVibrantDark;
|
||||
|
||||
case CPAppearanceNameAccessibilityHighContrastVibrantDark:
|
||||
return CPAppearanceNameAccessibilityHighContrastVibrantLight;
|
||||
|
||||
case CPAppearanceNameLightContent:
|
||||
return CPAppearanceNameDarkAqua;
|
||||
|
||||
default:
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the names of the appearances that the default theme supports: the value of its
|
||||
attribute "supported-appearances" in the class "color". A theme without the attribute
|
||||
supports Aqua only. A list must hold Aqua or DarkAqua, because a theme must draw at least
|
||||
one plain appearance. A list with neither is a defect of the theme: it logs one warning
|
||||
and counts as the default.
|
||||
*/
|
||||
function _CPAppearanceSupportedNames()
|
||||
{
|
||||
var theme = [CPTheme defaultTheme],
|
||||
names = [theme valueForAttributeWithName:@"supported-appearances" forClass:[CPColor defaultThemeClass]];
|
||||
|
||||
if (![names isKindOfClass:[CPArray class]])
|
||||
return _CPAppearanceDefaultSupportedNames;
|
||||
|
||||
if ([names containsObject:CPAppearanceNameAqua] || [names containsObject:CPAppearanceNameDarkAqua])
|
||||
return names;
|
||||
|
||||
var themeName = [theme name];
|
||||
|
||||
if (!_CPAppearanceInvalidListThemeNames.hasOwnProperty(themeName))
|
||||
{
|
||||
_CPAppearanceInvalidListThemeNames[themeName] = YES;
|
||||
CPLog.warn("Supported appearances: the theme " + themeName + " lists neither Aqua nor DarkAqua, so it counts as supporting Aqua only");
|
||||
}
|
||||
|
||||
return _CPAppearanceDefaultSupportedNames;
|
||||
}
|
||||
|
||||
/*
|
||||
Returns YES when the default theme supports the appearance named aName: when that
|
||||
appearance has a match in the theme's list, by -bestMatchFromAppearancesWithNames:.
|
||||
DarkAqua in the list covers every dark appearance; a vibrant name alone covers only the
|
||||
vibrant appearances of its lightness.
|
||||
*/
|
||||
function _CPAppearanceIsSupported(aName)
|
||||
{
|
||||
return !![[CPAppearance appearanceNamed:aName] bestMatchFromAppearancesWithNames:_CPAppearanceSupportedNames()];
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the supported form of the appearance named aName under the default theme: the
|
||||
appearance itself when the theme supports it; else its counterpart of the other lightness
|
||||
when the theme supports that; else the appearance itself. The system appearance, the
|
||||
built-in values of the system colours and the popover default use it. It never changes an
|
||||
appearance that code sets on the application, a window or a view.
|
||||
*/
|
||||
function _CPAppearanceSupportedName(aName)
|
||||
{
|
||||
if (_CPAppearanceIsSupported(aName))
|
||||
return aName;
|
||||
|
||||
var counterpart = _CPAppearanceCounterpartName(aName);
|
||||
|
||||
if (counterpart && _CPAppearanceIsSupported(counterpart))
|
||||
return counterpart;
|
||||
|
||||
return aName;
|
||||
}
|
||||
|
||||
|
||||
@protocol CPAppearanceCustomization <CPObject>
|
||||
|
||||
|
||||
+3
-1
@@ -278,7 +278,8 @@ function _CPThemeColorValue(aName, anAppearance)
|
||||
Besides its own attributes, CPColor has one attribute for each system colour, which holds
|
||||
the theme's values for that colour (see _CPSystemColorValue), and the attribute
|
||||
"theme-colors", which holds the values of the theme's own colours (see
|
||||
_CPThemeColorValue).
|
||||
_CPThemeColorValue). The attribute "supported-appearances" holds the names of the
|
||||
appearances that the theme supports (see _CPAppearanceSupportedNames).
|
||||
*/
|
||||
+ (CPDictionary)themeAttributes
|
||||
{
|
||||
@@ -288,6 +289,7 @@ function _CPThemeColorValue(aName, anAppearance)
|
||||
@"selected-text-background-color": [CPNull null],
|
||||
@"selected-text-inactive-background-color": [CPNull null],
|
||||
@"css-based": NO,
|
||||
@"supported-appearances": [CPAppearanceNameAqua],
|
||||
@"theme-colors": [CPNull null]
|
||||
};
|
||||
|
||||
|
||||
+23
-2
@@ -145,14 +145,35 @@ function _CPPopoverValueIsAppearance(aValue)
|
||||
|
||||
/*!
|
||||
Returns the appearance that the popover gives its window: the CPAppearance set; else
|
||||
VibrantDark for CPPopoverAppearanceHUD; else VibrantLight.
|
||||
VibrantDark for CPPopoverAppearanceHUD; else, for the default, VibrantDark when the default
|
||||
theme supports VibrantDark and the popover is shown from a view with a dark effective
|
||||
appearance; else VibrantLight. Before the popover is shown, the default is VibrantLight.
|
||||
*/
|
||||
- (CPAppearance)effectiveAppearance
|
||||
{
|
||||
if (_CPPopoverValueIsAppearance(_appearance))
|
||||
return _appearance;
|
||||
|
||||
return [CPAppearance appearanceNamed:_appearance == CPPopoverAppearanceHUD ? CPAppearanceNameVibrantDark : CPAppearanceNameVibrantLight];
|
||||
if (_appearance == CPPopoverAppearanceHUD || [self _defaultFollowsADarkPositioningView])
|
||||
return [CPAppearance appearanceNamed:CPAppearanceNameVibrantDark];
|
||||
|
||||
return [CPAppearance appearanceNamed:CPAppearanceNameVibrantLight];
|
||||
}
|
||||
|
||||
/*
|
||||
Returns YES when the default popover takes the dark form: the default theme supports
|
||||
VibrantDark (see _CPAppearanceIsSupported), and the effective appearance of the
|
||||
positioning view is dark. A theme without dark support gets no dark popover that the
|
||||
application did not ask for.
|
||||
*/
|
||||
- (BOOL)_defaultFollowsADarkPositioningView
|
||||
{
|
||||
if (!_positioningView || !_CPAppearanceIsSupported(CPAppearanceNameVibrantDark))
|
||||
return NO;
|
||||
|
||||
var positioningAppearance = [_positioningView effectiveAppearance];
|
||||
|
||||
return [positioningAppearance bestMatchFromAppearancesWithNames:[CPAppearanceNameAqua, CPAppearanceNameDarkAqua]] === CPAppearanceNameDarkAqua;
|
||||
}
|
||||
|
||||
+ (CPSet)keyPathsForValuesAffectingEffectiveAppearance
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
@import <Foundation/CPString.j>
|
||||
@import <Foundation/CPKeyedUnarchiver.j>
|
||||
@import <Foundation/CPBundle.j>
|
||||
@import <Foundation/CPNotificationCenter.j>
|
||||
|
||||
@class CPView
|
||||
@class _CPThemeAttribute
|
||||
@@ -38,6 +39,13 @@ var CPThemesByName = { },
|
||||
CPThemeDefaultTheme = nil,
|
||||
CPThemeDefaultHudTheme = nil;
|
||||
|
||||
/*
|
||||
Posted by +setDefaultTheme:. The appearance manager observes it, because the default theme
|
||||
states which appearances it supports, and so limits the system appearance. CPAppearance.j
|
||||
imports this file and the manager, so this file cannot call the manager itself.
|
||||
*/
|
||||
_CPThemeDefaultThemeDidChangeNotification = @"_CPThemeDefaultThemeDidChangeNotification";
|
||||
|
||||
|
||||
/*!
|
||||
@ingroup appkit
|
||||
@@ -53,6 +61,8 @@ var CPThemesByName = { },
|
||||
CPThemeDefaultTheme = aTheme;
|
||||
|
||||
[CPFont initializeSystemFontFromTheme:aTheme];
|
||||
|
||||
[[CPNotificationCenter defaultCenter] postNotificationName:_CPThemeDefaultThemeDidChangeNotification object:self];
|
||||
}
|
||||
|
||||
+ (CPTheme)defaultTheme
|
||||
|
||||
@@ -250,6 +250,7 @@ function A3MaskIcon(aMaskImage, aColor)
|
||||
[@"secondary-selected-control-color", [[CPColor alloc] _initWithRGBA:[0.83, 0.83, 0.83, 1.0]]],
|
||||
[@"selected-text-background-color", [CPColor colorWithHexString:"99CCFF"]],
|
||||
[@"selected-text-inactive-background-color", [CPColor colorWithHexString:"CCCCCC"]],
|
||||
[@"supported-appearances", [CPAppearanceNameAqua, CPAppearanceNameDarkAqua]],
|
||||
[@"theme-colors", A3ThemeColorValues],
|
||||
[@"theme-colors", A3DarkThemeColorValues, CPThemeStateAppearanceDarkAqua]
|
||||
];
|
||||
|
||||
@@ -21,6 +21,8 @@
|
||||
*/
|
||||
|
||||
@import <Foundation/CPObject.j>
|
||||
@import <Foundation/CPBundle.j>
|
||||
@import <Foundation/CPNotificationCenter.j>
|
||||
@import <Foundation/CPRunLoop.j>
|
||||
|
||||
/*
|
||||
@@ -34,6 +36,8 @@
|
||||
@global CPAppearanceNameDarkAqua
|
||||
@global CPAppearanceNameAccessibilityHighContrastAqua
|
||||
@global CPAppearanceNameAccessibilityHighContrastDarkAqua
|
||||
@global _CPThemeDefaultThemeDidChangeNotification
|
||||
@global _CPAppearanceSupportedName
|
||||
|
||||
var _CPAppearanceSharedManager = nil;
|
||||
|
||||
@@ -112,6 +116,13 @@ function _CPAppearanceStopObservingQuery(aQueryList, aHandler)
|
||||
select the high-contrast name of the scheme's lightness, and set -forcedColorsActive. When
|
||||
the system appearance changes, the manager tells the application. Cocoa has no public
|
||||
counterpart, so the class is private.
|
||||
|
||||
The system appearance is the supported form of the appearance that the traits give (see
|
||||
_CPAppearanceSupportedName): a theme that supports Aqua only keeps the system appearance
|
||||
light, and a theme that supports dark appearances only makes it dark. With the Info.plist
|
||||
key CPRequiresAquaSystemAppearance set to YES, the dark trait reads as off before that
|
||||
rule applies. The manager keeps the traits, so that a change of the default theme can
|
||||
lift or impose the limit without a media event.
|
||||
*/
|
||||
@implementation _CPAppearanceManager : CPObject
|
||||
{
|
||||
@@ -119,7 +130,9 @@ function _CPAppearanceStopObservingQuery(aQueryList, aHandler)
|
||||
JSObject _highContrastQuery;
|
||||
JSObject _forcedColorsQuery;
|
||||
Function _changeHandler;
|
||||
CPBundle _infoBundle;
|
||||
|
||||
CPString _traitAppearanceName;
|
||||
CPString _systemAppearanceName;
|
||||
BOOL _forcedColorsActive;
|
||||
}
|
||||
@@ -151,7 +164,13 @@ function _CPAppearanceStopObservingQuery(aQueryList, aHandler)
|
||||
[self _observeWithMatchMediaFunction:_CPAppearanceBrowserMatchMedia()];
|
||||
|
||||
_forcedColorsActive = _CPAppearanceQueryMatches(_forcedColorsQuery);
|
||||
_systemAppearanceName = [self _nameFromQueries];
|
||||
_traitAppearanceName = [self _nameFromQueries];
|
||||
_systemAppearanceName = [self _limitedName];
|
||||
|
||||
[[CPNotificationCenter defaultCenter] addObserver:self
|
||||
selector:@selector(_defaultThemeDidChange:)
|
||||
name:_CPThemeDefaultThemeDidChangeNotification
|
||||
object:nil];
|
||||
}
|
||||
|
||||
return self;
|
||||
@@ -185,6 +204,17 @@ function _CPAppearanceStopObservingQuery(aQueryList, aHandler)
|
||||
[self _mediaQueriesDidChange];
|
||||
}
|
||||
|
||||
/*
|
||||
Replaces the bundle whose Info.plist the manager reads for CPRequiresAquaSystemAppearance,
|
||||
and gives the system appearance again. nil returns to the main bundle. The unit tests use
|
||||
this to supply the key.
|
||||
*/
|
||||
- (void)_setInfoBundle:(CPBundle)aBundle
|
||||
{
|
||||
_infoBundle = aBundle;
|
||||
[self _systemAppearanceMayHaveChanged];
|
||||
}
|
||||
|
||||
- (void)_observeWithMatchMediaFunction:(Function)aMatchMediaFunction
|
||||
{
|
||||
_CPAppearanceStopObservingQuery(_darkQuery, _changeHandler);
|
||||
@@ -200,25 +230,64 @@ function _CPAppearanceStopObservingQuery(aQueryList, aHandler)
|
||||
_CPAppearanceObserveQuery(_forcedColorsQuery, _changeHandler);
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the name that the traits give, before the limit of the default theme. With
|
||||
CPRequiresAquaSystemAppearance set to YES, the dark trait reads as off.
|
||||
*/
|
||||
- (CPString)_nameFromQueries
|
||||
{
|
||||
var isDark = _CPAppearanceQueryMatches(_darkQuery),
|
||||
var isDark = _CPAppearanceQueryMatches(_darkQuery) && ![self _requiresAquaSystemAppearance],
|
||||
isHighContrast = _CPAppearanceQueryMatches(_highContrastQuery) || _CPAppearanceQueryMatches(_forcedColorsQuery);
|
||||
|
||||
return _CPAppearanceNameForSystemTraits(isDark, isHighContrast);
|
||||
}
|
||||
|
||||
- (BOOL)_requiresAquaSystemAppearance
|
||||
{
|
||||
var bundle = _infoBundle || [CPBundle mainBundle];
|
||||
|
||||
return [bundle objectForInfoDictionaryKey:@"CPRequiresAquaSystemAppearance"] === YES;
|
||||
}
|
||||
|
||||
/*
|
||||
Reads the queries again. When the name changes, the application's effective appearance
|
||||
changes too, unless the application has an appearance of its own. The run loop then runs
|
||||
once, as for a DOM event, so that layout and display happen in the same turn.
|
||||
Returns the system appearance's name: the supported form of the name that the traits give.
|
||||
*/
|
||||
- (CPString)_limitedName
|
||||
{
|
||||
return _CPAppearanceSupportedName(_traitAppearanceName);
|
||||
}
|
||||
|
||||
/*
|
||||
Reads the queries again, then gives the system appearance again.
|
||||
*/
|
||||
- (void)_mediaQueriesDidChange
|
||||
{
|
||||
var name = [self _nameFromQueries];
|
||||
|
||||
_forcedColorsActive = _CPAppearanceQueryMatches(_forcedColorsQuery);
|
||||
|
||||
[self _systemAppearanceMayHaveChanged];
|
||||
}
|
||||
|
||||
/*
|
||||
The default theme states which appearances it supports, so a new default theme can change
|
||||
the system appearance while the traits stay.
|
||||
*/
|
||||
- (void)_defaultThemeDidChange:(CPNotification)aNotification
|
||||
{
|
||||
[self _systemAppearanceMayHaveChanged];
|
||||
}
|
||||
|
||||
/*
|
||||
Gives the system appearance again from the traits and the default theme. When the name
|
||||
changes, the application's effective appearance changes too, unless the application has an
|
||||
appearance of its own. The run loop then runs once, as for a DOM event, so that layout and
|
||||
display happen in the same turn.
|
||||
*/
|
||||
- (void)_systemAppearanceMayHaveChanged
|
||||
{
|
||||
_traitAppearanceName = [self _nameFromQueries];
|
||||
|
||||
var name = [self _limitedName];
|
||||
|
||||
if (name === _systemAppearanceName)
|
||||
return;
|
||||
|
||||
|
||||
@@ -205,6 +205,16 @@ var Aristo3ThemeColorsTestObjectDescriptions = ["Catalog color", "Asset color",
|
||||
[self assert:[CPColor windowBackgroundColor] equals:bodyColor];
|
||||
}
|
||||
|
||||
/*
|
||||
Aristo3 has a dark set, so it supports Aqua and DarkAqua, and a dark system makes it dark.
|
||||
*/
|
||||
- (void)testAristo3SupportsAquaAndDarkAqua
|
||||
{
|
||||
[CPTheme setDefaultTheme:[self aristo3]];
|
||||
|
||||
[self assert:[CPAppearanceNameAqua, CPAppearanceNameDarkAqua] equals:_CPAppearanceSupportedNames()];
|
||||
}
|
||||
|
||||
/*
|
||||
Each System colour in Aristo3's dark set takes AppKit's DarkAqua value in DarkAqua and
|
||||
keeps AppKit's Aqua value in Aqua.
|
||||
|
||||
@@ -0,0 +1,382 @@
|
||||
/*
|
||||
* CPAppearanceDarkSupportTest.j
|
||||
* AppKit
|
||||
*
|
||||
* Created by David Richardson on 2026-11-18.
|
||||
* Copyright David Richardson 2026.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
@import <AppKit/AppKit.j>
|
||||
@import <AppKit/CPPopover.j>
|
||||
|
||||
var CPAppearanceDarkSupportTestDark = "(prefers-color-scheme: dark)",
|
||||
CPAppearanceDarkSupportTestHighContrast = "(prefers-contrast: more)";
|
||||
|
||||
/*
|
||||
Returns a stand-in for the browser's media queries. Each query in someMatchingQueries
|
||||
matches; set() changes whether a query matches and sends the change event.
|
||||
*/
|
||||
function CPAppearanceDarkSupportTestMedia(someMatchingQueries)
|
||||
{
|
||||
var media = { lists: {} };
|
||||
|
||||
media.matchMedia = function(aQuery)
|
||||
{
|
||||
var list = media.lists[aQuery];
|
||||
|
||||
if (!list)
|
||||
{
|
||||
list = { media: aQuery, matches: someMatchingQueries.indexOf(aQuery) !== -1, handlers: [] };
|
||||
list.addEventListener = function(aType, aHandler) { list.handlers.push(aHandler); };
|
||||
list.removeEventListener = function(aType, aHandler) { };
|
||||
media.lists[aQuery] = list;
|
||||
}
|
||||
|
||||
return list;
|
||||
};
|
||||
|
||||
media.set = function(aQuery, shouldMatch)
|
||||
{
|
||||
var list = media.matchMedia(aQuery);
|
||||
|
||||
list.matches = shouldMatch;
|
||||
|
||||
for (var i = 0; i < list.handlers.length; i++)
|
||||
list.handlers[i]({ media: aQuery, matches: shouldMatch });
|
||||
};
|
||||
|
||||
return media;
|
||||
}
|
||||
|
||||
/*
|
||||
An Info.plist with the key CPRequiresAquaSystemAppearance.
|
||||
*/
|
||||
@implementation CPAppearanceDarkSupportTestInfo : CPObject
|
||||
{
|
||||
BOOL requiresAqua;
|
||||
}
|
||||
|
||||
- (id)objectForInfoDictionaryKey:(CPString)aKey
|
||||
{
|
||||
return aKey === @"CPRequiresAquaSystemAppearance" ? requiresAqua : nil;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation CPAppearanceDarkSupportTest : OJTestCase
|
||||
{
|
||||
CPTheme _previousTheme;
|
||||
}
|
||||
|
||||
- (void)setUp
|
||||
{
|
||||
// This will init the global var CPApp which are used internally in the AppKit
|
||||
[[CPApplication alloc] init];
|
||||
|
||||
_previousTheme = [CPTheme defaultTheme];
|
||||
[CPTheme setDefaultTheme:[[CPTheme alloc] initWithName:@"CPAppearanceDarkSupportTestNoList"]];
|
||||
}
|
||||
|
||||
- (void)tearDown
|
||||
{
|
||||
var manager = [_CPAppearanceManager sharedManager];
|
||||
|
||||
[manager _setMatchMediaFunction:nil];
|
||||
[manager _setInfoBundle:nil];
|
||||
[CPApp setAppearance:nil];
|
||||
[CPTheme setDefaultTheme:_previousTheme];
|
||||
}
|
||||
|
||||
// MARK: Helpers
|
||||
|
||||
- (CPAppearance)appearance:(CPString)aName
|
||||
{
|
||||
return [CPAppearance appearanceNamed:aName];
|
||||
}
|
||||
|
||||
/*
|
||||
Returns a theme whose attribute "supported-appearances" holds someNames.
|
||||
*/
|
||||
- (CPTheme)themeSupporting:(CPArray)someNames
|
||||
{
|
||||
var theme = [[CPTheme alloc] initWithName:@"CPAppearanceDarkSupportTest " + someNames.join(" ")],
|
||||
template = [CPColor colorWithWhite:0.0 alpha:1.0];
|
||||
|
||||
[template setValue:someNames forThemeAttribute:@"supported-appearances"];
|
||||
[theme takeThemeFromObject:template];
|
||||
|
||||
return theme;
|
||||
}
|
||||
|
||||
- (CPString)systemAppearanceNameForQueries:(CPArray)someMatchingQueries
|
||||
{
|
||||
var manager = [_CPAppearanceManager sharedManager];
|
||||
|
||||
[manager _setMatchMediaFunction:CPAppearanceDarkSupportTestMedia(someMatchingQueries).matchMedia];
|
||||
|
||||
return [[manager systemAppearance] name];
|
||||
}
|
||||
|
||||
- (void)requireAqua:(BOOL)shouldRequireAqua
|
||||
{
|
||||
var info = [[CPAppearanceDarkSupportTestInfo alloc] init];
|
||||
|
||||
info.requiresAqua = shouldRequireAqua;
|
||||
[[_CPAppearanceManager sharedManager] _setInfoBundle:info];
|
||||
}
|
||||
|
||||
- (CPView)viewInWindow
|
||||
{
|
||||
var window = [[CPWindow alloc] initWithContentRect:CGRectMake(0.0, 0.0, 200.0, 200.0) styleMask:CPWindowNotSizable],
|
||||
view = [[CPView alloc] initWithFrame:CGRectMake(0.0, 0.0, 20.0, 20.0)];
|
||||
|
||||
[[window contentView] addSubview:view];
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
// MARK: The supported form
|
||||
|
||||
/*
|
||||
Each row gives a list and the supported form of each of the nine appearances under it, in
|
||||
the order of the names below.
|
||||
*/
|
||||
- (void)testTheSupportedFormOfEachAppearanceUnderFourLists
|
||||
{
|
||||
var names = [CPAppearanceNameAqua, CPAppearanceNameDarkAqua, CPAppearanceNameLightContent,
|
||||
CPAppearanceNameVibrantLight, CPAppearanceNameVibrantDark,
|
||||
CPAppearanceNameAccessibilityHighContrastAqua, CPAppearanceNameAccessibilityHighContrastDarkAqua,
|
||||
CPAppearanceNameAccessibilityHighContrastVibrantLight, CPAppearanceNameAccessibilityHighContrastVibrantDark],
|
||||
rows = [
|
||||
{
|
||||
list: [CPAppearanceNameAqua],
|
||||
forms: [CPAppearanceNameAqua, CPAppearanceNameAqua, CPAppearanceNameLightContent,
|
||||
CPAppearanceNameVibrantLight, CPAppearanceNameVibrantLight,
|
||||
CPAppearanceNameAccessibilityHighContrastAqua, CPAppearanceNameAccessibilityHighContrastAqua,
|
||||
CPAppearanceNameAccessibilityHighContrastVibrantLight, CPAppearanceNameAccessibilityHighContrastVibrantLight]
|
||||
},
|
||||
{
|
||||
list: [CPAppearanceNameAqua, CPAppearanceNameDarkAqua],
|
||||
forms: names
|
||||
},
|
||||
{
|
||||
list: [CPAppearanceNameDarkAqua],
|
||||
forms: [CPAppearanceNameDarkAqua, CPAppearanceNameDarkAqua, CPAppearanceNameDarkAqua,
|
||||
CPAppearanceNameVibrantDark, CPAppearanceNameVibrantDark,
|
||||
CPAppearanceNameAccessibilityHighContrastDarkAqua, CPAppearanceNameAccessibilityHighContrastDarkAqua,
|
||||
CPAppearanceNameAccessibilityHighContrastVibrantDark, CPAppearanceNameAccessibilityHighContrastVibrantDark]
|
||||
},
|
||||
{
|
||||
list: [CPAppearanceNameAqua, CPAppearanceNameVibrantDark],
|
||||
forms: [CPAppearanceNameAqua, CPAppearanceNameAqua, CPAppearanceNameLightContent,
|
||||
CPAppearanceNameVibrantLight, CPAppearanceNameVibrantDark,
|
||||
CPAppearanceNameAccessibilityHighContrastAqua, CPAppearanceNameAccessibilityHighContrastAqua,
|
||||
CPAppearanceNameAccessibilityHighContrastVibrantLight, CPAppearanceNameAccessibilityHighContrastVibrantDark]
|
||||
}
|
||||
];
|
||||
|
||||
for (var i = 0; i < rows.length; i++)
|
||||
{
|
||||
[CPTheme setDefaultTheme:[self themeSupporting:rows[i].list]];
|
||||
|
||||
for (var j = 0; j < names.length; j++)
|
||||
[self assert:rows[i].forms[j] equals:_CPAppearanceSupportedName(names[j]) message:@"The list " + rows[i].list + ", the appearance " + names[j]];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)testAThemeWithoutAListSupportsAquaOnly
|
||||
{
|
||||
[self assert:[CPAppearanceNameAqua] equals:_CPAppearanceSupportedNames()];
|
||||
[self assert:CPAppearanceNameAqua equals:_CPAppearanceSupportedName(CPAppearanceNameDarkAqua)];
|
||||
}
|
||||
|
||||
- (void)testAListWithNeitherAquaNorDarkAquaCountsAsTheDefault
|
||||
{
|
||||
[CPTheme setDefaultTheme:[self themeSupporting:[CPAppearanceNameVibrantDark]]];
|
||||
|
||||
[self assert:[CPAppearanceNameAqua] equals:_CPAppearanceSupportedNames()];
|
||||
[self assert:CPAppearanceNameVibrantLight equals:_CPAppearanceSupportedName(CPAppearanceNameVibrantDark)];
|
||||
}
|
||||
|
||||
// MARK: The system appearance
|
||||
|
||||
- (void)testADarkSystemGivesAquaUnderAThemeWithoutDarkSupport
|
||||
{
|
||||
[self assert:CPAppearanceNameAqua equals:[self systemAppearanceNameForQueries:[CPAppearanceDarkSupportTestDark]]];
|
||||
[self assert:[self appearance:CPAppearanceNameAqua] same:[CPApp effectiveAppearance]];
|
||||
}
|
||||
|
||||
- (void)testADarkSystemGivesDarkAquaUnderAThemeWithDarkSupport
|
||||
{
|
||||
[CPTheme setDefaultTheme:[self themeSupporting:[CPAppearanceNameAqua, CPAppearanceNameDarkAqua]]];
|
||||
|
||||
[self assert:CPAppearanceNameDarkAqua equals:[self systemAppearanceNameForQueries:[CPAppearanceDarkSupportTestDark]]];
|
||||
[self assert:[self appearance:CPAppearanceNameDarkAqua] same:[CPApp effectiveAppearance]];
|
||||
}
|
||||
|
||||
- (void)testHighContrastIsKeptUnderTheLimit
|
||||
{
|
||||
[self assert:CPAppearanceNameAccessibilityHighContrastAqua
|
||||
equals:[self systemAppearanceNameForQueries:[CPAppearanceDarkSupportTestDark, CPAppearanceDarkSupportTestHighContrast]]];
|
||||
}
|
||||
|
||||
- (void)testAThemeWithDarkAquaOnlyGivesDarkAquaInALightSystem
|
||||
{
|
||||
[CPTheme setDefaultTheme:[self themeSupporting:[CPAppearanceNameDarkAqua]]];
|
||||
|
||||
[self assert:CPAppearanceNameDarkAqua equals:[self systemAppearanceNameForQueries:[]]];
|
||||
[self assert:CPAppearanceNameAccessibilityHighContrastDarkAqua equals:[self systemAppearanceNameForQueries:[CPAppearanceDarkSupportTestHighContrast]]];
|
||||
}
|
||||
|
||||
- (void)testRequiringAquaKeepsADarkSystemLight
|
||||
{
|
||||
[CPTheme setDefaultTheme:[self themeSupporting:[CPAppearanceNameAqua, CPAppearanceNameDarkAqua]]];
|
||||
[self requireAqua:YES];
|
||||
|
||||
[self assert:CPAppearanceNameAqua equals:[self systemAppearanceNameForQueries:[CPAppearanceDarkSupportTestDark]]];
|
||||
[self assert:CPAppearanceNameAccessibilityHighContrastAqua
|
||||
equals:[self systemAppearanceNameForQueries:[CPAppearanceDarkSupportTestDark, CPAppearanceDarkSupportTestHighContrast]]];
|
||||
|
||||
[self requireAqua:NO];
|
||||
|
||||
[self assert:CPAppearanceNameAccessibilityHighContrastDarkAqua equals:[[[_CPAppearanceManager sharedManager] systemAppearance] name]];
|
||||
}
|
||||
|
||||
- (void)testRequiringAquaGivesDarkAquaUnderAThemeWithDarkAquaOnly
|
||||
{
|
||||
[CPTheme setDefaultTheme:[self themeSupporting:[CPAppearanceNameDarkAqua]]];
|
||||
[self requireAqua:YES];
|
||||
|
||||
[self assert:CPAppearanceNameDarkAqua equals:[self systemAppearanceNameForQueries:[CPAppearanceDarkSupportTestDark]]];
|
||||
}
|
||||
|
||||
- (void)testAnExplicitDarkAppearanceStaysUnderAThemeWithoutDarkSupport
|
||||
{
|
||||
var darkAqua = [self appearance:CPAppearanceNameDarkAqua],
|
||||
view = [self viewInWindow];
|
||||
|
||||
[view setAppearance:darkAqua];
|
||||
|
||||
[self assert:darkAqua same:[view effectiveAppearance]];
|
||||
[self assertTrue:[view hasThemeState:CPThemeStateAppearanceDarkAqua]];
|
||||
|
||||
[CPApp setAppearance:darkAqua];
|
||||
|
||||
[self assert:darkAqua same:[CPApp effectiveAppearance]];
|
||||
}
|
||||
|
||||
- (void)testAThemeSwitchWhileTheSystemIsDarkMovesTheLimit
|
||||
{
|
||||
var withDark = [self themeSupporting:[CPAppearanceNameAqua, CPAppearanceNameDarkAqua]],
|
||||
withoutDark = [self themeSupporting:[CPAppearanceNameAqua]],
|
||||
view = [self viewInWindow];
|
||||
|
||||
[self assert:CPAppearanceNameAqua equals:[self systemAppearanceNameForQueries:[CPAppearanceDarkSupportTestDark]]];
|
||||
|
||||
[CPTheme setDefaultTheme:withDark];
|
||||
|
||||
[self assert:[self appearance:CPAppearanceNameDarkAqua] same:[CPApp effectiveAppearance]];
|
||||
[self assertTrue:[view hasThemeState:CPThemeStateAppearanceDarkAqua]];
|
||||
|
||||
[CPTheme setDefaultTheme:withoutDark];
|
||||
|
||||
[self assert:[self appearance:CPAppearanceNameAqua] same:[CPApp effectiveAppearance]];
|
||||
[self assertTrue:[view hasThemeState:CPThemeStateAppearanceAqua]];
|
||||
}
|
||||
|
||||
- (void)testAMediaChangeIsLimitedToo
|
||||
{
|
||||
var media = CPAppearanceDarkSupportTestMedia([]),
|
||||
manager = [_CPAppearanceManager sharedManager];
|
||||
|
||||
[manager _setMatchMediaFunction:media.matchMedia];
|
||||
media.set(CPAppearanceDarkSupportTestDark, YES);
|
||||
|
||||
[self assert:CPAppearanceNameAqua equals:[[manager systemAppearance] name]];
|
||||
|
||||
[CPTheme setDefaultTheme:[self themeSupporting:[CPAppearanceNameAqua, CPAppearanceNameDarkAqua]]];
|
||||
media.set(CPAppearanceDarkSupportTestDark, NO);
|
||||
media.set(CPAppearanceDarkSupportTestDark, YES);
|
||||
|
||||
[self assert:CPAppearanceNameDarkAqua equals:[[manager systemAppearance] name]];
|
||||
}
|
||||
|
||||
// MARK: The popover default
|
||||
|
||||
/*
|
||||
A unit test cannot show a popover (see CPPopoverAppearanceTest). The popover records its
|
||||
positioning view when it is shown, so the tests set that view.
|
||||
*/
|
||||
- (CPPopover)popoverShownFrom:(CPView)aView
|
||||
{
|
||||
var popover = [[CPPopover alloc] init];
|
||||
|
||||
popover._positioningView = aView;
|
||||
|
||||
return popover;
|
||||
}
|
||||
|
||||
- (void)testADefaultPopoverFollowsADarkViewWhenTheThemeSupportsVibrantDark
|
||||
{
|
||||
var view = [self viewInWindow];
|
||||
|
||||
[CPTheme setDefaultTheme:[self themeSupporting:[CPAppearanceNameAqua, CPAppearanceNameDarkAqua]]];
|
||||
[view setAppearance:[self appearance:CPAppearanceNameDarkAqua]];
|
||||
|
||||
[self assert:[self appearance:CPAppearanceNameVibrantDark] same:[[self popoverShownFrom:view] effectiveAppearance]];
|
||||
|
||||
[view setAppearance:[self appearance:CPAppearanceNameAqua]];
|
||||
|
||||
[self assert:[self appearance:CPAppearanceNameVibrantLight] same:[[self popoverShownFrom:view] effectiveAppearance]];
|
||||
}
|
||||
|
||||
- (void)testADefaultPopoverStaysLightUnderAThemeWithoutDarkSupport
|
||||
{
|
||||
var view = [self viewInWindow];
|
||||
|
||||
[view setAppearance:[self appearance:CPAppearanceNameDarkAqua]];
|
||||
|
||||
[self assert:[self appearance:CPAppearanceNameVibrantLight] same:[[self popoverShownFrom:view] effectiveAppearance]];
|
||||
}
|
||||
|
||||
- (void)testADefaultPopoverIsLightBeforeItIsShown
|
||||
{
|
||||
[CPTheme setDefaultTheme:[self themeSupporting:[CPAppearanceNameAqua, CPAppearanceNameDarkAqua]]];
|
||||
[CPApp setAppearance:[self appearance:CPAppearanceNameDarkAqua]];
|
||||
|
||||
[self assert:[self appearance:CPAppearanceNameVibrantLight] same:[[[CPPopover alloc] init] effectiveAppearance]];
|
||||
}
|
||||
|
||||
- (void)testTheLegacyHUDValueAndAnExplicitAppearanceIgnoreThePositioningView
|
||||
{
|
||||
var view = [self viewInWindow],
|
||||
darkAqua = [self appearance:CPAppearanceNameDarkAqua],
|
||||
popover = [self popoverShownFrom:view];
|
||||
|
||||
[view setAppearance:[self appearance:CPAppearanceNameAqua]];
|
||||
[popover setAppearance:CPPopoverAppearanceHUD];
|
||||
|
||||
[self assert:[self appearance:CPAppearanceNameVibrantDark] same:[popover effectiveAppearance]];
|
||||
|
||||
[CPTheme setDefaultTheme:[self themeSupporting:[CPAppearanceNameAqua, CPAppearanceNameDarkAqua]]];
|
||||
[view setAppearance:darkAqua];
|
||||
[popover setAppearance:[self appearance:CPAppearanceNameAqua]];
|
||||
|
||||
[self assert:[self appearance:CPAppearanceNameAqua] same:[popover effectiveAppearance]];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -22,6 +22,8 @@
|
||||
|
||||
@import <AppKit/CPApplication.j>
|
||||
@import <AppKit/CPAppearance.j>
|
||||
@import <AppKit/CPColor.j>
|
||||
@import <AppKit/CPTheme.j>
|
||||
@import <AppKit/CPView.j>
|
||||
@import <AppKit/CPWindow.j>
|
||||
|
||||
@@ -73,6 +75,7 @@ function CPApplicationAppearanceTestDarkMedia(isDark)
|
||||
@implementation CPApplicationAppearanceTest : OJTestCase
|
||||
{
|
||||
CPArray observedValues;
|
||||
CPTheme _previousTheme;
|
||||
}
|
||||
|
||||
- (void)setUp
|
||||
@@ -81,12 +84,31 @@ function CPApplicationAppearanceTestDarkMedia(isDark)
|
||||
[[CPApplication alloc] init];
|
||||
|
||||
observedValues = [];
|
||||
|
||||
_previousTheme = [CPTheme defaultTheme];
|
||||
[CPTheme setDefaultTheme:[self themeSupportingBothLightnesses]];
|
||||
}
|
||||
|
||||
/*
|
||||
The system appearance is the supported form of the traits under the default theme. These
|
||||
tests read the traits, so they run under a theme that supports both lightnesses.
|
||||
*/
|
||||
- (CPTheme)themeSupportingBothLightnesses
|
||||
{
|
||||
var theme = [[CPTheme alloc] initWithName:@"CPApplicationAppearanceTestBothLightnesses"],
|
||||
template = [CPColor colorWithWhite:0.0 alpha:1.0];
|
||||
|
||||
[template setValue:[CPAppearanceNameAqua, CPAppearanceNameDarkAqua] forThemeAttribute:@"supported-appearances"];
|
||||
[theme takeThemeFromObject:template];
|
||||
|
||||
return theme;
|
||||
}
|
||||
|
||||
- (void)tearDown
|
||||
{
|
||||
[[_CPAppearanceManager sharedManager] _setMatchMediaFunction:nil];
|
||||
[CPApp setAppearance:nil];
|
||||
[CPTheme setDefaultTheme:_previousTheme];
|
||||
}
|
||||
|
||||
- (void)observeValueForKeyPath:(CPString)aKeyPath ofObject:(id)anObject change:(CPDictionary)aChange context:(id)aContext
|
||||
|
||||
@@ -22,6 +22,8 @@
|
||||
|
||||
@import <AppKit/CPApplication.j>
|
||||
@import <AppKit/CPAppearance.j>
|
||||
@import <AppKit/CPColor.j>
|
||||
@import <AppKit/CPTheme.j>
|
||||
|
||||
var _CPAppearanceManagerTestDark = "(prefers-color-scheme: dark)",
|
||||
_CPAppearanceManagerTestHighContrast = "(prefers-contrast: more)",
|
||||
@@ -79,16 +81,38 @@ function _CPAppearanceManagerTestMedia(someMatchingQueries)
|
||||
}
|
||||
|
||||
@implementation _CPAppearanceManagerTest : OJTestCase
|
||||
{
|
||||
CPTheme _previousTheme;
|
||||
}
|
||||
|
||||
- (void)setUp
|
||||
{
|
||||
// This will init the global var CPApp which are used internally in the AppKit
|
||||
[[CPApplication alloc] init];
|
||||
|
||||
_previousTheme = [CPTheme defaultTheme];
|
||||
[CPTheme setDefaultTheme:[self themeSupportingBothLightnesses]];
|
||||
}
|
||||
|
||||
/*
|
||||
The system appearance is the supported form of the traits under the default theme. These
|
||||
tests read the traits, so they run under a theme that supports both lightnesses.
|
||||
*/
|
||||
- (CPTheme)themeSupportingBothLightnesses
|
||||
{
|
||||
var theme = [[CPTheme alloc] initWithName:@"_CPAppearanceManagerTestBothLightnesses"],
|
||||
template = [CPColor colorWithWhite:0.0 alpha:1.0];
|
||||
|
||||
[template setValue:[CPAppearanceNameAqua, CPAppearanceNameDarkAqua] forThemeAttribute:@"supported-appearances"];
|
||||
[theme takeThemeFromObject:template];
|
||||
|
||||
return theme;
|
||||
}
|
||||
|
||||
- (void)tearDown
|
||||
{
|
||||
[[_CPAppearanceManager sharedManager] _setMatchMediaFunction:nil];
|
||||
[CPTheme setDefaultTheme:_previousTheme];
|
||||
}
|
||||
|
||||
- (CPString)systemAppearanceNameForQueries:(CPArray)someMatchingQueries
|
||||
|
||||
Reference in new issue
Block a user