-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathUTIInfoViewController.swift
146 lines (128 loc) · 4.49 KB
/
UTIInfoViewController.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
//
// UTIInfoViewController.swift
// Syntax Highlight
//
// Created by Sbarex on 07/01/21.
// Copyright © 2021 sbarex. All rights reserved.
//
import Cocoa
fileprivate enum InfoItem {
case single(name: String, value: String)
case multiple(name: String, values: [InfoItem])
}
class UTIInfoViewController: NSViewController {
@IBOutlet var outlineView: NSOutlineView!
var uti: UTI? {
didSet {
if let uti = uti {
initFromUTI(uti)
} else {
info = []
outlineView?.reloadData()
}
}
}
fileprivate var info : [InfoItem] = []
fileprivate func initFromUTI(_ uti: UTI) {
info = []
info.append(InfoItem.single(name: "Description", value: uti.description))
if uti.extensions.count > 1 {
var extensions: [InfoItem] = []
for (i, ext) in uti.extensions.enumerated() {
extensions.append(InfoItem.single(name: "Item \(i+1)", value: ext))
}
info.append(InfoItem.multiple(name: "Extensions", values: extensions))
} else if uti.extensions.count == 1 {
info.append(InfoItem.single(name: "Extension", value: uti.extensions.first!))
}
if uti.mimeTypes.count > 1 {
var mimes: [InfoItem] = []
for (i, mime) in uti.mimeTypes.enumerated() {
mimes.append(InfoItem.single(name: "Item \(i+1)", value: mime))
}
info.append(InfoItem.multiple(name: "Mime Types", values: mimes))
} else if uti.mimeTypes.count == 1 {
info.append(InfoItem.single(name: "Mime Type", value: uti.mimeTypes.first!))
}
info.append(InfoItem.single(name: "UTI", value: uti.UTI))
if uti.conformsTo.count > 1 {
var conforms: [InfoItem] = []
for (i, uti) in uti.conformsTo.enumerated() {
conforms.append(InfoItem.single(name: "Item \(i+1)", value: uti))
}
info.append(InfoItem.multiple(name: "Conforms to", values: conforms))
} else if uti.conformsTo.count == 1 {
info.append(InfoItem.single(name: "Conform to", value: uti.conformsTo.first!))
}
info.append(InfoItem.single(name: "Is dynamic", value: uti.isDynamic ? "Yes" : "No"))
outlineView?.reloadData()
outlineView?.expandItem(nil, expandChildren: true)
}
override func viewDidLoad() {
outlineView?.reloadData()
outlineView?.expandItem(nil, expandChildren: true)
}
}
extension UTIInfoViewController: NSOutlineViewDelegate {
}
extension UTIInfoViewController: NSOutlineViewDataSource {
func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {
if item == nil {
return info[index]
} else if let info = item as? InfoItem {
switch info {
case .single(_, _):
return info
case .multiple(_, let values):
return values[index]
}
} else {
return 0
}
}
func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool {
guard let info = item as? InfoItem else {
return false
}
switch info {
case .single(_, _):
return false
case .multiple(_, let values):
return values.count > 0
}
}
func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {
if item == nil {
return info.count
} else if let info = item as? InfoItem {
switch info {
case .single(_, _):
return 0
case .multiple(_, let values):
return values.count
}
} else {
return 0
}
}
func outlineView(_ outlineView: NSOutlineView, objectValueFor tableColumn: NSTableColumn?, byItem item: Any?) -> Any? {
guard let info = item as? InfoItem else {
return item
}
let name: String
let value: String
switch info {
case .single(let i_name, let i_value):
name = i_name
value = i_value
case .multiple(let i_name, _):
name = i_name
value = ""
}
if tableColumn?.identifier.rawValue == "name" {
return name
} else {
return value
}
}
}