import SwiftUI #if os(macOS) import AppKit #else import UIKit #endif /// The font picker: the shared list's families in its order, each name drawn /// in its own face (Paul, 2026-10-24). Names are the families' own and are /// not translated, and a row is the name alone: a metric twin does not say /// whose metrics it has (Paul, 2026-10-26), as the web's rows do not. The /// twin is still the shared list's, for paste and DOCX. /// /// Every face is bundled and registered before the first window, so a row is /// never drawn in a face still arriving and needs no hold. /// /// On the Mac the toolbar is a standard pop-up button and Format > Font is a /// menu; on iPhone and iPad it is a popover list, because UIKit menus draw /// every item in the system face. enum DocsFontPicker { struct Entry: Identifiable { let key: String let name: String /// The family's regular face at the picker's size. let font: DocsPlatformFont var id: String { key } } #if os(macOS) static var size: CGFloat { NSFont.systemFontSize } #else static var size: CGFloat { UIFont.preferredFont(forTextStyle: .body).pointSize } #endif @MainActor static var entries: [Entry] { DocsFonts.families.map { family in Entry(key: family.key, name: family.name, font: DocsFonts.font(family, bold: false, italic: false, size: size)) } } /// One family's name in its own face, for a menu. @MainActor static func label(_ entry: Entry) -> Text { Text(entry.name).font(Font(entry.font as CTFont)) } #if os(macOS) /// The pop-up button's menu: each item's title in its family's face, its /// key as the represented object, the current family checked. @MainActor static func menu(selected: String) -> NSMenu { let menu = NSMenu() for entry in entries { let item = NSMenuItem(title: entry.name, action: nil, keyEquivalent: "") item.representedObject = entry.key item.attributedTitle = NSAttributedString(string: entry.name, attributes: [.font: entry.font]) item.state = entry.key == selected ? .on : .off menu.addItem(item) } return menu } #endif } #if os(macOS) /// The toolbar's font control: a standard NSPopUpButton, its closed face /// showing the current family in that family's face. struct DocsFontPopUp: NSViewRepresentable { let selected: String let choose: (String) -> Void func makeCoordinator() -> Coordinator { Coordinator() } func makeNSView(context: Context) -> NSPopUpButton { let button = NSPopUpButton(frame: .zero, pullsDown: false) button.menu = DocsFontPicker.menu(selected: selected) button.target = context.coordinator button.action = #selector(Coordinator.picked(_:)) button.setAccessibilityLabel(String(localized: "native.font", bundle: DocsResources.bundle)) button.setAccessibilityIdentifier("docs.font-picker") button.setContentHuggingPriority(.required, for: .horizontal) return button } func updateNSView(_ button: NSPopUpButton, context: Context) { context.coordinator.choose = choose button.isEnabled = context.environment.isEnabled if let index = button.itemArray.firstIndex(where: { $0.representedObject as? String == selected }), button.indexOfSelectedItem != index { button.selectItem(at: index) } } @MainActor final class Coordinator: NSObject { var choose: (String) -> Void = { _ in } @objc func picked(_ sender: NSPopUpButton) { if let key = sender.selectedItem?.representedObject as? String { choose(key) } } } } #else /// The toolbar's font control on iPhone and iPad: the current family's name /// in its face, opening a list of every family in its own. struct DocsFontPickerButton: View { let selected: String let choose: (String) -> Void @State private var open = false /// The family's name scales with Dynamic Type, in its own face (spec ยง9). @ScaledMetric(relativeTo: .body) private var scale: CGFloat = 1 var body: some View { let entries = DocsFontPicker.entries let current = entries.first { $0.key == selected } ?? entries[0] Button { open = true } label: { Text(current.name).font(Font(current.font.withSize(current.font.pointSize * scale) as CTFont)).lineLimit(1).frame(minWidth: 80, alignment: .leading) } .accessibilityLabel(Text("native.font", bundle: DocsResources.bundle)) .accessibilityValue(Text(current.name)) .accessibilityIdentifier("docs.font-picker") .popover(isPresented: $open) { List(entries) { entry in Button { open = false; choose(entry.key) } label: { HStack { Text(entry.name).font(Font(entry.font as CTFont)) Spacer() if entry.key == selected { Image(systemName: "checkmark").foregroundStyle(Color.accentColor) } }.contentShape(Rectangle()) } .foregroundStyle(.primary) .accessibilityAddTraits(entry.key == selected ? .isSelected : []) } .listStyle(.plain) .frame(minWidth: 280, minHeight: 440) .presentationCompactAdaptation(.popover) } } } #endif