-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathVCSSettingsViewController.swift
257 lines (227 loc) · 8.31 KB
/
VCSSettingsViewController.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//
// VCSSettingsViewController.swift
// Syntax Highlight
//
// Created by Sbarex on 10/10/21.
// Copyright © 2021 sbarex. All rights reserved.
//
import Foundation
import AppKit
class VCSSettingsViewController: NSViewController {
@IBOutlet weak var tableView: NSTableView!
var gitPath: String = ""
var hgPath: String = ""
// var svnPath: String = ""
var customizeColor = true
var editLightColor = "#"
var editDarkColor = "#"
var addLightColor = "#"
var addDarkColor = "#"
var delLightColor = "#"
var delDarkColor = "#"
var settings: SettingsBase? {
didSet {
initSettings()
}
}
var onDismiss: (()->Void)?
@discardableResult
func initSettings() -> Bool {
tableView?.beginUpdates()
guard let settings = self.settings else {
tableView?.endUpdates()
return false
}
addLightColor = settings.vcsAddLightColor
addDarkColor = settings.vcsAddDarkColor
editLightColor = settings.vcsEditLightColor
editDarkColor = settings.vcsEditDarkColor
delLightColor = settings.vcsDelLightColor
delDarkColor = settings.vcsDelDarkColor
customizeColor = settings.isVCSDefined
if let settings = settings as? Settings {
gitPath = settings.gitPath
hgPath = settings.hgPath
// svnPath = settings.svnPath
customizeColor = true
}
tableView?.reloadData()
tableView?.endUpdates()
return true
}
@IBAction func handleSave(_ sender: Any) {
if customizeColor {
settings?.isVCSDefined = true
settings?.vcsAddLightColor = addLightColor
settings?.vcsAddDarkColor = addDarkColor
settings?.vcsEditLightColor = editLightColor
settings?.vcsEditDarkColor = editDarkColor
settings?.vcsDelLightColor = delLightColor
settings?.vcsDelDarkColor = delDarkColor
} else {
settings?.isVCSDefined = false
}
if let settings = settings as? Settings {
settings.gitPath = gitPath
settings.hgPath = hgPath
// settings.svnPath = svnPath
settings.isVCSDefined = true
}
self.dismiss(sender)
}
override func viewDidLoad() {
super.viewDidLoad()
initSettings()
}
override func viewDidDisappear() {
super.viewDidDisappear()
onDismiss?()
}
}
// MARK: -
extension VCSSettingsViewController: BrowseCellDelegate {
func browseCell(_ cell: BrowseCell, didChangeValue url: URL?) {
if cell.textField?.tag == 0 {
self.gitPath = url?.path ?? ""
} else if cell.textField?.tag == 1 {
self.hgPath = url?.path ?? ""
} /*else if cell.textField?.tag == 2 {
self.svnPath = url?.path ?? ""
}*/
}
}
extension VCSSettingsViewController: VCSColorCellDelegate {
func colorCellDidChange(_ cell: VCSColorCell) {
if cell.tag == 0 {
self.addLightColor = cell.lightColor.color.toHexString() ?? "#C9DEC1"
self.addDarkColor = cell.darkColor.color.toHexString() ?? "#009924"
} else if cell.tag == 1 {
self.editLightColor = cell.lightColor.color.toHexString() ?? "#C3D6E8"
self.editDarkColor = cell.darkColor.color.toHexString() ?? "#1AABFF"
} else if cell.tag == 2 {
self.delLightColor = cell.lightColor.color.toHexString() ?? "#ff0000"
self.delDarkColor = cell.darkColor.color.toHexString() ?? "#ff0000"
}
}
}
class MyNSTableRowView: NSTableRowView {
override var isEmphasized: Bool {
get {
return false
}
set {
}
}
}
// MARK: - NSTableViewDelegate
extension VCSSettingsViewController: NSTableViewDelegate {
func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
return MyNSTableRowView()
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
guard let settings = self.settings else {
return nil
}
var r = row
if tableColumn?.identifier.rawValue != "value" {
let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: row <= 1 ? "LabelCell" : "LabelCell2"), owner: nil) as! NSTableCellView
if settings is Settings {
if r < 2 {
if r == 0 {
cell.textField?.stringValue = "git path"
} else if r == 1 {
cell.textField?.stringValue = "hg path"
} /*else if r == 2 {
cell.textField?.stringValue = "svn path"
}*/
return cell
}
r -= 2
}
if r == 0 {
cell.textField?.stringValue = "new lines"
} else if r == 1 {
cell.textField?.stringValue = "changed lines"
} else if r == 2 {
cell.textField?.stringValue = "removed lines"
}
return cell
} else {
if settings is Settings {
if r < 2 {
let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "PathCell"), owner: nil) as! BrowseCell
cell.textField?.isEditable = true
cell.textField?.isEnabled = true
cell.browseButton.isEnabled = true
cell.delegate = self
cell.textField?.tag = r
if r == 0 {
cell.url = gitPath.isEmpty ? nil : URL(fileURLWithPath: gitPath)
// cell.textField?.toolTip = ""
} else if r == 1 {
cell.url = hgPath.isEmpty ? nil : URL(fileURLWithPath: hgPath)
} /*else if r == 2 {
cell.url = svnPath.isEmpty ? nil : URL(fileURLWithPath: svnPath)
}*/
return cell
}
r -= 2
}
let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "ColorCell"), owner: nil) as! VCSColorCell
cell.delegate = self
cell.tag = r
cell.lightColor.isEnabled = customizeColor
cell.darkColor.isEnabled = customizeColor
if r == 0 {
cell.lightColor.color = NSColor(fromHexString: self.addLightColor) ?? .systemGreen
cell.darkColor.color = NSColor(fromHexString: self.addDarkColor) ?? .systemGreen
} else if r == 1 {
cell.lightColor.color = NSColor(fromHexString: self.editLightColor) ?? .systemBlue
cell.darkColor.color = NSColor(fromHexString: self.editDarkColor) ?? .systemBlue
} else if r == 2 {
cell.lightColor.color = NSColor(fromHexString: self.delLightColor) ?? .systemRed
cell.darkColor.color = NSColor(fromHexString: self.delDarkColor) ?? .systemRed
}
return cell
}
}
func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
if row <= 1 {
return tableView.rowHeight
} else {
return 40
}
}
}
// MARK: - NSTableViewDataSource
extension VCSSettingsViewController: NSTableViewDataSource {
func numberOfRows(in tableView: NSTableView) -> Int {
guard let settings = self.settings else {
return 0
}
return settings is Settings ? 5 : 3
}
}
// MARK: -
@objc
protocol VCSColorCellDelegate: AnyObject {
@objc
optional func colorCellDidChange(_ cell: VCSColorCell)
}
class VCSColorCell: NSTableCellView {
private var _tag: Int = 0
override var tag: Int {
get {
return _tag
}
set {
_tag = newValue
}
}
@IBOutlet weak var lightColor: NSColorWell!
@IBOutlet weak var darkColor: NSColorWell!
@IBAction func handleColor(_ sender: NSColorWell) {
delegate?.colorCellDidChange?(self)
}
weak var delegate: VCSColorCellDelegate?
}