The Write.as desktop (GUI) app for macOS.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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