// The frame every pushed screen wears, decided in ONE place. // // Three facts about a Cotton screen are not a screen's business, and every one // of them was a screen's business until this file existed: // // 1. **The native navigation bar is hidden.** The design draws its own // chrome (`ScreenBar` — a floating back circle and a centred title over // full-bleed content), so a second bar with a second back button is a // second answer to "how do I leave". Sixty-two hosts each wrote // `.navigationBarBackButtonHidden()` for themselves; the ones that forgot // shipped two back buttons, and NOBODY hid the bar itself — an empty // transparent bar stayed mounted over every screen. // // 2. **The swipe back is the page surface's** (`TilePage`): the phone has // no navigation stack to pop since every page opens in place (lane // inplace, 16 Oct 2026), so the frame installs no pop delegate. // // 3. **The top safe-area inset is measurable from inside a scroller that // ignores it.** `ScreenBarClearance` needs it and cannot read it: a // `GeometryReader` under `.ignoresSafeArea(edges: .top)` reports zeroes. // So the app measures it ONCE, at the root, where nothing is ignored, and // hands it down as `\.cottonTopInset`. // // 4. **The keyboard is dismissed by the frame, never by a screen** — a tap // outside a text input, an interactive scroll, and a screen that leaves // taking its own responder with it. `Chrome/KeyboardDismissal.swift` // has the three rules and why a per-screen version is the bug. // // Applied by every host and page destination (`StackHost`, `ColumnsHost`, // `PageDestination`), so a screen // cannot opt in, opt out, or forget. `ScreenFrameTests` holds that: no source // outside this file may write `navigationBarBackButtonHidden` or hide the // navigation bar for itself. // import SwiftUI #if canImport(UIKit) import UIKit #endif // MARK: - The top inset, measured once private struct CottonTopInsetKey: EnvironmentKey { /// Zero, and zero is HONEST: it means nobody has measured yet. It is not a /// stand-in for a device inset — a screen that renders one frame with 0 /// draws its clearance 59-62pt short for that frame and then settles, and /// the alternative (guessing a notch height) would be wrong for ever on a /// device we guessed wrong about. static let defaultValue: CGFloat = 0 } extension EnvironmentValues { /// The window's top safe-area inset, measured at the app root. /// /// Read it wherever a view is laid out INSIDE `.ignoresSafeArea(edges: /// .top)` and still has to know where the safe area began — which on this /// app is `ScreenBarClearance` and nothing else. public var cottonTopInset: CGFloat { get { self[CottonTopInsetKey.self] } set { self[CottonTopInsetKey.self] = newValue } } } // MARK: - A column's root private struct CottonAtColumnRootKey: EnvironmentKey { /// False, and false is the phone: every screen the phone draws is pushed /// onto one stack, so its bar always has somewhere to go back to. static let defaultValue = false } extension EnvironmentValues { /// Whether the screen inside is the ROOT of a column rather than a push. /// /// The Mac's host sets it true on the first screen of column 2 and of /// column 3, where there is nothing under the screen to pop to, and /// `ScreenBar` answers by leaving the leading circle out — a back control /// that cannot go back is a promise the window cannot keep. The title /// stays centred because it never was laid out inside the control row. /// /// **Chrome reads this; no screen does.** A screen that branched on it /// would be a screen that knows which platform it is on, which is exactly /// what `ScreenPlatformSweepTests` exists to prevent. public var cottonAtColumnRoot: Bool { get { self[CottonAtColumnRootKey.self] } set { self[CottonAtColumnRootKey.self] = newValue } } } private struct TopInsetPreference: PreferenceKey { static let defaultValue: CGFloat = 0 static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) { value = max(value, nextValue()) } } /// Measure this view's top safe-area inset and publish it as /// `\.cottonTopInset` to everything inside. /// /// It must be applied where the safe area is NOT ignored — the app root — and /// exactly once. A second reader further down would measure a subview that has /// already had the inset consumed and publish zero over the real answer. public struct MeasuresTopInset: ViewModifier { @State private var inset: CGFloat = 0 public init() {} public func body(content: Content) -> some View { content .background( GeometryReader { proxy in Color.clear.preference( key: TopInsetPreference.self, value: proxy.safeAreaInsets.top ) } ) .onPreferenceChange(TopInsetPreference.self) { measured in // Guarded so a transient zero (a background layout pass while // a sheet is up) never un-measures the app. if measured > 0 { inset = measured } } .environment(\.cottonTopInset, inset) } } /// A page in a UIKit host of its own (a fan tile's stack, `TileStack`) /// measures the inset ITS frame has and publishes that instead of the /// window's. The tile's host is laid out below the status bar, so its pages /// have no top inset left; reading the root's measure there counted the /// status bar twice — about 60 pt of empty paper between the head row and the /// first content (fan3 part 10 refinement). Zero is the honest answer here, /// so unlike `MeasuresTopInset` it is published; apply it at the host's root, /// where nothing is ignored. Guard: `TopOfPageTests`. public struct MeasuresHostTopInset: ViewModifier { @State private var inset: CGFloat = 0 public init() {} public func body(content: Content) -> some View { content .onGeometryChange(for: CGFloat.self, of: { $0.safeAreaInsets.top }) { inset = $0 } .environment(\.cottonTopInset, inset) } } // MARK: - The top wash private struct CottonPageGroundKey: EnvironmentKey { static let defaultValue: ColorSlot = .ground } extension EnvironmentValues { /// The ground the page stands on, so the top wash can fade from THAT /// colour rather than from the default one. /// /// This closes the filed screen-bar wash-shade gap: the muted /// pages (`groundMuted`) were being washed with `ground`, one shade off, /// and the fix was never a second wash drawn by the screen — that would be /// a second answer to "what does a screen's top look like". public var cottonPageGround: ColorSlot { get { self[CottonPageGroundKey.self] } set { self[CottonPageGroundKey.self] = newValue } } } extension View { /// Say which ground this page stands on, so its top wash matches it. public func cottonPageGround(_ slot: ColorSlot) -> some View { environment(\.cottonPageGround, slot) } /// Say that this page stands on a PHOTOGRAPH over its ground, so the two /// washes draw the same photograph rather than the flat colour under it. func cottonPageTexture(_ texture: PageTexture?) -> some View { environment(\.cottonPageTexture, texture) } } // MARK: - A textured ground /// A photographic ground — a seamless TILE drawn over the page's colour. /// Home's plaster is the one there is: the board its project caps sit on /// (`Paper/ProjectCaps.swift`). /// /// A tile rather than a full-bleed photograph, on purpose. The page draws /// its ground full-screen; the top wash draws it in a band a status bar /// tall; the bottom wash draws it behind the footer, wherever the keyboard /// has put the footer. A photograph would have to be laid out at the /// screen's size in each of those and shifted by the fill's own position, or /// the wash shows a different crop from the page it is washing and reads as /// a bar — and that shift is exactly the thing that went wrong on the first /// simulator run (the footer's wash came out as the flat colour). A tile has /// no crop: every fill tiles it from its own origin, and plaster grain is /// too fine for the phase between two fills to be seen. /// /// ONE photograph: the app is light-only, so the imageset carries no /// `luminosity: dark` variant for anything to resolve to. /// /// THE TILE IS RELIEF, NOT COLOUR. It is grey, centred on 50 %, and overlaid /// on the ground colour, so the page's colour is the `groundMuted` token and /// nothing else: a photographed plaster used to cover the token completely, /// and the page wore whatever tone the photograph happened to have (a cool /// 234,227,220 under a token nobody saw). The tile is cut by /// `scripts/gen-caps/export_caps.py` (`relief`). struct PageTexture: Equatable, Sendable { /// The tiling imageset. let asset: String /// Home's plaster. static let plaster = PageTexture(asset: "home-plaster") } private struct CottonPageTextureKey: EnvironmentKey { static let defaultValue: PageTexture? = nil } extension EnvironmentValues { /// The photograph the page stands on, when it stands on one. Read by the /// two washes beside the colour, and by the page's own ground. var cottonPageTexture: PageTexture? { get { self[CottonPageTextureKey.self] } set { self[CottonPageTextureKey.self] = newValue } } } /// The page's ground as ONE fill: the `\.cottonPageGround` colour, and over /// it the `\.cottonPageTexture` tile when the page has one. The washes mask /// this; the page draws it whole. /// /// The Mac has no resource bundle (`ResourceBundleGuardTests`) and no page /// asks it for a texture; it draws the colour. struct PageGroundFill: View { @Environment(\.cottonPageGround) private var ground @Environment(\.cottonPageTexture) private var texture @Environment(\.theme) private var theme var body: some View { ZStack { theme.colors.color(ground) #if canImport(UIKit) // A theme with no grain lays its ground flat: the plaster is // paper's material. if let texture, theme.texture.grain { let _ = CottonMedia.expect(texture.asset) // Grey relief, OVERLAID: at 50 % grey an overlay leaves the // ground exactly as it is, so the page's mean colour is the // ground token's and the tile adds only the plaster's grain // (`PageTexture`). Image(texture.asset, bundle: CottonResources.bundle) .resizable(resizingMode: .tile) .blendMode(.overlay) // ON THE TILE, not only on the fill around it. An // imageset drawn by name carries that name as its // accessibility label, and a hide written on the // enclosing `ZStack` is a hide the tile itself never // wears: the two washes below re-draw this fill through // a `.mask`, and VoiceOver went on reading // "home-plaster" in both bands after the container was // hidden (the 2026-09-15 re-shoot). Decoration is // hidden where it is DRAWN, so the hide travels with the // picture into every frame that re-draws it. .accessibilityHidden(true) } #endif } .compositingGroup() .clipped() // **The ground is drawn once and kept.** It is a colour with a grey // relief OVERLAID on it — a blend, so the compositor re-composited // the whole screen's worth of tile under every scrolling page, and // the two washes re-draw this same view through a mask. Nothing in // it changes while a page is open, so it rasterises. .drawingGroup() // Ground, not content. A tile drawn from an imageset carries the // ASSET'S NAME as its label unless it is hidden, so VoiceOver read // "home-plaster" on every screen of the app (the 2026-09-15 walk, // row 000). Nothing here is readable; nothing here is an element. .accessibilityHidden(true) } } /// The page's own ground at the top of the screen, fading to nothing. Not a /// bar: a wash. `ScreenBar` wears it, and so does any screen whose content /// scrolls up under the status bar without a bar of its own. /// /// **It is anchored to the bar, not spread evenly over the band.** Its three /// stops were once fractions of the whole height — which put roughly 60 % /// opacity behind the title, and a list sliding under a 60 % wash is a list you /// can still read THROUGH the screen's own name (build 130's screenshot: a /// helper paragraph legible straight through the screen's name). So the /// ground is solid from the physical top to the control row's bottom edge and /// fades to nothing over the 24pt below it. There is still no filled bar and no /// hairline: what ends the wash is the fade, and the fade begins where the /// title ends. /// /// The inset comes from `\.cottonTopInset` for `ScreenBarClearance`'s reason — /// this view ignores the top edge, so it cannot measure it. public struct ScreenTopWash: View { /// How far below the safe-area top the wash stays solid. The bar's own /// height by default; a screen with no bar passes its own figure. private let solidBelowInset: CGFloat? private let fade: CGFloat @Environment(\.cottonTopInset) private var topInset @Environment(\.cottonPageGround) private var ground @Environment(\.theme) private var theme /// `solidBelowInset` nil is the worn bar's own height. public init(solidBelowInset: CGFloat? = nil, fade: CGFloat = 24) { self.solidBelowInset = solidBelowInset self.fade = fade } public var body: some View { let solid = topInset + (solidBelowInset ?? CaptureSurfaceKnobs.chromeScreenBarHeight) let total = solid + fade // The ground — colour, or the page's photograph — through a MASK of // the same three stops, so a textured page washes with its own // texture and a plain one with its own colour. return PageGroundFill() .mask( LinearGradient( stops: [ .init(color: .black, location: 0), .init(color: .black, location: solid / total), .init(color: .black.opacity(0.55), location: (solid + fade * 0.45) / total), .init(color: .black.opacity(0), location: 1), ], startPoint: .top, endPoint: .bottom ) ) .frame(height: total) // Laid out at the safe-area top and expanded upward over it, so the // `total` above really is measured from the physical top. .ignoresSafeArea(edges: .top) .allowsHitTesting(false) // The wash is the page's own ground re-drawn in a band. Nothing in it // is readable and nothing in it is an element — said here as well as // on the tile, because a wash is a SECOND drawing of the ground and // VoiceOver counted it as a second stop. .accessibilityHidden(true) } } // MARK: - The frame /// What every pushed screen wears. See this file's header for the three facts /// it decides and why none of them is a screen's business. struct CottonScreenFrame: ViewModifier { public init() {} public func body(content: Content) -> some View { content // The bar itself, not just its back button: an empty transparent // navigation bar still sits over the screen and still eats the // taps in its band. #if os(iOS) .toolbar(.hidden, for: .navigationBar) #endif // Belt and braces, and not redundant: a screen that presents its // own stack, or a future iOS that draws a bar it was told to hide, // must still never show a second back button. .navigationBarBackButtonHidden(true) // The keyboard's three rules — scroll, tap-outside, leave — for // every screen at once. `Chrome/KeyboardDismissal.swift`. .dismissesKeyboard() // A screen pushed on a view's own stack is not on the navigator's // path, so a narrowed list it opens goes on that stack too: a // route pushed from above it would pop it. `RouteScreen` — the one // screen that IS on the path — says so again inside. .environment(\.listsOnPath, false) .environment(\.screenRoute, nil) } } extension View { /// The frame every pushed screen wears. Written ONCE, at `RootView`'s /// `navigationDestination`. public func cottonScreenFrame() -> some View { modifier(CottonScreenFrame()) } /// Measure the top safe-area inset here and publish it downward. The app /// root, and nowhere else. public func measuresTopInset() -> some View { modifier(MeasuresTopInset()) } }