| 1 | #if os(macOS) |
| 2 | import AppKit |
| 3 | import RastrilloApple |
| 4 | import SwiftUI |
| 5 | |
| 6 | /// The floating Format inspector (Paul's ruling 1, option B; spec §1): a |
| 7 | /// 248pt glass panel over the desk, 12pt below the toolbar and 12pt from the |
| 8 | /// trailing edge, with the web inspector's four sections. An overlay in the |
| 9 | /// editor's ZStack, never SwiftUI's `.inspector` (a closed `.inspector` |
| 10 | /// invalidates hosting constraints on macOS 27 while the editor loads). |
| 11 | /// |
| 12 | /// Its controls act on the text view's selection: buttons and pop-ups on the |
| 13 | /// Mac do not take first responder from a click, so the caret stays where |
| 14 | /// it was. Selected states come from `DocumentToolbarSelection`, as the |
| 15 | /// menus' do: the model's one observed `toolbarSelection`, computed from the |
| 16 | /// live text at the selection and kept while input is pending (S1a), so |
| 17 | /// moving the caret re-evaluates this and not the body hosting the text |
| 18 | /// view. Its commands act on the text view's own storage, never a render. |
| 19 | @MainActor struct DocsFormatInspector: View { |
| 20 | let model: DocsModel |
| 21 | @Environment(\.accessibilityReduceTransparency) private var reduceTransparency |
| 22 | @Environment(\.colorScheme) private var colorScheme |
| 23 | @Environment(\.locale) private var locale |
| 24 | |
| 25 | static let sizes = [8, 9, 10, 11, 12, 14, 18, 24, 36, 48, 72] |
| 26 | |
| 27 | private var actions: DocumentCommands { DocumentCommands(model: model, content: model.surface.content) } |
| 28 | /// Runs a command now, or once the input pending when it was clicked |
| 29 | /// has been drawn (`DocsModel.whenCommandsReady`). |
| 30 | private func ready(_ command: @escaping @MainActor (DocumentCommands) -> Void) { |
| 31 | model.whenCommandsReady(command) |
| 32 | } |
| 33 | |
| 34 | var body: some View { |
| 35 | #if DOCS_PROBES |
| 36 | let _ = DocsProbes.inspectorBodies += 1 |
| 37 | #endif |
| 38 | let actions = self.actions |
| 39 | let selection = model.toolbarSelection |
| 40 | VStack(alignment: .leading, spacing: DocsSpacing.sp4) { |
| 41 | section("docs.editor.toolbar.group.style") { |
| 42 | stylePicker(selection) |
| 43 | HStack(spacing: DocsSpacing.sp2) { |
| 44 | fontPicker(selection) |
| 45 | sizePicker(selection).frame(width: 64) |
| 46 | } |
| 47 | } |
| 48 | section("docs.editor.toolbar.group.text") { |
| 49 | DocsSegmentedRow(items: [ |
| 50 | .init(key: "docs.editor.toolbar.bold", icon: "bold", on: selection.marks.contains("strong")) { ready { $0.perform("bold") } }, |
| 51 | .init(key: "docs.editor.toolbar.italic", icon: "italic", on: selection.marks.contains("em")) { ready { $0.perform("italic") } }, |
| 52 | .init(key: "docs.editor.toolbar.code", icon: "chevron.left.forwardslash.chevron.right", on: selection.marks.contains("code")) { ready { $0.perform("code") } }, |
| 53 | ]) |
| 54 | } |
| 55 | section("docs.editor.toolbar.group.align") { |
| 56 | DocsSegmentedRow(items: ["left", "center", "right"].map { value in |
| 57 | .init(key: "docs.editor.toolbar.align." + value, icon: "text.align" + value, on: selection.alignment == value) { |
| 58 | ready { $0.blockAttribute("align", value: value == "left" ? nil : value) } |
| 59 | } |
| 60 | }) |
| 61 | } |
| 62 | section("docs.editor.toolbar.group.lists") { |
| 63 | DocsSegmentedRow(items: [ |
| 64 | .init(key: "docs.editor.toolbar.bulletlist", icon: "list.bullet", on: selection.blocks.contains("bullet_list")) { ready { $0.block("bullet_list") } }, |
| 65 | .init(key: "docs.editor.toolbar.orderedlist", icon: "list.number", on: selection.blocks.contains("ordered_list")) { ready { $0.block("ordered_list") } }, |
| 66 | .init(key: "docs.editor.toolbar.quote", icon: "text.quote", on: selection.blocks.contains("blockquote")) { ready { $0.block("blockquote") } }, |
| 67 | ]) |
| 68 | // Native keeps the mock-up's second row (spec §13 Q2). |
| 69 | DocsSegmentedRow(items: [ |
| 70 | .init(key: "docs.editor.menu.format.outdent", icon: "decrease.indent", on: false, mirrors: true) { ready { $0.perform("outdent") } }, |
| 71 | .init(key: "docs.editor.menu.format.indent", icon: "increase.indent", on: false, mirrors: true) { ready { $0.perform("indent") } }, |
| 72 | .init(key: "docs.editor.menu.format.clear", icon: "eraser", on: false, enabled: selection.hasSelection) { ready { $0.perform("clear-format") } }, |
| 73 | ]) |
| 74 | } |
| 75 | } |
| 76 | .padding(DocsSpacing.sp3) |
| 77 | .frame(width: DocsDeskLayout.inspectorWidth, alignment: .leading) |
| 78 | .background { panel } |
| 79 | .overlay { |
| 80 | // A hairline accent rim: accent mixed into the line colour, 70% in |
| 81 | // light, 30% in dark. Kept under Increase Contrast. |
| 82 | RoundedRectangle(cornerRadius: 12) |
| 83 | .strokeBorder(OficinaPalette.accent(.docs).mix(with: OficinaPalette.line, by: colorScheme == .dark ? 0.7 : 0.3), lineWidth: 1) |
| 84 | } |
| 85 | .compositingGroup() |
| 86 | .shadow(color: .black.opacity(colorScheme == .dark ? 0.4 : 0.14), radius: 16, y: 6) |
| 87 | // Replica work briefly gates commands on every keystroke; keep that |
| 88 | // out of the disabled styling so the panel does not blink (and is |
| 89 | // not evaluated again twice a key, S1b step 0): a click then runs |
| 90 | // once the input is drawn. Held for longer, the panel shows disabled. |
| 91 | .disabled(!actions.isAvailable || model.commandsHeld) |
| 92 | .accessibilityElement(children: .contain) |
| 93 | .accessibilityLabel(Text("docs.editor.inspector.label", bundle: DocsResources.bundle)) |
| 94 | .accessibilityIdentifier("docs.format.inspector") |
| 95 | } |
| 96 | |
| 97 | @ViewBuilder private var panel: some View { |
| 98 | if reduceTransparency { |
| 99 | RoundedRectangle(cornerRadius: 12).fill(OficinaPalette.surface) |
| 100 | } else { |
| 101 | RoundedRectangle(cornerRadius: 12).fill(.clear).glassEffect(.regular, in: RoundedRectangle(cornerRadius: 12)) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | private func section<Content: View>(_ key: String, @ViewBuilder content: () -> Content) -> some View { |
| 106 | VStack(alignment: .leading, spacing: DocsSpacing.sp2) { |
| 107 | Text(LocalizedStringKey(key), bundle: DocsResources.bundle) |
| 108 | .font(.system(size: 11, weight: .semibold)) |
| 109 | .foregroundStyle(OficinaPalette.textMuted) |
| 110 | .fixedSize(horizontal: false, vertical: true) |
| 111 | .accessibilityAddTraits(.isHeader) |
| 112 | content() |
| 113 | } |
| 114 | .accessibilityElement(children: .contain) |
| 115 | .accessibilityLabel(Text(LocalizedStringKey(key), bundle: DocsResources.bundle)) |
| 116 | } |
| 117 | |
| 118 | /// Body text and Heading 1–3, each in its own face. |
| 119 | private func stylePicker(_ selection: DocumentToolbarSelection) -> some View { |
| 120 | let strings = DocsStrings(locale: locale) |
| 121 | let entries = Self.styleEntries(selected: selection.style, strings: strings) |
| 122 | return DocsPopUp(entries: entries, selected: selection.style, label: strings("docs.editor.toolbar.block"), identifier: "docs.style-picker") { style in |
| 123 | if style.hasPrefix("heading"), let level = Int(style.dropFirst("heading".count)) { ready { $0.block("heading", level: level) } } |
| 124 | else { ready { $0.block("paragraph") } } |
| 125 | } |
| 126 | .frame(maxWidth: .infinity) |
| 127 | } |
| 128 | |
| 129 | /// Families in their own faces (spec §1), stretching to the column the |
| 130 | /// size leaves: a pop-up that hugs its widest name ran past the panel. |
| 131 | private func fontPicker(_ selection: DocumentToolbarSelection) -> some View { |
| 132 | let strings = DocsStrings(locale: locale) |
| 133 | return DocsPopUp(entries: DocsFontPicker.entries.map { .init(id: $0.key, title: $0.name, font: $0.font) }, |
| 134 | selected: selection.font, label: strings("native.font"), identifier: "docs.font-picker") { key in ready { $0.setFont(key) } } |
| 135 | .frame(minWidth: 0, maxWidth: .infinity) |
| 136 | } |
| 137 | |
| 138 | /// Body text and Headings 1–3, each in its own face, and the selection's |
| 139 | /// own level when it is in a Heading 4–6. |
| 140 | static func styleEntries(selected: String, strings: DocsStrings) -> [DocsPopUp.Entry] { |
| 141 | var entries = [DocsPopUp.Entry(id: "paragraph", title: strings("docs.editor.toolbar.block.paragraph"), |
| 142 | font: DocsFonts.font(marks: [:], size: 13, heading: false))] |
| 143 | for level in 1...3 { |
| 144 | entries.append(.init(id: "heading" + String(level), title: strings(EditorToolbar.headingKey(level)), |
| 145 | font: DocsFonts.font(marks: [:], size: 13, heading: true))) |
| 146 | } |
| 147 | // Headings 4–6 still render and edit but are not offered (spec §1, |
| 148 | // menus). A selection in one shows its own level, never the style |
| 149 | // the pop-up last showed. |
| 150 | if selected.hasPrefix("heading"), let level = Int(selected.dropFirst("heading".count)), level > 3 { |
| 151 | entries.append(.init(id: selected, title: Self.headingTitle(level, strings: strings), |
| 152 | font: DocsFonts.font(marks: [:], size: 13, heading: true))) |
| 153 | } |
| 154 | return entries |
| 155 | } |
| 156 | |
| 157 | /// "Heading 4" and so on, from the catalog's own format ("Heading %d"). |
| 158 | static func headingTitle(_ level: Int, strings: DocsStrings) -> String { |
| 159 | let format = strings.bundle.localizedString(forKey: "Heading %d", value: nil, table: "Mac") |
| 160 | return String(format: format, level) |
| 161 | } |
| 162 | |
| 163 | private func sizePicker(_ selection: DocumentToolbarSelection) -> some View { |
| 164 | let strings = DocsStrings(locale: locale) |
| 165 | return DocsPopUp(entries: Self.sizes.map { .init(id: String($0), title: $0.formatted(.number.locale(locale)), font: nil) }, |
| 166 | selected: String(selection.size), label: strings("docs.editor.toolbar.size"), identifier: "docs.size-picker") { size in |
| 167 | ready { $0.format("size_" + size, exclusive: Self.sizes.map { "size_" + String($0) } + ["size_sm", "size_lg", "size_xl"]) } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /// A Mac pop-up button over fixed entries, each title in its own face when |
| 173 | /// it has one, as `DocsFontPopUp` draws the families. It stretches to the |
| 174 | /// width it is given. |
| 175 | struct DocsPopUp: NSViewRepresentable { |
| 176 | struct Entry { let id: String; let title: String; let font: NSFont? } |
| 177 | let entries: [Entry] |
| 178 | let selected: String |
| 179 | let label: String |
| 180 | let identifier: String |
| 181 | let choose: (String) -> Void |
| 182 | func makeCoordinator() -> Coordinator { Coordinator() } |
| 183 | func makeNSView(context: Context) -> NSPopUpButton { |
| 184 | let button = NSPopUpButton(frame: .zero, pullsDown: false) |
| 185 | button.target = context.coordinator |
| 186 | button.action = #selector(Coordinator.picked(_:)) |
| 187 | button.setContentHuggingPriority(.defaultLow, for: .horizontal) |
| 188 | button.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) |
| 189 | button.setAccessibilityIdentifier(identifier) |
| 190 | return button |
| 191 | } |
| 192 | func updateNSView(_ button: NSPopUpButton, context: Context) { |
| 193 | context.coordinator.choose = choose |
| 194 | button.isEnabled = context.environment.isEnabled |
| 195 | button.setAccessibilityLabel(label) |
| 196 | let titles = entries.map(\.title) |
| 197 | if button.itemArray.map(\.title) != titles { |
| 198 | let menu = NSMenu() |
| 199 | for entry in entries { |
| 200 | let item = NSMenuItem(title: entry.title, action: nil, keyEquivalent: "") |
| 201 | item.representedObject = entry.id |
| 202 | if let font = entry.font { item.attributedTitle = NSAttributedString(string: entry.title, attributes: [.font: font]) } |
| 203 | menu.addItem(item) |
| 204 | } |
| 205 | button.menu = menu |
| 206 | } |
| 207 | if let index = entries.firstIndex(where: { $0.id == selected }), button.indexOfSelectedItem != index { button.selectItem(at: index) } |
| 208 | } |
| 209 | @MainActor final class Coordinator: NSObject { |
| 210 | var choose: (String) -> Void = { _ in } |
| 211 | @objc func picked(_ sender: NSPopUpButton) { |
| 212 | if let id = sender.selectedItem?.representedObject as? String { choose(id) } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /// Joined 28pt buttons in one well; a lit one is accent-soft with an accent |
| 218 | /// glyph, and says so to VoiceOver (`.isSelected`), never by colour alone. |
| 219 | struct DocsSegmentedRow: View { |
| 220 | struct Item: Identifiable { |
| 221 | let key: String |
| 222 | let icon: String |
| 223 | let on: Bool |
| 224 | var enabled = true |
| 225 | /// Indent and outdent point along the line: they mirror right to left. |
| 226 | var mirrors = false |
| 227 | let action: () -> Void |
| 228 | var id: String { key } |
| 229 | } |
| 230 | let items: [Item] |
| 231 | var body: some View { |
| 232 | let accent = OficinaPalette.accent(.docs) |
| 233 | // spacing-allow: the joined buttons share one well, 2pt apart, as the |
| 234 | // web inspector's segmented rows. |
| 235 | HStack(spacing: 2) { |
| 236 | ForEach(items) { item in |
| 237 | Button(action: item.action) { |
| 238 | Image(systemName: item.icon) |
| 239 | .font(.system(size: 13, weight: .medium)) |
| 240 | .flipsForRightToLeftLayoutDirection(item.mirrors) |
| 241 | .frame(maxWidth: .infinity, minHeight: 28) // spec §1: joined 28pt buttons |
| 242 | .contentShape(Rectangle()) |
| 243 | } |
| 244 | .buttonStyle(.plain) |
| 245 | .foregroundStyle(item.on ? accent : OficinaPalette.text) |
| 246 | .background(RoundedRectangle(cornerRadius: 6).fill(item.on ? OficinaPalette.accentSoft(.docs) : .clear)) |
| 247 | .overlay(RoundedRectangle(cornerRadius: 6).strokeBorder(item.on ? accent.opacity(0.55) : .clear, lineWidth: 1)) |
| 248 | .disabled(!item.enabled) |
| 249 | .opacity(item.enabled ? 1 : 0.4) |
| 250 | .help(Text(LocalizedStringKey(item.key), bundle: DocsResources.bundle)) |
| 251 | .accessibilityLabel(Text(LocalizedStringKey(item.key), bundle: DocsResources.bundle)) |
| 252 | .accessibilityAddTraits(item.on ? .isSelected : []) |
| 253 | } |
| 254 | } |
| 255 | .padding(2) // spacing-allow: the well's inset around its joined buttons |
| 256 | .frame(height: 32) // the buttons' 28pt and the well's 2pt inset each side |
| 257 | .background(RoundedRectangle(cornerRadius: 8).fill(OficinaPalette.sunk)) |
| 258 | } |
| 259 | } |
| 260 | #endif |
| 261 | |