DSFAppearanceManager

main

Theme and Appearance handling for macOS Appkit (Swift/Objective-C).
dagronf/DSFAppearanceManager

DSFAppearanceManager

A class for simplifying macOS appearance values and detecting setting changes (Swift/Objective-C).

Supported back to macOS 10.11 with sensible fallbacks on older systems to reduce the #available/@available dance in your code.

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.

  1. On different macOS systems, the method for retrieving these values can differ (and on earlier systems are quite difficult to extract reliably). This library wraps away all these inconsistencies so your code can remain clean(er).
  2. 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. macOS calls some methods automatically when the appearance changes :-

NSView

  • updateLayer
  • drawRect(dirtyRect: NSRect)
  • layout
  • updateConstraints

NSViewController

  • updateViewConstraints
  • viewWillLayout
  • viewDidLayout

but there are times where you need to manage this yourself. This is where the ChangeDetector class is used.

Declare a variable of type DSFAppearanceManager.ChangeDetector()

private let appearanceChangeDetector = DSFAppearanceManager.ChangeDetector()

... and set the callback block. Note that this callback is guaranteed to be called on the main thread.

appearanceChangeDetector.appearanceChangeCallback = { [weak self] change in
   // Handle the change here.
   // `change` contains the _types_ of change(s) that occurred. This might be theme, accent, contrastOrAccessibility etc
   let currentHighlightColor = DSFAppearanceManager.HighlightColor
   ...
}

Change detection types

The change object indicates 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:^(DSFAppearanceManagerChange * _Nonnull change) {
      // Change detected! Do something to update display
   }];
}
@end

Centralized notifications (DSFAppearanceCache)

If you have lots and lots of little classes that need to be updated, it may be more efficient to centralize the change notifications in a common location.

The library provides a default global (lazy) DSFAppearanceCache.shared object instance you can use, or you can create and manage one yourself.

The appearance cache provides two mechanisms for receiving appearance update notifications.

Register for updates directly with the cache

You can register an object to receive appearance updates by conforming your object to the DSFAppearanceCacheNotifiable protocol. The object is held weakly within the cache object.

Example

class LevelGauge: CustomLayer, DSFAppearanceCacheNotifiable {
   init() {
      DSFAppearanceCache.shared.register(self)
   }

   deinit {
      DSFAppearanceCache.shared.deregister(self)
   }

   func appearanceDidChange() {
      // Update the object
   }
}

Register for updates via NotificationCenter

The change center object DSFAppearanceCache generates notifications on NotificationCenter.default.

Notification name: DSFAppearanceCache.ChangeNotificationName

You can register for notifications using the standard addObserver mechanisms.

Example

self.observer = NotificationCenter.default.addObserver(
   forName: DSFAppearanceCache.ChangeNotificationName,
   object: DSFAppearanceCache.shared,
   queue: OperationQueue.main) { _ in
      // Do something with the change
}

Make sure to keep the code in your 'appearanceDidChange' fast!

Additional support

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.usingEffectiveAppearance {
      // Requests for dynamic colors etc. within the block will automatically use the correct appearance for the view.
   }
}

Rolling your own dynamic NSColor

If you can't use the Assets.xcassets to store your dynamic NSColors (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.

Description

  • Swift Tools 5.3.0
View More Packages from this Author

Dependencies

  • None
Last updated: Mon Sep 19 2022 02:01:58 GMT-0500 (GMT-05:00)