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 { |
| 17 | 17 | private var order: LayerOrder |
| 18 | 18 | |
| 19 | 19 | 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() } } |
| 25 | 21 | } |
| 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. |
| 27 | 24 | private(set) var isFinished = false |
| 28 | 25 | /// Whether the finished model ignores drags and adjustment. Orbit and zoom still work. |
| 29 | 26 | private(set) var isLocked = false |
| @@ -96,7 +93,14 @@ final class FoldSession { |
| 96 | 93 | func toggleLock() { |
| 97 | 94 | guard isFinished else { return } |
| 98 | 95 | 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 } |
| 100 | 104 | } |
| 101 | 105 | |
| 102 | 106 | /// 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 { |
| 125 | 129 | fieldOfView = Self.defaultFieldOfView |
| 126 | 130 | cachedMapping = nil |
| 127 | 131 | if t == 0 { pose() } else { t = 0 } |
| 132 | settle() |
| 128 | 133 | } |
| 129 | 134 | |
| 130 | 135 | /// What maps a one-finger drag's translation to `t`. |
| @@ -186,10 +191,12 @@ final class FoldSession { |
| 186 | 191 | cancelDrag() |
| 187 | 192 | } |
| 188 | 193 | |
| 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. |
| 190 | 196 | func cancelDrag() { |
| 191 | 197 | grab = nil |
| 192 | 198 | isDragSuspended = false |
| 199 | settle() |
| 193 | 200 | } |
| 194 | 201 | |
| 195 | 202 | /// Called when a two-finger gesture begins. A drag in progress is suspended and its effect on |
| @@ -225,6 +232,7 @@ final class FoldSession { |
| 225 | 232 | guard !isLocked else { return } |
| 226 | 233 | let tenths = direction > 0 ? (t * 10 + 1e-9).rounded(.down) + 1 : (t * 10 - 1e-9).rounded(.up) - 1 |
| 227 | 234 | t = min(max(tenths, 0), 10) / 10 |
| 235 | settle() |
| 228 | 236 | if t == 1 { completeStep() } |
| 229 | 237 | } |
| 230 | 238 | |
App/Sources/StepView.swift
+6 −3
| @@ -119,7 +119,10 @@ struct StepView: View { |
| 119 | 119 | .sensoryFeedback(.success, trigger: session.finishCount) |
| 120 | 120 | .task(id: session.finishCount) { |
| 121 | 121 | 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() |
| 123 | 126 | withAnimation(.easeIn(duration: 0.6)) { isCelebrating = true } |
| 124 | 127 | do { try await Task.sleep(for: .seconds(Confetti.duration)) } catch { return } |
| 125 | 128 | withAnimation { isCelebrating = false } |
| @@ -147,7 +150,7 @@ struct StepView: View { |
| 147 | 150 | } |
| 148 | 151 | } |
| 149 | 152 | |
| 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. |
| 151 | 154 | private var celebration: some View { |
| 152 | 155 | Group { |
| 153 | 156 | if reduceMotion { |
| @@ -164,7 +167,7 @@ struct StepView: View { |
| 164 | 167 | } |
| 165 | 168 | } |
| 166 | 169 | .allowsHitTesting(false) |
| 167 | | .transition(.opacity) |
| 170 | .transition(reduceMotion ? .opacity : .identity) |
| 168 | 171 | .accessibilityIdentifier("confetti") |
| 169 | 172 | } |
| 170 | 173 | |
App/Tests/FoldSessionTests.swift
+28 −2
| @@ -287,10 +287,10 @@ private func finishedCup() throws -> FoldSession { |
| 287 | 287 | session.beginFoldDrag() |
| 288 | 288 | session.drag(translation: translation(session, from: 1, to: 0.5)) |
| 289 | 289 | #expect(abs(session.t - 0.5) < 0.01) |
| 290 | | #expect(!session.isFinished) |
| 291 | | #expect(!session.isLocked) |
| 292 | 290 | session.endDrag(translation: translation(session, from: 1, to: 0.5)) |
| 291 | #expect(abs(session.t - 0.5) < 0.01) |
| 293 | 292 | #expect(!session.isFinished) |
| 293 | #expect(!session.isLocked) |
| 294 | 294 | } |
| 295 | 295 | |
| 296 | 296 | @MainActor @Test func foldingTheLastStepAgainFinishesAgain() throws { |
| @@ -342,3 +342,29 @@ private func finishedCup() throws -> FoldSession { |
| 342 | 342 | #expect(session.camera.fieldOfView != camera.fieldOfView) |
| 343 | 343 | #expect(session.t == 1 && session.isLocked) |
| 344 | 344 | } |
| 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 | } |