ThemeManager
public class ThemeManager: NSObject
Use ThemeManager
shared instance to perform app-wide theming related operations,
such as:
- Get information about current theme/appearance
- Change current
theme
(can also be changed fromNSUserDefaults
) - List available themes
- Define
ThemeKit
behaviour
-
ThemeManager shared manager.
Declaration
Swift
public static let shared = ThemeManager()
-
Enables or disables ThemeKit functionality.
Declaration
Swift
@objc public var isEnabled: Bool
-
Sets or returns the current theme.
This property is KVO compliant. Value is stored on user defaults under key
userDefaultsThemeKey
.Declaration
Swift
@objc public var theme: Theme
-
Returns the current effective theme (read-only).
This property is KVO compliant. This can return a different result than
theme
, as if current theme is set toSystemTheme
, effective theme will be eitherlightTheme
ordarkTheme
, respecting user preference at System Preferences > General > Appearance.Declaration
Swift
@objc public var effectiveTheme: Theme
-
List all available themes:
- Built-in
lightTheme
- Built-in
darkTheme
- Built-in
systemTheme
- All native themes (extending
NSObject
and conforming toTheme
protocol) - All user themes (loaded from
.theme
files)
This property is KVO compliant and will change when changes occur on user themes folder.
Declaration
Swift
@objc public var themes: [Theme]
- Built-in
-
Declaration
Swift
@objc public var userThemes: [Theme]
-
Convenience method for accessing the light theme.
This property can be changed so that
SystemTheme
resolves to this theme instead of the defaultLightTheme
.Declaration
Swift
@objc public static var lightTheme: Theme = LightTheme()
-
Convenience method for accessing the dark theme.
This property can be changed so that
SystemTheme
resolves to this theme instead of the defaultDarkTheme
. -
Convenience method for accessing the theme that dynamically changes to
ThemeManager.lightTheme
orThemeManager.darkTheme
, respecting user preference at System Preferences > General > Appearance.Declaration
Swift
@objc public static let systemTheme = SystemTheme()
-
Set/get default theme to be used on the first run (default:
ThemeManager.systemTheme
).Declaration
Swift
@objc public static var defaultTheme: Theme = ThemeManager.systemTheme
-
Get the theme with specified identifier.
Declaration
Swift
@objc public func theme(withIdentifier identifier: String?) -> Theme?
Parameters
identifier
The unique
Theme.identifier
string.Return Value
The
Theme
instance with the given identifier. -
User defaults key for current
theme
.Current
theme.identifier
will be stored under the"ThemeKitTheme"
NSUserDefaults
key.Declaration
Swift
@objc static public let userDefaultsThemeKey = "ThemeKitTheme"
-
Apply last applied theme, or default, if none.
Get last applied theme from user defaults and load it. If no theme was previously applied, load the default theme (
ThemeManager.defaultTheme
).Declaration
Swift
@objc public func applyLastOrDefaultTheme()
-
Force-apply current
theme
.Normally you should not need to invoke this method, as this will force-apply the same theme.
Declaration
Swift
@objc public func reApplyCurrentTheme()
-
Location of user provided themes (.theme files).
Ideally, this should be on a shared location, like
Application Support/{app_bundle_id}/Themes
for example. Here’s an example of how to get this folder (*):public var applicationSupportUserThemesFolderURL: URL { let applicationSupportURLs = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true) let thisAppSupportURL = URL(fileURLWithPath: applicationSupportURLs.first!).appendingPathComponent(Bundle.main.bundleIdentifier!) return thisAppSupportURL.appendingPathComponent("Themes") }
*: force wrapping (!) is for illustrative purposes only.
You can also bundle these files with your application bundle, if you don’t want them to be changed.
Declaration
Swift
@objc public var userThemesFolderURL: URL?
-
Appearance in use for effective theme.
Declaration
Swift
@objc public var effectiveThemeAppearance: NSAppearance
-
Convenience method to get the light appearance.
Declaration
Swift
@objc public var lightAppearance: NSAppearance?
-
Convenience method to get the dark appearance.
Declaration
Swift
@objc public var darkAppearance: NSAppearance?
-
Window theme policies that define which windows should be automatically themed, if any.
Swift
By default, all application windows (except
NSPanel
) will be themed (.themeAllWindows
).themeAllWindows
: Theme all application windows, exceptNSPanel
subclasses (default).themeSomeWindows
: Only theme windows of the specified classes.doNotThemeSomeWindows
: Do not theme windows of the specified classes.doNotThemeWindows
: Do not theme any window.E.g.:
E.g.:
ThemeManager.shared.windowThemePolicy = .themeSomeWindows(windowClasses: [CustomWindow.self])
Objective-C
By default, all application windows (except
NSPanel
) will be themed (TKThemeManagerWindowThemePolicyThemeAllWindows
).TKThemeManagerWindowThemePolicyThemeAllWindows
: Theme all application windows, exceptNSPanel
subclasses (default).TKThemeManagerWindowThemePolicyThemeSomeWindows
: Only theme windows of the specified classes (usethemableWindowClasses
property).TKThemeManagerWindowThemePolicyDoNotThemeSomeWindows
: Do not theme windows of the specified classes (usenotThemableWindowClasses
property).TKThemeManagerWindowThemePolicyDoNotThemeWindows
: Do not theme any window.
Example:
[TKThemeManager sharedManager].windowThemePolicy = TKThemeManagerWindowThemePolicyThemeSomeWindows; [TKThemeManager sharedManager].themableWindowClasses = @[[CustomWindow class]];
NSWindow Extension
-
Theme window if appearance needs update. Doesn’t check for policy compliance.
NSWindow.isCompliantWithWindowThemePolicy()
Check if window complies to current policy.
NSWindow.themeIfCompliantWithWindowThemePolicy()
Theme window if compliant to
windowThemePolicy
(and if appearance needs update).-
Theme all windows compliant to ThemeManager.windowThemePolicy (and if appearance needs update).
Declaration
Swift
public enum WindowThemePolicy
-
Current window theme policy.
Declaration
Swift
public var windowThemePolicy: WindowThemePolicy = .themeAllWindows
-
Animate theme transitions?
Declaration
Swift
@objc public var animateThemeTransitions: Bool = true
-
ThemeKit notification sent when current theme is about to change.
Declaration
Swift
@objc public static let willChangeThemeNotification = Notification.Name.willChangeTheme
-
ThemeKit notification sent when current theme did change.
Declaration
Swift
@objc public static let didChangeThemeNotification = Notification.Name.didChangeTheme
-
ThemeKit notification sent when system theme did change (System Preference > General > Appearance).
Declaration
Swift
@objc public static let didChangeSystemThemeNotification = Notification.Name.didChangeSystemTheme