-
Notifications
You must be signed in to change notification settings - Fork 8
/
ViewController.swift
154 lines (125 loc) · 4.85 KB
/
ViewController.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
//
// ViewController.swift
//
// Moped - A general purpose text editor, small and light.
// Copyright © 2019 Roberto Machorro. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
import Cocoa
import Highlightr
class ViewController: NSViewController, NSTextViewDelegate {
// TextView Editor
@IBOutlet var textView: NSTextView!
// Status bar components
@IBOutlet weak var statusLabel: NSTextFieldCell!
@IBOutlet weak var languagePopup: NSPopUpButtonCell!
@IBOutlet weak var themePopup: NSPopUpButtonCell!
let userPreferences = Preferences.userShared
let highlightrTextStorage: CodeAttributedString? = CodeAttributedString()
override func viewDidLoad() {
super.viewDidLoad()
textView.isAutomaticSpellingCorrectionEnabled = false
textView.isAutomaticQuoteSubstitutionEnabled = false
textView.isAutomaticDashSubstitutionEnabled = false
statusLabel.stringValue = ""
if let storage = highlightrTextStorage {
storage.addLayoutManager(textView.layoutManager!)
updateViewTo(theme: userPreferences.theme)
languagePopup.removeAllItems()
languagePopup.addItems(withTitles: storage.highlightr.supportedLanguages().sorted())
languagePopup.selectItem(withTitle: userPreferences.language)
themePopup.removeAllItems()
themePopup.addItems(withTitles: storage.highlightr.availableThemes().sorted())
themePopup.selectItem(withTitle: userPreferences.theme)
} else {
textView.font = NSFont(name: userPreferences.font, size: userPreferences.fontSizeFloat)
}
setupPreferencesObserver()
}
override func viewWillAppear() {
super.viewWillAppear()
if let storage = highlightrTextStorage, let language = document?.model.docTypeLanguage {
languagePopup.selectItem(withTitle: language)
storage.language = language
}
}
override var representedObject: Any? {
didSet {
// Pass down ContentModel to all of the child view controllers
for child in children {
child.representedObject = representedObject
}
}
}
// MARK: - Language / Popup Theme Changes
@IBAction func languagePopupAction(_ sender: NSPopUpButtonCell) {
if let storage = highlightrTextStorage {
storage.language = sender.titleOfSelectedItem ?? userPreferences.language
}
}
@IBAction func themePopupAction(_ sender: NSPopUpButtonCell) {
updateViewTo(theme: sender.titleOfSelectedItem ?? userPreferences.theme)
}
// MARK: - Accessor Helpers
weak var windowController: WindowController? {
return view.window?.windowController as? WindowController
}
weak var document: Document? {
if let window = windowController, let doc = window.document as? Document {
return doc
}
return nil
}
// MARK: - NSTextViewDelegate
func textDidBeginEditing(_ notification: Notification) {
document?.objectDidBeginEditing(self)
}
func textDidEndEditing(_ notification: Notification) {
document?.objectDidEndEditing(self)
}
func textView(_ textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
/* TODO: Setup Preferences for this option, autodectect from file as well
if (commandSelector == #selector(NSResponder.insertTab(_:))) {
textView.insertText(" ", replacementRange: textView.selectedRange())
return true
}
*/
return false
}
}
extension ViewController {
func setupPreferencesObserver() {
let notificationName = Notification.Name(rawValue: "PreferencesChanged")
NotificationCenter.default.addObserver(forName: notificationName, object: nil, queue: nil) { (notification) in
// TODO: Check for self referencing ARC leak
self.updateViewTo(theme: self.userPreferences.theme)
// self.themePopup.selectItem(withTitle: self.defaultTheme)
}
}
func updateViewTo(theme: String) {
if let storage = highlightrTextStorage {
storage.highlightr.setTheme(to: theme)
storage.highlightr.theme.codeFont = NSFont(name: userPreferences.font, size: userPreferences.fontSizeFloat)
textView.backgroundColor = storage.highlightr.theme.themeBackgroundColor
textView.insertionPointColor = invertColor(textView.backgroundColor)
}
}
func invertColor(_ color: NSColor) -> NSColor {
var r:CGFloat = 0, g:CGFloat = 0, b:CGFloat = 0
// FIXME: Fix/convert colorspace (not valid for the NSColor Generic Gray Gamma 2.2)
color.getRed(&r, green: &g, blue: &b, alpha: nil)
return NSColor(red:1.0-r, green: 1.0-g, blue: 1.0-b, alpha: 1)
}
}