Make Night Mode global for all windows

- Now it changes all windows when enabled
- Newly launched windows respect the setting
- Multiple windows restored on launch respect the setting
This commit is contained in:
Matt Baer 2017-08-03 18:16:14 -04:00
parent c2185d3c9f
commit 27c398ec40
2 changed files with 20 additions and 6 deletions

View File

@ -20,12 +20,18 @@ class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var formatFontSansItem: NSMenuItem!
@IBOutlet weak var formatFontMonoItem: NSMenuItem!
struct GlobalSettings {
var isNight = false
}
var globalSettings = GlobalSettings()
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Set night mode state
nightModeItem.state = Int(UserDefaults.standard.string(forKey: "night_mode_state") ?? String(NSOffState))!
if nightModeItem.state == NSOnState {
let wc = NSApplication.shared().mainWindow!.windowController as? WindowController
wc?.toggle(isNight: true)
let nightModeState = Int(UserDefaults.standard.string(forKey: "night_mode_state") ?? String(NSOffState))!
globalSettings.isNight = nightModeState == NSOnState
if globalSettings.isNight {
// Toggle Night Mode menu item on
toggleNightMode(nightModeItem)
}
// Set font
@ -64,10 +70,14 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
@IBAction func toggleNightMode(_ sender: NSMenuItem) {
let wc = NSApplication.shared().mainWindow!.windowController as? WindowController
let isOff = sender.state == NSOffState
for w in NSApplication.shared().windows {
let wc = w.windowController as? WindowController
wc?.toggle(isNight: isOff)
}
sender.state = isOff ? NSOnState : NSOffState
globalSettings.isNight = isOff
UserDefaults.standard.set(String(sender.state), forKey: "night_mode_state")
}

View File

@ -18,6 +18,10 @@ class WindowController: NSWindowController {
super.windowDidLoad()
vc = (self.window?.contentViewController)! as! ViewController
let delegate = NSApplication.shared().delegate as! AppDelegate
// Set night mode state
vc?.toggle(isNight: delegate.globalSettings.isNight)
}
func toggle(isNight: Bool) {