-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathUTISummary.swift
94 lines (85 loc) · 3.55 KB
/
UTISummary.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
//
// UTISummary.swift
// Syntax Highlight
//
// Created by Sbarex on 03/03/22.
// Copyright © 2022 sbarex. All rights reserved.
//
import Foundation
import AppKit
class UTISummary: NSViewController {
@IBOutlet weak var tableView: NSTableView!
var allFileTypes: [UTI] = [] {
didSet {
tableView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.allFileTypes = (NSApplication.shared.delegate as? AppDelegate)?.handledUTIs.sorted(by: { $0.description < $1.description }) ?? []
}
}
extension UTISummary: NSTableViewDataSource {
func numberOfRows(in tableView: NSTableView) -> Int {
return allFileTypes.count
}
func tableView(_ tableView: NSTableView, sizeToFitWidthOfColumn column: Int) -> CGFloat {
let tableColumn = tableView.tableColumns[column]
let text: [String]
switch tableColumn.identifier.rawValue {
case "uti":
text = allFileTypes.map { return $0.UTI }
case "mime":
text = allFileTypes.map { return $0.mimeTypes.joined(separator: ", ") }
case "ext":
text = allFileTypes.map { return $0.extensions.joined(separator: ", ") }
case "desc":
text = allFileTypes.map { return $0.description }
default:
return 100
}
let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "cell"), owner: nil) as? NSTableCellView
let font = cell?.textField?.font ?? NSFont.systemFont(ofSize: NSFont.smallSystemFontSize)
var greatest: CGFloat = 0
for s in text {
let w = (s as NSString).size(withAttributes: [.font: font])
greatest = max(greatest, w.width)
}
return greatest + 20
}
func tableView(_ tableView: NSTableView, sortDescriptorsDidChange oldDescriptors: [NSSortDescriptor]) {
guard let sort = tableView.sortDescriptors.first else {
return
}
switch sort.key {
case "desc":
allFileTypes = allFileTypes.sorted(by: { sort.ascending ? $0.description < $1.description : $0.description > $1.description})
case "uti":
allFileTypes = allFileTypes.sorted(by: { sort.ascending ? $0.UTI < $1.UTI : $0.UTI > $1.UTI})
case "ext":
allFileTypes = allFileTypes.sorted(by: { sort.ascending ? $0.extensions.first ?? "" < $1.extensions.first ?? "" : $0.extensions.first ?? "" > $1.extensions.first ?? ""})
case "mime":
allFileTypes = allFileTypes.sorted(by: { sort.ascending ? $0.mimeTypes.first ?? "" < $1.mimeTypes.first ?? "" : $0.mimeTypes.first ?? "" > $1.mimeTypes.first ?? ""})
default:
break
}
}
}
extension UTISummary: NSTableViewDelegate {
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "cell"), owner: nil) as! NSTableCellView
switch tableColumn?.identifier.rawValue ?? "" {
case "uti":
cell.textField?.stringValue = allFileTypes[row].UTI
case "mime":
cell.textField?.stringValue = allFileTypes[row].mimeTypes.joined(separator: ", ")
case "ext":
cell.textField?.stringValue = allFileTypes[row].extensions.joined(separator: ", ")
case "desc":
cell.textField?.stringValue = allFileTypes[row].description
default:
break
}
return cell
}
}