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

View File

@ -18,6 +18,10 @@ class WindowController: NSWindowController {
super.windowDidLoad() super.windowDidLoad()
vc = (self.window?.contentViewController)! as! ViewController 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) { func toggle(isNight: Bool) {