| 1 | #if os(iOS) |
| 2 | import RastrilloApple |
| 3 | import SwiftUI |
| 4 | import UIKit |
| 5 | |
| 6 | /// The iPhone document's large title (spec 2026-11-22 §3, "Header and |
| 7 | /// scrolling"): the title, bold, at most three lines, with the §6 subtitle |
| 8 | /// under it. It is hosted inside the text view's own scroll view, above the |
| 9 | /// text (`DocsPagedTextView.headerHeight` is its measured height), so it |
| 10 | /// scrolls with the text and there is no outer scroll view. |
| 11 | /// |
| 12 | /// Its state is this small object, separate from the text view's host: the |
| 13 | /// text view reports scrolling and editing here by callback, the workspace |
| 14 | /// the Format sheet, and only the header and the bar's inline title read it. |
| 15 | @MainActor @Observable final class DocumentHeaderState { |
| 16 | /// Scrolled past the large title. |
| 17 | var scrolledPast = false { didSet { if oldValue != scrolledPast { update() } } } |
| 18 | /// The text view has the keyboard (typing). |
| 19 | var editing = false { didSet { if oldValue != editing { update() } } } |
| 20 | /// The Format sheet is open. |
| 21 | var sheetOpen = false { didSet { if oldValue != sheetOpen { update() } } } |
| 22 | /// The title folds into the bar: on scroll, on the keyboard, on the |
| 23 | /// sheet; it unfolds only at the top with the keyboard down (§3). |
| 24 | private(set) var collapsed = false |
| 25 | private func update() { |
| 26 | let next = scrolledPast || editing || sheetOpen |
| 27 | if next != collapsed { collapsed = next } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | /// The large title and its subtitle, as the text view hosts them. |
| 32 | @MainActor struct DocsPhoneLargeTitle: View { |
| 33 | let model: DocsModel |
| 34 | let team: String? |
| 35 | let state: DocumentHeaderState |
| 36 | @Environment(\.accessibilityReduceMotion) private var reduceMotion |
| 37 | var body: some View { |
| 38 | VStack(alignment: .leading, spacing: DocsSpacing.sp1) { |
| 39 | Text(verbatim: model.detail?.title ?? " ") |
| 40 | .font(.title2.bold()) |
| 41 | .foregroundStyle(OficinaPalette.text) |
| 42 | .lineLimit(3) |
| 43 | .fixedSize(horizontal: false, vertical: true) |
| 44 | .accessibilityAddTraits(.isHeader) |
| 45 | .accessibilityIdentifier("docs.document.title") |
| 46 | DocsSubtitleView(model: model, team: team, announces: !state.collapsed) |
| 47 | } |
| 48 | // Its own height whatever frame it is in, so a larger Dynamic Type |
| 49 | // size never squeezes the subtitle while the bar measures it again. |
| 50 | .fixedSize(horizontal: false, vertical: true) |
| 51 | .frame(maxWidth: .infinity, alignment: .leading) |
| 52 | .padding(.horizontal, DocsSpacing.sp5) |
| 53 | .padding(.top, DocsSpacing.sp2) |
| 54 | .padding(.bottom, DocsSpacing.sp4) |
| 55 | // Folded into the bar: it fades (and under Reduce Motion too, with |
| 56 | // no movement: the title in the bar takes its place). |
| 57 | .opacity(state.collapsed ? 0 : 1) |
| 58 | .animation(reduceMotion ? .linear(duration: 0.12) : .easeOut(duration: 0.2), value: state.collapsed) |
| 59 | .accessibilityHidden(state.collapsed) |
| 60 | .accessibilityElement(children: .contain) |
| 61 | .accessibilityIdentifier("docs.document.header") |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /// The bar's two-line title while the large one is folded away: the title, |
| 66 | /// truncated, and the subtitle under it. |
| 67 | @MainActor struct DocsPhoneInlineTitle: View { |
| 68 | let model: DocsModel |
| 69 | let team: String? |
| 70 | let state: DocumentHeaderState |
| 71 | @Environment(\.accessibilityReduceMotion) private var reduceMotion |
| 72 | var body: some View { |
| 73 | // Present only while folded, so the bar holds nothing (and |
| 74 | // VoiceOver reads nothing) while the large title shows. |
| 75 | ZStack { |
| 76 | if state.collapsed { |
| 77 | VStack(spacing: 0) { |
| 78 | Text(verbatim: model.detail?.title ?? "") |
| 79 | .font(.headline) |
| 80 | .lineLimit(1) |
| 81 | .truncationMode(.tail) |
| 82 | DocsSubtitleView(model: model, team: team) |
| 83 | } |
| 84 | .transition(.opacity) |
| 85 | .accessibilityElement(children: .combine) |
| 86 | // The bar caps its type at the largest sizes, as every bar |
| 87 | // does: a long press shows it large (spec §9). |
| 88 | .accessibilityShowsLargeContentViewer() |
| 89 | .accessibilityIdentifier("docs.document.inlineTitle") |
| 90 | } |
| 91 | } |
| 92 | .animation(reduceMotion ? .linear(duration: 0.12) : .easeOut(duration: 0.2), value: state.collapsed) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /// What the text view hosts above its text: the large title's hosting |
| 97 | /// controller, sized by the text view, reporting its height as it changes. |
| 98 | @MainActor final class DocsPhoneHeaderHost { |
| 99 | let controller: UIHostingController<AnyView> |
| 100 | let state: DocumentHeaderState |
| 101 | /// The height the title measured, for the text view's top inset. |
| 102 | var heightChanged: ((CGFloat) -> Void)? |
| 103 | init(model: DocsModel, team: String?, state: DocumentHeaderState) { |
| 104 | self.state = state |
| 105 | var report: ((CGFloat) -> Void)? |
| 106 | controller = UIHostingController(rootView: AnyView(EmptyView())) |
| 107 | controller.rootView = AnyView(DocsPhoneLargeTitle(model: model, team: team, state: state) |
| 108 | .onGeometryChange(for: CGFloat.self) { $0.size.height } action: { report?($0) }) |
| 109 | controller.view.backgroundColor = .clear |
| 110 | controller.sizingOptions = [] |
| 111 | // It scrolls with the text, under the bar: the text view's frame is |
| 112 | // its layout, not the screen's safe area. |
| 113 | controller.safeAreaRegions = [] |
| 114 | report = { [weak self] height in self?.heightChanged?(height) } |
| 115 | } |
| 116 | var view: UIView { controller.view } |
| 117 | func height(forWidth width: CGFloat) -> CGFloat { |
| 118 | controller.sizeThatFits(in: CGSize(width: width, height: .greatestFiniteMagnitude)).height |
| 119 | } |
| 120 | } |
| 121 | #endif |
| 122 | |