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.

148 lines
4.4 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 formatAddLink(_ sender: Any) {
  82. let wc = NSApplication.shared().mainWindow?.windowController as? WindowController
  83. wc?.addLink()
  84. }
  85. @IBAction func setFontSerif(_ sender: NSMenuItem) {
  86. deselectAllFonts()
  87. sender.state = NSOnState
  88. vc?.setFont(.serif)
  89. Preferences.setFont(.serif)
  90. }
  91. @IBAction func setFontSans(_ sender: NSMenuItem) {
  92. deselectAllFonts()
  93. sender.state = NSOnState
  94. vc?.setFont(.sans)
  95. Preferences.setFont(.sans)
  96. }
  97. @IBAction func setFontMono(_ sender: NSMenuItem) {
  98. deselectAllFonts()
  99. sender.state = NSOnState
  100. vc?.setFont(.mono)
  101. Preferences.setFont(.mono)
  102. }
  103. func deselectAllFonts() {
  104. formatFontSerifItem.state = NSOffState
  105. formatFontSansItem.state = NSOffState
  106. formatFontMonoItem.state = NSOffState
  107. }
  108. @IBAction func saveDocument(_ sender: AnyObject) {
  109. vc?.saveDocument()
  110. }
  111. @IBAction func publishDoc(_ sender: Any) {
  112. filePublishItem.isEnabled = false
  113. vc?.publish()
  114. }
  115. @IBAction func viewIncTextSize(_ sender: Any) {
  116. vc?.adjustTextSize(increment: true)
  117. }
  118. @IBAction func viewDecTextSize(_ sender: Any) {
  119. vc?.adjustTextSize(increment: false)
  120. }
  121. }