Skip to content

Commit ffb4748

Browse files
authored
support setting generic font families (#312)
1 parent 8834308 commit ffb4748

File tree

6 files changed

+125
-87
lines changed

6 files changed

+125
-87
lines changed
116 Bytes
Binary file not shown.
120 Bytes
Binary file not shown.

src/config/FontView.swift

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import SwiftUI
2+
3+
let genericFamilies = [
4+
"cursive",
5+
"fangsong",
6+
"fantasy",
7+
"kai",
8+
"khmer-mul",
9+
// "math", // Not supported by Safari
10+
"monospace",
11+
"nastaliq",
12+
"sans-serif",
13+
"serif",
14+
"system-ui",
15+
"ui-monospace",
16+
"ui-rounded",
17+
"ui-sans-serif",
18+
"ui-serif",
19+
]
20+
21+
struct FontOptionView: OptionView {
22+
let label: String
23+
@ObservedObject var model: FontOption
24+
@State private var selectorIsOpen = false
25+
@State var searchInput = ""
26+
@State var previewInput = NSLocalizedString("Preview", comment: "")
27+
28+
// If initialize [], the sheet will list nothing on first open.
29+
@State var availableFontFamilies = NSFontManager.shared.availableFontFamilies
30+
var filteredFontFamilies: [String] {
31+
if searchInput.trimmingCharacters(in: .whitespaces).isEmpty {
32+
return availableFontFamilies
33+
} else {
34+
return availableFontFamilies.filter {
35+
$0.localizedCaseInsensitiveContains(searchInput)
36+
|| localize($0).localizedCaseInsensitiveContains(searchInput)
37+
}
38+
}
39+
}
40+
@State private var selectedFontFamily: String?
41+
42+
var body: some View {
43+
Button(action: openSelector) {
44+
if model.value.isEmpty {
45+
Text("Select font")
46+
} else {
47+
Text(localize(model.value))
48+
}
49+
}
50+
.sheet(isPresented: $selectorIsOpen) {
51+
VStack {
52+
TabView {
53+
VStack {
54+
TextField(NSLocalizedString("Search", comment: ""), text: $searchInput)
55+
TextField(NSLocalizedString("Preview", comment: ""), text: $previewInput)
56+
Text(previewInput).font(Font.custom(selectedFontFamily ?? model.value, size: 32)).frame(
57+
height: 64)
58+
List(selection: $selectedFontFamily) {
59+
ForEach(filteredFontFamilies, id: \.self) { family in
60+
HStack {
61+
Text(localize(family)).font(Font.custom(family, size: 14))
62+
Spacer()
63+
Text(localize(family))
64+
}
65+
}
66+
}.contextMenu(forSelectionType: String.self) { items in
67+
} primaryAction: { items in
68+
// Double click
69+
select()
70+
}
71+
}.padding()
72+
.tabItem { Text("Font family") }
73+
74+
VStack {
75+
List(selection: $selectedFontFamily) {
76+
ForEach(genericFamilies, id: \.self) { family in
77+
Text(family)
78+
}
79+
}.contextMenu(forSelectionType: String.self) { items in
80+
} primaryAction: { items in
81+
// Double click
82+
select()
83+
}
84+
}.padding()
85+
.tabItem { Text("Generic font families") }
86+
}
87+
88+
HStack {
89+
Button {
90+
selectorIsOpen = false
91+
} label: {
92+
Text("Cancel")
93+
}
94+
Spacer()
95+
Button {
96+
select()
97+
} label: {
98+
Text("Select")
99+
}.buttonStyle(.borderedProminent)
100+
.disabled(selectedFontFamily == nil)
101+
}.padding([.leading, .trailing, .bottom])
102+
}
103+
.padding(.top)
104+
.frame(minWidth: 500, minHeight: 600)
105+
}
106+
}
107+
108+
private func openSelector() {
109+
availableFontFamilies = NSFontManager.shared.availableFontFamilies
110+
selectorIsOpen = true
111+
}
112+
113+
private func select() {
114+
if let selectedFontFamily = selectedFontFamily {
115+
model.value = selectedFontFamily
116+
}
117+
selectorIsOpen = false
118+
}
119+
120+
private func localize(_ fontFamily: String) -> String {
121+
return NSFontManager.shared.localizedName(forFamily: fontFamily, face: nil)
122+
}
123+
}

src/config/optionviews.swift

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -387,91 +387,6 @@ struct ListOptionView<T: Option & EmptyConstructible>: OptionView {
387387
}
388388
}
389389

390-
struct FontOptionView: OptionView {
391-
let label: String
392-
@ObservedObject var model: FontOption
393-
@State private var selectorIsOpen = false
394-
@State var searchInput = ""
395-
@State var previewInput = NSLocalizedString("Preview", comment: "")
396-
397-
// If initialize [], the sheet will list nothing on first open.
398-
@State var availableFontFamilies = NSFontManager.shared.availableFontFamilies
399-
var filteredFontFamilies: [String] {
400-
if searchInput.trimmingCharacters(in: .whitespaces).isEmpty {
401-
return availableFontFamilies
402-
} else {
403-
return availableFontFamilies.filter {
404-
$0.localizedCaseInsensitiveContains(searchInput)
405-
|| localize($0).localizedCaseInsensitiveContains(searchInput)
406-
}
407-
}
408-
}
409-
@State private var selectedFontFamily: String?
410-
411-
var body: some View {
412-
Button(action: openSelector) {
413-
if model.value.isEmpty {
414-
Text("Select font")
415-
} else {
416-
Text(localize(model.value))
417-
}
418-
}
419-
.sheet(isPresented: $selectorIsOpen) {
420-
VStack {
421-
TextField(NSLocalizedString("Search", comment: ""), text: $searchInput)
422-
TextField(NSLocalizedString("Preview", comment: ""), text: $previewInput)
423-
Text(previewInput).font(Font.custom(selectedFontFamily ?? model.value, size: 32)).frame(
424-
height: 64)
425-
List(selection: $selectedFontFamily) {
426-
ForEach(filteredFontFamilies, id: \.self) { family in
427-
HStack {
428-
Text(localize(family)).font(Font.custom(family, size: 14))
429-
Spacer()
430-
Text(localize(family))
431-
}
432-
}
433-
}.contextMenu(forSelectionType: String.self) { items in
434-
} primaryAction: { items in
435-
// Double click
436-
select()
437-
}
438-
HStack {
439-
Button {
440-
selectorIsOpen = false
441-
} label: {
442-
Text("Cancel")
443-
}
444-
Spacer()
445-
Button {
446-
select()
447-
} label: {
448-
Text("Select")
449-
}.buttonStyle(.borderedProminent)
450-
.disabled(selectedFontFamily == nil)
451-
}
452-
}
453-
.padding()
454-
.frame(minWidth: 500, minHeight: 600)
455-
}
456-
}
457-
458-
private func openSelector() {
459-
availableFontFamilies = NSFontManager.shared.availableFontFamilies
460-
selectorIsOpen = true
461-
}
462-
463-
private func select() {
464-
if let selectedFontFamily = selectedFontFamily {
465-
model.value = selectedFontFamily
466-
}
467-
selectorIsOpen = false
468-
}
469-
470-
private func localize(_ fontFamily: String) -> String {
471-
return NSFontManager.shared.localizedName(forFamily: fontFamily, face: nil)
472-
}
473-
}
474-
475390
struct PunctuationMapOptionView: OptionView {
476391
let label: String
477392
@ObservedObject var model: PunctuationMapOption

0 commit comments

Comments
 (0)