Add fonts

- Supports choosing from serif, sans, or mono
- Remembers preference
- Submits the type with the post
This commit is contained in:
Matt Baer 2017-07-31 19:05:54 -04:00
parent 26ec04a028
commit cda7517224
4 changed files with 133 additions and 1 deletions

View File

@ -14,12 +14,29 @@ class AppDelegate: NSObject, NSApplicationDelegate {
var vc: ViewController? var vc: ViewController?
@IBOutlet weak var nightModeItem: NSMenuItem! @IBOutlet weak var nightModeItem: NSMenuItem!
@IBOutlet weak var formatFontSerifItem: NSMenuItem!
@IBOutlet weak var formatFontSansItem: NSMenuItem!
@IBOutlet weak var formatFontMonoItem: NSMenuItem!
func applicationDidFinishLaunching(_ aNotification: Notification) { func applicationDidFinishLaunching(_ aNotification: Notification) {
// Set night mode state
nightModeItem.state = Int(UserDefaults.standard.string(forKey: "night_mode_state") ?? String(NSOffState))! nightModeItem.state = Int(UserDefaults.standard.string(forKey: "night_mode_state") ?? String(NSOffState))!
if nightModeItem.state == NSOnState { if nightModeItem.state == NSOnState {
vc?.toggle(isNight: true) 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) { func applicationWillTerminate(_ aNotification: Notification) {
@ -39,6 +56,33 @@ class AppDelegate: NSObject, NSApplicationDelegate {
UserDefaults.standard.set(String(sender.state), forKey: "night_mode_state") 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) { @IBAction func saveDocument(_ sender: AnyObject) {
vc?.saveDocument() vc?.saveDocument()
} }

View File

@ -235,6 +235,62 @@ DQ
</items> </items>
</menu> </menu>
</menuItem> </menuItem>
<menuItem title="Format" id="jxT-CU-nIS">
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="Format" id="GEO-Iw-cKr">
<items>
<menuItem title="Font" id="Gi5-1S-RQB">
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="Font" systemMenu="font" id="aXa-aM-Jaq">
<items>
<menuItem title="Serif" tag="2" id="GB9-OM-e27">
<attributedString key="attributedTitle">
<fragment content="Serif">
<attributes>
<font key="NSFont" size="13" name="Lora-Regular"/>
<paragraphStyle key="NSParagraphStyle" alignment="natural" lineBreakMode="wordWrapping" baseWritingDirection="natural" tighteningFactorForTruncation="0.0"/>
</attributes>
</fragment>
</attributedString>
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="setFontSerif:" target="Voe-Tx-rLC" id="B5Z-AG-dYj"/>
</connections>
</menuItem>
<menuItem title="Sans-serif" tag="1" id="Vjx-xi-njq">
<attributedString key="attributedTitle">
<fragment content="Sans-serif">
<attributes>
<font key="NSFont" size="13" name="OpenSans-Regular"/>
<paragraphStyle key="NSParagraphStyle" alignment="natural" lineBreakMode="wordWrapping" baseWritingDirection="natural" tighteningFactorForTruncation="0.0"/>
</attributes>
</fragment>
</attributedString>
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="setFontSans:" target="Voe-Tx-rLC" id="BCb-aN-CNi"/>
</connections>
</menuItem>
<menuItem title="Monospace" id="WRG-CD-K1S">
<attributedString key="attributedTitle">
<fragment content="Monospace">
<attributes>
<font key="NSFont" size="13" name="Hack-Regular"/>
<paragraphStyle key="NSParagraphStyle" alignment="natural" lineBreakMode="wordWrapping" baseWritingDirection="natural" tighteningFactorForTruncation="0.0"/>
</attributes>
</fragment>
</attributedString>
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="setFontMono:" target="Voe-Tx-rLC" id="bck-1A-bhC"/>
</connections>
</menuItem>
</items>
</menu>
</menuItem>
</items>
</menu>
</menuItem>
<menuItem title="View" id="H8h-7b-M4v"> <menuItem title="View" id="H8h-7b-M4v">
<modifierMask key="keyEquivalentModifierMask"/> <modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="View" id="HyV-fh-RgO"> <menu key="submenu" title="View" id="HyV-fh-RgO">
@ -304,6 +360,9 @@ DQ
</application> </application>
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="Write_as" customModuleProvider="target"> <customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="Write_as" customModuleProvider="target">
<connections> <connections>
<outlet property="formatFontMonoItem" destination="WRG-CD-K1S" id="919-ab-Ig1"/>
<outlet property="formatFontSansItem" destination="Vjx-xi-njq" id="LIr-Ct-Ied"/>
<outlet property="formatFontSerifItem" destination="GB9-OM-e27" id="rVW-JC-eyr"/>
<outlet property="nightModeItem" destination="jYO-0u-qN9" id="8xF-SV-B70"/> <outlet property="nightModeItem" destination="jYO-0u-qN9" id="8xF-SV-B70"/>
</connections> </connections>
</customObject> </customObject>

View File

@ -6,10 +6,34 @@
import Foundation import Foundation
class Preferences { 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 { class func getFontSize() -> CGFloat {
let size = Int(UserDefaults.standard.string(forKey: "editor_text_size") ?? "16") ?? 16 let size = Int(UserDefaults.standard.string(forKey: "editor_text_size") ?? "16") ?? 16
return CGFloat(size) 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")
}
} }

View File

@ -99,6 +99,7 @@ class ViewController: NSViewController, NSTextViewDelegate, NSUserNotificationCe
DispatchQueue.global(qos: .background).async { DispatchQueue.global(qos: .background).async {
let task = Process() let task = Process()
task.launchPath = Bundle.main.executablePath! + "/../../Resources/writeas" task.launchPath = Bundle.main.executablePath! + "/../../Resources/writeas"
task.arguments = ["--font", Preferences.getFont().rawValue]
let pipe = Pipe() let pipe = Pipe()
task.standardInput = pipe task.standardInput = pipe
task.launch() task.launch()
@ -148,5 +149,9 @@ class ViewController: NSViewController, NSTextViewDelegate, NSUserNotificationCe
UserDefaults.standard.set(String(Int(size)), forKey: "editor_text_size") UserDefaults.standard.set(String(Int(size)), forKey: "editor_text_size")
configureWindow() configureWindow()
} }
func setFont(_ font: Preferences.PostFont) {
self.writerText.font = NSFont(name: font.typeface, size: Preferences.getFontSize())
}
} }