// A page a screen opens of its own (lane inplace, 16 Oct 2026). // // Most pages are routes and open through the navigator, in place on the page // surface's stack (`PageOpening`, `TilePage`). A few are a screen's own and // carry what no route can: a group's // members with the change they report back, a record of another database's // store, a section's whole list under its own title. They used to be the // screen's own `navigationDestination`, which on the phone slid in from the // right over the whole screen with a chevron back; the owner ruled every page // opens as a record does. // // So on the phone such a page is a LAYER of the page surface it was opened // on: it cross-fades in over that page (`fan_open_fade_ms`; Reduce Motion // too), the page under it kept as it was; its head row has the X at its right // with its title at the left, a long press on the X lists the pages under it, // and the left-edge swipe closes it. A layer is a page surface of its own, so // a page it opens is a layer over it. A sheet is its own presentation: a // layer opened inside one stands over the sheet's page, never the page under // the sheet. The Mac keeps its window push (`navigationDestination`). // // The layer travels UP as a preference rather than down as a handle: the // destination is drawn fresh with every pass of the screen that opened it, // and a preference stops at the presentation it was raised in. import SwiftUI /// One page a screen opened of its own, raised over its page surface. struct PageLayer { let id: String let page: AnyView /// The environment of the screen that opened it. let environment: EnvironmentValues let close: () -> Void } struct PageLayerKey: PreferenceKey { static var defaultValue: [PageLayer] { [] } static func reduce(value: inout [PageLayer], nextValue: () -> [PageLayer]) { value.append(contentsOf: nextValue()) } } /// The title a page draws in its head row, read by the page surface it /// stands on: the line a layer's long-press list says for the page under it. struct PageTitleKey: PreferenceKey { static let defaultValue: String? = nil static func reduce(value: inout String?, nextValue: () -> String?) { value = value ?? nextValue() } } extension View { /// Opens `destination` for `item` while it is set: in place on the phone /// (a layer of the page surface), pushed in the Mac's window. func pageDestination( item: Binding, @ViewBuilder destination: @escaping (Item) -> Destination ) -> some View { #if os(iOS) modifier(PageDestinationLayer( id: item.wrappedValue.map { String(describing: $0.hashValue) }, page: { item.wrappedValue.map { AnyView(destination($0)) } }, close: { item.wrappedValue = nil })) #else navigationDestination(item: item, destination: destination) #endif } /// Opens `destination` while `isPresented` is true: in place on the /// phone (a layer of the page surface), pushed in the Mac's window. func pageDestination( isPresented: Binding, @ViewBuilder destination: @escaping () -> Destination ) -> some View { #if os(iOS) modifier(PageDestinationLayer( id: isPresented.wrappedValue ? "shown" : nil, page: { isPresented.wrappedValue ? AnyView(destination()) : nil }, close: { isPresented.wrappedValue = false })) #else navigationDestination(isPresented: isPresented, destination: destination) #endif } /// This view is a page surface: the layers its screens open stand over /// it, the top one shown (`PageDestination.swift`), and the cards they /// open over those (`CardLayer`). func pageSurface() -> some View { #if os(iOS) modifier(PageSurface()).cardLayers() #else cardLayers() #endif } } #if os(iOS) /// Raises the destination as a layer while it is open, drawn in the opening /// screen's environment. private struct PageDestinationLayer: ViewModifier { let id: String? let page: () -> AnyView? let close: () -> Void @Environment(\.self) private var environment /// Where this screen stands, so two screens' layers never share an id. @State private var place = UUID().uuidString func body(content: Content) -> some View { content.transformPreference(PageLayerKey.self) { layers in guard let id, let page = page() else { return } layers.append(PageLayer(id: place + id, page: page, environment: environment, close: close)) } } } /// The page surface: its page, with the top layer its screens opened over /// it. A layer's head closes it; the edge swipe closes it; the pages under /// it are its long-press list. private struct PageSurface: ViewModifier { @Environment(\.theme) private var theme @Environment(\.accessibilityReduceMotion) private var reduceMotion @Environment(\.tileHere) private var here @Environment(\.pageLayerShown) private var shown @Environment(\.pageOwnTitle) private var ownTitle @State private var title: String? /// A layer stands over the page: the page under it is not read out. @State private var layered = false func body(content: Content) -> some View { content .coveredUnread(layered) .onPreferenceChange(PageTitleKey.self) { title = $0 } .overlayPreferenceValue(PageLayerKey.self) { layers in ZStack { if let top = layers.last { LayerPage(layer: top, under: here?.adding(title: title ?? ownTitle ?? CottonStrings.back)) .id(top.id) .transition(.opacity.combined(with: LeavesAtOnce())) } } .animation(.easeOut(duration: fade), value: layers.last?.id) .onChange(of: layers.last?.id, initial: true) { _, top in layered = top != nil shown?(top != nil) } } // The layers stop here: the surface under this one never draws them. .transformPreference(PageLayerKey.self) { $0 = [] } .transformPreference(PageTitleKey.self) { $0 = nil } } private var fade: Double { Double(theme.scale[.fanOpenFadeMs]) / 1000 } } /// One layer over its page: the page's ground under it, its X closing it, /// the left-edge swipe closing it, and a surface of its own for what it opens. private struct LayerPage: View { let layer: PageLayer let under: TileBack? @Environment(\.cottonHaptics) private var haptics var body: some View { var environment = layer.environment environment.tileBack = under?.layered(close: layer.close) environment.tileHere = environment.tileBack // A layer is never a column's root, whatever page it was opened // from (a fan tile's own list is one): it wears its X. environment.cottonAtColumnRoot = false environment.pageLayerShown = nil environment.pageOwnTitle = nil // A page opened on a card closes by its own X, never the card's. environment.closeCard = nil return layer.page .pageSurface() .environment(\.self, environment) .background { PageGroundFill().ignoresSafeArea() } // A value fan belongs to the opened page but stands beside its // host. Let the reader reach it while editing; the covered // page remains unread through PageSurface. .coversTheRest(!DeckEditor.session.isEditing) .overlay(alignment: .leading) { // The left-edge swipe: a step back, as on every other page. Color.clear .frame(width: PageLayerKnobs.edgeWidth) .frame(maxHeight: .infinity) .contentShape(Rectangle()) .gesture(DragGesture(minimumDistance: PageLayerKnobs.edgeStart).onEnded { drag in guard drag.translation.width > PageLayerKnobs.edgeTravel else { return } haptics.press() layer.close() }) .ignoresSafeArea() } } } /// The sizes the layer's edge swipe reads: Swift's own, beside their reader. enum PageLayerKnobs { /// How wide the strip at the left edge a swipe back starts in is. static let edgeWidth: CGFloat = 20 static let edgeStart: CGFloat = 10 /// How far the swipe travels before it closes the layer. static let edgeTravel: CGFloat = 80 } private struct PageLayerShownKey: EnvironmentKey { static let defaultValue: (@MainActor (Bool) -> Void)? = nil } private struct PageOwnTitleKey: EnvironmentKey { static let defaultValue: String? = nil } extension EnvironmentValues { /// The title line of the page a surface shows when the page draws none /// in its head row (a record's): the stack's own words for it. var pageOwnTitle: String? { get { self[PageOwnTitleKey.self] } set { self[PageOwnTitleKey.self] = newValue } } /// Told whether a layer stands over a fan tile's page, so the stack's /// own edge swipe gives way to the layer's (`TileStack`). var pageLayerShown: (@MainActor (Bool) -> Void)? { get { self[PageLayerShownKey.self] } set { self[PageLayerShownKey.self] = newValue } } } #endif /// A page fading in or out is not on the accessibility tree: a closed page /// leaves it at once, not at the end of its fade (owner's rule 8). private struct LeavesAtOnce: Transition { func body(content: Content, phase: TransitionPhase) -> some View { content.accessibilityHidden(!phase.isIdentity) } }