diff --git a/writeas/AppDelegate.swift b/writeas/AppDelegate.swift index 4c60e28..93779a3 100644 --- a/writeas/AppDelegate.swift +++ b/writeas/AppDelegate.swift @@ -14,12 +14,29 @@ class AppDelegate: NSObject, NSApplicationDelegate { var vc: ViewController? @IBOutlet weak var nightModeItem: NSMenuItem! - + @IBOutlet weak var formatFontSerifItem: NSMenuItem! + @IBOutlet weak var formatFontSansItem: NSMenuItem! + @IBOutlet weak var formatFontMonoItem: NSMenuItem! + func applicationDidFinishLaunching(_ aNotification: Notification) { + // Set night mode state nightModeItem.state = Int(UserDefaults.standard.string(forKey: "night_mode_state") ?? String(NSOffState))! if nightModeItem.state == NSOnState { vc?.toggle(isNight: true) } + + // Set font + let font = Preferences.getFont() + if font == Preferences.PostFont.serif { + formatFontSerifItem.state = NSOnState + vc?.setFont(.serif) + } else if font == Preferences.PostFont.sans { + formatFontSansItem.state = NSOnState + vc?.setFont(.sans) + } else if font == Preferences.PostFont.mono { + formatFontMonoItem.state = NSOnState + vc?.setFont(.mono) + } } func applicationWillTerminate(_ aNotification: Notification) { @@ -39,6 +56,33 @@ class AppDelegate: NSObject, NSApplicationDelegate { UserDefaults.standard.set(String(sender.state), forKey: "night_mode_state") } + @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() } diff --git a/writeas/Base.lproj/Main.storyboard b/writeas/Base.lproj/Main.storyboard index e13aaa8..c55d8b8 100644 --- a/writeas/Base.lproj/Main.storyboard +++ b/writeas/Base.lproj/Main.storyboard @@ -235,6 +235,62 @@ DQ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -304,6 +360,9 @@ DQ + + + diff --git a/writeas/Preferences.swift b/writeas/Preferences.swift index 3063196..a4eae4e 100644 --- a/writeas/Preferences.swift +++ b/writeas/Preferences.swift @@ -6,10 +6,34 @@ import Foundation class Preferences { + enum PostFont: String { + case serif = "norm" + case sans = "sans" + case mono = "wrap" + + var typeface: String { + switch self { + case .serif: + return "Lora" + case .sans: + return "Open Sans" + case .mono: + return "Hack" + } + } + } class func getFontSize() -> CGFloat { let size = Int(UserDefaults.standard.string(forKey: "editor_text_size") ?? "16") ?? 16 return CGFloat(size) } + + class func getFont() -> PostFont { + return PostFont(rawValue: UserDefaults.standard.string(forKey: "editor_font") ?? PostFont.serif.rawValue) ?? PostFont.serif + } + + class func setFont(_ font: PostFont) { + UserDefaults.standard.set(font.rawValue, forKey: "editor_font") + } } diff --git a/writeas/ViewController.swift b/writeas/ViewController.swift index a2490d2..fef0eee 100644 --- a/writeas/ViewController.swift +++ b/writeas/ViewController.swift @@ -99,6 +99,7 @@ class ViewController: NSViewController, NSTextViewDelegate, NSUserNotificationCe DispatchQueue.global(qos: .background).async { let task = Process() task.launchPath = Bundle.main.executablePath! + "/../../Resources/writeas" + task.arguments = ["--font", Preferences.getFont().rawValue] let pipe = Pipe() task.standardInput = pipe task.launch() @@ -148,5 +149,9 @@ class ViewController: NSViewController, NSTextViewDelegate, NSUserNotificationCe UserDefaults.standard.set(String(Int(size)), forKey: "editor_text_size") configureWindow() } + + func setFont(_ font: Preferences.PostFont) { + self.writerText.font = NSFont(name: font.typeface, size: Preferences.getFontSize()) + } }