Browse Source

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
master
Matt Baer 5 years ago
parent
commit
27c398ec40
2 changed files with 20 additions and 6 deletions
  1. +16
    -6
      writeas/AppDelegate.swift
  2. +4
    -0
      writeas/WindowController.swift

+ 16
- 6
writeas/AppDelegate.swift 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
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
globalSettings.isNight = isOff
UserDefaults.standard.set(String(sender.state), forKey: "night_mode_state")
}



+ 4
- 0
writeas/WindowController.swift 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) {


Loading…
Cancel
Save