Skip to content

Commit a767eaf

Browse files
committed
Added ability to enable/disable theming app-wide
1 parent 38d563e commit a767eaf

File tree

3 files changed

+51
-19
lines changed

3 files changed

+51
-19
lines changed

Demo/Demo/AppDelegate.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ class AppDelegate: NSObject, NSApplicationDelegate {
1717

1818
func applicationWillFinishLaunching(_ notification: Notification) {
1919

20+
/* 0. Enable/disable theming capabilities. */
21+
//ThemeManager.shared.isEnabled = false
22+
2023
/* 1. Simpler usage: switch between light and dark theme directly. */
2124

2225
//ThemeManager.lightTheme.apply()

Sources/NSColor+ThemeKit.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ extension NSColor {
9696
themeKitSet()
9797

9898
// check if the user provides an alternative color
99-
if isThemeOverriden {
99+
if ThemeManager.shared.isEnabled && isThemeOverriden {
100100
// call ThemeColor.set() function
101101
ThemeColor.color(with: Selector(colorNameComponent)).set()
102102
}
@@ -108,7 +108,7 @@ extension NSColor {
108108
themeKitSetFill()
109109

110110
// check if the user provides an alternative color
111-
if isThemeOverriden {
111+
if ThemeManager.shared.isEnabled && isThemeOverriden {
112112
// call ThemeColor.setFill() function
113113
ThemeColor.color(with: Selector(colorNameComponent)).setFill()
114114
}
@@ -120,7 +120,7 @@ extension NSColor {
120120
themeKitSetStroke()
121121

122122
// check if the user provides an alternative color
123-
if isThemeOverriden {
123+
if ThemeManager.shared.isEnabled && isThemeOverriden {
124124
// call ThemeColor.setStroke() function
125125
ThemeColor.color(with: Selector(colorNameComponent)).setStroke()
126126
}

Sources/ThemeManager.swift

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,53 @@ public class ThemeManager: NSObject {
3333
private override init() {
3434
super.init()
3535

36-
// Initialize custom NSColor code (swizzle NSColor, if needed)
37-
NSColor.swizzleNSColor()
36+
isEnabled = true
37+
}
3838

39-
// Observe and theme new windows (before being displayed onscreen)
40-
self.obj = NotificationCenter.default.addObserver(forName: NSWindow.didUpdateNotification, object: nil, queue: nil) { (notification) in
41-
if let window = notification.object as? NSWindow {
42-
window.themeIfCompliantWithWindowThemePolicy()
43-
}
39+
deinit {
40+
isEnabled = false
41+
}
42+
43+
/// Enables or disables ThemeKit functionality.
44+
@objc public var isEnabled: Bool {
45+
get {
46+
return _isEnabled ?? false
4447
}
48+
set {
49+
guard _isEnabled != newValue else { return }
50+
_isEnabled = newValue
51+
52+
// enable
53+
if newValue {
54+
// Initialize custom NSColor code (swizzle NSColor, if needed - done only once)
55+
NSColor.swizzleNSColor()
56+
57+
// Observe and theme new windows (before being displayed onscreen)
58+
self.obj = NotificationCenter.default.addObserver(forName: NSWindow.didUpdateNotification, object: nil, queue: nil) { (notification) in
59+
if let window = notification.object as? NSWindow {
60+
window.themeIfCompliantWithWindowThemePolicy()
61+
}
62+
}
4563

46-
// Observe current theme on User Defaults
47-
NSUserDefaultsController.shared.addObserver(self, forKeyPath: themeChangeKVOKeyPath, options: NSKeyValueObservingOptions(rawValue: 0), context: nil)
64+
// Observe current theme on User Defaults
65+
NSUserDefaultsController.shared.addObserver(self, forKeyPath: themeChangeKVOKeyPath, options: NSKeyValueObservingOptions(rawValue: 0), context: nil)
4866

49-
// Observe current system theme (macOS Apple Interface Theme)
50-
NotificationCenter.default.addObserver(self, selector: #selector(systemThemeDidChange(_:)), name: .didChangeSystemTheme, object: nil)
51-
}
67+
// Observe current system theme (macOS Apple Interface Theme)
68+
NotificationCenter.default.addObserver(self, selector: #selector(systemThemeDidChange(_:)), name: .didChangeSystemTheme, object: nil)
69+
}
5270

53-
deinit {
54-
if let object = self.obj {
55-
NotificationCenter.default.removeObserver(object)
71+
// disable
72+
else {
73+
if let object = self.obj {
74+
NotificationCenter.default.removeObserver(object)
75+
}
76+
NSUserDefaultsController.shared.removeObserver(self, forKeyPath: themeChangeKVOKeyPath)
77+
NotificationCenter.default.removeObserver(self, name: .didChangeSystemTheme, object: nil)
78+
}
5679
}
57-
NSUserDefaultsController.shared.removeObserver(self, forKeyPath: themeChangeKVOKeyPath)
5880
}
81+
/// Internal storage for `isEnabled` property.
82+
private var _isEnabled: Bool?
5983

6084
// MARK: -
6185
// MARK: Themes
@@ -69,6 +93,8 @@ public class ThemeManager: NSObject {
6993
return _theme ?? ThemeManager.defaultTheme
7094
}
7195
set(newTheme) {
96+
guard isEnabled else { return }
97+
7298
// Apply theme
7399
if _theme == nil || newTheme.effectiveTheme != _theme! || newTheme.effectiveTheme.isUserTheme {
74100
applyTheme(newTheme)
@@ -208,6 +234,8 @@ public class ThemeManager: NSObject {
208234
/// Get last applied theme from user defaults and load it. If no theme was
209235
/// previously applied, load the default theme (`ThemeManager.defaultTheme`).
210236
@objc public func applyLastOrDefaultTheme() {
237+
guard isEnabled else { return }
238+
211239
let userDefaultsTheme = theme(withIdentifier: UserDefaults.standard.string(forKey: ThemeManager.userDefaultsThemeKey))
212240
(userDefaultsTheme ?? ThemeManager.defaultTheme).apply()
213241
}
@@ -444,6 +472,7 @@ public class ThemeManager: NSObject {
444472

445473
/// Apply a new `theme`.
446474
private func applyTheme(_ newTheme: Theme) {
475+
guard isEnabled else { return }
447476

448477
// Make theme effective
449478
func makeThemeEffective(_ newTheme: Theme) {

0 commit comments

Comments
 (0)