The Write.as desktop (GUI) app for macOS.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.8 KiB

  1. //
  2. // AppDelegate.swift
  3. // writeas
  4. //
  5. // Created by Matt Baer on 12/29/15.
  6. // Copyright © 2015 A Bunch Tell. All rights reserved.
  7. //
  8. import Cocoa
  9. import Foundation
  10. @NSApplicationMain
  11. class AppDelegate: NSObject, NSApplicationDelegate {
  12. var vc: ViewController?
  13. @IBOutlet weak var filePublishItem: NSMenuItem!
  14. @IBOutlet weak var nightModeItem: NSMenuItem!
  15. @IBOutlet weak var formatFontSerifItem: NSMenuItem!
  16. @IBOutlet weak var formatFontSansItem: NSMenuItem!
  17. @IBOutlet weak var formatFontMonoItem: NSMenuItem!
  18. struct GlobalSettings {
  19. var isNight = false
  20. }
  21. var globalSettings = GlobalSettings()
  22. func applicationDidFinishLaunching(_ aNotification: Notification) {
  23. // Set night mode state
  24. let nightModeState = Int(UserDefaults.standard.string(forKey: "night_mode_state") ?? String(NSOffState))!
  25. globalSettings.isNight = nightModeState == NSOnState
  26. if globalSettings.isNight {
  27. // Toggle Night Mode menu item on
  28. toggleNightMode(nightModeItem)
  29. }
  30. // Set font
  31. let font = Preferences.getFont()
  32. if font == Preferences.PostFont.serif {
  33. formatFontSerifItem.state = NSOnState
  34. } else if font == Preferences.PostFont.sans {
  35. formatFontSansItem.state = NSOnState
  36. } else if font == Preferences.PostFont.mono {
  37. formatFontMonoItem.state = NSOnState
  38. }
  39. }
  40. func applicationWillTerminate(_ aNotification: Notification) {
  41. vc?.saveDocument()
  42. }
  43. func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
  44. return true
  45. }
  46. func applicationDidBecomeActive(_ notification: Notification) {
  47. if vc == nil {
  48. vc = NSApplication.shared().mainWindow?.contentViewController as? ViewController
  49. }
  50. DispatchQueue.global(qos: .background).async {
  51. if !FileManager.default.fileExists(atPath: Constants.draftDir, isDirectory: nil) {
  52. do {
  53. try FileManager.default.createDirectory(atPath: Constants.draftDir, withIntermediateDirectories: false, attributes: nil)
  54. } catch let error as NSError {
  55. print(error.localizedDescription);
  56. }
  57. }
  58. }
  59. }
  60. @IBAction func toggleNightMode(_ sender: NSMenuItem) {
  61. let isOff = sender.state == NSOffState
  62. for w in NSApplication.shared().windows {
  63. let wc = w.windowController as? WindowController
  64. wc?.toggle(isNight: isOff)
  65. }
  66. sender.state = isOff ? NSOnState : NSOffState
  67. globalSettings.isNight = isOff
  68. UserDefaults.standard.set(String(sender.state), forKey: "night_mode_state")
  69. }
  70. @IBAction func setFontSerif(_ sender: NSMenuItem) {
  71. deselectAllFonts()
  72. sender.state = NSOnState
  73. vc?.setFont(.serif)
  74. Preferences.setFont(.serif)
  75. }
  76. @IBAction func setFontSans(_ sender: NSMenuItem) {
  77. deselectAllFonts()
  78. sender.state = NSOnState
  79. vc?.setFont(.sans)
  80. Preferences.setFont(.sans)
  81. }
  82. @IBAction func setFontMono(_ sender: NSMenuItem) {
  83. deselectAllFonts()
  84. sender.state = NSOnState
  85. vc?.setFont(.mono)
  86. Preferences.setFont(.mono)
  87. }
  88. func deselectAllFonts() {
  89. formatFontSerifItem.state = NSOffState
  90. formatFontSansItem.state = NSOffState
  91. formatFontMonoItem.state = NSOffState
  92. }
  93. @IBAction func saveDocument(_ sender: AnyObject) {
  94. vc?.saveDocument()
  95. }
  96. @IBAction func publishDoc(_ sender: Any) {
  97. filePublishItem.isEnabled = false
  98. vc?.publish()
  99. }
  100. @IBAction func viewIncTextSize(_ sender: Any) {
  101. vc?.adjustTextSize(increment: true)
  102. }
  103. @IBAction func viewDecTextSize(_ sender: Any) {
  104. vc?.adjustTextSize(increment: false)
  105. }
  106. }