// The press a row wears — and why it is a `Button`. // // THE SCROLLER OWNS THE FINGER. A `DragGesture(minimumDistance: 0)` on a row // claims a touch the moment it lands, before the scroll view above it has seen // any movement, and a `ScrollView` whose rows all do that cannot be scrolled // from a row: the finger has to start on whitespace, or on a paragraph, for the // list to move. The owner reported exactly that shape on every inner screen — // "cannot reach the bottom unless I scroll to the top and reverse", "cannot // scroll back up" — and it reproduced on the simulator on the project page: a // swipe from the summary text scrolls, the same swipe from a row does nothing // (lane I6, 2026-09-11). `RecordRowPress.isTap` measured the travel and let go // of the ROW's action, but by then the scroller had already lost the drag. // // A `Button` is what the platform scrolls around. UIKit delays a button's // touch inside a scroll view and hands the finger to the scroller on the first // ten points of travel, and SwiftUI's `Button` inherits that. So every row is // a `Button` with this style — the same 0.6 press dim the rows always had — // and NEVER a gesture that competes with the pan. // // A LONG PRESS IS STILL NOT A TAP. The hold is a `highPriorityGesture`: when it // succeeds it takes the touch and the button's action does not fire on the // release, so a row that opened its menu never also navigates. When it fails — // the finger lifted early, or moved — the button gets the tap, or the scroller // the drag. `ScrollingSweepTests` holds that no zero-distance drag comes back. // // ## A DRAG IS NOT A TAP EITHER, AND A BUTTON ALONE CANNOT TELL // // A button lets go of its press when the finger leaves its BOUNDS. A row is // 52pt tall and the full width of the card, so a drag DOWN leaves it after a // few points and the press is cancelled — but a drag ACROSS never leaves it at // all. Nothing else was cancelling that press either: UIKit hands a button's // touch to the scroll view when the scroller's pan begins, and a VERTICAL // scroll view never claims a SIDEWAYS drag. So a finger dragged 260pt across a // Home row released as a tap and opened the row — the owner's report after // TestFlight 145, reproduced on the simulator: a left-to-right drag on the // "Quillby" row opened the project, a right-to-left drag on "6 thoughts to // file" opened Thoughts. // // ## THE TRAVEL IS WATCHED, NEVER CLAIMED — AND THAT IS WHY IT IS NOT A GESTURE // // The obvious fix is a `DragGesture(minimumDistance: 10)` as a // `.simultaneousGesture` on the row. **It was built, and it was measured, and // it re-broke scrolling from a row** — Home would not scroll at all from a row // while it scrolled fine from the title above it, which is lane I6's bug // wearing a bigger minimum distance. "Simultaneous" combines a SwiftUI gesture // with other SwiftUI gestures; against UIKit's scroll pan a SwiftUI drag that // recognises still takes the finger. **There is no passive `DragGesture`.** // // So the travel is read where it can be read without asking for it: a // `UIGestureRecognizer` on the WINDOW that never recognises. It is attached // once, by the root (`cottonWatchesTheFinger()`), it never delays a touch, // never cancels one, allows every other recogniser, and stays in `.possible` // until it fails — so nothing in the app can tell it is there. It only writes // down how far the finger that is down right now has moved, and the button's // action reads that before it fires. // // That also makes the clearing trivial, which two earlier attempts were not: a // `@GestureState` flag cleared itself the instant the finger lifted (so the // action read it as already down), and a flag cleared from a button style's // `isPressed` edge never ran at all (so a dragged row went dead to every later // tap). The travel is reset when the NEXT touch begins — one unambiguous // moment, long before any action can read it. // // THE MAC HAS NO SUCH RULE. A pointer press is not a finger on a scroller and // AppKit ends a click that leaves the control; the watcher is iOS-only and the // travel on macOS is always "none". import SwiftUI #if os(iOS) import UIKit #endif /// How long a finger must rest before the press is a hold rather than a tap, /// and how far it may travel and still be a tap. public enum PressRule { public static let longPressSeconds: TimeInterval = 0.5 /// The travel that turns a press into a drag, in points. /// /// **Ten, because that is iOS's own scroll threshold.** Higher and the /// scroller has already taken the finger while the row still thinks it owns /// it; lower and an ordinary thumb tap — which always slides a little — /// stops opening rows. public static let slop: CGFloat = 10 } /// What the finger that is down right now has done: whether it has travelled /// further than a tap. /// /// One value for the whole app, because it is a fact about the TOUCH and not /// about any row — the row that reads it is whichever row the touch is over. @MainActor final class FingerTravel { static let shared = FingerTravel() /// True once the live touch has moved past `PressRule.slop`. Set by the /// watcher below, cleared when the next touch begins, and read by /// `TravellingPress` at the moment the button would act. fileprivate(set) var hasTravelled = false /// When the live (or last) touch came down, first moved past the slop, /// and whether it is down now: the ink reads a hold from the first two /// and waits for a letter's next stroke on the third (`InkGround`). fileprivate(set) var downAt: Date? fileprivate(set) var travelledAt: Date? fileprivate(set) var isDown = false private init() {} #if os(iOS) fileprivate func began() { hasTravelled = false downAt = .now travelledAt = nil isDown = true } fileprivate func travelled() { if !hasTravelled { travelledAt = .now } hasTravelled = true } fileprivate func lifted() { isDown = false } #endif } /// The press every row-shaped control in the app wears: the platform's own /// `Button`, plus the travel rule this file's header explains. /// /// It is a view rather than a modifier because the travel has to gate the /// button's ACTION, and a modifier cannot reach inside one. `style` is the /// caller's own press feel — the rows dim, the record rows dim the same 0.6. struct TravellingPress: View { let style: Style let action: (() -> Void)? @ViewBuilder let label: () -> Label var body: some View { Button { // The release of a DRAG is not a tap. Everything else about the // button is unchanged: the platform still delays the touch, still // hands it to the scroller on a vertical drag, still reads as a // button to VoiceOver. guard !FingerTravel.shared.hasTravelled else { return } action?() } label: { label() } .buttonStyle(style) } } /// A press with no look of its own: the label draws exactly as it is. The /// record slip wears it under `TravellingPress`, because its label holds /// buttons of its own (a todo's check, an invitation's answer) that keep /// their own taps, and the slip itself must not dim. struct BareButtonStyle: ButtonStyle { func makeBody(configuration: Configuration) -> some View { configuration.label } } extension View { /// Watch how far each touch travels, for the whole app, so a row can tell a /// tap from a drag without taking the drag from the scroller. /// /// **The root wears this and nothing else does** — it attaches ONE /// recogniser to the window, and every `TravellingPress` in the app reads /// what it wrote. A screen that is missing it does not misbehave visibly: /// its rows simply go back to opening on a sideways drag, which is why /// `ScrollingSweepTests` holds that the root still applies it. public func cottonWatchesTheFinger() -> some View { #if os(iOS) background(FingerWatcherInstaller().allowsHitTesting(false)) #else self #endif } } #if os(iOS) /// Puts the watcher on the window the app is living in. /// /// The window, not this view: a recogniser only sees touches that land in /// its own view, and the rows are drawn by SwiftUI's hosting view, not by /// this one. The window is the root of every hit test in the app — sheets /// and covers included — so one recogniser there sees every finger. private struct FingerWatcherInstaller: PlatformViewRepresentable { func makeUIView(context: Context) -> UIView { WatcherAnchor() } func updateUIView(_ view: UIView, context: Context) {} } private final class WatcherAnchor: UIView { override init(frame: CGRect) { super.init(frame: frame) isUserInteractionEnabled = false } @available(*, unavailable) required init?(coder: NSCoder) { fatalError("not from a nib") } override func didMoveToWindow() { super.didMoveToWindow() guard let window else { return } // Idempotent: the root can be rebuilt, and a second watcher would // be harmless but is still two answers to one question. guard window.gestureRecognizers?.contains(where: { $0 is FingerWatcher }) != true else { return } window.addGestureRecognizer(FingerWatcher()) } } /// A recogniser that never recognises: it reads the finger and gets out of /// the way of everything that wants to act on it. private final class FingerWatcher: UIGestureRecognizer, UIGestureRecognizerDelegate { /// The finger being watched — the FIRST one down. A second finger /// landing on a list is not a new tap and must not clear the travel the /// first one has already made. private weak var finger: UITouch? private var origin: CGPoint = .zero override init(target: Any?, action: Selector?) { super.init(target: target, action: action) // Invisible to everything else: it swallows nothing, delays // nothing, and refuses no other recogniser. cancelsTouchesInView = false delaysTouchesBegan = false delaysTouchesEnded = false delegate = self } override func touchesBegan(_ touches: Set, with event: UIEvent) { guard finger == nil, let touch = touches.first else { return } finger = touch // `nil` is the window's own coordinate space, which is the one // space every row in the app shares. origin = touch.location(in: nil) FingerTravel.shared.began() } override func touchesMoved(_ touches: Set, with event: UIEvent) { guard let finger, touches.contains(finger) else { return } let now = finger.location(in: nil) if hypot(now.x - origin.x, now.y - origin.y) > PressRule.slop { FingerTravel.shared.travelled() } } override func touchesEnded(_ touches: Set, with event: UIEvent) { finish(touches) } override func touchesCancelled(_ touches: Set, with event: UIEvent) { finish(touches) } /// The travel is deliberately NOT cleared here. The button's action is /// delivered inside the same lift this method sees, and clearing it now /// would race that — which is exactly how the first two attempts at this /// rule failed. It is cleared when the next touch begins. private func finish(_ touches: Set) { guard let finger, touches.contains(finger) else { return } self.finger = nil FingerTravel.shared.lifted() // Failing is how a recogniser that never recognises gets reset for // the next touch; a failure cancels nothing and requires nothing. state = .failed } func gestureRecognizer( _ recogniser: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith other: UIGestureRecognizer ) -> Bool { true } func gestureRecognizer( _ recogniser: UIGestureRecognizer, shouldRequireFailureOf other: UIGestureRecognizer ) -> Bool { false } func gestureRecognizer( _ recogniser: UIGestureRecognizer, shouldBeRequiredToFailBy other: UIGestureRecognizer ) -> Bool { false } } #endif