/
Dictater.swift
156 lines (137 loc) · 3.7 KB
/
Dictater.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// Dictater.swift
// Dictater
//
// Created by Kyle Carson on 9/2/15.
// Copyright © 2015 Kyle Carson. All rights reserved.
//
import Foundation
import Cocoa
class Dictater
{
enum PreferenceKeys : String
{
case SkipBoundary = "skipBoundary"
case HasBeenUsed = "hasBeenUsed"
case FontName = "fontName"
case FontSize = "fontSize"
case LineHeightMultiple = "lineHeightMultiple"
case ProgressBarEnabled = "progressBarEnabled"
case AutoScrollEnabled = "autoScrollEnabled"
}
static let TextAppearanceChangedNotification = "Dictater.FontChanged"
static func setupDefaults()
{
let font = self.defaultFont
UserDefaults.standard.register(
defaults: [
PreferenceKeys.SkipBoundary.rawValue : Speech.Boundary.Sentence.rawValue,
PreferenceKeys.HasBeenUsed.rawValue : false,
PreferenceKeys.FontName.rawValue : font.fontName,
PreferenceKeys.FontSize.rawValue : Double(font.pointSize),
PreferenceKeys.LineHeightMultiple.rawValue : 1.2,
PreferenceKeys.ProgressBarEnabled.rawValue : true,
PreferenceKeys.AutoScrollEnabled.rawValue : true,
])
}
static var skipBoundary : Speech.Boundary
{
get
{
let rawValue = UserDefaults.standard.integer(forKey: PreferenceKeys.SkipBoundary.rawValue)
if let boundary = Speech.Boundary(rawValue: rawValue)
{
return boundary
}
return .Sentence
}
set
{
UserDefaults.standard.set(newValue.rawValue, forKey: PreferenceKeys.SkipBoundary.rawValue)
}
}
static var hasBeenUsed : Bool
{
get
{
return UserDefaults.standard.bool(forKey: PreferenceKeys.HasBeenUsed.rawValue)
}
set
{
UserDefaults.standard.set(newValue, forKey: PreferenceKeys.HasBeenUsed.rawValue)
}
}
static var autoScrollEnabled : Bool
{
get
{
return UserDefaults.standard.bool(forKey: PreferenceKeys.AutoScrollEnabled.rawValue)
}
set
{
UserDefaults.standard.set(newValue, forKey: PreferenceKeys.AutoScrollEnabled.rawValue)
}
}
static var isProgressBarEnabled : Bool
{
get {
return UserDefaults.standard.bool(forKey: PreferenceKeys.ProgressBarEnabled.rawValue)
}
set
{
UserDefaults.standard.set(newValue, forKey: PreferenceKeys.ProgressBarEnabled.rawValue)
}
}
static var font : NSFont
{
let cgfloat = CGFloat(self.fontSize)
return NSFont(name: self.fontName, size: cgfloat) ?? self.defaultFont
}
static var defaultFont : NSFont
{
return NSFont.messageFont(ofSize: 14)
}
static var fontName : String
{
get {
if let string = UserDefaults.standard.string(forKey: PreferenceKeys.FontName.rawValue)
{
return string
} else {
return self.defaultFont.fontName
}
}
set {
UserDefaults.standard.set(newValue, forKey: PreferenceKeys.FontName.rawValue)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: self.TextAppearanceChangedNotification), object: nil)
}
}
static var fontSize : Int
{
get {
return UserDefaults.standard.integer(forKey: PreferenceKeys.FontSize.rawValue)
}
set {
UserDefaults.standard.set(newValue, forKey: PreferenceKeys.FontSize.rawValue)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: self.TextAppearanceChangedNotification), object: nil)
}
}
static var lineHeightMultiple : Double
{
get {
return UserDefaults.standard.double(forKey: PreferenceKeys.LineHeightMultiple.rawValue)
}
set {
UserDefaults.standard.set(newValue, forKey: PreferenceKeys.LineHeightMultiple.rawValue)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: self.TextAppearanceChangedNotification), object: nil)
}
}
class ParagraphStyle : NSParagraphStyle
{
let multiple = Dictater.lineHeightMultiple
override var lineHeightMultiple : CGFloat
{
return CGFloat(self.multiple)
}
}
}