Theme handling macOS Appkit (Swift/Objective-C)

Overview

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.
   }
}

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.
You might also like...
LinkedIn's Official Swift Style Guide

Swift Style Guide Make sure to read Apple's API Design Guidelines. Specifics from these guidelines + additional remarks are mentioned below. This guid

A starter project for Sample Project in swift 5, Xcode 12.5
A starter project for Sample Project in swift 5, Xcode 12.5

A starter project for Sample Project in swift 5, Xcode 12.5 (also bridging header included so you could use objective c code in it as well ).

Declarative view styling in Swift. Inspired by CSS modules.
Declarative view styling in Swift. Inspired by CSS modules.

Gaikan gives you powerful styling capabilities using a declarative DSL in Swift. Inspired by React: CSS in JS and CSS modules. To style UIView(s) just

JSPatch bridge Objective-C and Javascript using the Objective-C runtime. You can call any Objective-C class and method in JavaScript by just including a small engine. JSPatch is generally used to hotfix iOS App.

JSPatch 中文介绍 | 文档 | JSPatch平台 请大家不要自行接入 JSPatch,统一接入 JSPatch 平台,让热修复在一个安全和可控的环境下使用。原因详见 这里 JSPatch bridges Objective-C and JavaScript using the Object

The sample implementation of zip-archived document for a macOS AppKit platform.
The sample implementation of zip-archived document for a macOS AppKit platform.

The sample implementation of zip-archived document for a macOS AppKit platform. You can implement NSDocument-based I/O of archived document in your application like .sketch or .key.

A Swift library for documenting, isolating, and testing SwiftUI, UIKIt & AppKit components.
A Swift library for documenting, isolating, and testing SwiftUI, UIKIt & AppKit components.

A Swift library for documenting, isolating, and testing SwiftUI, UIKit & AppKit components. Minimal Example An example demonstrated with the Slider ui

RichTextKit is a Swift-based library for working with rich text in UIKit, AppKit and SwiftUI.
RichTextKit is a Swift-based library for working with rich text in UIKit, AppKit and SwiftUI.

About RichTextKit RichTextKit is a Swift-based library that lets you work with rich text in UIKit, AppKit and SwiftUI. RichTextKit is under developmen

Advanced Catalyst Example with sidebar, list view, SwiftUI detail view, toolbar & AppKit bundle
Advanced Catalyst Example with sidebar, list view, SwiftUI detail view, toolbar & AppKit bundle

Advanced Catalyst Example This is an example of a Catalyst app using a three-column layout, with a primary toolbar. It includes topics such as: Drag &

An example of adding a faux notch using AppKit + SwiftUI
An example of adding a faux notch using AppKit + SwiftUI

faux-notch An example of adding a faux notch using AppKit + SwiftUI What is this? It's a bare-bones example that renders a fake MacBook notch in macOS

AppKit Carbon key codes to UIKey-compatible enums

KeyCodes Versions of UIKey, UIKeyboardHIDUsage, and UIKeyModifierFlags that work with AppKit's NSEvent. No need for Carbon.HIToolbox. Aside from being

Match animations in SwiftUI and UIKit/AppKit
Match animations in SwiftUI and UIKit/AppKit

MatchedAnimation Match animations in SwiftUI and UIKit/AppKit. /// Draw a box in

Catalyst example using an AppKit-provided NSVisualEffectView to provide a translucent blurred window
Catalyst example using an AppKit-provided NSVisualEffectView to provide a translucent blurred window

CatalystEffectViewChrome This project demonstrates how to insert an NSVisualEffe

AppKitFocusOverlay - Add hotkey(s) to display the key focus path for any window in your (running) AppKit application
AppKitFocusOverlay - Add hotkey(s) to display the key focus path for any window in your (running) AppKit application

AppKitFocusOverlay A simple package for displaying the current focus target path

FastLayout - A UIKit or AppKit package for fast UI design

FastLayout FastLayout is a UIKit or AppKit package for fast UI design. Layout Ex

This is a simple Mac Catalyst example showcasing how to build a dropdown menu toolbar button using AppKit.
This is a simple Mac Catalyst example showcasing how to build a dropdown menu toolbar button using AppKit.

CatalystToolbarMenuButton This is a simple Mac Catalyst example showcasing how to build a dropdown menu toolbar button using AppKit. There are ways to

RavynOS File manager built in Cocoa/Appkit and ObjC

Filer A file manager and re-implementation of macOS's Finder. A key component of ravynOS, Filer is the first application you see after you start ravyn

👕👚 Theme management in Swift
👕👚 Theme management in Swift

Themes Story Ever want to support Night mode? Or skin the app differently depending on the seasons? Or toggle features according to paid status? Well,

FlexibleImage is implemented with the hope that anyone could easily develop an app that provides features such as Camera Filter and Theme.
FlexibleImage is implemented with the hope that anyone could easily develop an app that provides features such as Camera Filter and Theme.

FlexibleImage is implemented with the hope that anyone could easily develop an app that provides features such as Camera Filter and Theme. When you wr

Owner
Darren Ford
Darren Ford
🎨 Powerful theme/skin manager for iOS 9+ 主题/换肤, 暗色模式

Introduction - Demos - Installation - Documents - FAQ - Contribution - 中文文档 Screenshot Running:open SwiftTheme.xcworkspace, run target PlistDemo Intro

Gesen 2.4k Jan 1, 2023
A Publish theme. ckitakishi.com is built with PaletteTheme

PaletteTheme A Publish theme. ckitakishi.com is built with PaletteTheme. Features Simple and fast Mobile friendly Support both Light/Dark mode Customi

Yuhan Chen 10 Nov 29, 2022
This repository contains 🎨 My Xcode theme that I use. It is compatible with all versions of Xcode.

DRL Xcodetheme Installation Automatic (via script) ./install.sh which will install the file in Xcode FontAndColorThemes directory. Restart Xcode Go t

durul dalkanat 19 Oct 21, 2022
A powerful lightweight theme 🎨 manager for SwiftUI

SwiftTheming ?? is a handy lightweight handy theme manager which handles multiple themes based on system-wide appearances - light and dark appearances

Dscyre Scotti  38 Dec 29, 2022
Solarized Dark Theme for Xcode. Compatible with all modern versions of Xcode since 2013!

Solarized Dark for Xcode Note I've moved away from using Solarized to a Night-Shift/Dark-Mode-friendly palette of my own creation; Cognac. It's availa

Arthur Ariel Sabintsev 365 Nov 25, 2022
The Objective-C Style Guide used by The New York Times

NYTimes Objective-C Style Guide This style guide outlines the coding conventions of the iOS teams at The New York Times. We welcome your feedback in i

The New York Times 5.8k Jan 6, 2023
Style guide & coding conventions for Objective-C projects

This repository is no longer active. These guidelines build on Apple's existing Coding Guidelines for Cocoa. Unless explicitly contradicted below, ass

GitHub 1.7k Dec 9, 2022
The official Swift style guide for raywenderlich.com.

The Official raywenderlich.com Swift Style Guide. Updated for Swift 5 This style guide is different from others you may see, because the focus is cent

raywenderlich 12.5k Dec 30, 2022
Style guide & coding conventions for Swift projects

This repository is no longer active. A guide to our Swift style and conventions. This is an attempt to encourage patterns that accomplish the followin

GitHub 4.8k Jan 4, 2023
A style guide for Swift.

Table Of Contents Overview Linter Standards Naming Conventions File Structure Types Statement Termination Variable Declaration Self Structs & Classes

Prolific Interactive 171 Oct 4, 2022