// The one paper. Every object in the app that is a piece of paper is this view // at a size the screen chooses. // // It replaces six photographs with a drawing, and the reason is not weight: it // is that a photograph has a fixed aspect, a baked-in shadow and no way to grow. // A note that is two lines and a note that is forty are the same object and the // reference pictures draw them as one sheet that grows; a photograph cannot do // that without nine-slicing, and a nine-sliced photograph of paper stretches its // own fibre. So the paper is assembled instead, out of four things the // photograph had: // // 1. the DYE — the measured colour of the master (`PaperDye`); // 2. the GRAIN — one seamless tile cut from the master, multiplied over the // dye (`PaperGrain`, `scripts/gen-cards/export_grain.py`); // 3. the EDGE — a path whose sides are nibbled by seeded noise // (`PaperShape`), so a cut card, a torn slip and a perforated stub are // three edges of one shape; // 4. the LIFT — the corner shading and the two shadow layers that put the // sheet on the plaster rather than in it. // // `PaperCard`, `PaperSheet`, `PaperSlip` and `PaperRow` are NOT types. They are // this view at the four sizes `PaperMetrics` documents, and a screen chooses // between them by choosing a frame. // // EVERYTHING IS SEEDED BY `seed` — pass the record's id. Tilt, wear and deckle // are then the same on every launch and every device, which is what lets a // screenshot be re-taken and a diff mean something. // // WHAT THE KIT DELIBERATELY DOES NOT HAVE: a switch. The owner's review retired // metal from everything but the key-tag ring, and a drawn switch is named in the // "too skeuomorphic" list by name. A switch on paper is `Toggle` with // `.tint(theme.colors.accent.color)` — the plain iOS one — and it is written down here // so nobody draws one. import SwiftUI /// How high off the ground the sheet is lying. public enum PaperShadow: String, CaseIterable, Sendable { /// Lying on the plaster. The todo card's two layers, which is the lift the /// owner approved. case resting /// Picked up — a drag, a press, the top of a stack. case lifted /// Inside another sheet: a paper on a paper casts nothing, or the inner one /// reads as a sticker. case none } public struct Paper: View { @Environment(\.theme) private var theme /// A list's row: its surface is a kept picture, and so is its resting lift. @Environment(\.paperSurfaceImages) private var listRow private let dye: PaperDye private let edge: PaperEdge private let seed: String private let tilts: Bool private let shadow: PaperShadow private let inset: CGFloat? private let fills: Bool private let content: Content /// `inset` overrides the width fraction for a paper whose proportion the /// fraction was not drawn for — a full-width ROW is as wide as a sheet and /// a fifth as tall, and a sheet's margin on it leaves a strip of text. /// /// **`fills` is how a sheet says it is a SHAPE rather than a piece of /// writing.** A sheet is as tall as what is written on it (`PaperInsetLayout`); /// a card in a grid is not — its height is the grid's, and a note of four /// words next to a note of forty must draw the same card. So every paper /// whose size is decided OUTSIDE it — an `.aspectRatio`, a fixed frame, a /// row of a `Grid`, a strip as long as an event — passes `fills: true`, and /// then it takes the height it is handed. It is a parameter and not a /// modifier because the modifier is what went wrong: `.frame(height:)` /// around a paper says nothing to the paper, and the sheet that answered any /// proposal with the whole of it answered the stacks too. public init( dye: PaperDye = .cream, edge: PaperEdge = .cut, seed: String, tilt: Bool = true, shadow: PaperShadow = .resting, inset: CGFloat? = nil, fills: Bool = false, @ViewBuilder content: () -> Content ) { self.dye = dye self.edge = edge self.seed = seed self.tilts = tilt self.shadow = shadow self.inset = inset self.fills = fills self.content = content() } public var body: some View { PaperInsetLayout(scale: theme.scale, fixed: inset, fills: fills) { content // What is drawn on this sheet knows it is ON paper, so a piece // of chrome that is a sheet of its own (a summary, a skeleton // bar) draws its words rather than a second sheet under them. .environment(\.isOnPaper, true) } .background { PaperSurface(dye: dye, edge: edge, seed: seed) } .compositingGroup() // A list's resting row casts its lift from a kept picture // (`PaperRestingShadow`), never blurred again as the list scrolls. .background { if keptLift { PaperRestingShadow(edge: edge, seed: seed, fill: theme.sheet(dye)) } } .rotationEffect(.degrees(tilts ? theme.tilt(seed: seed) : 0)) .modifier(PaperLift(shadow: keptLift ? .none : shadow)) } /// The resting lift is the kept picture: on a list's row, on the phone /// (the Mac's hover grows the lift live). private var keptLift: Bool { #if os(iOS) listRow && shadow == .resting #else false #endif } } /// Whether a view is drawn on a `Paper` — set by `Paper` for its content. private struct IsOnPaperKey: EnvironmentKey { static let defaultValue = false } extension EnvironmentValues { /// True under a `Paper`. Chrome that would otherwise lay its own sheet /// down reads it, so paper is never stacked on paper by accident. public var isOnPaper: Bool { get { self[IsOnPaperKey.self] } set { self[IsOnPaperKey.self] = newValue } } } /// The sheet itself, with nothing on it — the photograph, clipped to one /// outline. /// /// Its own view so the empty object an `ObjectEmpty` draws and the background /// behind a caller's own layout are the same paper as a `Paper`'s. /// /// THE SURFACE IS THE PHOTOGRAPH AND THE OUTLINE IS DRAWN. Why, and the /// arithmetic that lets one picture be a 28pt chip and a 700pt sheet, is /// `PaperPhoto.swift`. Three things are drawn here and nothing else: the /// picture, nine-sliced so its rim keeps its own thickness; the fibre tile, /// at the strength the stretch cost; and the clip. public struct PaperSurface: View { private let dye: PaperDye private let edge: PaperEdge private let seed: String @Environment(\.paperSurfaceImages) private var usesImages @Environment(\.displayScale) private var scale @Environment(\.colorScheme) private var scheme @Environment(\.theme) private var theme public init(dye: PaperDye = .cream, edge: PaperEdge = .cut, seed: String) { self.dye = dye self.edge = edge self.seed = seed } public var body: some View { GeometryReader { geo in if usesImages, let image = picture(size: geo.size) { Image(decorative: image, scale: scale) .resizable() .frame(width: geo.size.width, height: geo.size.height) } else { PaperSurfaceDrawing(dye: dye, edge: edge, seed: seed, size: geo.size) } } .compositingGroup() // Lists opt in to a cached picture of this same drawing. // The sheet's SURFACE, under everything written on it. It was an // unlabelled accessibility element per sheet before this (the // 2026-09-15 walk, row 000); what a reader wants is the writing. .accessibilityHidden(true) } private func picture(size: CGSize) -> CGImage? { let key = PaperSurfaceImageCache.Key( dye: dye, edge: edge, seed: seed, size: size, scale: scale, scheme: scheme, corner: theme.scale, texture: theme.texture) return PaperSurfaceImageCache.shared.image(for: key) { let renderer = ImageRenderer(content: PaperSurfaceDrawing(dye: dye, edge: edge, seed: seed, size: size) .frame(width: size.width, height: size.height) .environment(\.colorScheme, scheme) .environment(\.displayScale, scale) // The renderer starts a fresh environment: the sheet is // cut to the theme it will be shown in. .environment(\.theme, theme)) renderer.scale = scale return renderer.cgImage } } } /// The original photograph, grain and seeded outline, also used to make the /// list's cached image. The writing and lift remain outside this drawing. struct PaperSurfaceDrawing: View { @Environment(\.theme) private var theme let dye: PaperDye let edge: PaperEdge let seed: String let size: CGSize var body: some View { switch theme.family { case .paper: ZStack { photograph(size: size) let lost = PaperGrain.lostToStretch(at: size, scale: theme.scale) if lost > Double(PaperKnobs.paperGrainMinStrength), theme.texture.grain { PaperGrainFill(strength: lost) } } .clipShape(PaperShape(edge: edge, seed: seed, theme: theme)) case .plain: // A flat card: the theme's paper colour, its hairline, its corner. // Every dye is the one card — colour never names a kind. let shape = PaperShape(edge: edge, seed: seed, theme: theme) shape.fill(theme.colors.paper.color) .overlay(shape.strokeBorder(theme.colors.hairline.color, lineWidth: theme.scale.hairline)) } } /// The picture, sliced. A build with no picture — a Mac without its /// resource bundle — draws the measured dye flat, which is visibly a /// stand-in and is meant to be. @ViewBuilder private func photograph(size: CGSize) -> some View { let photo = PaperPhoto(scale: theme.scale) if let picture = CottonMedia.image( dye.photograph, scale: photo.drawingScale(for: size)) { let cap = photo.cap(for: size) let turn = PaperPhoto.flip(seed: seed) picture .resizable( capInsets: EdgeInsets(top: cap, leading: cap, bottom: cap, trailing: cap), resizingMode: .stretch ) .interpolation(.medium) // On the picture, not only on the surface around it: a sheet // is a photograph, and what a reader wants is the writing. .accessibilityHidden(true) // The sheet turned over or around — four arrangements of one // picture, so a grid of twelve is not one card twelve times. .scaleEffect(x: turn.x, y: turn.y) } else { Rectangle().fill(theme.sheet(dye)) } } } /// The grain tile, multiplied. Its own view because the instant print's frame /// wears the same tile at a whisper of the strength. /// /// AT `PaperGrain.masterScale`, NEVER a plain read: the tile is written at 3x /// and macOS has no 3x, so the Mac's rendition of it is a 2x twin of the same /// 512 px file and a plain read would tile it at 256 points instead of 170.67 /// — the paper's weave half again life size on every sheet. Asking for the /// scale sizes from the picture's PIXELS, so both hosts lay the same fibre. public struct PaperGrainFill: View { let strength: Double public init(strength: Double) { self.strength = strength } public var body: some View { if let tile = CottonMedia.image(PaperGrain.asset, scale: PaperGrain.masterScale) { tile .resizable(resizingMode: .tile) .blendMode(.multiply) .opacity(strength) // VoiceOver read "paper-grain" wherever this was drawn. .accessibilityHidden(true) } } } /// One layer of a paper's shadow. struct PaperShadowLayer: Hashable { let opacity: Double let radius: CGFloat let x: CGFloat let y: CGFloat } /// The two shadow layers, by name. public struct PaperLift: ViewModifier { @Environment(\.theme) private var theme let shadow: PaperShadow public init(shadow: PaperShadow) { self.shadow = shadow } /// Set by `HoverLift` when a pointer is over the pressable this paper is /// in. Always false on the phone. @Environment(\.cottonPaperHovered) private var hovered /// What a hovered paper adds to its FAR layer, in points of blur and of /// drop (`paper_lift_hover`): enough to read as the sheet coming up off /// the desk, not enough to read as a different shadow. static func hoverLift(_ scale: ThemeScale) -> CGFloat { PaperKnobs.paperLiftHover } /// The layers, near first. A paper inside another sheet casts nothing, /// hovered or not. The theme's `paper_lift_*` knobs. static func layers(_ shadow: PaperShadow, hovered: Bool, scale: ThemeScale) -> [PaperShadowLayer] { let lift = hovered ? hoverLift(scale) : 0 switch shadow { case .none: return [] case .resting: // A CONTACT shadow: tight, and thrown DOWN AND RIGHT, which is // where the light in every reference picture comes from. An even // glow on all four sides is what makes a sheet float instead of // rest, and that is what the acceptance comparison saw. return [ PaperShadowLayer( opacity: Double(PaperKnobs.paperLiftRestingNearOpacity), radius: PaperKnobs.paperLiftRestingNearRadius, x: PaperKnobs.paperLiftRestingNearX, y: PaperKnobs.paperLiftRestingNearY), PaperShadowLayer( opacity: Double(PaperKnobs.paperLiftRestingFarOpacity), radius: PaperKnobs.paperLiftRestingFarRadius + lift, x: PaperKnobs.paperLiftRestingFarX, y: PaperKnobs.paperLiftRestingFarY + lift), ] case .lifted: return [ PaperShadowLayer( opacity: Double(PaperKnobs.paperLiftLiftedNearOpacity), radius: PaperKnobs.paperLiftLiftedNearRadius, x: PaperKnobs.paperLiftLiftedNearX, y: PaperKnobs.paperLiftLiftedNearY), PaperShadowLayer( opacity: Double(PaperKnobs.paperLiftLiftedFarOpacity), radius: PaperKnobs.paperLiftLiftedFarRadius + lift, x: PaperKnobs.paperLiftLiftedFarX, y: PaperKnobs.paperLiftLiftedFarY + lift), ] } } public func body(content: Content) -> some View { // A theme that casts nothing lifts nothing, resting or picked up. let layers = theme.texture.shadow == .none ? [] : Self.layers(shadow, hovered: hovered, scale: theme.scale) // Both layers or none: a near layer and exactly one far one. if layers.dropFirst().count == 1 { content .shadow(color: theme.colors.shadow.color.opacity(layers[0].opacity), radius: layers[0].radius, x: layers[0].x, y: layers[0].y) .shadow(color: theme.colors.shadow.color.opacity(layers[1].opacity), radius: layers[1].radius, x: layers[1].x, y: layers[1].y) } else { content } } /// The lifted pair at `amount` (0…1) of its strength. A caller that lifts /// a view MID-GESTURE fades this rather than switching the modifier, which /// would give the view a new identity at pickup (`ReorderableList`). /// The same two layers `layers(.lifted, hovered: false, scale:)` names, so the /// numbers live once. public static func lifted(_ view: V, amount: Double) -> some View { view.modifier(PaperPickup(amount: amount)) } } /// `PaperLift.lifted`'s pair, in the theme's shadow colour — a modifier so the /// theme is read where the view is drawn. private struct PaperPickup: ViewModifier { let amount: Double @Environment(\.theme) private var theme func body(content: Content) -> some View { let layers = theme.texture.shadow == .none ? [] : PaperLift.layers(.lifted, hovered: false, scale: theme.scale) if layers.dropFirst().count == 1 { content .shadow(color: theme.colors.shadow.color.opacity(layers[0].opacity * amount), radius: layers[0].radius, x: layers[0].x, y: layers[0].y) .shadow(color: theme.colors.shadow.color.opacity(layers[1].opacity * amount), radius: layers[1].radius, x: layers[1].x, y: layers[1].y) } else { content } } } /// The inset, as a fraction of the paper's own width. /// /// A `Layout` rather than a `padding`, because the fraction needs the width and /// the width is what the parent proposes: reading it with a `GeometryReader` and /// feeding it back as padding is a layout loop that settles a frame late and /// flickers on first draw. A layout is handed the proposal directly. /// /// **A SHEET IS AS TALL AS WHAT IS WRITTEN ON IT.** This used to answer any /// proposed height with that height, which made every paper in the app /// infinitely flexible downwards — and a flexible view in a stack takes the /// leftover. So the strip of Macs on `/code` drew three lines of text on a /// card that ran to the bottom of the phone (the `HStack` hands its children /// the scroller's full height), and the News edition ended at *That's /// everything.* with a hand's depth of empty newsprint under it (the page's /// `VStack` had nothing else to give the space to). Two reports on /// 2026-09-23 are the receipt. /// /// A paper whose size is decided outside it — a card at an aspect, a strip as /// long as its event, a grid row — says so in one word, `Paper(fills: true)`, /// and takes the height it is handed. struct PaperInsetLayout: Layout { /// The theme's scale, whose inset a paper takes when it names none. let scale: ThemeScale /// A caller's own inset, or nil for the width fraction. var fixed: CGFloat? = nil /// Whether this paper's height is the proposal's rather than its writing's /// — `Paper.init(fills:)` has the account. var fills: Bool = false private func margin(_ width: CGFloat) -> CGFloat { fixed ?? scale.inset(forWidth: width) } /// A proposed height, or nil when there is nothing usable in it. /// /// **AN INFINITE PROPOSAL IS "YOU DECIDE", NOT A HEIGHT** — the rule /// `ScrollColumnLayout.usable` states for the width, in the same words and /// for the same reason: a stack probes a child's flexibility by proposing /// `.infinity`, and a layout that answers one with an infinite size has /// declared itself greedy. static func usable(_ dimension: CGFloat?) -> CGFloat? { guard let dimension, dimension.isFinite else { return nil } return dimension } /// **The height a sheet states.** What is written on it, and the proposal /// only where the writing would overflow it — never more. /// /// `fills` inverts the first half and not the second: a card at an aspect /// takes the height it is handed whatever is on it, and a NON-finite /// proposal is still "you decide", because a layout that answers a stack's /// probe with an infinite size has declared itself greedy either way. /// /// A static function, so the rule can be asserted without a layout pass /// (`PaperHeightTests`), the way `ScrollColumnLayout.stated` is. static func stated(proposal: CGFloat?, written: CGFloat, fills: Bool = false) -> CGFloat { guard let proposed = usable(proposal) else { return written } return fills ? proposed : min(proposed, written) } func sizeThatFits( proposal: ProposedViewSize, subviews: Subviews, cache: inout () ) -> CGSize { // Asked to decide (a fixed-size sheet), the sheet is its writing's // width plus both margins — the writing's width alone left the writing // the sheet minus its margins, and it ran off the paper's edge. let width = proposal.width ?? Self.around( subviews.map { $0.sizeThatFits(.unspecified).width }.max() ?? 0, margin: margin) let inset = margin(width) // The content is measured against what is left of the proposal after // the margins, height included — so a content that is itself greedy // answers the full height and the sheet fills, and a content that is // three lines of type answers three lines. let inner = ProposedViewSize( width: max(0, width - (inset + inset)), height: Self.usable(proposal.height).map { max(0, $0 - (inset + inset)) } ) let tallest = subviews.map { $0.sizeThatFits(inner).height }.max() ?? 0 return CGSize( width: width, height: Self.stated( proposal: proposal.height, written: tallest + (inset + inset), fills: fills) ) } /// **The width of a sheet around writing `written` wide**: wide enough /// that what is left inside its margins holds the writing whole. The /// margin may grow with the sheet, so it is settled at the sheet's width. static func around(_ written: CGFloat, margin: (CGFloat) -> CGFloat) -> CGFloat { var width = written + 2 * margin(written) for _ in 0..<4 where width - 2 * margin(width) < written { width = written + 2 * margin(width) } return width } func placeSubviews( in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout () ) { let inset = margin(bounds.width) let inner = CGSize( width: max(0, bounds.width - (inset + inset)), height: max(0, bounds.height - (inset + inset)) ) for subview in subviews { subview.place( at: CGPoint(x: bounds.minX + inset, y: bounds.minY + inset), anchor: .topLeading, proposal: ProposedViewSize(inner) ) } } } /// The sizes, counts and timings Paper draws with: Swift's own, beside /// their reader — a theme sets only its tokens (`scale.tokens`). enum PaperKnobs { static let paperGrainMinStrength: CGFloat = 0.01 static let paperLiftHover: CGFloat = 2 static let paperLiftLiftedFarOpacity: CGFloat = 0.17 static let paperLiftLiftedFarRadius: CGFloat = 14 static let paperLiftLiftedFarX: CGFloat = 4 static let paperLiftLiftedFarY: CGFloat = 10 static let paperLiftLiftedNearOpacity: CGFloat = 0.18 static let paperLiftLiftedNearRadius: CGFloat = 2 static let paperLiftLiftedNearX: CGFloat = 1 static let paperLiftLiftedNearY: CGFloat = 2 static let paperLiftRestingFarOpacity: CGFloat = 0.15 static let paperLiftRestingFarRadius: CGFloat = 5 static let paperLiftRestingFarX: CGFloat = 1.5 static let paperLiftRestingFarY: CGFloat = 3.5 static let paperLiftRestingNearOpacity: CGFloat = 0.2 static let paperLiftRestingNearRadius: CGFloat = 1 static let paperLiftRestingNearX: CGFloat = 0.5 static let paperLiftRestingNearY: CGFloat = 1 }