Skip to content

Commit 6f71966

Browse files
committed
Add font size control to RuntimeObjectDetail
1 parent e654f29 commit 6f71966

File tree

2 files changed

+80
-4
lines changed

2 files changed

+80
-4
lines changed

HeaderViewer/RuntimeObjectDetail.swift

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ import SwiftUI
99
import ClassDump
1010

1111
struct RuntimeObjectDetail: View {
12+
// influenced by
13+
// https://developer.apple.com/design/human-interface-guidelines/typography
14+
private static var defaultFontSize: Int {
15+
#if os(iOS) || os(visionOS)
16+
16
17+
#elseif os(macOS)
18+
13
19+
#else
20+
#warning("Unknown platform")
21+
12
22+
#endif
23+
}
24+
1225
let type: RuntimeObjectType
1326

1427
@State private var stripProtocolConformance: Bool = false
@@ -17,6 +30,8 @@ struct RuntimeObjectDetail: View {
1730
@State private var stripSynthesized: Bool = true
1831
@State private var addSymbolImageComments: Bool = false
1932

33+
@AppStorage("code_font_size") private var fontSize: Int = Self.defaultFontSize
34+
2035
private var generationOptions: CDGenerationOptions {
2136
let options: CDGenerationOptions = .init()
2237
options.stripProtocolConformance = stripProtocolConformance
@@ -36,7 +51,7 @@ struct RuntimeObjectDetail: View {
3651
if let cls = NSClassFromString(name) {
3752
let semanticString: CDSemanticString = CDClassModel(with: cls)
3853
.semanticLines(with: generationOptions)
39-
SemanticStringView(semanticString)
54+
SemanticStringView(semanticString, fontSize: CGFloat(fontSize))
4055
} else {
4156
Text("No class named \(Text(name).font(.callout.monospaced())) found")
4257
.scenePadding()
@@ -45,7 +60,7 @@ struct RuntimeObjectDetail: View {
4560
if let prtcl = NSProtocolFromString(name) {
4661
let semanticString: CDSemanticString = CDProtocolModel(with: prtcl)
4762
.semanticLines(with: generationOptions)
48-
SemanticStringView(semanticString)
63+
SemanticStringView(semanticString, fontSize: CGFloat(fontSize))
4964
} else {
5065
Text("No protocol named \(Text(name).font(.callout.monospaced())) found")
5166
.scenePadding()
@@ -54,6 +69,8 @@ struct RuntimeObjectDetail: View {
5469
}
5570
.navigationTitle(type.name)
5671
.toolbar {
72+
FontToolbarItem(fontSize: $fontSize)
73+
5774
ToolbarItem {
5875
Menu {
5976
Toggle("Strip protocol conformance", isOn: $stripProtocolConformance)
@@ -74,3 +91,60 @@ struct RuntimeObjectDetail: View {
7491
#endif
7592
}
7693
}
94+
95+
private struct FontToolbarItem: ToolbarContent {
96+
@Binding var fontSize: Int
97+
98+
private var sizeDescription: String {
99+
fontSize.formatted(.number)
100+
}
101+
102+
private var smallerButton: some View {
103+
Button("Smaller", systemImage: "textformat.size.smaller") {
104+
guard fontSize > 4 else { return }
105+
fontSize -= 1
106+
}
107+
}
108+
109+
private var largerButton: some View {
110+
Button("Larger", systemImage: "textformat.size.larger") {
111+
guard fontSize < 28 else { return }
112+
fontSize += 1
113+
}
114+
}
115+
116+
private func sizeControlGroup(withDescription: Bool) -> some View {
117+
ControlGroup("Font Size", systemImage: "textformat.size") {
118+
smallerButton
119+
if withDescription {
120+
Text(sizeDescription)
121+
}
122+
largerButton
123+
}
124+
}
125+
126+
var body: some ToolbarContent {
127+
ToolbarItem {
128+
#if os(iOS) || os(visionOS)
129+
if #available(iOS 16.4, *) {
130+
Menu {
131+
sizeControlGroup(withDescription: true)
132+
.controlGroupStyle(.compactMenu)
133+
} label: {
134+
Label("Font", systemImage: "textformat.size")
135+
}
136+
.menuActionDismissBehavior(.disabled)
137+
} else {
138+
Menu {
139+
sizeControlGroup(withDescription: true)
140+
} label: {
141+
Label("Font", systemImage: "textformat.size")
142+
}
143+
}
144+
#else
145+
sizeControlGroup(withDescription: false)
146+
.controlGroupStyle(.navigation)
147+
#endif
148+
}
149+
}
150+
}

HeaderViewer/SemanticStringView.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import ClassDump
1010

1111
struct SemanticStringView: View {
1212
let semanticString: CDSemanticString
13+
let fontSize: CGFloat
1314

14-
init(_ semanticString: CDSemanticString) {
15+
init(_ semanticString: CDSemanticString, fontSize: CGFloat) {
1516
self.semanticString = semanticString
17+
self.fontSize = fontSize
1618
}
1719

1820
var body: some View {
@@ -34,7 +36,7 @@ struct SemanticStringView: View {
3436
}
3537
.accessibilityTextContentType(.sourceCode)
3638
}
37-
.font(.body.monospaced())
39+
.font(.system(size: fontSize, design: .monospaced))
3840
.textSelection(.enabled)
3941
.multilineTextAlignment(.leading)
4042
.scenePadding()

0 commit comments

Comments
 (0)