#if os(iOS) import RastrilloApple import SwiftUI import UIKit /// The iPhone document's large title (spec 2026-11-22 §3, "Header and /// scrolling"): the title, bold, at most three lines, with the §6 subtitle /// under it. It is hosted inside the text view's own scroll view, above the /// text (`DocsPagedTextView.headerHeight` is its measured height), so it /// scrolls with the text and there is no outer scroll view. /// /// Its state is this small object, separate from the text view's host: the /// text view reports scrolling and editing here by callback, the workspace /// the Format sheet, and only the header and the bar's inline title read it. @MainActor @Observable final class DocumentHeaderState { /// Scrolled past the large title. var scrolledPast = false { didSet { if oldValue != scrolledPast { update() } } } /// The text view has the keyboard (typing). var editing = false { didSet { if oldValue != editing { update() } } } /// The Format sheet is open. var sheetOpen = false { didSet { if oldValue != sheetOpen { update() } } } /// The title folds into the bar: on scroll, on the keyboard, on the /// sheet; it unfolds only at the top with the keyboard down (§3). private(set) var collapsed = false private func update() { let next = scrolledPast || editing || sheetOpen if next != collapsed { collapsed = next } } } /// The large title and its subtitle, as the text view hosts them. @MainActor struct DocsPhoneLargeTitle: View { let model: DocsModel let team: String? let state: DocumentHeaderState @Environment(\.accessibilityReduceMotion) private var reduceMotion var body: some View { VStack(alignment: .leading, spacing: DocsSpacing.sp1) { Text(verbatim: model.detail?.title ?? " ") .font(.title2.bold()) .foregroundStyle(OficinaPalette.text) .lineLimit(3) .fixedSize(horizontal: false, vertical: true) .accessibilityAddTraits(.isHeader) .accessibilityIdentifier("docs.document.title") DocsSubtitleView(model: model, team: team, announces: !state.collapsed) } // Its own height whatever frame it is in, so a larger Dynamic Type // size never squeezes the subtitle while the bar measures it again. .fixedSize(horizontal: false, vertical: true) .frame(maxWidth: .infinity, alignment: .leading) .padding(.horizontal, DocsSpacing.sp5) .padding(.top, DocsSpacing.sp2) .padding(.bottom, DocsSpacing.sp4) // Folded into the bar: it fades (and under Reduce Motion too, with // no movement: the title in the bar takes its place). .opacity(state.collapsed ? 0 : 1) .animation(reduceMotion ? .linear(duration: 0.12) : .easeOut(duration: 0.2), value: state.collapsed) .accessibilityHidden(state.collapsed) .accessibilityElement(children: .contain) .accessibilityIdentifier("docs.document.header") } } /// The bar's two-line title while the large one is folded away: the title, /// truncated, and the subtitle under it. @MainActor struct DocsPhoneInlineTitle: View { let model: DocsModel let team: String? let state: DocumentHeaderState @Environment(\.accessibilityReduceMotion) private var reduceMotion var body: some View { // Present only while folded, so the bar holds nothing (and // VoiceOver reads nothing) while the large title shows. ZStack { if state.collapsed { VStack(spacing: 0) { Text(verbatim: model.detail?.title ?? "") .font(.headline) .lineLimit(1) .truncationMode(.tail) DocsSubtitleView(model: model, team: team) } .transition(.opacity) .accessibilityElement(children: .combine) // The bar caps its type at the largest sizes, as every bar // does: a long press shows it large (spec §9). .accessibilityShowsLargeContentViewer() .accessibilityIdentifier("docs.document.inlineTitle") } } .animation(reduceMotion ? .linear(duration: 0.12) : .easeOut(duration: 0.2), value: state.collapsed) } } /// What the text view hosts above its text: the large title's hosting /// controller, sized by the text view, reporting its height as it changes. @MainActor final class DocsPhoneHeaderHost { let controller: UIHostingController let state: DocumentHeaderState /// The height the title measured, for the text view's top inset. var heightChanged: ((CGFloat) -> Void)? init(model: DocsModel, team: String?, state: DocumentHeaderState) { self.state = state var report: ((CGFloat) -> Void)? controller = UIHostingController(rootView: AnyView(EmptyView())) controller.rootView = AnyView(DocsPhoneLargeTitle(model: model, team: team, state: state) .onGeometryChange(for: CGFloat.self) { $0.size.height } action: { report?($0) }) controller.view.backgroundColor = .clear controller.sizingOptions = [] // It scrolls with the text, under the bar: the text view's frame is // its layout, not the screen's safe area. controller.safeAreaRegions = [] report = { [weak self] height in self?.heightChanged?(height) } } var view: UIView { controller.view } func height(forWidth width: CGFloat) -> CGFloat { controller.sizeThatFits(in: CGSize(width: width, height: .greatestFiniteMagnitude)).height } } #endif