-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathFontSelectorViewController.swift
107 lines (88 loc) · 3.95 KB
/
FontSelectorViewController.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
//
// FontSelectorViewController.swift
// Syntax Highlight
//
// Created by Sbarex on 08/03/21.
// Copyright © 2021 sbarex. All rights reserved.
//
import Cocoa
class FontSelectorViewController: NSViewController {
@IBOutlet weak var fontPopupMenu: NSPopUpButton!
@IBOutlet weak var fontSizeField: NSTextField!
typealias FontInfo = (name: String, title: String, font: NSFont)
var fonts: [NSFont] = []
var handler: ((String, CGFloat, Bool)->Void)?
@objc dynamic var fontSize: CGFloat = 12 {
didSet {
handler?(fontName, self.fontSize, false)
}
}
@objc dynamic var fontName: String = "-" {
didSet {
handler?(fontName, self.fontSize, false)
}
}
override func viewDidLoad() {
var fontNames: Set<String> = Set(NSFontManager.shared.availableFontNames(with: [.fixedPitchFontMask]) ?? [])
fontNames.subtract(Set(NSFontManager.shared.availableFontNames(with: [.fixedPitchFontMask, .boldFontMask]) ?? [])) // Remove the bold variants
fontNames.subtract(Set(NSFontManager.shared.availableFontNames(with: [.fixedPitchFontMask, .italicFontMask]) ?? [])) // Remove the italic variants
var minimumCharacterSetMutable = CharacterSet()
minimumCharacterSetMutable.formUnion(CharacterSet(charactersIn: "0"..."9"))
minimumCharacterSetMutable.formUnion(CharacterSet(charactersIn: "a"..."z"))
minimumCharacterSetMutable.formUnion(CharacterSet(charactersIn: "A"..."Z"))
for name in fontNames {
guard let font = NSFont(name: name, size: 0) else {
continue
}
guard let displayName = font.displayName, !displayName.isEmpty else {
continue
}
guard let firstChar = displayName.first, firstChar != "#" && firstChar != "." else {
continue
}
guard font.coveredCharacterSet.isSuperset(of: minimumCharacterSetMutable) else {
continue // Weed out some useless fonts, like Monotype Sorts
}
fonts.append(font)
}
let insertFontItem = { (_ font: NSFont) in
// fontPopupMenu.addItem(withTitle: item.title)
let menu = NSMenuItem(title: font.displayName ?? font.fontName, action: nil, keyEquivalent: "")
let astr = NSAttributedString(string: font.displayName ?? font.fontName, attributes: [.font: font])
menu.attributedTitle = astr
menu.representedObject = font
menu.toolTip = font.displayName ?? font.fontName
self.fontPopupMenu.menu?.addItem(menu)
}
fonts.sorted(by: { $0.displayName!.lowercased() < $1.displayName!.lowercased()}).forEach { item in
insertFontItem(item)
}
if fonts.first(where: {$0.fontName == fontName}) == nil {
if let font = NSFont(name: fontName, size: 0), font.displayName != nil {
fonts.append(font)
fontPopupMenu.menu?.addItem(NSMenuItem.separator())
insertFontItem(font)
}
}
if fontName == "-" {
fontPopupMenu.selectItem(at: 0)
} else if let font = fonts.first(where: { $0.fontName == fontName}) {
fontPopupMenu.selectItem(withTitle: font.displayName!)
}
}
@IBAction func openFontPanel(_ sender: Any) {
handler?(fontName, self.fontSize, true)
}
@IBAction func handleFontPopup(_ sender: Any) {
if fontPopupMenu.indexOfSelectedItem == 0 {
// ui-monospace
self.fontName = "-"
} else if let font = fontPopupMenu.selectedItem?.representedObject as? NSFont {
self.fontName = font.fontName
}
}
@IBAction func handleDone(_ sender: Any) {
handler?(fontName, self.fontSize, false)
self.dismiss(self)
}
}