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