AppKit: Add appearance names and best-match lookup to CPAppearance

Add DarkAqua and high-contrast appearance names with AppKit's strings under CP prefix. Implement best-match lookup and drawing appearance as in AppKit.

* CPAppearance.j:
  - Add CPAppearanceNameDarkAqua and four CPAppearanceNameAccessibilityHighContrast names; take each string from AppKit with CP prefix, so nib2cib maps archived names by prefix.
  - Add -bestMatchFromAppearancesWithNames:; keep lightness, fall back from vibrant to plain appearance and from high contrast to normal contrast.
  - Add +currentDrawingAppearance and -performAsCurrentDrawingAppearance:; restore previous appearance after block, also on exception.

* CPAppearanceTest.j: Add tests for appearance names, best-match lookup and drawing appearance.
This commit is contained in:
David Richardson committed 2026-11-09 12:18:17 -06:00
1 parent af2edf6c97
commit b0bd39345c
2 files changed
+294 -4

No files matched your search

+110 -4
View File
@@ -23,14 +23,68 @@
@import <Foundation/Foundation.j>
@import "CPTheme.j"
CPAppearanceNameAqua = @"CPAppearanceNameAqua";
CPAppearanceNameLightContent = @"CPAppearanceNameLightContent";
CPAppearanceNameVibrantDark = @"CPAppearanceNameVibrantDark";
CPAppearanceNameVibrantLight = @"CPAppearanceNameVibrantLight";
/*
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
"HighContrast": NSAppearanceNameAccessibilityHighContrastAqua is "NSAppearanceNameAccessibilityAqua".
*/
CPAppearanceNameAqua = @"CPAppearanceNameAqua";
CPAppearanceNameDarkAqua = @"CPAppearanceNameDarkAqua";
CPAppearanceNameLightContent = @"CPAppearanceNameLightContent";
CPAppearanceNameVibrantDark = @"CPAppearanceNameVibrantDark";
CPAppearanceNameVibrantLight = @"CPAppearanceNameVibrantLight";
CPAppearanceNameAccessibilityHighContrastAqua = @"CPAppearanceNameAccessibilityAqua";
CPAppearanceNameAccessibilityHighContrastDarkAqua = @"CPAppearanceNameAccessibilityDarkAqua";
CPAppearanceNameAccessibilityHighContrastVibrantLight = @"CPAppearanceNameAccessibilityVibrantLight";
CPAppearanceNameAccessibilityHighContrastVibrantDark = @"CPAppearanceNameAccessibilityVibrantDark";
var _CPAppearanceCurrent = nil,
_CPAppearancesRegistry = @{};
/*
Returns the names an appearance matches, closest first, for
-bestMatchFromAppearancesWithNames:. As in AppKit:
- a match keeps lightness, so a light appearance matches no dark name, and the reverse;
- a vibrant appearance falls back to the plain appearance of the same lightness, and a
plain appearance matches no vibrant name;
- light content falls back to Aqua;
- any other name matches only itself.
A high-contrast vibrant appearance gives up vibrancy before high contrast. Every
high-contrast appearance then falls back to normal contrast. An appearance of normal
contrast matches no high-contrast name.
*/
function _CPAppearanceMatchingNames(aName)
{
switch (aName)
{
case CPAppearanceNameVibrantLight:
return [CPAppearanceNameVibrantLight, CPAppearanceNameAqua];
case CPAppearanceNameVibrantDark:
return [CPAppearanceNameVibrantDark, CPAppearanceNameDarkAqua];
case CPAppearanceNameAccessibilityHighContrastAqua:
return [CPAppearanceNameAccessibilityHighContrastAqua, CPAppearanceNameAqua];
case CPAppearanceNameAccessibilityHighContrastDarkAqua:
return [CPAppearanceNameAccessibilityHighContrastDarkAqua, CPAppearanceNameDarkAqua];
case CPAppearanceNameAccessibilityHighContrastVibrantLight:
return [CPAppearanceNameAccessibilityHighContrastVibrantLight, CPAppearanceNameAccessibilityHighContrastAqua,
CPAppearanceNameVibrantLight, CPAppearanceNameAqua];
case CPAppearanceNameAccessibilityHighContrastVibrantDark:
return [CPAppearanceNameAccessibilityHighContrastVibrantDark, CPAppearanceNameAccessibilityHighContrastDarkAqua,
CPAppearanceNameVibrantDark, CPAppearanceNameDarkAqua];
case CPAppearanceNameLightContent:
return [CPAppearanceNameLightContent, CPAppearanceNameAqua];
default:
return [aName];
}
}
@protocol CPAppearanceCustomization <CPObject>
@@ -84,6 +138,16 @@ CPThemeStateAppearanceVibrantDark = CPThemeState("appearance-vibrant-dark")
_CPAppearanceCurrent = anAppearance;
}
/*!
Returns the appearance used to resolve colours and images while drawing. It is the
current appearance: Aqua, unless +setCurrentAppearance: or
-performAsCurrentDrawingAppearance: has set another.
*/
+ (CPAppearance)currentDrawingAppearance
{
return [self currentAppearance];
}
/*! Returns the CPAppearance object with the given name
@param name the name of the appearance
*/
@@ -130,6 +194,48 @@ CPThemeStateAppearanceVibrantDark = CPThemeState("appearance-vibrant-dark")
return _name;
}
/*!
Returns the name in \c appearances that most closely matches the receiver, or \c nil if
none matches. A match keeps the receiver's lightness, so a light appearance matches no
dark name. A vibrant appearance falls back to the plain appearance of the same lightness,
and a high-contrast appearance to the appearance of normal contrast.
@param appearances the names of the appearances the caller supports
@return the closest name, or \c nil
*/
- (CPString)bestMatchFromAppearancesWithNames:(CPArray)appearances
{
var matchingNames = _CPAppearanceMatchingNames(_name);
for (var i = 0, count = matchingNames.length; i < count; i++)
{
if ([appearances containsObject:matchingNames[i]])
return matchingNames[i];
}
return nil;
}
/*!
Makes the receiver the current drawing appearance while \c aBlock runs, then restores
the previous one, also when \c aBlock raises an exception.
@param aBlock the function to run
*/
- (void)performAsCurrentDrawingAppearance:(Function)aBlock
{
var previousAppearance = _CPAppearanceCurrent;
_CPAppearanceCurrent = self;
try
{
aBlock();
}
finally
{
_CPAppearanceCurrent = previousAppearance;
}
}
- (BOOL)isEqual:(id)anObject
{
if (![anObject isKindOfClass:CPAppearance])
+184
View File
@@ -0,0 +1,184 @@
/*
* CPAppearanceTest.j
* AppKit
*
* Created by David Richardson on 2026-11-09.
* 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/CPAppearance.j>
/*
The expected matches for appearances of normal contrast are the results of AppKit's
-[NSAppearance bestMatchFromAppearancesWithNames:] on macOS 27, for the same names and
lists. AppKit reports a high-contrast appearance under its plain name unless Increase
Contrast is on, so the high-contrast expectations follow the rule in CPAppearance.j.
*/
@implementation CPAppearanceTest : OJTestCase
- (void)tearDown
{
[CPAppearance setCurrentAppearance:nil];
}
- (CPString)match:(CPString)aName in:(CPArray)someNames
{
return [[CPAppearance appearanceNamed:aName] bestMatchFromAppearancesWithNames:someNames];
}
// MARK: Names
- (void)testNamesUseAppKitStringsWithTheCPPrefix
{
var expectedNames = [
[CPAppearanceNameAqua, @"NSAppearanceNameAqua"],
[CPAppearanceNameDarkAqua, @"NSAppearanceNameDarkAqua"],
[CPAppearanceNameVibrantLight, @"NSAppearanceNameVibrantLight"],
[CPAppearanceNameVibrantDark, @"NSAppearanceNameVibrantDark"],
[CPAppearanceNameLightContent, @"NSAppearanceNameLightContent"],
[CPAppearanceNameAccessibilityHighContrastAqua, @"NSAppearanceNameAccessibilityAqua"],
[CPAppearanceNameAccessibilityHighContrastDarkAqua, @"NSAppearanceNameAccessibilityDarkAqua"],
[CPAppearanceNameAccessibilityHighContrastVibrantLight, @"NSAppearanceNameAccessibilityVibrantLight"],
[CPAppearanceNameAccessibilityHighContrastVibrantDark, @"NSAppearanceNameAccessibilityVibrantDark"]
];
expectedNames.forEach(function(aPair)
{
[self assert:aPair[1].replace(/^NS/, "CP") equals:aPair[0]];
});
}
// MARK: Best match
- (void)testAMatchKeepsLightness
{
[self assertNull:[self match:CPAppearanceNameAqua in:[CPAppearanceNameDarkAqua]]];
[self assertNull:[self match:CPAppearanceNameDarkAqua in:[CPAppearanceNameAqua]]];
[self assertNull:[self match:CPAppearanceNameVibrantDark in:[CPAppearanceNameAqua]]];
[self assert:CPAppearanceNameAqua equals:[self match:CPAppearanceNameAqua in:[CPAppearanceNameDarkAqua, CPAppearanceNameAqua]]];
[self assert:CPAppearanceNameDarkAqua equals:[self match:CPAppearanceNameDarkAqua in:[CPAppearanceNameAqua, CPAppearanceNameDarkAqua]]];
}
- (void)testAVibrantAppearanceFallsBackToThePlainAppearance
{
[self assert:CPAppearanceNameAqua equals:[self match:CPAppearanceNameVibrantLight in:[CPAppearanceNameAqua, CPAppearanceNameDarkAqua]]];
[self assert:CPAppearanceNameDarkAqua equals:[self match:CPAppearanceNameVibrantDark in:[CPAppearanceNameAqua, CPAppearanceNameDarkAqua]]];
[self assert:CPAppearanceNameVibrantDark
equals:[self match:CPAppearanceNameVibrantDark in:[CPAppearanceNameDarkAqua, CPAppearanceNameVibrantDark]]];
}
- (void)testAPlainAppearanceMatchesNoVibrantOrHighContrastName
{
[self assertNull:[self match:CPAppearanceNameAqua in:[CPAppearanceNameVibrantLight, CPAppearanceNameAccessibilityHighContrastAqua]]];
[self assertNull:[self match:CPAppearanceNameDarkAqua in:[CPAppearanceNameVibrantDark, CPAppearanceNameAccessibilityHighContrastDarkAqua]]];
[self assertNull:[self match:CPAppearanceNameVibrantLight in:[CPAppearanceNameAccessibilityHighContrastVibrantLight]]];
}
- (void)testAHighContrastAppearanceGivesUpVibrancyBeforeHighContrast
{
var highContrastVibrantDark = CPAppearanceNameAccessibilityHighContrastVibrantDark;
[self assert:CPAppearanceNameAccessibilityHighContrastDarkAqua
equals:[self match:highContrastVibrantDark in:[CPAppearanceNameVibrantDark, CPAppearanceNameAccessibilityHighContrastDarkAqua]]];
[self assert:CPAppearanceNameVibrantDark
equals:[self match:highContrastVibrantDark in:[CPAppearanceNameDarkAqua, CPAppearanceNameVibrantDark]]];
[self assert:CPAppearanceNameDarkAqua equals:[self match:highContrastVibrantDark in:[CPAppearanceNameAqua, CPAppearanceNameDarkAqua]]];
[self assertNull:[self match:highContrastVibrantDark in:[CPAppearanceNameAqua, CPAppearanceNameAccessibilityHighContrastAqua]]];
}
- (void)testAHighContrastAppearanceFallsBackToNormalContrast
{
[self assert:CPAppearanceNameAccessibilityHighContrastAqua
equals:[self match:CPAppearanceNameAccessibilityHighContrastAqua in:[CPAppearanceNameAqua, CPAppearanceNameAccessibilityHighContrastAqua]]];
[self assert:CPAppearanceNameAqua equals:[self match:CPAppearanceNameAccessibilityHighContrastAqua in:[CPAppearanceNameAqua]]];
[self assert:CPAppearanceNameVibrantLight
equals:[self match:CPAppearanceNameAccessibilityHighContrastVibrantLight in:[CPAppearanceNameAqua, CPAppearanceNameVibrantLight]]];
}
- (void)testLightContentFallsBackToAqua
{
[self assert:CPAppearanceNameAqua equals:[self match:CPAppearanceNameLightContent in:[CPAppearanceNameAqua, CPAppearanceNameDarkAqua]]];
[self assertNull:[self match:CPAppearanceNameLightContent in:[CPAppearanceNameDarkAqua]]];
}
- (void)testAnUnknownNameMatchesOnlyItself
{
var unknownName = @"CPAppearanceTestUnknownAppearance";
[self assertNull:[self match:unknownName in:[CPAppearanceNameAqua, CPAppearanceNameDarkAqua]]];
[self assert:unknownName equals:[self match:unknownName in:[CPAppearanceNameAqua, unknownName]]];
}
- (void)testAnEmptyListMatchesNothing
{
[self assertNull:[self match:CPAppearanceNameAqua in:[]]];
}
// MARK: Drawing appearance
- (void)testTheDrawingAppearanceIsAquaByDefault
{
[self assert:[CPAppearance appearanceNamed:CPAppearanceNameAqua] equals:[CPAppearance currentDrawingAppearance]];
}
- (void)testPerformAsCurrentDrawingAppearanceSetsItForTheBlockOnly
{
var darkAqua = [CPAppearance appearanceNamed:CPAppearanceNameDarkAqua],
appearanceInBlock = nil;
[darkAqua performAsCurrentDrawingAppearance:function()
{
appearanceInBlock = [CPAppearance currentDrawingAppearance];
}];
[self assert:darkAqua equals:appearanceInBlock];
[self assert:[CPAppearance appearanceNamed:CPAppearanceNameAqua] equals:[CPAppearance currentDrawingAppearance]];
}
- (void)testPerformAsCurrentDrawingAppearanceNests
{
var darkAqua = [CPAppearance appearanceNamed:CPAppearanceNameDarkAqua],
vibrantDark = [CPAppearance appearanceNamed:CPAppearanceNameVibrantDark],
appearanceAfterInnerBlock = nil;
[darkAqua performAsCurrentDrawingAppearance:function()
{
[vibrantDark performAsCurrentDrawingAppearance:function() { }];
appearanceAfterInnerBlock = [CPAppearance currentDrawingAppearance];
}];
[self assert:darkAqua equals:appearanceAfterInnerBlock];
}
- (void)testPerformAsCurrentDrawingAppearanceRestoresTheAppearanceAfterAnException
{
var darkAqua = [CPAppearance appearanceNamed:CPAppearanceNameDarkAqua];
[self assertThrows:function()
{
[darkAqua performAsCurrentDrawingAppearance:function()
{
[CPException raise:CPInternalInconsistencyException reason:@"raised inside the block"];
}];
}];
[self assert:[CPAppearance appearanceNamed:CPAppearanceNameAqua] equals:[CPAppearance currentDrawingAppearance]];
}
@end