DSFAppearanceManager
A class for simplifying macOS appearance values and detecting setting changes (Swift/Objective-C).
Why?
If you're performing custom drawing within your macOS app, it's important to obey the user's display and accessibility settings when performing your drawing so you can adapt accordingly.
Secondly, when the user changes their settings (eg. when the system changes automatically light/dark modes) I wanted my app to be notified of the change so I can update the drawing to match the new setting(s).
Appearance
DSFAppearanceManager
has a number of properties to simplify macOS appearance settings
Available properties
These are the static properties available on the DSFAppearanceManager
Properties | Description |
---|---|
IsDark |
Is the UI currently being displayed as dark |
IsDarkMenu |
Are the menu and dock currently being displayed as dark |
AccentColor |
The current accent color |
HighlightColor |
The current highlight color |
AquaVariant |
The current aqua variant |
IncreaseContrast |
The user's 'Increase Contrast' accessibility setting |
DifferentiateWithoutColor |
The user's 'Differentiate without color' accessibility setting |
ReduceTransparency |
The user's 'Reduce transparency' accessibility setting |
InvertColors |
The user's 'Invert colors' accessibility setting |
ReduceMotion |
The user's 'Reduce motion' accessibility setting |
So, for example, to get the current macOS highlight color, call DSFAppearanceManager.HighlightColor
.
Change detection
You can ask to be notified when appearance settings changes.
Declare a variable of type DSFAppearanceManager.ChangeDetector()
private let appearanceChangeDetector = DSFAppearanceManager.ChangeDetector()
... and set the callback block.
appearanceChangeDetector.appearanceChangeCallback = { [weak self] manager, change in
// Handle the change here.
// `change` contains the _types_ of change(s) that occurred.
// This might be theme, accent, contrastOrAccessibility etc
let newColor = manager.accentColor
...
}
Done!
Change detection types
The change object will indicate the type of change that occurred.
Change type | Description |
---|---|
theme |
The system appearance (eg. dark/light) changed |
accent |
The user changed the accent color(s) eg. accent/highlight |
aquaVariant |
For older macOS versions, the variant (blue, graphite) |
systemColors |
The user changed the system colors |
finderLabelColorsChanged |
The user changed finder label color(s) |
accessibility |
The accessibility display settings changed |
Note that the change detection class debounces changes to reduce the number of callbacks when a change occurs. The change
object passed in the callback block contains a set of the changes that occurred.
Objective-C support
@interface ViewController ()
@property(nonatomic, strong) DSFAppearanceManagerChangeDetector* detector;
@end
@implementation ViewController
- (void)viewDidAppear {
[super viewDidAppear];
[self setDetector: [[DSFAppearanceManagerChangeDetector alloc] init]];
[[self detector] setAppearanceChangeCallback:^(DSFAppearanceManager * _Nonnull manager, DSFAppearanceManagerChange * _Nonnull change) {
// Change detected! Do something to update display
}];
}
@end
Additional info
NSView
appearance drawing
DSFAppearanceManager
provides extensions to NSView
as a convenience for automatically handling the view's effective drawing appearance.
func drawRect(_ dirtyRect: CGRect) {
...
self.performUsingEffectiveAppearance { appearance in
// Do your drawing using 'appearance'
// Requests for dynamic colors etc. will automatically use the correct appearance for the view.
}
}
NSColor
Rolling your own dynamic If you can't use the Assets.xcassets
to store your dynamic NSColor
s (or you want to move your app's configuration into code) you'll find that the default NSColor
doesn't have much support for automatically handling light/dark mode changes.
Dusk is a small swift framework to aid in supporting Dark Mode on macOS. It provides an NSColor
subclass (DynamicColor
) that automatically provides light/dark mode variants when required.
lazy var c1 = DynamicColor(name: "uniqueColorName") { (appearance) in
// return the color to use for this appearance
}
let c1 = DynamicColor(name: "uniqueColorName", lightColor: NSColor.white, darkColor: NSColor.black)
And because DynamicColor
inherits from NSColor
, it can be used wherever NSColor
can be used.
Thanks!
ChimeHQ
for developing the awesome dynamic NSColor subclass.
License
MIT. Use it and abuse it for anything you want, just attribute my work. Let me know if you do use it somewhere, I'd love to hear about it!
MIT License
Copyright (c) 2022 Darren Ford
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.