// 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) import QuartzCore import SwiftUI import UIKit @MainActor @Observable final class NotesDrawer { /// 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 private var target: CGFloat = 0 /// In widths a second. @ObservationIgnored private var speed: CGFloat = 0 @ObservationIgnored private var base: CGFloat = 0 @ObservationIgnored private var 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). static func 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 { let open = abs(vx) > 400 ? vx > 0 : shown > 0.5 go(open, speed: vx / max(width, 1)) return open } /// 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 : 0 if let now { speed = now } if reduceMotion { stop() shown = target return } if shown == target, abs(speed) < 0.01 { stop() return } if ticker == nil { ticker = NotesTicker { [weak self] dt in self?.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 = 400 let damping = 2 * stiffness.squareRoot() let dt = min(dt, 1.0 / 30) speed += (stiffness * (target - shown) - damping * speed) * dt var next = shown + speed * dt if next <= 0 || next >= 1 { next = min(1, max(0, next)) speed = 0 } shown = next if abs(target - shown) < 0.002, abs(speed) < 0.05 { shown = target stop() } } private func stop() { ticker?.stop() ticker = nil } } /// The screen's own frames, for the spring: each frame says how long since the last. @MainActor private final class NotesTicker: NSObject { private var link: CADisplayLink? private var last: CFTimeInterval = 0 private let tick: (CGFloat) -> Void init(_ tick: @escaping (CGFloat) -> Void) { self.tick = tick super.init() let link = CADisplayLink(target: self, selector: #selector(frame(_:))) link.add(to: .main, forMode: .common) self.link = link } @objc private func frame(_ link: CADisplayLink) { let now = link.targetTimestamp let 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. struct NotesListEdge: View { @Bindable var model: NotesModel @State private var holding = false var body: some View { Color.black.opacity(0.06 * model.drawer.shown) .contentShape(Rectangle()) .gesture( DragGesture(minimumDistance: 0, coordinateSpace: .global) .onChanged { drag in if !holding { holding = true model.drawer.grab() } model.drawer.follow(drag.translation.width) } .onEnded { drag in holding = false let 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