Commit acd4cb11f5
acd4cb11f5c24007c92d10da7880ed0e789fe750
parent: 5da727623b
Verified · cmc
cmc <hello@cleberg.net> · 2026-10-22 16:43 UTC
Cancel a drag when a step is opened; scroll the step list to the current step
Closes #9
Layout: unified · split
App/Sources/FoldSession.swift
+5 −4
| @@ -7,7 +7,7 @@ import simd |
| 7 | |
7 | |
| 8 | /// One model being folded: the current step, its fold parameter `t` in 0...1, and the model |
8 | /// One model being folded: the current step, its fold parameter `t` in 0...1, and the model |
| 9 | /// view's orientation. Nothing here changes `t` on its own; the step changes only when the user |
9 | /// view's orientation. Nothing here changes `t` on its own; the step changes only when the user |
| 10 | /// completes it. |
10 | /// completes it or opens another. |
| 11 | @MainActor @Observable |
11 | @MainActor @Observable |
| 12 | final class FoldSession { |
12 | final class FoldSession { |
| 13 | let model: FoldFile |
13 | let model: FoldFile |
| @@ -76,11 +76,12 @@ final class FoldSession { |
| 76 | open(step: step.index + 1) |
76 | open(step: step.index + 1) |
| 77 | } |
77 | } |
| 78 | |
78 | |
| 79 | /// Opens step `n`, counting from 1, at t = 0, seen from its viewpoint at the default zoom. |
79 | /// Opens step `n`, counting from 1, at t = 0, seen from its viewpoint at the default zoom. A |
| 80 | /// Opening the current step leaves it as it is. A step that cannot be read leaves the session |
80 | /// drag in progress no longer moves `t` or completes a step. Opening the current step leaves it |
| 81 | /// as it is and stops a Debug build. |
81 | /// as it is. A step that cannot be read leaves the session as it is and stops a Debug build. |
| 82 | func open(step n: Int) { |
82 | func open(step n: Int) { |
| 83 | guard n != step.index else { return } |
83 | guard n != step.index else { return } |
| |
84 | cancelDrag() |
| 84 | let next: FoldStep, nextOrder: LayerOrder |
85 | let next: FoldStep, nextOrder: LayerOrder |
| 85 | do { |
86 | do { |
| 86 | next = try readStep(n, in: model) |
87 | next = try readStep(n, in: model) |
App/Sources/StepListView.swift
+35 −24
| @@ -1,9 +1,9 @@ |
| 1 | import Folding |
1 | import Folding |
| 2 | import SwiftUI |
2 | import SwiftUI |
| 3 | |
3 | |
| 4 | /// Every step of the session's model by number and instruction, the current one checked. |
4 | /// Every step of the session's model by number and instruction, the current one checked and |
| 5 | /// Choosing a step opens it at its start; choosing the current step leaves it as it is. Either |
5 | /// scrolled into view. Choosing a step opens it at its start; choosing the current step leaves it |
| 6 | /// closes the list. |
6 | /// as it is. Either closes the list. |
| 7 | struct StepListView: View { |
7 | struct StepListView: View { |
| 8 | let session: FoldSession |
8 | let session: FoldSession |
| 9 | private let steps: [FoldStep] |
9 | private let steps: [FoldStep] |
| @@ -15,28 +15,39 @@ struct StepListView: View { |
| 15 | } |
15 | } |
| 16 | |
16 | |
| 17 | var body: some View { |
17 | var body: some View { |
| 18 | List(steps, id: \.index) { step in |
18 | VStack(spacing: 0) { |
| 19 | let isCurrent = step.index == session.step.index |
19 | Text("Steps") |
| 20 | Button { |
20 | .font(.headline) |
| 21 | session.open(step: step.index) |
21 | .accessibilityAddTraits(.isHeader) |
| 22 | dismiss() |
22 | .padding() |
| 23 | } label: { |
23 | ScrollViewReader { proxy in |
| 24 | HStack(alignment: .firstTextBaseline, spacing: 12) { |
24 | List(steps, id: \.index, rowContent: row) |
| 25 | Text("\(step.index)") |
25 | .listStyle(.plain) |
| 26 | .font(.headline) |
26 | .onAppear { proxy.scrollTo(session.step.index) } |
| 27 | .monospacedDigit() |
| |
| 28 | Text(step.instruction) |
| |
| 29 | .frame(maxWidth: .infinity, alignment: .leading) |
| |
| 30 | Image(systemName: "checkmark") |
| |
| 31 | .fontWeight(.semibold) |
| |
| 32 | .opacity(isCurrent ? 1 : 0) |
| |
| 33 | } |
| |
| 34 | } |
27 | } |
| 35 | .foregroundStyle(.primary) |
| |
| 36 | .accessibilityLabel("Step \(step.index), \(step.instruction)") |
| |
| 37 | .accessibilityAddTraits(isCurrent ? .isSelected : []) |
| |
| 38 | .accessibilityIdentifier("step.\(step.index)") |
| |
| 39 | } |
28 | } |
| 40 | .listStyle(.plain) |
29 | } |
| |
30 | |
| |
31 | private func row(_ step: FoldStep) -> some View { |
| |
32 | let isCurrent = step.index == session.step.index |
| |
33 | return Button { |
| |
34 | session.open(step: step.index) |
| |
35 | dismiss() |
| |
36 | } label: { |
| |
37 | HStack(alignment: .firstTextBaseline, spacing: 12) { |
| |
38 | Text("\(step.index)") |
| |
39 | .font(.headline) |
| |
40 | .monospacedDigit() |
| |
41 | Text(step.instruction) |
| |
42 | .frame(maxWidth: .infinity, alignment: .leading) |
| |
43 | Image(systemName: "checkmark") |
| |
44 | .fontWeight(.semibold) |
| |
45 | .opacity(isCurrent ? 1 : 0) |
| |
46 | } |
| |
47 | } |
| |
48 | .foregroundStyle(.primary) |
| |
49 | .accessibilityLabel("Step \(step.index), \(step.instruction)") |
| |
50 | .accessibilityAddTraits(isCurrent ? .isSelected : []) |
| |
51 | .accessibilityIdentifier("step.\(step.index)") |
| 41 | } |
52 | } |
| 42 | } |
53 | } |
App/Tests/FoldSessionTests.swift
+24
| @@ -214,3 +214,27 @@ private func cupInView(step: Int = 1) throws -> FoldSession { |
| 214 | session.open(step: 4) |
214 | session.open(step: 4) |
| 215 | #expect(session.meshPositions == (try cup(step: 4)).meshPositions) |
215 | #expect(session.meshPositions == (try cup(step: 4)).meshPositions) |
| 216 | } |
216 | } |
| |
217 | |
| |
218 | @MainActor @Test func openingAStepDuringADragLeavesTheNewStepAlone() throws { |
| |
219 | let session = try cupInView() |
| |
220 | session.beginFoldDrag() |
| |
221 | let end = translation(session, to: 1) |
| |
222 | session.drag(translation: translation(session, to: 0.5)) |
| |
223 | session.open(step: 4) |
| |
224 | session.drag(translation: end) |
| |
225 | session.endDrag(translation: end) |
| |
226 | #expect(session.step.index == 4) |
| |
227 | #expect(session.t == 0) |
| |
228 | } |
| |
229 | |
| |
230 | @MainActor @Test func anOrbitAfterOpeningAStepDuringADragLeavesTAlone() throws { |
| |
231 | let session = try cupInView() |
| |
232 | session.t = 0.2 |
| |
233 | session.beginFoldDrag() |
| |
234 | session.drag(translation: translation(session, from: 0.2, to: 0.6)) |
| |
235 | session.open(step: 4) |
| |
236 | session.beginOrbit() |
| |
237 | #expect(session.t == 0) |
| |
238 | #expect(!session.isDragSuspended) |
| |
239 | session.endOrbit() |
| |
240 | } |
App/UITests/AccessibilityAuditTests.swift
+3 −3
| @@ -2,9 +2,9 @@ import XCTest |
| 2 | |
2 | |
| 3 | /// Opens the step list and every step of the cup by the labels VoiceOver sees and runs Xcode's |
3 | /// Opens the step list and every step of the cup by the labels VoiceOver sees and runs Xcode's |
| 4 | /// accessibility audit on each screen, at the default text size and at the largest accessibility |
4 | /// accessibility audit on each screen, at the default text size and at the largest accessibility |
| 5 | /// size. Also checks |
5 | /// size. Also checks that two-finger gestures on the paper change the view without advancing the |
| 6 | /// that two-finger gestures on the paper change the view without advancing the fold or touching |
6 | /// fold or touching the diagram. XCUITest has no two-finger drag, so a pinch and a rotation stand |
| 7 | /// the diagram. XCUITest has no two-finger drag, so a pinch and a rotation stand in for it. |
7 | /// in for it. |
| 8 | final class AccessibilityAuditTests: XCTestCase { |
8 | final class AccessibilityAuditTests: XCTestCase { |
| 9 | static let largestText = "UICTContentSizeCategoryAccessibilityXXXL" |
9 | static let largestText = "UICTContentSizeCategoryAccessibilityXXXL" |
| 10 | /// Elements that exist only under `-UITesting`. |
10 | /// Elements that exist only under `-UITesting`. |
App/UITests/StepListTests.swift
+20
| @@ -30,6 +30,26 @@ final class StepListTests: XCTestCase { |
| 30 | XCTAssertEqual(app.buttons["step.1"].label, "Step 1, \(caption)") |
30 | XCTAssertEqual(app.buttons["step.1"].label, "Step 1, \(caption)") |
| 31 | XCTAssertTrue(app.buttons["step.1"].isSelected) |
31 | XCTAssertTrue(app.buttons["step.1"].isSelected) |
| 32 | XCTAssertFalse(app.buttons["step.2"].isSelected) |
32 | XCTAssertFalse(app.buttons["step.2"].isSelected) |
| |
33 | XCTAssertTrue(app.staticTexts["Steps"].exists) |
| |
34 | } |
| |
35 | |
| |
36 | func testTheStepListOpensScrolledToTheCurrentStep() { |
| |
37 | // at the largest text size the cup's six rows do not fit the sheet |
| |
38 | let app = XCUIApplication() |
| |
39 | app.launchArguments = ["-UIPreferredContentSizeCategoryName", AccessibilityAuditTests.largestText] |
| |
40 | app.launch() |
| |
41 | app.buttons["Cup"].tap() |
| |
42 | app.buttons["steps.button"].tap() |
| |
43 | XCTAssertTrue(app.buttons["step.1"].waitForExistence(timeout: 5)) |
| |
44 | let last = app.buttons["step.6"] |
| |
45 | for _ in 0..<8 where !(last.exists && last.isHittable) { app.collectionViews.firstMatch.swipeUp() } |
| |
46 | last.tap() |
| |
47 | XCTAssertTrue(last.waitForNonExistence(timeout: 5)) |
| |
48 | app.buttons["steps.button"].tap() |
| |
49 | let row = app.buttons["step.6"] |
| |
50 | XCTAssertTrue(row.waitForExistence(timeout: 5)) |
| |
51 | XCTAssertTrue(row.isHittable) |
| |
52 | XCTAssertFalse(app.buttons["step.1"].isHittable) |
| 33 | } |
53 | } |
| 34 | |
54 | |
| 35 | func testChoosingTheCurrentStepClosesTheListAndLeavesTheFoldAlone() { |
55 | func testChoosingTheCurrentStepClosesTheListAndLeavesTheFoldAlone() { |