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.

115 rivejä
3.5 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. func applicationDidFinishLaunching(_ aNotification: Notification) {
  19. // Set night mode state
  20. nightModeItem.state = Int(UserDefaults.standard.string(forKey: "night_mode_state") ?? String(NSOffState))!
  21. if nightModeItem.state == NSOnState {
  22. let wc = NSApplication.shared().mainWindow!.windowController as? WindowController
  23. wc?.toggle(isNight: true)
  24. }
  25. // Set font
  26. let font = Preferences.getFont()
  27. if font == Preferences.PostFont.serif {
  28. formatFontSerifItem.state = NSOnState
  29. } else if font == Preferences.PostFont.sans {
  30. formatFontSansItem.state = NSOnState
  31. } else if font == Preferences.PostFont.mono {
  32. formatFontMonoItem.state = NSOnState
  33. }
  34. }
  35. func applicationWillTerminate(_ aNotification: Notification) {
  36. vc?.saveDocument()
  37. }
  38. func applicationDidBecomeActive(_ notification: Notification) {
  39. if vc == nil {
  40. vc = NSApplication.shared().mainWindow?.contentViewController as? ViewController
  41. }
  42. DispatchQueue.global(qos: .background).async {
  43. if !FileManager.default.fileExists(atPath: Constants.draftDir, isDirectory: nil) {
  44. do {
  45. try FileManager.default.createDirectory(atPath: Constants.draftDir, withIntermediateDirectories: false, attributes: nil)
  46. } catch let error as NSError {
  47. print(error.localizedDescription);
  48. }
  49. }
  50. }
  51. }
  52. @IBAction func toggleNightMode(_ sender: NSMenuItem) {
  53. let wc = NSApplication.shared().mainWindow!.windowController as? WindowController
  54. let isOff = sender.state == NSOffState
  55. wc?.toggle(isNight: isOff)
  56. sender.state = isOff ? NSOnState : NSOffState
  57. UserDefaults.standard.set(String(sender.state), forKey: "night_mode_state")
  58. }
  59. @IBAction func setFontSerif(_ sender: NSMenuItem) {
  60. deselectAllFonts()
  61. sender.state = NSOnState
  62. vc?.setFont(.serif)
  63. Preferences.setFont(.serif)
  64. }
  65. @IBAction func setFontSans(_ sender: NSMenuItem) {
  66. deselectAllFonts()
  67. sender.state = NSOnState
  68. vc?.setFont(.sans)
  69. Preferences.setFont(.sans)
  70. }
  71. @IBAction func setFontMono(_ sender: NSMenuItem) {
  72. deselectAllFonts()
  73. sender.state = NSOnState
  74. vc?.setFont(.mono)
  75. Preferences.setFont(.mono)
  76. }
  77. func deselectAllFonts() {
  78. formatFontSerifItem.state = NSOffState
  79. formatFontSansItem.state = NSOffState
  80. formatFontMonoItem.state = NSOffState
  81. }
  82. @IBAction func saveDocument(_ sender: AnyObject) {
  83. vc?.saveDocument()
  84. }
  85. @IBAction func publishDoc(_ sender: Any) {
  86. filePublishItem.isEnabled = false
  87. vc?.publish()
  88. }
  89. @IBAction func viewIncTextSize(_ sender: Any) {
  90. vc?.adjustTextSize(increment: true)
  91. }
  92. @IBAction func viewDecTextSize(_ sender: Any) {
  93. vc?.adjustTextSize(increment: false)
  94. }
  95. }