-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathUTIsListView.swift
329 lines (279 loc) · 10.7 KB
/
UTIsListView.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//
// UTIsListView.swift
// Syntax Highlight
//
// Created by Sbarex on 12/03/21.
// Copyright © 2021 sbarex. All rights reserved.
//
import Cocoa
class UTIsListView: NSView, SettingsSplitViewElement {
@IBOutlet weak var contentView: NSView!
@IBOutlet weak var outlineView: NSOutlineView!
/// Search field for filter the UTI list.
@IBOutlet weak var searchField: NSSearchField!
@IBOutlet weak var filterPopupButton: NSPopUpButton!
@IBOutlet weak var showCustomizedMenuItem: NSMenuItem!
@IBOutlet weak var showInacessibileMenuItem: NSMenuItem!
@IBOutlet weak var showUnsavedMenuItem: NSMenuItem!
@IBOutlet weak var showUTIMenuItem: NSMenuItem!
@IBOutlet weak var noResultsFoundWarning: NSTextField!
var showUTI = false {
didSet {
self.outlineView.reloadData()
showUTIMenuItem.state = showUTI ? .on : .off
}
}
/// All supported UTIs.
var allFileTypes: [UTI] = []
/// Filtered supported UTIs.
var fileTypes: [UTI] = [] {
didSet {
guard oldValue != fileTypes else {
return
}
outlineView?.reloadData()
if let currentUTI = self.currentUTI, let index = fileTypes.firstIndex(of: currentUTI) {
outlineView?.selectRowIndexes(IndexSet(integer: index), byExtendingSelection: false)
outlineView?.scrollRowToVisible(index)
}
noResultsFoundWarning.isHidden = fileTypes.count > 0
}
}
var currentUTI: UTI? {
didSet {
guard oldValue != currentUTI else {
return
}
self.previewView?.selectExample(forUTI: currentUTI)
self.settingsController?.selectUTI(currentUTI)
}
}
/// Filter for the UTI description.
var filter: String = "" {
didSet {
guard oldValue != filter else {
return
}
filterUTIs()
}
}
/// Show only UTI with custom settings.
var filterOnlyChanged: Bool = false {
didSet {
guard oldValue != filterOnlyChanged else {
return
}
showCustomizedMenuItem.state = filterOnlyChanged ? .on : .off
filterUTIs()
}
}
var filterInacessibile: Bool = true {
didSet {
guard oldValue != filterInacessibile else {
return
}
showInacessibileMenuItem.state = filterInacessibile ? .on : .off
filterUTIs()
}
}
var filterDirty: Bool = false {
didSet {
guard oldValue != filterDirty else {
return
}
showUnsavedMenuItem.state = filterDirty ? .on : .off
filterUTIs()
}
}
deinit {
NotificationCenter.default.removeObserver(self, name: .SettingsIsDirty, object: nil)
}
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
setup()
}
required init?(coder decoder: NSCoder) {
super.init(coder: decoder)
setup()
}
private func setup() {
let bundle = Bundle(for: type(of: self))
let nib = NSNib(nibNamed: .init(String(describing: type(of: self))), bundle: bundle)!
nib.instantiate(withOwner: self, topLevelObjects: nil)
addSubview(contentView)
contentView.frame = self.bounds
contentView.autoresizingMask = [.width, .height]
// Populate UTIs list.
allFileTypes = ((NSApplication.shared.delegate as? AppDelegate)?.handledUTIs ?? []).filter({ uti in
guard uti.isDynamic, let settings = SCSHWrapper.shared.settings else {
return true
}
if let _ = settings.searchStandaloneUTI(for: uti) {
return false
} else {
return true
}
})
showCustomizedMenuItem.state = filterOnlyChanged ? .on : .off
showInacessibileMenuItem.state = filterInacessibile ? .on : .off
showUnsavedMenuItem.state = filterDirty ? .on : .off
filterUTIs()
NotificationCenter.default.addObserver(self, selector: #selector(self.onSettingsDirty(_:)), name: .SettingsIsDirty, object: nil)
}
@objc internal func onSettingsDirty(_ notification: Notification) {
if let settings = notification.object as? SettingsFormat, let i = self.fileTypes.firstIndex(where: { $0.UTI == settings.uti }) {
outlineView.reloadData(forRowIndexes: IndexSet(integer: i), columnIndexes: IndexSet(integer: 0))
}
}
func selectUTI(_ uti: UTI?) {
if let uti = uti, let u = fileTypes.firstIndex(where: {$0.UTI == uti.UTI }) {
if currentUTI != uti {
outlineView.selectRowIndexes(IndexSet(integer: u), byExtendingSelection: false)
outlineView.scrollRowToVisible(u)
}
} else {
outlineView.deselectAll(self)
currentUTI = nil
}
}
/// Handle change on search field.
func controlTextDidChange(_ obj: Notification) {
guard obj.object as? NSSearchField == self.searchField else {
return
}
filter = self.searchField.stringValue
}
/// Filter the visible UTIs based on search criteria.
func filterUTIs() {
filterPopupButton.contentTintColor = (filterOnlyChanged || filterInacessibile || filterDirty) ? .controlAccentColor : nil
guard !filter.isEmpty || filterOnlyChanged || filterInacessibile || filterDirty else {
fileTypes = self.allFileTypes
return
}
let filter = self.filter.lowercased()
let w = SCSHWrapper.shared
fileTypes = self.allFileTypes.filter({ (uti) -> Bool in
if filterInacessibile && uti.isSuppressed {
return false
}
if filterDirty && !(SCSHWrapper.shared.settings?.utiSettings[uti.UTI]?.isDirty ?? false) {
return false
}
if filterOnlyChanged && !(w.hasCustomizedSettings(forUTI: uti.UTI)) {
return false
}
if !filter.isEmpty && !uti.fullDescription.lowercased().contains(filter) && !uti.UTI.lowercased().contains(filter) {
return false;
}
return true
})
}
// MARK: -
@IBAction func handleShowOnlyCustomized(_ sender: NSMenuItem) {
self.filterOnlyChanged = !self.filterOnlyChanged
}
@IBAction func handleInaccessible(_ sender: NSMenuItem) {
self.filterInacessibile = !self.filterInacessibile
}
@IBAction func handleDirty(_ sender: NSMenuItem) {
self.filterDirty = !self.filterDirty
}
/// Show UTIs or extensions.
@IBAction func handleShowUTI(_ sender: NSMenuItem) {
self.showUTI = !self.showUTI
}
}
// MARK: - NSOutlineViewDataSource
extension UTIsListView: NSOutlineViewDataSource {
func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {
if item == nil {
return self.fileTypes[index]
} else {
return false
}
}
func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool {
return false
}
func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {
return item == nil ? self.fileTypes.count : 0
}
/*
func outlineView(_ outlineView: NSOutlineView, objectValueFor tableColumn: NSTableColumn?, byItem item: Any?) -> Any? {
return self.fileTypes[index]
}
*/
}
// MARK: NSOutlineViewDelegate
extension UTIsListView: NSOutlineViewDelegate {
/*
func outlineViewSelectionIsChanging(_ notification: Notification) {
if currentUTI == nil {
if settings != nil {
// Save the appearance settings of the global settings.
saveGlobalSettings()
}
} else {
saveCurrentUtiSettings(currentUTI!.uti.UTI)
}
}
*/
func outlineViewSelectionDidChange(_ notification: Notification) {
let index = self.outlineView.selectedRow
guard index >= 0 else {
return
}
if index >= 0 {
currentUTI = self.fileTypes[index]
// _ = selectUTI(self.fileTypes[index].uti.UTI)
}
}
func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
let view = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "UTICell"), owner: nil) as! UTICellView
if let uti = item as? UTI {
view.fillWithUTI(uti, showUTI: showUTI)
}
return view
}
}
class UTICellView: NSTableCellView {
@IBOutlet weak var UTILabel: NSTextField?
@IBOutlet weak var changedLabel: NSView!
@IBOutlet weak var imageWidthConstraint: NSLayoutConstraint!
static var customizedFont: NSFont = {
let f = NSFont.labelFont(ofSize: NSFont.systemFontSize)
return NSFontManager.shared.convert(f, toHaveTrait: [.boldFontMask])
}()
static var standaloneFont: NSFont = {
return NSFont.labelFont(ofSize: NSFont.systemFontSize)
}()
func fillWithUTI(_ uti: UTI, showUTI: Bool) {
imageView?.image = uti.image
imageWidthConstraint?.constant = imageView?.image == nil ? 0 : 24
/*
if let image = uti.image, let settings = SCSHWrapper.shared.utiSettings[uti.UTI], settings.isDirty, let img = image.image(withBrightness: -0.2, contrast: 1-0.3, saturation: 1) {
imageView?.image = img
} else {
imageView?.image = uti.image
}
*/
textField?.stringValue = uti.description
textField?.textColor = uti.isSuppressed ? .disabledControlTextColor : .labelColor
let utiFormat = SCSHWrapper.shared.settings?.utiSettings[uti.UTI]
changedLabel?.isHidden = !(utiFormat?.isDirty ?? false)
let extensions = uti.extensions.count > 0 ? "." + uti.extensions.joined(separator: ", .") : ""
if showUTI {
UTILabel?.stringValue = uti.UTI
UTILabel?.toolTip = extensions
} else {
UTILabel?.stringValue = extensions
UTILabel?.toolTip = uti.UTI
}
UTILabel?.textColor = textField?.textColor ?? .labelColor
if utiFormat?.isCustomized ?? false {
textField?.font = UTICellView.customizedFont
} else {
textField?.font = UTICellView.standaloneFont
}
}
}