oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1import SwiftUI
2#if os(macOS)
3import AppKit
4#else
5import UIKit
6#endif
7
8/// The font picker: the shared list's families in its order, each name drawn
9/// in its own face (Paul, 2026-10-24). Names are the families' own and are
10/// not translated, and a row is the name alone: a metric twin does not say
11/// whose metrics it has (Paul, 2026-10-26), as the web's rows do not. The
12/// twin is still the shared list's, for paste and DOCX.
13///
14/// Every face is bundled and registered before the first window, so a row is
15/// never drawn in a face still arriving and needs no hold.
16///
17/// On the Mac the toolbar is a standard pop-up button and Format > Font is a
18/// menu; on iPhone and iPad it is a popover list, because UIKit menus draw
19/// every item in the system face.
20enum DocsFontPicker {
21 struct Entry: Identifiable {
22 let key: String
23 let name: String
24 /// The family's regular face at the picker's size.
25 let font: DocsPlatformFont
26 var id: String { key }
27 }
28
29 #if os(macOS)
30 static var size: CGFloat { NSFont.systemFontSize }
31 #else
32 static var size: CGFloat { UIFont.preferredFont(forTextStyle: .body).pointSize }
33 #endif
34
35 @MainActor static var entries: [Entry] {
36 DocsFonts.families.map { family in
37 Entry(key: family.key, name: family.name, font: DocsFonts.font(family, bold: false, italic: false, size: size))
38 }
39 }
40
41 /// One family's name in its own face, for a menu.
42 @MainActor static func label(_ entry: Entry) -> Text {
43 Text(entry.name).font(Font(entry.font as CTFont))
44 }
45
46 #if os(macOS)
47 /// The pop-up button's menu: each item's title in its family's face, its
48 /// key as the represented object, the current family checked.
49 @MainActor static func menu(selected: String) -> NSMenu {
50 let menu = NSMenu()
51 for entry in entries {
52 let item = NSMenuItem(title: entry.name, action: nil, keyEquivalent: "")
53 item.representedObject = entry.key
54 item.attributedTitle = NSAttributedString(string: entry.name, attributes: [.font: entry.font])
55 item.state = entry.key == selected ? .on : .off
56 menu.addItem(item)
57 }
58 return menu
59 }
60 #endif
61}
62
63#if os(macOS)
64/// The toolbar's font control: a standard NSPopUpButton, its closed face
65/// showing the current family in that family's face.
66struct DocsFontPopUp: NSViewRepresentable {
67 let selected: String
68 let choose: (String) -> Void
69 func makeCoordinator() -> Coordinator { Coordinator() }
70 func makeNSView(context: Context) -> NSPopUpButton {
71 let button = NSPopUpButton(frame: .zero, pullsDown: false)
72 button.menu = DocsFontPicker.menu(selected: selected)
73 button.target = context.coordinator
74 button.action = #selector(Coordinator.picked(_:))
75 button.setAccessibilityLabel(String(localized: "native.font", bundle: DocsResources.bundle))
76 button.setAccessibilityIdentifier("docs.font-picker")
77 button.setContentHuggingPriority(.required, for: .horizontal)
78 return button
79 }
80 func updateNSView(_ button: NSPopUpButton, context: Context) {
81 context.coordinator.choose = choose
82 button.isEnabled = context.environment.isEnabled
83 if let index = button.itemArray.firstIndex(where: { $0.representedObject as? String == selected }), button.indexOfSelectedItem != index {
84 button.selectItem(at: index)
85 }
86 }
87 @MainActor final class Coordinator: NSObject {
88 var choose: (String) -> Void = { _ in }
89 @objc func picked(_ sender: NSPopUpButton) {
90 if let key = sender.selectedItem?.representedObject as? String { choose(key) }
91 }
92 }
93}
94#else
95/// The toolbar's font control on iPhone and iPad: the current family's name
96/// in its face, opening a list of every family in its own.
97struct DocsFontPickerButton: View {
98 let selected: String
99 let choose: (String) -> Void
100 @State private var open = false
101 /// The family's name scales with Dynamic Type, in its own face (spec §9).
102 @ScaledMetric(relativeTo: .body) private var scale: CGFloat = 1
103 var body: some View {
104 let entries = DocsFontPicker.entries
105 let current = entries.first { $0.key == selected } ?? entries[0]
106 Button { open = true } label: {
107 Text(current.name).font(Font(current.font.withSize(current.font.pointSize * scale) as CTFont)).lineLimit(1).frame(minWidth: 80, alignment: .leading)
108 }
109 .accessibilityLabel(Text("native.font", bundle: DocsResources.bundle))
110 .accessibilityValue(Text(current.name))
111 .accessibilityIdentifier("docs.font-picker")
112 .popover(isPresented: $open) {
113 List(entries) { entry in
114 Button { open = false; choose(entry.key) } label: {
115 HStack {
116 Text(entry.name).font(Font(entry.font as CTFont))
117 Spacer()
118 if entry.key == selected { Image(systemName: "checkmark").foregroundStyle(Color.accentColor) }
119 }.contentShape(Rectangle())
120 }
121 .foregroundStyle(.primary)
122 .accessibilityAddTraits(entry.key == selected ? .isSelected : [])
123 }
124 .listStyle(.plain)
125 .frame(minWidth: 280, minHeight: 440)
126 .presentationCompactAdaptation(.popover)
127 }
128 }
129}
130#endif
131