#if os(macOS) import AppKit import RastrilloApple import SwiftUI /// The floating Format inspector (Paul's ruling 1, option B; spec §1): a /// 248pt glass panel over the desk, 12pt below the toolbar and 12pt from the /// trailing edge, with the web inspector's four sections. An overlay in the /// editor's ZStack, never SwiftUI's `.inspector` (a closed `.inspector` /// invalidates hosting constraints on macOS 27 while the editor loads). /// /// Its controls act on the text view's selection: buttons and pop-ups on the /// Mac do not take first responder from a click, so the caret stays where /// it was. Selected states come from `DocumentToolbarSelection`, as the /// menus' do: the model's one observed `toolbarSelection`, computed from the /// live text at the selection and kept while input is pending (S1a), so /// moving the caret re-evaluates this and not the body hosting the text /// view. Its commands act on the text view's own storage, never a render. @MainActor struct DocsFormatInspector: View { let model: DocsModel @Environment(\.accessibilityReduceTransparency) private var reduceTransparency @Environment(\.colorScheme) private var colorScheme @Environment(\.locale) private var locale static let sizes = [8, 9, 10, 11, 12, 14, 18, 24, 36, 48, 72] private var actions: DocumentCommands { DocumentCommands(model: model, content: model.surface.content) } /// Runs a command now, or once the input pending when it was clicked /// has been drawn (`DocsModel.whenCommandsReady`). private func ready(_ command: @escaping @MainActor (DocumentCommands) -> Void) { model.whenCommandsReady(command) } var body: some View { #if DOCS_PROBES let _ = DocsProbes.inspectorBodies += 1 #endif let actions = self.actions let selection = model.toolbarSelection VStack(alignment: .leading, spacing: DocsSpacing.sp4) { section("docs.editor.toolbar.group.style") { stylePicker(selection) HStack(spacing: DocsSpacing.sp2) { fontPicker(selection) sizePicker(selection).frame(width: 64) } } section("docs.editor.toolbar.group.text") { DocsSegmentedRow(items: [ .init(key: "docs.editor.toolbar.bold", icon: "bold", on: selection.marks.contains("strong")) { ready { $0.perform("bold") } }, .init(key: "docs.editor.toolbar.italic", icon: "italic", on: selection.marks.contains("em")) { ready { $0.perform("italic") } }, .init(key: "docs.editor.toolbar.code", icon: "chevron.left.forwardslash.chevron.right", on: selection.marks.contains("code")) { ready { $0.perform("code") } }, ]) } section("docs.editor.toolbar.group.align") { DocsSegmentedRow(items: ["left", "center", "right"].map { value in .init(key: "docs.editor.toolbar.align." + value, icon: "text.align" + value, on: selection.alignment == value) { ready { $0.blockAttribute("align", value: value == "left" ? nil : value) } } }) } section("docs.editor.toolbar.group.lists") { DocsSegmentedRow(items: [ .init(key: "docs.editor.toolbar.bulletlist", icon: "list.bullet", on: selection.blocks.contains("bullet_list")) { ready { $0.block("bullet_list") } }, .init(key: "docs.editor.toolbar.orderedlist", icon: "list.number", on: selection.blocks.contains("ordered_list")) { ready { $0.block("ordered_list") } }, .init(key: "docs.editor.toolbar.quote", icon: "text.quote", on: selection.blocks.contains("blockquote")) { ready { $0.block("blockquote") } }, ]) // Native keeps the mock-up's second row (spec §13 Q2). DocsSegmentedRow(items: [ .init(key: "docs.editor.menu.format.outdent", icon: "decrease.indent", on: false, mirrors: true) { ready { $0.perform("outdent") } }, .init(key: "docs.editor.menu.format.indent", icon: "increase.indent", on: false, mirrors: true) { ready { $0.perform("indent") } }, .init(key: "docs.editor.menu.format.clear", icon: "eraser", on: false, enabled: selection.hasSelection) { ready { $0.perform("clear-format") } }, ]) } } .padding(DocsSpacing.sp3) .frame(width: DocsDeskLayout.inspectorWidth, alignment: .leading) .background { panel } .overlay { // A hairline accent rim: accent mixed into the line colour, 70% in // light, 30% in dark. Kept under Increase Contrast. RoundedRectangle(cornerRadius: 12) .strokeBorder(OficinaPalette.accent(.docs).mix(with: OficinaPalette.line, by: colorScheme == .dark ? 0.7 : 0.3), lineWidth: 1) } .compositingGroup() .shadow(color: .black.opacity(colorScheme == .dark ? 0.4 : 0.14), radius: 16, y: 6) // Replica work briefly gates commands on every keystroke; keep that // out of the disabled styling so the panel does not blink (and is // not evaluated again twice a key, S1b step 0): a click then runs // once the input is drawn. Held for longer, the panel shows disabled. .disabled(!actions.isAvailable || model.commandsHeld) .accessibilityElement(children: .contain) .accessibilityLabel(Text("docs.editor.inspector.label", bundle: DocsResources.bundle)) .accessibilityIdentifier("docs.format.inspector") } @ViewBuilder private var panel: some View { if reduceTransparency { RoundedRectangle(cornerRadius: 12).fill(OficinaPalette.surface) } else { RoundedRectangle(cornerRadius: 12).fill(.clear).glassEffect(.regular, in: RoundedRectangle(cornerRadius: 12)) } } private func section(_ key: String, @ViewBuilder content: () -> Content) -> some View { VStack(alignment: .leading, spacing: DocsSpacing.sp2) { Text(LocalizedStringKey(key), bundle: DocsResources.bundle) .font(.system(size: 11, weight: .semibold)) .foregroundStyle(OficinaPalette.textMuted) .fixedSize(horizontal: false, vertical: true) .accessibilityAddTraits(.isHeader) content() } .accessibilityElement(children: .contain) .accessibilityLabel(Text(LocalizedStringKey(key), bundle: DocsResources.bundle)) } /// Body text and Heading 1–3, each in its own face. private func stylePicker(_ selection: DocumentToolbarSelection) -> some View { let strings = DocsStrings(locale: locale) let entries = Self.styleEntries(selected: selection.style, strings: strings) return DocsPopUp(entries: entries, selected: selection.style, label: strings("docs.editor.toolbar.block"), identifier: "docs.style-picker") { style in if style.hasPrefix("heading"), let level = Int(style.dropFirst("heading".count)) { ready { $0.block("heading", level: level) } } else { ready { $0.block("paragraph") } } } .frame(maxWidth: .infinity) } /// Families in their own faces (spec §1), stretching to the column the /// size leaves: a pop-up that hugs its widest name ran past the panel. private func fontPicker(_ selection: DocumentToolbarSelection) -> some View { let strings = DocsStrings(locale: locale) return DocsPopUp(entries: DocsFontPicker.entries.map { .init(id: $0.key, title: $0.name, font: $0.font) }, selected: selection.font, label: strings("native.font"), identifier: "docs.font-picker") { key in ready { $0.setFont(key) } } .frame(minWidth: 0, maxWidth: .infinity) } /// Body text and Headings 1–3, each in its own face, and the selection's /// own level when it is in a Heading 4–6. static func styleEntries(selected: String, strings: DocsStrings) -> [DocsPopUp.Entry] { var entries = [DocsPopUp.Entry(id: "paragraph", title: strings("docs.editor.toolbar.block.paragraph"), font: DocsFonts.font(marks: [:], size: 13, heading: false))] for level in 1...3 { entries.append(.init(id: "heading" + String(level), title: strings(EditorToolbar.headingKey(level)), font: DocsFonts.font(marks: [:], size: 13, heading: true))) } // Headings 4–6 still render and edit but are not offered (spec §1, // menus). A selection in one shows its own level, never the style // the pop-up last showed. if selected.hasPrefix("heading"), let level = Int(selected.dropFirst("heading".count)), level > 3 { entries.append(.init(id: selected, title: Self.headingTitle(level, strings: strings), font: DocsFonts.font(marks: [:], size: 13, heading: true))) } return entries } /// "Heading 4" and so on, from the catalog's own format ("Heading %d"). static func headingTitle(_ level: Int, strings: DocsStrings) -> String { let format = strings.bundle.localizedString(forKey: "Heading %d", value: nil, table: "Mac") return String(format: format, level) } private func sizePicker(_ selection: DocumentToolbarSelection) -> some View { let strings = DocsStrings(locale: locale) return DocsPopUp(entries: Self.sizes.map { .init(id: String($0), title: $0.formatted(.number.locale(locale)), font: nil) }, selected: String(selection.size), label: strings("docs.editor.toolbar.size"), identifier: "docs.size-picker") { size in ready { $0.format("size_" + size, exclusive: Self.sizes.map { "size_" + String($0) } + ["size_sm", "size_lg", "size_xl"]) } } } } /// A Mac pop-up button over fixed entries, each title in its own face when /// it has one, as `DocsFontPopUp` draws the families. It stretches to the /// width it is given. struct DocsPopUp: NSViewRepresentable { struct Entry { let id: String; let title: String; let font: NSFont? } let entries: [Entry] let selected: String let label: String let identifier: String let choose: (String) -> Void func makeCoordinator() -> Coordinator { Coordinator() } func makeNSView(context: Context) -> NSPopUpButton { let button = NSPopUpButton(frame: .zero, pullsDown: false) button.target = context.coordinator button.action = #selector(Coordinator.picked(_:)) button.setContentHuggingPriority(.defaultLow, for: .horizontal) button.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) button.setAccessibilityIdentifier(identifier) return button } func updateNSView(_ button: NSPopUpButton, context: Context) { context.coordinator.choose = choose button.isEnabled = context.environment.isEnabled button.setAccessibilityLabel(label) let titles = entries.map(\.title) if button.itemArray.map(\.title) != titles { let menu = NSMenu() for entry in entries { let item = NSMenuItem(title: entry.title, action: nil, keyEquivalent: "") item.representedObject = entry.id if let font = entry.font { item.attributedTitle = NSAttributedString(string: entry.title, attributes: [.font: font]) } menu.addItem(item) } button.menu = menu } if let index = entries.firstIndex(where: { $0.id == 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 id = sender.selectedItem?.representedObject as? String { choose(id) } } } } /// Joined 28pt buttons in one well; a lit one is accent-soft with an accent /// glyph, and says so to VoiceOver (`.isSelected`), never by colour alone. struct DocsSegmentedRow: View { struct Item: Identifiable { let key: String let icon: String let on: Bool var enabled = true /// Indent and outdent point along the line: they mirror right to left. var mirrors = false let action: () -> Void var id: String { key } } let items: [Item] var body: some View { let accent = OficinaPalette.accent(.docs) // spacing-allow: the joined buttons share one well, 2pt apart, as the // web inspector's segmented rows. HStack(spacing: 2) { ForEach(items) { item in Button(action: item.action) { Image(systemName: item.icon) .font(.system(size: 13, weight: .medium)) .flipsForRightToLeftLayoutDirection(item.mirrors) .frame(maxWidth: .infinity, minHeight: 28) // spec §1: joined 28pt buttons .contentShape(Rectangle()) } .buttonStyle(.plain) .foregroundStyle(item.on ? accent : OficinaPalette.text) .background(RoundedRectangle(cornerRadius: 6).fill(item.on ? OficinaPalette.accentSoft(.docs) : .clear)) .overlay(RoundedRectangle(cornerRadius: 6).strokeBorder(item.on ? accent.opacity(0.55) : .clear, lineWidth: 1)) .disabled(!item.enabled) .opacity(item.enabled ? 1 : 0.4) .help(Text(LocalizedStringKey(item.key), bundle: DocsResources.bundle)) .accessibilityLabel(Text(LocalizedStringKey(item.key), bundle: DocsResources.bundle)) .accessibilityAddTraits(item.on ? .isSelected : []) } } .padding(2) // spacing-allow: the well's inset around its joined buttons .frame(height: 32) // the buttons' 28pt and the well's 2pt inset each side .background(RoundedRectangle(cornerRadius: 8).fill(OficinaPalette.sunk)) } } #endif