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.

143 lines
4.3 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 fileClose(_ sender: Any) {
  61. NSApplication.shared().mainWindow!.close()
  62. }
  63. @IBAction func toggleNightMode(_ sender: NSMenuItem) {
  64. let isOff = sender.state == NSOffState
  65. for w in NSApplication.shared().windows {
  66. let wc = w.windowController as? WindowController
  67. wc?.toggle(isNight: isOff)
  68. }
  69. sender.state = isOff ? NSOnState : NSOffState
  70. globalSettings.isNight = isOff
  71. UserDefaults.standard.set(String(sender.state), forKey: "night_mode_state")
  72. }
  73. @IBAction func formatStrong(_ sender: Any) {
  74. let wc = NSApplication.shared().mainWindow?.windowController as? WindowController
  75. wc?.embolden()
  76. }
  77. @IBAction func formatEmphasis(_ sender: Any) {
  78. let wc = NSApplication.shared().mainWindow?.windowController as? WindowController
  79. wc?.emphasize()
  80. }
  81. @IBAction func setFontSerif(_ sender: NSMenuItem) {
  82. deselectAllFonts()
  83. sender.state = NSOnState
  84. vc?.setFont(.serif)
  85. Preferences.setFont(.serif)
  86. }
  87. @IBAction func setFontSans(_ sender: NSMenuItem) {
  88. deselectAllFonts()
  89. sender.state = NSOnState
  90. vc?.setFont(.sans)
  91. Preferences.setFont(.sans)
  92. }
  93. @IBAction func setFontMono(_ sender: NSMenuItem) {
  94. deselectAllFonts()
  95. sender.state = NSOnState
  96. vc?.setFont(.mono)
  97. Preferences.setFont(.mono)
  98. }
  99. func deselectAllFonts() {
  100. formatFontSerifItem.state = NSOffState
  101. formatFontSansItem.state = NSOffState
  102. formatFontMonoItem.state = NSOffState
  103. }
  104. @IBAction func saveDocument(_ sender: AnyObject) {
  105. vc?.saveDocument()
  106. }
  107. @IBAction func publishDoc(_ sender: Any) {
  108. filePublishItem.isEnabled = false
  109. vc?.publish()
  110. }
  111. @IBAction func viewIncTextSize(_ sender: Any) {
  112. vc?.adjustTextSize(increment: true)
  113. }
  114. @IBAction func viewDecTextSize(_ sender: Any) {
  115. vc?.adjustTextSize(increment: false)
  116. }
  117. }