Commit a2c75668f8

a2c75668f89dc954b9c514605810e28598023300

parent: fc04501554

Unsigned

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

Apply the release position when a drag ends

Ref #6

Layout: unified · split

App/Sources/FoldGesture.swift +7 −20
@@ -1,5 +1,4 @@
11import FoldFormat
2import Folding
32import SwiftUI
43
54extension View {
@@ -13,35 +12,23 @@ extension View {
1312
1413private struct FoldGesture: ViewModifier {
1514 let session: FoldSession
16 @GestureState private var grab: Grab?
17
18 /// The mapping, the moving faces' screen point and t when the drag began.
19 private struct Grab {
20 let mapping: FoldParameterMapping
21 let point: SIMD2<Double>
22 let t: Double
23 }
15 @GestureState private var isDragging = false
2416
2517 func body(content: Content) -> some View {
2618 let folds = session.step.kind == .fold || session.step.kind == .foldAndUnfold
2719 content
2820 .gesture(drag, isEnabled: folds)
29 .onChange(of: grab == nil) { _, ended in if ended { session.cancelDrag() } }
21 .onChange(of: isDragging) { _, dragging in if !dragging { session.cancelDrag() } }
3022 }
3123
3224 /// A two-finger gesture during the drag suspends it (`FoldSession.beginOrbit()`).
3325 private var drag: some Gesture {
3426 DragGesture()
35 .updating($grab) { value, grab, _ in
36 if grab == nil { session.beginDrag() }
37 let start = grab ?? Grab(mapping: session.mapping, point: session.mapping.point(at: session.t), t: session.t)
38 grab = start
39 guard !session.isDragSuspended else { return }
40 session.t = start.mapping.parameter(
41 grabbedAt: start.point,
42 translation: SIMD2(value.translation.width, value.translation.height),
43 current: start.mapping.isDegenerate ? start.t : session.t)
27 .updating($isDragging) { value, isDragging, _ in
28 if !isDragging { session.beginFoldDrag() }
29 isDragging = true
30 session.drag(translation: SIMD2(value.translation.width, value.translation.height))
4431 }
45 .onEnded { _ in session.endDrag() }
32 .onEnded { value in session.endDrag(translation: SIMD2(value.translation.width, value.translation.height)) }
4633 }
4734}
App/Sources/FoldSession.swift +44 −10
@@ -88,29 +88,63 @@ 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?
91 /// What maps a one-finger drag's translation to `t`.
92 private enum DragMapping {
93 case fold(FoldParameterMapping, grabbedAt: SIMD2<Double>)
94 case reorient(ReorientMapping)
95 }
96
97 /// The current one-finger drag's mapping and `t` when it began, while one is in progress.
98 @ObservationIgnored private var grab: (mapping: DragMapping, start: Double)?
9399 /// Two-finger gestures in progress.
94100 @ObservationIgnored private var orbits = 0
95101 /// Whether a two-finger gesture began during the current drag. The drag then leaves `t` as it
96102 /// was when the drag began until the finger lifts, and does not complete the step.
97103 @ObservationIgnored private(set) var isDragSuspended = false
98104
99 /// Called when a one-finger drag that drives `t` begins.
100 func beginDrag() {
101 dragStart = t
105 /// Called when a one-finger drag on a fold step begins: the moving faces then follow the
106 /// finger along `mapping`'s path.
107 func beginFoldDrag() {
108 let mapping = self.mapping
109 beginDrag(.fold(mapping, grabbedAt: mapping.point(at: t)))
110 }
111
112 /// Called when a one-finger drag on a turn-over or rotate step begins: its horizontal
113 /// component then turns the model across its on-screen width, measured on the frame the step
114 /// starts from, which a vertical turn-over would otherwise shrink to an edge midway.
115 func beginReorientDrag() {
116 beginDrag(.reorient(ReorientMapping(positions: interpolator.positions(at: 0), camera: camera,
117 viewportSize: viewportSize)))
118 }
119
120 private func beginDrag(_ mapping: DragMapping) {
121 grab = (mapping, t)
102122 isDragSuspended = orbits > 0
103123 }
104124
105 /// Called when the drag's finger lifts: completes the step unless the drag was suspended.
106 func endDrag() {
125 /// Moves `t` to where the drag's `translation` maps it, unless the drag is suspended.
126 func drag(translation: SIMD2<Double>) {
127 guard let grab, !isDragSuspended else { return }
128 switch grab.mapping {
129 case let .fold(mapping, grabbedAt):
130 t = mapping.parameter(grabbedAt: grabbedAt, translation: translation,
131 current: mapping.isDegenerate ? grab.start : t)
132 case let .reorient(mapping):
133 t = mapping.parameter(translation: translation.x, current: grab.start)
134 }
135 }
136
137 /// Called when the drag's finger lifts at `translation`: moves `t` there as `drag(translation:)`
138 /// does, then completes the step unless the drag was suspended.
139 func endDrag(translation: SIMD2<Double>) {
140 drag(translation: translation)
107141 if !isDragSuspended { completeStep() }
108142 cancelDrag()
109143 }
110144
111145 /// Called when the drag ends by any means, including cancellation.
112146 func cancelDrag() {
113 dragStart = nil
147 grab = nil
114148 isDragSuspended = false
115149 }
116150
@@ -118,8 +152,8 @@ final class FoldSession {
118152 /// `t` undone.
119153 func beginOrbit() {
120154 orbits += 1
121 if let dragStart {
122 t = dragStart
155 if let grab {
156 t = grab.start
123157 isDragSuspended = true
124158 }
125159 }
App/Sources/ReorientGesture.swift +8 −22
@@ -1,5 +1,4 @@
11import FoldFormat
2import Folding
32import SwiftUI
43
54extension View {
@@ -13,36 +12,23 @@ extension View {
1312
1413struct ReorientGesture: ViewModifier {
1514 let session: FoldSession
16 @GestureState private var grab: Grab?
17
18 /// The mapping and t when the drag began.
19 private struct Grab {
20 let mapping: ReorientMapping
21 let t: Double
22 }
15 @GestureState private var isDragging = false
2316
2417 func body(content: Content) -> some View {
2518 let reorients = session.step.kind == .turnOver || session.step.kind == .rotate
2619 content
2720 .gesture(drag, isEnabled: reorients)
28 .onChange(of: grab == nil) { _, ended in if ended { session.cancelDrag() } }
21 .onChange(of: isDragging) { _, dragging in if !dragging { session.cancelDrag() } }
2922 }
3023
31 /// The width is measured on the frame the step starts from, which a vertical turn-over would
32 /// otherwise shrink to an edge midway. A two-finger gesture during the drag suspends it
33 /// (`FoldSession.beginOrbit()`).
24 /// A two-finger gesture during the drag suspends it (`FoldSession.beginOrbit()`).
3425 private var drag: some Gesture {
3526 DragGesture()
36 .updating($grab) { value, grab, _ in
37 if grab == nil { session.beginDrag() }
38 let start = grab ?? Grab(
39 mapping: ReorientMapping(positions: session.interpolator.positions(at: 0), camera: session.camera,
40 viewportSize: session.viewportSize),
41 t: session.t)
42 grab = start
43 guard !session.isDragSuspended else { return }
44 session.t = start.mapping.parameter(translation: value.translation.width, current: start.t)
27 .updating($isDragging) { value, isDragging, _ in
28 if !isDragging { session.beginReorientDrag() }
29 isDragging = true
30 session.drag(translation: SIMD2(value.translation.width, value.translation.height))
4531 }
46 .onEnded { _ in session.endDrag() }
32 .onEnded { value in session.endDrag(translation: SIMD2(value.translation.width, value.translation.height)) }
4733 }
4834}
App/Tests/FoldSessionTests.swift +61 −15
@@ -1,6 +1,7 @@
11import FoldFormat
22import Models
33import Testing
4import simd
45@testable import Fold
56
67@MainActor
@@ -69,48 +70,93 @@ private func cup(step: Int = 1) throws -> FoldSession {
6970 #expect(session.camera.fieldOfView == start.fieldOfView)
7071}
7172
73/// The translation that takes a fold drag begun at `start` to `t` along the session's path, and
74/// 30 points past its end when `t` is 1.
75@MainActor
76private func translation(_ session: FoldSession, from start: Double = 0, to t: Double) -> SIMD2<Double> {
77 let m = session.mapping
78 let past = t == 1 ? simd_normalize(m.point(at: 1) - m.point(at: 127.0 / 128)) * 30 : .zero
79 return m.point(at: t) - m.point(at: start) + past
80}
81
82@MainActor
83private func cupInView(step: Int = 1) throws -> FoldSession {
84 let session = try cup(step: step)
85 session.viewportSize = SIMD2(400, 400)
86 return session
87}
88
7289@MainActor @Test func anOrbitDuringADragRestoresTAndTheDragDoesNotComplete() throws {
73 let session = try cup()
90 let session = try cupInView()
7491 session.t = 0.2
75 session.beginDrag()
76 session.t = 1
92 session.beginFoldDrag()
93 session.drag(translation: translation(session, from: 0.2, to: 1))
94 #expect(session.t == 1)
7795 session.beginOrbit()
7896 #expect(session.t == 0.2)
7997 #expect(session.isDragSuspended)
8098 session.endOrbit()
8199 #expect(session.isDragSuspended)
82 session.endDrag()
100 session.endDrag(translation: translation(session, from: 0.2, to: 1))
83101 #expect(session.step.index == 1)
84102 #expect(session.t == 0.2)
85103 #expect(!session.isDragSuspended)
86104}
87105
88106@MainActor @Test func aDragThatStartsDuringAnOrbitIsSuspended() throws {
89 let session = try cup()
107 let session = try cupInView()
90108 session.beginOrbit()
91 session.beginDrag()
109 session.beginFoldDrag()
92110 #expect(session.isDragSuspended)
93111 session.endOrbit()
94 session.endDrag()
112 session.endDrag(translation: translation(session, to: 1))
95113 #expect(session.step.index == 1)
114 #expect(session.t == 0)
96115}
97116
98117@MainActor @Test func aDragWithoutAnOrbitCompletesAtTheEnd() throws {
99 let session = try cup()
100 session.beginDrag()
101 session.t = 1
102 session.endDrag()
118 let session = try cupInView()
119 session.beginFoldDrag()
120 session.drag(translation: translation(session, to: 1))
121 session.endDrag(translation: translation(session, to: 1))
122 #expect(session.step.index == 2)
123}
124
125@MainActor @Test func theReleasePositionIsAppliedBeforeTheLift() throws {
126 // the last movement can arrive only with the lift
127 let session = try cupInView()
128 session.beginFoldDrag()
129 session.drag(translation: translation(session, to: 0.5))
130 #expect(abs(session.t - 0.5) < 0.01)
131 session.endDrag(translation: translation(session, to: 1))
103132 #expect(session.step.index == 2)
133 #expect(session.t == 0)
134}
135
136@MainActor @Test func aReleaseShortOfTheEndLeavesTWhereTheFingerLifted() throws {
137 let session = try cupInView()
138 session.beginFoldDrag()
139 session.endDrag(translation: translation(session, to: 0.5))
140 #expect(session.step.index == 1)
141 #expect(abs(session.t - 0.5) < 0.01)
142}
143
144@MainActor @Test func aTurnOverReleasedPastTheModelsWidthCompletes() throws {
145 let session = try cupInView(step: 5)
146 #expect(session.step.kind == .turnOver)
147 session.beginReorientDrag()
148 session.endDrag(translation: SIMD2(1000, 0))
149 #expect(session.step.index == 6)
104150}
105151
106152@MainActor @Test func anOrbitAfterACancelledDragLeavesTAlone() throws {
107 let session = try cup()
153 let session = try cupInView()
108154 session.t = 0.2
109 session.beginDrag()
110 session.t = 0.6
155 session.beginFoldDrag()
156 session.drag(translation: translation(session, from: 0.2, to: 0.6))
111157 session.cancelDrag()
112158 session.beginOrbit()
113 #expect(session.t == 0.6)
159 #expect(abs(session.t - 0.6) < 0.01)
114160 #expect(!session.isDragSuspended)
115161 session.endOrbit()
116162}
App/UITests/FoldGestureTests.swift +25 −7
@@ -31,8 +31,9 @@ func completeCurrentFold(_ app: XCUIApplication) {
3131
3232/// Drags the current step's moving faces along their path, read from `fold.path` (the app must be
3333/// launched with `-UITesting`), to `fraction` of it, then lifts. Each drag is one straight
34/// press-and-drag through a quarter of the path's samples; a drag to the end continues 30 points
35/// past it.
34/// press-and-drag through every 32nd sample, with short ones merged so each moves at least
35/// `minimumChord` points: a DragGesture does not begin under 10. A drag to the end continues 30
36/// points past it. Each holds before lifting, so its last position arrives before the lift.
3637func dragCurrentFold(_ app: XCUIApplication, to fraction: Double) {
3738 let paper = app.otherElements["fold.paper"]
3839 let marker = app.descendants(matching: .any)["fold.path"]
@@ -43,15 +44,32 @@ func dragCurrentFold(_ app: XCUIApplication, to fraction: Double) {
4344 }
4445 XCTAssertFalse(samples.isEmpty)
4546 let size = paper.frame.size
47 func chord(_ a: Int, _ b: Int) -> CGVector {
48 CGVector(dx: (samples[b].dx - samples[a].dx) * size.width, dy: (samples[b].dy - samples[a].dy) * size.height)
49 }
50 func length(_ v: CGVector) -> Double { (v.dx * v.dx + v.dy * v.dy).squareRoot() }
4651 let last = Int((fraction * Double(samples.count - 1)).rounded())
47 let stops = Array(stride(from: 0, to: last, by: 32)) + [last]
52 var stops = [0]
53 for i in stride(from: 32, through: last, by: 32) where length(chord(stops.last!, i)) >= minimumChord {
54 stops.append(i)
55 }
56 if stops.last != last {
57 if stops.count > 1 && length(chord(stops.last!, last)) < minimumChord {
58 stops[stops.count - 1] = last
59 } else {
60 stops.append(last)
61 }
62 }
4863 for (a, b) in zip(stops, stops.dropFirst()) {
4964 var end = paper.coordinate(withNormalizedOffset: samples[b])
5065 if b == samples.count - 1 {
51 let dx = (samples[b].dx - samples[a].dx) * size.width, dy = (samples[b].dy - samples[a].dy) * size.height
52 let length = max((dx * dx + dy * dy).squareRoot(), 1)
53 end = end.withOffset(CGVector(dx: dx / length * 30, dy: dy / length * 30))
66 let d = chord(a, b), l = max(length(d), 1)
67 end = end.withOffset(CGVector(dx: d.dx / l * 30, dy: d.dy / l * 30))
5468 }
55 paper.coordinate(withNormalizedOffset: samples[a]).press(forDuration: 0.1, thenDragTo: end)
69 paper.coordinate(withNormalizedOffset: samples[a])
70 .press(forDuration: 0.1, thenDragTo: end, withVelocity: .default, thenHoldForDuration: 0.3)
5671 }
5772}
73
74/// The shortest straight drag `dragCurrentFold` makes, in points.
75private let minimumChord = 12.0