Commit 7ba851220c

7ba851220c8516e53c0494ae547b5802bf4d755f

parent: 30cbc803de

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-23 05:04 UTC

Keep the finished state through a drag's transient t; show confetti without a fade

Ref #10

Layout: unified · split

App/Sources/FoldSession.swift +16 −8
@@ -17,13 +17,10 @@ final class FoldSession {
1717 private var order: LayerOrder
1818
1919 var t = 0.0 {
20 didSet {
21 guard t != oldValue else { return }
22 if t < 1 { isFinished = false; isLocked = false }
23 pose()
24 }
20 didSet { if t != oldValue { pose() } }
2521 }
26 /// Whether the last step has been completed and `t` has stayed at 1 since.
22 /// Whether the last step has been completed and no drag, adjustment or other step has since
23 /// left `t` below 1.
2724 private(set) var isFinished = false
2825 /// Whether the finished model ignores drags and adjustment. Orbit and zoom still work.
2926 private(set) var isLocked = false
@@ -96,7 +93,14 @@ final class FoldSession {
9693 func toggleLock() {
9794 guard isFinished else { return }
9895 isLocked.toggle()
99 if isLocked { cancelDrag() }
96 guard isLocked else { return }
97 if let grab { t = grab.start }
98 cancelDrag()
99 }
100
101 /// Leaves the finished state if `t` is below 1.
102 private func settle() {
103 if t < 1 { isFinished = false; isLocked = false }
100104 }
101105
102106 /// Opens step `n`, counting from 1, at t = 0, seen from its viewpoint at the default zoom. A
@@ -125,6 +129,7 @@ final class FoldSession {
125129 fieldOfView = Self.defaultFieldOfView
126130 cachedMapping = nil
127131 if t == 0 { pose() } else { t = 0 }
132 settle()
128133 }
129134
130135 /// What maps a one-finger drag's translation to `t`.
@@ -186,10 +191,12 @@ final class FoldSession {
186191 cancelDrag()
187192 }
188193
189 /// Called when the drag ends by any means, including cancellation.
194 /// Called when the drag ends by any means, including cancellation. A finished model left below
195 /// t = 1 is no longer finished.
190196 func cancelDrag() {
191197 grab = nil
192198 isDragSuspended = false
199 settle()
193200 }
194201
195202 /// Called when a two-finger gesture begins. A drag in progress is suspended and its effect on
@@ -225,6 +232,7 @@ final class FoldSession {
225232 guard !isLocked else { return }
226233 let tenths = direction > 0 ? (t * 10 + 1e-9).rounded(.down) + 1 : (t * 10 - 1e-9).rounded(.up) - 1
227234 t = min(max(tenths, 0), 10) / 10
235 settle()
228236 if t == 1 { completeStep() }
229237 }
230238
App/Sources/StepView.swift +6 −3
@@ -119,7 +119,10 @@ struct StepView: View {
119119 .sensoryFeedback(.success, trigger: session.finishCount)
120120 .task(id: session.finishCount) {
121121 guard session.finishCount > 0 else { return }
122 AccessibilityNotification.Announcement("\(title) folded").post()
122 // high priority, so that the paper's value changing on an adjust does not cut it off
123 var announcement = AttributedString("\(title) folded")
124 announcement.accessibilitySpeechAnnouncementPriority = .high
125 AccessibilityNotification.Announcement(announcement).post()
123126 withAnimation(.easeIn(duration: 0.6)) { isCelebrating = true }
124127 do { try await Task.sleep(for: .seconds(Confetti.duration)) } catch { return }
125128 withAnimation { isCelebrating = false }
@@ -147,7 +150,7 @@ struct StepView: View {
147150 }
148151 }
149152
150 /// Confetti over the paper, or under Reduce Motion a label that fades in.
153 /// Confetti over the paper, or under Reduce Motion a label that fades in and out.
151154 private var celebration: some View {
152155 Group {
153156 if reduceMotion {
@@ -164,7 +167,7 @@ struct StepView: View {
164167 }
165168 }
166169 .allowsHitTesting(false)
167 .transition(.opacity)
170 .transition(reduceMotion ? .opacity : .identity)
168171 .accessibilityIdentifier("confetti")
169172 }
170173
App/Tests/FoldSessionTests.swift +28 −2
@@ -287,10 +287,10 @@ private func finishedCup() throws -> FoldSession {
287287 session.beginFoldDrag()
288288 session.drag(translation: translation(session, from: 1, to: 0.5))
289289 #expect(abs(session.t - 0.5) < 0.01)
290 #expect(!session.isFinished)
291 #expect(!session.isLocked)
292290 session.endDrag(translation: translation(session, from: 1, to: 0.5))
291 #expect(abs(session.t - 0.5) < 0.01)
293292 #expect(!session.isFinished)
293 #expect(!session.isLocked)
294294}
295295
296296@MainActor @Test func foldingTheLastStepAgainFinishesAgain() throws {
@@ -342,3 +342,29 @@ private func finishedCup() throws -> FoldSession {
342342 #expect(session.camera.fieldOfView != camera.fieldOfView)
343343 #expect(session.t == 1 && session.isLocked)
344344}
345
346@MainActor @Test func anOrbitDuringADragBackLeavesTheModelFinished() throws {
347 let session = try finishedCup()
348 session.toggleLock()
349 session.beginFoldDrag()
350 session.drag(translation: translation(session, from: 1, to: 0.7))
351 session.beginOrbit()
352 #expect(session.t == 1)
353 session.endOrbit()
354 session.endDrag(translation: translation(session, from: 1, to: 0.7))
355 #expect(session.t == 1)
356 #expect(session.isFinished && !session.isLocked)
357 #expect(session.finishCount == 1)
358}
359
360@MainActor @Test func lockingDuringADragBackReturnsTheModelToTheEnd() throws {
361 let session = try finishedCup()
362 session.toggleLock()
363 session.beginFoldDrag()
364 session.drag(translation: translation(session, from: 1, to: 0.7))
365 session.toggleLock()
366 #expect(session.t == 1)
367 #expect(session.isFinished && session.isLocked)
368 session.endDrag(translation: translation(session, from: 1, to: 0.7))
369 #expect(session.t == 1 && session.isLocked)
370}