148 行
4.4 KiB
Swift
148 行
4.4 KiB
Swift
//
|
|
// AppDelegate.swift
|
|
// writeas
|
|
//
|
|
// Created by Matt Baer on 12/29/15.
|
|
// Copyright © 2015 A Bunch Tell. All rights reserved.
|
|
//
|
|
|
|
import Cocoa
|
|
import Foundation
|
|
|
|
@NSApplicationMain
|
|
class AppDelegate: NSObject, NSApplicationDelegate {
|
|
|
|
var vc: ViewController?
|
|
|
|
@IBOutlet weak var filePublishItem: NSMenuItem!
|
|
@IBOutlet weak var nightModeItem: NSMenuItem!
|
|
@IBOutlet weak var formatFontSerifItem: NSMenuItem!
|
|
@IBOutlet weak var formatFontSansItem: NSMenuItem!
|
|
@IBOutlet weak var formatFontMonoItem: NSMenuItem!
|
|
|
|
struct GlobalSettings {
|
|
var isNight = false
|
|
}
|
|
var globalSettings = GlobalSettings()
|
|
|
|
func applicationDidFinishLaunching(_ aNotification: Notification) {
|
|
// Set night mode state
|
|
let nightModeState = Int(UserDefaults.standard.string(forKey: "night_mode_state") ?? String(NSOffState))!
|
|
globalSettings.isNight = nightModeState == NSOnState
|
|
if globalSettings.isNight {
|
|
// Toggle Night Mode menu item on
|
|
toggleNightMode(nightModeItem)
|
|
}
|
|
|
|
// Set font
|
|
let font = Preferences.getFont()
|
|
if font == Preferences.PostFont.serif {
|
|
formatFontSerifItem.state = NSOnState
|
|
} else if font == Preferences.PostFont.sans {
|
|
formatFontSansItem.state = NSOnState
|
|
} else if font == Preferences.PostFont.mono {
|
|
formatFontMonoItem.state = NSOnState
|
|
}
|
|
}
|
|
|
|
func applicationWillTerminate(_ aNotification: Notification) {
|
|
vc?.saveDocument()
|
|
}
|
|
|
|
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
|
|
return true
|
|
}
|
|
|
|
func applicationDidBecomeActive(_ notification: Notification) {
|
|
if vc == nil {
|
|
vc = NSApplication.shared().mainWindow?.contentViewController as? ViewController
|
|
}
|
|
|
|
DispatchQueue.global(qos: .background).async {
|
|
if !FileManager.default.fileExists(atPath: Constants.draftDir, isDirectory: nil) {
|
|
do {
|
|
try FileManager.default.createDirectory(atPath: Constants.draftDir, withIntermediateDirectories: false, attributes: nil)
|
|
} catch let error as NSError {
|
|
print(error.localizedDescription);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@IBAction func fileClose(_ sender: Any) {
|
|
NSApplication.shared().mainWindow!.close()
|
|
}
|
|
|
|
@IBAction func toggleNightMode(_ sender: NSMenuItem) {
|
|
let isOff = sender.state == NSOffState
|
|
|
|
for w in NSApplication.shared().windows {
|
|
let wc = w.windowController as? WindowController
|
|
wc?.toggle(isNight: isOff)
|
|
}
|
|
sender.state = isOff ? NSOnState : NSOffState
|
|
globalSettings.isNight = isOff
|
|
UserDefaults.standard.set(String(sender.state), forKey: "night_mode_state")
|
|
}
|
|
|
|
@IBAction func formatStrong(_ sender: Any) {
|
|
let wc = NSApplication.shared().mainWindow?.windowController as? WindowController
|
|
wc?.embolden()
|
|
}
|
|
|
|
@IBAction func formatEmphasis(_ sender: Any) {
|
|
let wc = NSApplication.shared().mainWindow?.windowController as? WindowController
|
|
wc?.emphasize()
|
|
}
|
|
|
|
@IBAction func formatAddLink(_ sender: Any) {
|
|
let wc = NSApplication.shared().mainWindow?.windowController as? WindowController
|
|
wc?.addLink()
|
|
}
|
|
|
|
@IBAction func setFontSerif(_ sender: NSMenuItem) {
|
|
deselectAllFonts()
|
|
sender.state = NSOnState
|
|
vc?.setFont(.serif)
|
|
Preferences.setFont(.serif)
|
|
}
|
|
|
|
@IBAction func setFontSans(_ sender: NSMenuItem) {
|
|
deselectAllFonts()
|
|
sender.state = NSOnState
|
|
vc?.setFont(.sans)
|
|
Preferences.setFont(.sans)
|
|
}
|
|
|
|
@IBAction func setFontMono(_ sender: NSMenuItem) {
|
|
deselectAllFonts()
|
|
sender.state = NSOnState
|
|
vc?.setFont(.mono)
|
|
Preferences.setFont(.mono)
|
|
}
|
|
|
|
func deselectAllFonts() {
|
|
formatFontSerifItem.state = NSOffState
|
|
formatFontSansItem.state = NSOffState
|
|
formatFontMonoItem.state = NSOffState
|
|
}
|
|
|
|
@IBAction func saveDocument(_ sender: AnyObject) {
|
|
vc?.saveDocument()
|
|
}
|
|
|
|
@IBAction func publishDoc(_ sender: Any) {
|
|
filePublishItem.isEnabled = false
|
|
vc?.publish()
|
|
}
|
|
|
|
@IBAction func viewIncTextSize(_ sender: Any) {
|
|
vc?.adjustTextSize(increment: true)
|
|
}
|
|
|
|
@IBAction func viewDecTextSize(_ sender: Any) {
|
|
vc?.adjustTextSize(increment: false)
|
|
}
|
|
}
|
|
|