// 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.importSwiftUI/// One page a screen opened of its own, raised over its page surface.structPageLayer {let id: Stringlet page: AnyView/// The environment of the screen that opened it.let environment: EnvironmentValueslet close: () -> Void}structPageLayerKey: PreferenceKey {staticvar defaultValue: [PageLayer] { [] }staticfunc 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.structPageTitleKey: PreferenceKey {staticlet defaultValue: String? = nilstaticfunc reduce(value: inoutString?, nextValue: () -> String?) { value = value ?? nextValue() }}extensionView {/// 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: Hashable, Destination: View>( item: Binding<Item?>, @ViewBuilder destination: @escaping (Item) -> Destination ) -> someView { #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<Destination: View>( isPresented: Binding<Bool>, @ViewBuilder destination: @escaping () -> Destination ) -> someView { #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() -> someView { #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.privatestructPageDestinationLayer: ViewModifier {let id: String?let page: () -> AnyView?let close: () -> Void @Environment(\.self) privatevar environment/// Where this screen stands, so two screens' layers never share an id. @State privatevar place = UUID().uuidStringfunc body(content: Content) -> someView { content.transformPreference(PageLayerKey.self) { layers inguardlet 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.privatestructPageSurface: ViewModifier { @Environment(\.theme) privatevar theme @Environment(\.accessibilityReduceMotion) privatevar reduceMotion @Environment(\.tileHere) privatevar here @Environment(\.pageLayerShown) privatevar shown @Environment(\.pageOwnTitle) privatevar ownTitle @State privatevar title: String?/// A layer stands over the page: the page under it is not read out. @State privatevar layered = falsefunc body(content: Content) -> someView { content .coveredUnread(layered) .onPreferenceChange(PageTitleKey.self) { title = $0 } .overlayPreferenceValue(PageLayerKey.self) { layers in ZStack {iflet 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 } }privatevar 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.privatestructLayerPage: View {let layer: PageLayerlet under: TileBack? @Environment(\.cottonHaptics) privatevar hapticsvar body: someView {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 = nilreturn 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 inguard 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.enumPageLayerKnobs {/// How wide the strip at the left edge a swipe back starts in is.staticlet edgeWidth: CGFloat = 20staticlet edgeStart: CGFloat = 10/// How far the swipe travels before it closes the layer.staticlet edgeTravel: CGFloat = 80}privatestructPageLayerShownKey: EnvironmentKey {staticlet defaultValue: (@MainActor (Bool) -> Void)? = nil}privatestructPageOwnTitleKey: EnvironmentKey {staticlet defaultValue: String? = nil}extensionEnvironmentValues {/// 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).privatestructLeavesAtOnce: Transition {func body(content: Content, phase: TransitionPhase) -> someView { content.accessibilityHidden(!phase.isIdentity) }}