Commit b170d6099b

b170d6099be802f9dc967ac41d6eef79ea79012f

parent: 02dab02f24

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-20 08:17 UTC

Suspend a one-finger drag when a two-finger gesture begins

Ref #6

Layout: unified · split

App/Sources/FoldGesture.swift +7 −2
@@ -24,19 +24,24 @@ private struct FoldGesture: ViewModifier {
2424
2525 func body(content: Content) -> some View {
2626 let folds = session.step.kind == .fold || session.step.kind == .foldAndUnfold
27 content.gesture(drag, isEnabled: folds)
27 content
28 .gesture(drag, isEnabled: folds)
29 .onChange(of: grab == nil) { _, ended in if ended { session.cancelDrag() } }
2830 }
2931
32 /// A two-finger gesture during the drag suspends it (`FoldSession.beginOrbit()`).
3033 private var drag: some Gesture {
3134 DragGesture()
3235 .updating($grab) { value, grab, _ in
36 if grab == nil { session.beginDrag() }
3337 let start = grab ?? Grab(mapping: session.mapping, point: session.mapping.point(at: session.t), t: session.t)
3438 grab = start
39 guard !session.isDragSuspended else { return }
3540 session.t = start.mapping.parameter(
3641 grabbedAt: start.point,
3742 translation: SIMD2(value.translation.width, value.translation.height),
3843 current: start.mapping.isDegenerate ? start.t : session.t)
3944 }
40 .onEnded { _ in session.completeStep() }
45 .onEnded { _ in session.endDrag() }
4146 }
4247}
App/Sources/FoldSession.swift +40
@@ -88,6 +88,46 @@ final class FoldSession {
8888 t = 0
8989 }
9090
91 /// `t` when the current one-finger drag began, while one is in progress.
92 @ObservationIgnored private var dragStart: Double?
93 /// Two-finger gestures in progress.
94 @ObservationIgnored private var orbits = 0
95 /// Whether a two-finger gesture began during the current drag. The drag then leaves `t` as it
96 /// was when the drag began until the finger lifts, and does not complete the step.
97 @ObservationIgnored private(set) var isDragSuspended = false
98
99 /// Called when a one-finger drag that drives `t` begins.
100 func beginDrag() {
101 dragStart = t
102 isDragSuspended = orbits > 0
103 }
104
105 /// Called when the drag's finger lifts: completes the step unless the drag was suspended.
106 func endDrag() {
107 if !isDragSuspended { completeStep() }
108 cancelDrag()
109 }
110
111 /// Called when the drag ends by any means, including cancellation.
112 func cancelDrag() {
113 dragStart = nil
114 isDragSuspended = false
115 }
116
117 /// Called when a two-finger gesture begins. A drag in progress is suspended and its effect on
118 /// `t` undone.
119 func beginOrbit() {
120 orbits += 1
121 if let dragStart {
122 t = dragStart
123 isDragSuspended = true
124 }
125 }
126
127 func endOrbit() {
128 orbits = max(orbits - 1, 0)
129 }
130
91131 /// Turns the view of the model by `angles` radians, as `Camera.orbited(by:)` does.
92132 func orbit(by angles: SIMD2<Double>) {
93133 let turned = camera.orbited(by: angles)
App/Sources/OrbitGesture.swift +19 −4
@@ -4,7 +4,8 @@ import UIKit
44
55extension View {
66 /// Lets a two-finger drag on the view orbit the session's camera around the model, and a pinch
7 /// change its field of view. Neither changes `t` or the diagram.
7 /// change its field of view. Neither changes `t` or the diagram: either one beginning suspends
8 /// a one-finger drag in progress and undoes what it did to `t`.
89 func orbitGesture(_ session: FoldSession) -> some View {
910 modifier(OrbitGesture(session: session))
1011 }
@@ -17,14 +18,14 @@ struct OrbitGesture: ViewModifier {
1718
1819 func body(content: Content) -> some View {
1920 content
20 .gesture(TwoFingerPan { translation in
21 .gesture(TwoFingerPan(session: session) { translation in
2122 session.orbit(by: SIMD2(translation.x, translation.y) * Self.radiansPerPoint)
2223 })
23 .gesture(Pinch { scale in session.zoom(by: scale) })
24 .gesture(Pinch(session: session) { scale in session.zoom(by: scale) })
2425 }
2526}
2627
27/// The names of the two recognizers, which may recognize together but with nothing else.
28/// The names of the two recognizers, which may recognize together.
2829private let orbitRecognizers: Set<String> = ["orbit.pan", "orbit.pinch"]
2930
3031private final class Simultaneous: NSObject, UIGestureRecognizerDelegate {
@@ -34,8 +35,19 @@ private final class Simultaneous: NSObject, UIGestureRecognizerDelegate {
3435 }
3536}
3637
38/// Tells the session when the recognizer begins and ends.
39@MainActor
40private func track(_ state: UIGestureRecognizer.State, _ session: FoldSession) {
41 switch state {
42 case .began: session.beginOrbit()
43 case .ended, .cancelled, .failed: session.endOrbit()
44 default: break
45 }
46}
47
3748/// Reports a two-finger drag's translation in points since the last report.
3849private struct TwoFingerPan: UIGestureRecognizerRepresentable {
50 let session: FoldSession
3951 let changed: (CGPoint) -> Void
4052
4153 func makeCoordinator(converter: CoordinateSpaceConverter) -> Simultaneous { Simultaneous() }
@@ -50,6 +62,7 @@ private struct TwoFingerPan: UIGestureRecognizerRepresentable {
5062 }
5163
5264 func handleUIGestureRecognizerAction(_ recognizer: UIPanGestureRecognizer, context: Context) {
65 track(recognizer.state, session)
5366 guard recognizer.state == .began || recognizer.state == .changed else { return }
5467 let translation = recognizer.translation(in: recognizer.view)
5568 recognizer.setTranslation(.zero, in: recognizer.view)
@@ -59,6 +72,7 @@ private struct TwoFingerPan: UIGestureRecognizerRepresentable {
5972
6073/// Reports a pinch's scale since the last report.
6174private struct Pinch: UIGestureRecognizerRepresentable {
75 let session: FoldSession
6276 let changed: (Double) -> Void
6377
6478 func makeCoordinator(converter: CoordinateSpaceConverter) -> Simultaneous { Simultaneous() }
@@ -71,6 +85,7 @@ private struct Pinch: UIGestureRecognizerRepresentable {
7185 }
7286
7387 func handleUIGestureRecognizerAction(_ recognizer: UIPinchGestureRecognizer, context: Context) {
88 track(recognizer.state, session)
7489 guard recognizer.state == .began || recognizer.state == .changed else { return }
7590 let scale = recognizer.scale
7691 recognizer.scale = 1
App/Sources/ReorientGesture.swift +8 −3
@@ -23,21 +23,26 @@ struct ReorientGesture: ViewModifier {
2323
2424 func body(content: Content) -> some View {
2525 let reorients = session.step.kind == .turnOver || session.step.kind == .rotate
26 content.gesture(drag, isEnabled: reorients)
26 content
27 .gesture(drag, isEnabled: reorients)
28 .onChange(of: grab == nil) { _, ended in if ended { session.cancelDrag() } }
2729 }
2830
2931 /// The width is measured on the frame the step starts from, which a vertical turn-over would
30 /// otherwise shrink to an edge midway.
32 /// otherwise shrink to an edge midway. A two-finger gesture during the drag suspends it
33 /// (`FoldSession.beginOrbit()`).
3134 private var drag: some Gesture {
3235 DragGesture()
3336 .updating($grab) { value, grab, _ in
37 if grab == nil { session.beginDrag() }
3438 let start = grab ?? Grab(
3539 mapping: ReorientMapping(positions: session.interpolator.positions(at: 0), camera: session.camera,
3640 viewportSize: session.viewportSize),
3741 t: session.t)
3842 grab = start
43 guard !session.isDragSuspended else { return }
3944 session.t = start.mapping.parameter(translation: value.translation.width, current: start.t)
4045 }
41 .onEnded { _ in session.completeStep() }
46 .onEnded { _ in session.endDrag() }
4247 }
4348}
App/Tests/FoldSessionTests.swift +46
@@ -68,3 +68,49 @@ private func cup(step: Int = 1) throws -> FoldSession {
6868 #expect(session.eyeDirection == SIMD3(0, 0, 1))
6969 #expect(session.camera.fieldOfView == start.fieldOfView)
7070}
71
72@MainActor @Test func anOrbitDuringADragRestoresTAndTheDragDoesNotComplete() throws {
73 let session = try cup()
74 session.t = 0.2
75 session.beginDrag()
76 session.t = 1
77 session.beginOrbit()
78 #expect(session.t == 0.2)
79 #expect(session.isDragSuspended)
80 session.endOrbit()
81 #expect(session.isDragSuspended)
82 session.endDrag()
83 #expect(session.step.index == 1)
84 #expect(session.t == 0.2)
85 #expect(!session.isDragSuspended)
86}
87
88@MainActor @Test func aDragThatStartsDuringAnOrbitIsSuspended() throws {
89 let session = try cup()
90 session.beginOrbit()
91 session.beginDrag()
92 #expect(session.isDragSuspended)
93 session.endOrbit()
94 session.endDrag()
95 #expect(session.step.index == 1)
96}
97
98@MainActor @Test func aDragWithoutAnOrbitCompletesAtTheEnd() throws {
99 let session = try cup()
100 session.beginDrag()
101 session.t = 1
102 session.endDrag()
103 #expect(session.step.index == 2)
104}
105
106@MainActor @Test func anOrbitAfterACancelledDragLeavesTAlone() throws {
107 let session = try cup()
108 session.t = 0.2
109 session.beginDrag()
110 session.t = 0.6
111 session.cancelDrag()
112 session.beginOrbit()
113 #expect(session.t == 0.6)
114 #expect(!session.isDragSuspended)
115 session.endOrbit()
116}
App/UITests/AccessibilityAuditTests.swift +9 −3
@@ -44,12 +44,16 @@ final class AccessibilityAuditTests: XCTestCase {
4444 }
4545
4646 func testDiagramDoesNotChangeWhenTheModelViewChanges() {
47 let app = XCUIApplication(); app.launch()
47 let app = XCUIApplication(); app.launchArguments = ["-UITesting"]; app.launch()
4848 app.buttons["Cup"].tap()
4949 let diagram = app.descendants(matching: .any)["diagram"]
50 let camera = app.descendants(matching: .any)["paper.camera"]
5051 XCTAssertTrue(diagram.waitForExistence(timeout: 5))
52 XCTAssertTrue(camera.waitForExistence(timeout: 5))
5153 let before = diagram.screenshot().pngRepresentation
54 let cameraBefore = camera.value as? String
5255 twoFingerGestures(app.otherElements["fold.paper"])
56 XCTAssertNotEqual(camera.value as? String, cameraBefore)
5357 XCTAssertEqual(diagram.screenshot().pngRepresentation, before)
5458 }
5559
@@ -76,6 +80,8 @@ final class AccessibilityAuditTests: XCTestCase {
7680 }
7781 if index.label == step { break }
7882 }
83 // The cup has six steps; stopping earlier means an advance failed.
84 XCTAssertEqual(index.label, "6")
7985 }
8086
8187 private func audit(_ app: XCUIApplication, _ name: String) throws {
@@ -117,8 +123,8 @@ final class AccessibilityAuditTests: XCTestCase {
117123 if sizeName == "default", issue.auditType == .dynamicType { return true }
118124 // Contrast is measured from pixels, so text partly off screen measures wrong.
119125 if issue.auditType == .contrast, let element = issue.element, !screen.contains(element.frame) { return true }
120 // The same for text partly scrolled out of its scroll view, as a long caption is at the largest size.
121 if issue.auditType == .contrast, let element = issue.element,
126 // The same for the caption, which at the largest size runs past the bottom of its scroll view.
127 if issue.auditType == .contrast, let element = issue.element, element.identifier == "diagram.caption",
122128 scrollFrames.contains(where: { $0.intersects(element.frame) && !$0.contains(element.frame) }) {
123129 return true
124130 }