// HOW FAR THE SIDE PAGE STANDS OPEN (the owner, 22 Nov 2026: the side page// pushes the list, it does not cover it). 0 is closed, 1 is open: the page// stands at the left, `width` wide, and the list beside it has moved right by// as much, its left edge still showing in the last fifth of the screen. A// finger moves it point for point (the edge swipe on the closed list, a drag// on the page, a drag on the list's edge); let go, it springs the rest of the// way, open past half way or after a flick that way. It springs by itself,// frame by frame, so a finger that takes hold while it moves catches it where// it is drawn and goes on from there. Android built it first; both follow// DECISIONS.md "The drawer" (a flick over 400 points a second, a spring of// stiffness 400 with no bounce, the list dimmed 6% at most).#if os(iOS)importQuartzCoreimportSwiftUIimport UIKit@MainActor@ObservablefinalclassNotesDrawer {/// How far open, 0…1, as drawn now.private(set) var shown: CGFloat = 0/// The page's width in points: the list moves right by this much at 1.var width: CGFloat = 312/// Under Reduce Motion it goes the rest of the way at once. @ObservationIgnored var reduceMotion = false @ObservationIgnored privatevar target: CGFloat = 0/// In widths a second. @ObservationIgnored privatevar speed: CGFloat = 0 @ObservationIgnored privatevar base: CGFloat = 0 @ObservationIgnored privatevar ticker: NotesTicker?/// The page's width for a screen `across` points wide: four fifths of it, so the list's/// edge shows in the last fifth (no wider than 380 on a wide screen).staticfunc width(across: CGFloat) -> CGFloat { min(380, (across * 0.8).rounded()) }/// Anything of the page is drawn.var visible: Bool { shown > 0.001 }/// A finger takes hold: it stops where it is drawn, mid-way too, and follows from there.func grab() { stop() base = shown speed = 0 }/// The finger has moved `dx` points across since it took hold.func follow(_ dx: CGFloat) { shown = min(1, max(0, base + dx / max(width, 1))) }/// The finger lets go, moving `vx` points a second across: open or closed, said back./// A flick (over 400 points a second) goes the way it was going; a slow let-go goes to the nearer end.func release(_ vx: CGFloat) -> Bool {letopen = abs(vx) > 400 ? vx > 0 : shown > 0.5 go(open, speed: vx / max(width, 1))returnopen }/// Go open or closed from where it is drawn, keeping the speed it has unless `speed` says one.func go(_ open: Bool, speed now: CGFloat? = nil) { target = open ? 1 : 0iflet now { speed = now }if reduceMotion { stop() shown = targetreturn }if shown == target, abs(speed) < 0.01 { stop()return }if ticker == nil { ticker = NotesTicker { [weakself] dt inself?.step(dt) } } }/// One frame of a spring with no bounce (stiffness 400, damped critically): about a quarter of a second.func step(_ dt: CGFloat) {let stiffness: CGFloat = 400let damping = 2 * stiffness.squareRoot()let dt = min(dt, 1.0 / 30) speed += (stiffness * (target - shown) - damping * speed) * dtvar next = shown + speed * dtif next <= 0 || next >= 1 { next = min(1, max(0, next)) speed = 0 } shown = nextif abs(target - shown) < 0.002, abs(speed) < 0.05 { shown = target stop() } }privatefunc stop() { ticker?.stop() ticker = nil }}/// The screen's own frames, for the spring: each frame says how long since the last.@MainActorprivatefinalclassNotesTicker: NSObject {privatevar link: CADisplayLink?privatevar last: CFTimeInterval = 0privatelet tick: (CGFloat) -> Voidinit(_ tick: @escaping (CGFloat) -> Void) {self.tick = ticksuper.init()let link = CADisplayLink(target: self, selector: #selector(frame(_:))) link.add(to: .main, forMode: .common)self.link = link } @objc privatefunc frame(_ link: CADisplayLink) {let now = link.targetTimestamplet dt = last == 0 ? 1.0 / 60 : now - last last = now tick(CGFloat(dt)) }func stop() { link?.invalidate() link = nil }}/// The list's edge while the side page stands open: a tap on it closes the page, a drag on it moves the/// page with the finger (and catches it mid-way). VoiceOver reads it as Close the side page.structNotesListEdge: View { @Bindable var model: NotesModel @State privatevar holding = falsevar body: someView {Color.black.opacity(0.06 * model.drawer.shown) .contentShape(Rectangle()) .gesture(DragGesture(minimumDistance: 0, coordinateSpace: .global) .onChanged { drag inif !holding { holding = true model.drawer.grab() } model.drawer.follow(drag.translation.width) } .onEnded { drag in holding = falselet moved = hypot(drag.translation.width, drag.translation.height)// A tap (a touch that hardly moved) closes it; a drag goes where it was let go. model.letGoOfSide(moved < 10 ? nil : drag.velocity.width) } ) .ignoresSafeArea() .accessibilityElement() .accessibilityLabel(Text(verbatim: notesLine("side.close"))) .accessibilityAddTraits(.isButton) .accessibilityAction { model.sideOpen = false } .accessibilityIdentifier("side-edge") }}#endif