| 1 | #if os(iOS) |
| 2 | import RastrilloApple |
| 3 | import SwiftUI |
| 4 | import UIKit |
| 5 | |
| 6 | /// What the iPhone document's text view needs from its window, created once |
| 7 | /// per document window and compared by identity: the header's state, the |
| 8 | /// keyboard bar's model, and how Aa opens the Format sheet. |
| 9 | @MainActor final class DocsPhoneEditorChrome { |
| 10 | let model: DocsModel |
| 11 | let team: String? |
| 12 | let header = DocumentHeaderState() |
| 13 | /// Aa, from the keyboard bar. |
| 14 | var openFormat: () -> Void = {} |
| 15 | init(model: DocsModel, team: String?) { self.model = model; self.team = team } |
| 16 | |
| 17 | /// The keyboard bar: a `UIInputView` hosting the SwiftUI capsule (spec |
| 18 | /// §3, "Keyboard bar"), never `ToolbarItemGroup(placement: .keyboard)`, |
| 19 | /// which does not attach reliably to a wrapped UITextView. |
| 20 | func makeKeyboardBar(hide: @escaping () -> Void) -> UIView { |
| 21 | let input = UIInputView(frame: CGRect(x: 0, y: 0, width: 320, height: 60), inputViewStyle: .keyboard) |
| 22 | input.allowsSelfSizing = true |
| 23 | let host = UIHostingController(rootView: DocsKeyboardBar(model: model, openFormat: { [weak self] in self?.openFormat() }, hide: hide)) |
| 24 | host.view.backgroundColor = .clear |
| 25 | host.view.translatesAutoresizingMaskIntoConstraints = false |
| 26 | host.sizingOptions = [.intrinsicContentSize] |
| 27 | // With a hardware keyboard the bar sits on the bottom edge, over the |
| 28 | // home indicator: the screen's inset is not the bar's to pad. |
| 29 | host.safeAreaRegions = [] |
| 30 | input.addSubview(host.view) |
| 31 | NSLayoutConstraint.activate([ |
| 32 | host.view.leadingAnchor.constraint(equalTo: input.leadingAnchor), |
| 33 | host.view.trailingAnchor.constraint(equalTo: input.trailingAnchor), |
| 34 | host.view.topAnchor.constraint(equalTo: input.topAnchor), |
| 35 | host.view.bottomAnchor.constraint(equalTo: input.bottomAnchor), |
| 36 | ]) |
| 37 | objc_setAssociatedObject(input, &DocsPhoneEditorChrome.hostKey, host, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) |
| 38 | return input |
| 39 | } |
| 40 | /// The bar's hosting controller (tests). |
| 41 | static func host(of bar: UIView) -> UIHostingController<DocsKeyboardBar>? { |
| 42 | objc_getAssociatedObject(bar, &hostKey) as? UIHostingController<DocsKeyboardBar> |
| 43 | } |
| 44 | nonisolated(unsafe) private static var hostKey = 0 |
| 45 | } |
| 46 | |
| 47 | /// The quick format bar above the keyboard, a writer's: Aa, Bold, Italic, |
| 48 | /// Bulleted list, Numbered list, Quote, then Hide keyboard. Marks and lists |
| 49 | /// show `.isSelected` from `DocumentToolbarSelection`; targets are 44pt, and |
| 50 | /// at accessibility sizes the capsule scrolls rather than shrinking. |
| 51 | @MainActor struct DocsKeyboardBar: View { |
| 52 | let model: DocsModel |
| 53 | let openFormat: () -> Void |
| 54 | let hide: () -> Void |
| 55 | var body: some View { |
| 56 | let selection = model.toolbarSelection |
| 57 | ScrollView(.horizontal) { |
| 58 | HStack(spacing: 0) { |
| 59 | button("docs.editor.inspector.toggle", identifier: "docs.keyboard.format", selected: false) { |
| 60 | Text(verbatim: "Aa").font(.body.weight(.semibold)) |
| 61 | } action: { openFormat() } |
| 62 | symbol("docs.editor.toolbar.bold", "bold", identifier: "docs.keyboard.bold", selected: selection.marks.contains("strong")) { ready { $0.perform("bold") } } |
| 63 | symbol("docs.editor.toolbar.italic", "italic", identifier: "docs.keyboard.italic", selected: selection.marks.contains("em")) { ready { $0.perform("italic") } } |
| 64 | symbol("docs.editor.toolbar.bulletlist", "list.bullet", identifier: "docs.keyboard.bullets", selected: selection.blocks.contains("bullet_list")) { ready { $0.block("bullet_list") } } |
| 65 | symbol("docs.editor.toolbar.orderedlist", "list.number", identifier: "docs.keyboard.numbers", selected: selection.blocks.contains("ordered_list")) { ready { $0.block("ordered_list") } } |
| 66 | symbol("docs.editor.toolbar.quote", "text.quote", identifier: "docs.keyboard.quote", selected: selection.blocks.contains("blockquote")) { ready { $0.block("blockquote") } } |
| 67 | Rectangle().fill(OficinaPalette.line).frame(width: 1, height: 24).padding(.horizontal, DocsSpacing.sp1) |
| 68 | symbol("native.keyboard.hide", "keyboard.chevron.compact.down", identifier: "docs.keyboard.hide", selected: false) { hide() } |
| 69 | } |
| 70 | .padding(.horizontal, DocsSpacing.sp1) |
| 71 | .background(Capsule().fill(OficinaPalette.surface).shadow(color: .black.opacity(0.12), radius: 8, y: 2)) |
| 72 | .padding(.horizontal, DocsSpacing.sp2) |
| 73 | .padding(.vertical, DocsSpacing.sp1) |
| 74 | } |
| 75 | .scrollIndicators(.hidden) |
| 76 | .scrollBounceBehavior(.basedOnSize, axes: .horizontal) |
| 77 | .disabled(model.commandsHeld) |
| 78 | .accessibilityElement(children: .contain) |
| 79 | .accessibilityIdentifier("docs.keyboard.bar") |
| 80 | } |
| 81 | |
| 82 | private func ready(_ command: @escaping @MainActor (DocumentCommands) -> Void) { model.whenCommandsReady(command) } |
| 83 | |
| 84 | private func symbol(_ key: String, _ name: String, identifier: String, selected: Bool, action: @escaping () -> Void) -> some View { |
| 85 | button(key, identifier: identifier, selected: selected) { |
| 86 | Image(systemName: name).font(.body.weight(.medium)) |
| 87 | } action: { action() } |
| 88 | } |
| 89 | |
| 90 | private func button<Label: View>(_ key: String, identifier: String, selected: Bool, @ViewBuilder label: () -> Label, action: @escaping () -> Void) -> some View { |
| 91 | Button(action: action) { |
| 92 | label() |
| 93 | .frame(minWidth: 44, minHeight: 44) |
| 94 | .foregroundStyle(selected ? OficinaPalette.accent(.docs) : OficinaPalette.text) |
| 95 | .background(Circle().fill(selected ? OficinaPalette.accentSoft(.docs) : .clear).frame(width: 38, height: 38)) |
| 96 | .contentShape(Rectangle()) |
| 97 | } |
| 98 | .buttonStyle(.plain) |
| 99 | .accessibilityLabel(Text(LocalizedStringKey(key), bundle: DocsResources.bundle)) |
| 100 | .accessibilityAddTraits(selected ? .isSelected : []) |
| 101 | .accessibilityIdentifier(identifier) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /// The Format sheet (Paul's ruling 3; spec §3, "Format sheet"): the web |
| 106 | /// inspector's sections in one screen at the default size, at the `.medium` |
| 107 | /// detent with the text still live above it. Every control applies to the |
| 108 | /// current selection and the sheet stays up. |
| 109 | @MainActor struct DocsFormatSheet: View { |
| 110 | let model: DocsModel |
| 111 | let close: () -> Void |
| 112 | @Environment(\.dynamicTypeSize) private var typeSize |
| 113 | @Environment(\.locale) private var locale |
| 114 | static let sizes = [8, 9, 10, 11, 12, 14, 18, 24, 36, 48, 72] |
| 115 | |
| 116 | var body: some View { |
| 117 | let selection = model.toolbarSelection |
| 118 | let twoRows = typeSize >= .accessibility1 |
| 119 | ScrollView { |
| 120 | VStack(alignment: .leading, spacing: DocsSpacing.sp4) { |
| 121 | HStack { |
| 122 | Text("docs.editor.inspector.label", bundle: DocsResources.bundle) |
| 123 | .font(.title3.bold()) |
| 124 | .accessibilityAddTraits(.isHeader) |
| 125 | Spacer() |
| 126 | Button(action: close) { |
| 127 | Image(systemName: "xmark").font(.body.weight(.semibold)) |
| 128 | .frame(width: 44, height: 44) |
| 129 | .background(Circle().fill(OficinaPalette.sunk).frame(width: 32, height: 32)) |
| 130 | } |
| 131 | .buttonStyle(.plain) |
| 132 | .accessibilityLabel(Text("native.close", bundle: DocsResources.bundle)) |
| 133 | .accessibilityIdentifier("docs.format.close") |
| 134 | } |
| 135 | section("docs.editor.toolbar.group.style") { styles(selection) } |
| 136 | section("docs.editor.toolbar.font") { fontAndSize(selection, twoRows: twoRows) } |
| 137 | section("docs.editor.toolbar.group.text") { |
| 138 | rows(twoRows, [ |
| 139 | DocsSheetSegment(key: "docs.editor.toolbar.bold", icon: "bold", on: selection.marks.contains("strong"), identifier: "docs.format.bold") { ready { $0.perform("bold") } }, |
| 140 | DocsSheetSegment(key: "docs.editor.toolbar.italic", icon: "italic", on: selection.marks.contains("em"), identifier: "docs.format.italic") { ready { $0.perform("italic") } }, |
| 141 | DocsSheetSegment(key: "docs.editor.toolbar.code", icon: "chevron.left.forwardslash.chevron.right", on: selection.marks.contains("code"), identifier: "docs.format.code") { ready { $0.perform("code") } }, |
| 142 | ], ["left", "center", "right"].map { value in |
| 143 | DocsSheetSegment(key: "docs.editor.toolbar.align." + value, icon: "text.align" + value, on: selection.alignment == value, identifier: "docs.format.align." + value) { |
| 144 | ready { $0.blockAttribute("align", value: value == "left" ? nil : value) } |
| 145 | } |
| 146 | }) |
| 147 | } |
| 148 | section("docs.editor.toolbar.group.lists") { |
| 149 | rows(twoRows, [ |
| 150 | DocsSheetSegment(key: "docs.editor.toolbar.bulletlist", icon: "list.bullet", on: selection.blocks.contains("bullet_list"), identifier: "docs.format.bullets") { ready { $0.block("bullet_list") } }, |
| 151 | DocsSheetSegment(key: "docs.editor.toolbar.orderedlist", icon: "list.number", on: selection.blocks.contains("ordered_list"), identifier: "docs.format.numbers") { ready { $0.block("ordered_list") } }, |
| 152 | DocsSheetSegment(key: "docs.editor.toolbar.quote", icon: "text.quote", on: selection.blocks.contains("blockquote"), identifier: "docs.format.quote") { ready { $0.block("blockquote") } }, |
| 153 | ], [ |
| 154 | DocsSheetSegment(key: "docs.editor.menu.format.outdent", icon: "decrease.indent", on: false, mirrors: true, identifier: "docs.format.outdent") { ready { $0.perform("outdent") } }, |
| 155 | DocsSheetSegment(key: "docs.editor.menu.format.indent", icon: "increase.indent", on: false, mirrors: true, identifier: "docs.format.indent") { ready { $0.perform("indent") } }, |
| 156 | DocsSheetSegment(key: "docs.editor.menu.format.clear", icon: "eraser", on: false, enabled: selection.hasSelection, identifier: "docs.format.clear") { ready { $0.perform("clear-format") } }, |
| 157 | ]) |
| 158 | } |
| 159 | } |
| 160 | .padding(.horizontal, DocsSpacing.sp5) |
| 161 | .padding(.top, DocsSpacing.sp4) |
| 162 | .padding(.bottom, DocsSpacing.sp5) |
| 163 | } |
| 164 | .scrollBounceBehavior(.basedOnSize) |
| 165 | .disabled(model.commandsHeld) |
| 166 | .accessibilityElement(children: .contain) |
| 167 | .accessibilityLabel(Text("docs.editor.inspector.label", bundle: DocsResources.bundle)) |
| 168 | .accessibilityIdentifier("docs.format.sheet") |
| 169 | } |
| 170 | |
| 171 | private func ready(_ command: @escaping @MainActor (DocumentCommands) -> Void) { model.whenCommandsReady(command) } |
| 172 | |
| 173 | private func section<Content: View>(_ key: String, @ViewBuilder content: () -> Content) -> some View { |
| 174 | VStack(alignment: .leading, spacing: DocsSpacing.sp2) { |
| 175 | Text(LocalizedStringKey(key), bundle: DocsResources.bundle) |
| 176 | .font(.footnote.weight(.semibold)) |
| 177 | .foregroundStyle(OficinaPalette.textMuted) |
| 178 | .accessibilityAddTraits(.isHeader) |
| 179 | content() |
| 180 | } |
| 181 | .accessibilityElement(children: .contain) |
| 182 | .accessibilityLabel(Text(LocalizedStringKey(key), bundle: DocsResources.bundle)) |
| 183 | } |
| 184 | |
| 185 | /// Body text and Heading 1–3 as chips, each in its own face; the current |
| 186 | /// one outlined in the accent. They wrap onto more rows when they do not |
| 187 | /// fit (spec §9: the sheet re-flows, nothing is clipped). |
| 188 | private func styles(_ selection: DocumentToolbarSelection) -> some View { |
| 189 | let strings = DocsStrings(locale: locale) |
| 190 | let entries: [(id: String, key: String, size: CGFloat, heading: Bool)] = [ |
| 191 | ("paragraph", "docs.editor.toolbar.block.paragraph", 17, false), |
| 192 | ("heading1", EditorToolbar.headingKey(1), 24, true), |
| 193 | ("heading2", EditorToolbar.headingKey(2), 20, true), |
| 194 | ("heading3", EditorToolbar.headingKey(3), 17, true), |
| 195 | ] |
| 196 | return DocsFlowLayout(spacing: DocsSpacing.sp2) { |
| 197 | ForEach(entries, id: \.id) { entry in |
| 198 | let on = selection.style == entry.id |
| 199 | Button { |
| 200 | if entry.heading, let level = Int(entry.id.dropFirst("heading".count)) { ready { $0.block("heading", level: level) } } |
| 201 | else { ready { $0.block("paragraph") } } |
| 202 | } label: { |
| 203 | Text(verbatim: strings(entry.key)) |
| 204 | // The style's own face, scaling with Dynamic |
| 205 | // Type from its size (spec §9). |
| 206 | .font(.custom(DocsFonts.font(marks: [:], size: entry.size, heading: entry.heading).fontName, |
| 207 | size: entry.size, relativeTo: .body)) |
| 208 | // Whole at every size: a chip wider than the |
| 209 | // row wraps its words. |
| 210 | .fixedSize(horizontal: false, vertical: true) |
| 211 | .padding(.horizontal, DocsSpacing.sp4) |
| 212 | .frame(minHeight: 52) |
| 213 | .foregroundStyle(on ? OficinaPalette.accent(.docs) : OficinaPalette.text) |
| 214 | .background(RoundedRectangle(cornerRadius: 14).fill(on ? OficinaPalette.accentSoft(.docs) : OficinaPalette.sunk)) |
| 215 | .overlay(RoundedRectangle(cornerRadius: 14).strokeBorder(on ? OficinaPalette.accent(.docs) : .clear, lineWidth: 1.5)) |
| 216 | } |
| 217 | .buttonStyle(.plain) |
| 218 | .accessibilityAddTraits(on ? .isSelected : []) |
| 219 | .accessibilityIdentifier("docs.format.style." + entry.id) |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /// The font picker (families in their own faces), then the size: − the |
| 225 | /// value + over the eleven sizes. |
| 226 | @ViewBuilder private func fontAndSize(_ selection: DocumentToolbarSelection, twoRows: Bool) -> some View { |
| 227 | let layout = twoRows ? AnyLayout(VStackLayout(alignment: .leading, spacing: DocsSpacing.sp2)) : AnyLayout(HStackLayout(spacing: DocsSpacing.sp2)) |
| 228 | layout { |
| 229 | DocsFontPickerButton(selected: selection.font) { key in ready { $0.setFont(key) } } |
| 230 | .padding(.horizontal, DocsSpacing.sp3) |
| 231 | .frame(maxWidth: .infinity, minHeight: 44, alignment: .leading) |
| 232 | .background(RoundedRectangle(cornerRadius: 12).strokeBorder(OficinaPalette.line, lineWidth: 1)) |
| 233 | stepper(selection.size) |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | private func stepper(_ size: Int) -> some View { |
| 238 | let sizes = Self.sizes |
| 239 | let index = sizes.firstIndex(of: size) ?? sizes.firstIndex { $0 > size }.map { max(0, $0 - 1) } ?? sizes.count - 1 |
| 240 | func set(_ value: Int) { |
| 241 | ready { $0.format("size_" + String(value), exclusive: sizes.map { "size_" + String($0) } + ["size_sm", "size_lg", "size_xl"]) } |
| 242 | } |
| 243 | return HStack(spacing: 0) { |
| 244 | Button { if index > 0 { set(sizes[index - 1]) } } label: { |
| 245 | Image(systemName: "minus").frame(width: 44, height: 44).contentShape(Rectangle()) |
| 246 | } |
| 247 | .disabled(index == 0) |
| 248 | .accessibilityLabel(Text("native.format.size.smaller", bundle: DocsResources.bundle)) |
| 249 | .accessibilityIdentifier("docs.format.size.smaller") |
| 250 | Text(size.formatted(.number.locale(locale))) |
| 251 | .font(.body.monospacedDigit()) |
| 252 | .frame(minWidth: 36) |
| 253 | .accessibilityLabel(Text("docs.editor.toolbar.size", bundle: DocsResources.bundle)) |
| 254 | .accessibilityValue(Text(size.formatted(.number.locale(locale)))) |
| 255 | Button { if index + 1 < sizes.count { set(sizes[index + 1]) } } label: { |
| 256 | Image(systemName: "plus").frame(width: 44, height: 44).contentShape(Rectangle()) |
| 257 | } |
| 258 | .disabled(index + 1 >= sizes.count) |
| 259 | .accessibilityLabel(Text("native.format.size.larger", bundle: DocsResources.bundle)) |
| 260 | .accessibilityIdentifier("docs.format.size.larger") |
| 261 | } |
| 262 | .buttonStyle(.plain) |
| 263 | .foregroundStyle(OficinaPalette.text) |
| 264 | .background(RoundedRectangle(cornerRadius: 12).fill(OficinaPalette.sunk)) |
| 265 | .fixedSize() |
| 266 | } |
| 267 | |
| 268 | /// Two groups on one row, or one each from AX1 (spec §9). |
| 269 | private func rows(_ twoRows: Bool, _ first: [DocsSheetSegment], _ second: [DocsSheetSegment]) -> some View { |
| 270 | let layout = twoRows ? AnyLayout(VStackLayout(spacing: DocsSpacing.sp2)) : AnyLayout(HStackLayout(spacing: DocsSpacing.sp2)) |
| 271 | return layout { |
| 272 | DocsSheetSegmentRow(items: first) |
| 273 | DocsSheetSegmentRow(items: second) |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /// One button of a joined row in the Format sheet. |
| 279 | struct DocsSheetSegment { |
| 280 | let key: String |
| 281 | let icon: String |
| 282 | let on: Bool |
| 283 | var mirrors = false |
| 284 | var enabled = true |
| 285 | let identifier: String |
| 286 | let action: @MainActor () -> Void |
| 287 | } |
| 288 | |
| 289 | /// A joined row of 44pt buttons: Bold, Italic, Code and the like. |
| 290 | @MainActor struct DocsSheetSegmentRow: View { |
| 291 | let items: [DocsSheetSegment] |
| 292 | var body: some View { |
| 293 | HStack(spacing: 2) { |
| 294 | ForEach(items, id: \.identifier) { item in |
| 295 | Button { item.action() } label: { |
| 296 | Image(systemName: item.icon) |
| 297 | .font(.body.weight(.medium)) |
| 298 | .flipsForRightToLeftLayoutDirection(item.mirrors) |
| 299 | .frame(maxWidth: .infinity, minHeight: 44) |
| 300 | .foregroundStyle(item.on ? OficinaPalette.accent(.docs) : OficinaPalette.text) |
| 301 | .background(RoundedRectangle(cornerRadius: 10).fill(item.on ? OficinaPalette.accentSoft(.docs) : .clear)) |
| 302 | .overlay(RoundedRectangle(cornerRadius: 10).strokeBorder(item.on ? OficinaPalette.accent(.docs) : .clear, lineWidth: 1.5)) |
| 303 | .contentShape(Rectangle()) |
| 304 | } |
| 305 | .buttonStyle(.plain) |
| 306 | .disabled(!item.enabled) |
| 307 | .opacity(item.enabled ? 1 : 0.4) |
| 308 | .accessibilityLabel(Text(LocalizedStringKey(item.key), bundle: DocsResources.bundle)) |
| 309 | .accessibilityAddTraits(item.on ? .isSelected : []) |
| 310 | .accessibilityIdentifier(item.identifier) |
| 311 | } |
| 312 | } |
| 313 | .padding(2) |
| 314 | .background(RoundedRectangle(cornerRadius: 12).fill(OficinaPalette.sunk)) |
| 315 | } |
| 316 | } |
| 317 | /// Its subviews left to right (leading to trailing), onto a new row when |
| 318 | /// the next would not fit; one wider than a row is offered the row's width. |
| 319 | struct DocsFlowLayout: Layout { |
| 320 | var spacing: CGFloat |
| 321 | private func rows(_ subviews: Subviews, width: CGFloat) -> [[(index: Int, size: CGSize)]] { |
| 322 | var rows: [[(index: Int, size: CGSize)]] = [[]] |
| 323 | var x: CGFloat = 0 |
| 324 | for index in subviews.indices { |
| 325 | var size = subviews[index].sizeThatFits(.unspecified) |
| 326 | if size.width > width { size = subviews[index].sizeThatFits(ProposedViewSize(width: width, height: nil)) } |
| 327 | if !rows[rows.count - 1].isEmpty, x + spacing + size.width > width { rows.append([]); x = 0 } |
| 328 | x += (rows[rows.count - 1].isEmpty ? 0 : spacing) + size.width |
| 329 | rows[rows.count - 1].append((index, size)) |
| 330 | } |
| 331 | return rows |
| 332 | } |
| 333 | func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize { |
| 334 | let rows = rows(subviews, width: proposal.width ?? .infinity) |
| 335 | let widths = rows.map { row in row.reduce(0) { $0 + $1.size.width } + spacing * CGFloat(max(row.count - 1, 0)) } |
| 336 | let heights = rows.map { row in row.map(\.size.height).max() ?? 0 } |
| 337 | return CGSize(width: proposal.width ?? widths.max() ?? 0, |
| 338 | height: heights.reduce(0, +) + spacing * CGFloat(max(rows.count - 1, 0))) |
| 339 | } |
| 340 | func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) { |
| 341 | var y = bounds.minY |
| 342 | for row in rows(subviews, width: bounds.width) { |
| 343 | var x = bounds.minX |
| 344 | for item in row { |
| 345 | subviews[item.index].place(at: CGPoint(x: x, y: y), proposal: ProposedViewSize(item.size)) |
| 346 | x += item.size.width + spacing |
| 347 | } |
| 348 | y += (row.map(\.size.height).max() ?? 0) + spacing |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | #endif |
| 353 | |