Commit b170d6099b
Verified · cmc
Layout: unified · split
App/Sources/FoldGesture.swift +7 −2
| @@ -24,19 +24,24 @@ private struct FoldGesture: ViewModifier { | |||
| 24 | 24 | ||
| 25 | func body(content: Content) -> some View { | 25 | func body(content: Content) -> some View { |
| 26 | let folds = session.step.kind == .fold || session.step.kind == .foldAndUnfold | 26 | let folds = session.step.kind == .fold || session.step.kind == .foldAndUnfold |
| 27 | content.gesture(drag, isEnabled: folds) | 27 | content |
| 28 | .gesture(drag, isEnabled: folds) | ||
| 29 | .onChange(of: grab == nil) { _, ended in if ended { session.cancelDrag() } } | ||
| 28 | } | 30 | } |
| 29 | 31 | ||
| 32 | /// A two-finger gesture during the drag suspends it (`FoldSession.beginOrbit()`). | ||
| 30 | private var drag: some Gesture { | 33 | private var drag: some Gesture { |
| 31 | DragGesture() | 34 | DragGesture() |
| 32 | .updating($grab) { value, grab, _ in | 35 | .updating($grab) { value, grab, _ in |
| 36 | if grab == nil { session.beginDrag() } | ||
| 33 | let start = grab ?? Grab(mapping: session.mapping, point: session.mapping.point(at: session.t), t: session.t) | 37 | let start = grab ?? Grab(mapping: session.mapping, point: session.mapping.point(at: session.t), t: session.t) |
| 34 | grab = start | 38 | grab = start |
| 39 | guard !session.isDragSuspended else { return } | ||
| 35 | session.t = start.mapping.parameter( | 40 | session.t = start.mapping.parameter( |
| 36 | grabbedAt: start.point, | 41 | grabbedAt: start.point, |
| 37 | translation: SIMD2(value.translation.width, value.translation.height), | 42 | translation: SIMD2(value.translation.width, value.translation.height), |
| 38 | current: start.mapping.isDegenerate ? start.t : session.t) | 43 | current: start.mapping.isDegenerate ? start.t : session.t) |
| 39 | } | 44 | } |
| 40 | .onEnded { _ in session.completeStep() } | 45 | .onEnded { _ in session.endDrag() } |
| 41 | } | 46 | } |
| 42 | } | 47 | } |
App/Sources/FoldSession.swift +40
| @@ -88,6 +88,46 @@ final class FoldSession { | |||
| 88 | t = 0 | 88 | t = 0 |
| 89 | } | 89 | } |
| 90 | 90 | ||
| 91 | /// `t` when the current one-finger drag began, while one is in progress. | ||
| 92 | @ObservationIgnored private var dragStart: Double? | ||
| 93 | /// Two-finger gestures in progress. | ||
| 94 | @ObservationIgnored private var orbits = 0 | ||
| 95 | /// Whether a two-finger gesture began during the current drag. The drag then leaves `t` as it | ||
| 96 | /// was when the drag began until the finger lifts, and does not complete the step. | ||
| 97 | @ObservationIgnored private(set) var isDragSuspended = false | ||
| 98 | |||
| 99 | /// Called when a one-finger drag that drives `t` begins. | ||
| 100 | func beginDrag() { | ||
| 101 | dragStart = t | ||
| 102 | isDragSuspended = orbits > 0 | ||
| 103 | } | ||
| 104 | |||
| 105 | /// Called when the drag's finger lifts: completes the step unless the drag was suspended. | ||
| 106 | func endDrag() { | ||
| 107 | if !isDragSuspended { completeStep() } | ||
| 108 | cancelDrag() | ||
| 109 | } | ||
| 110 | |||
| 111 | /// Called when the drag ends by any means, including cancellation. | ||
| 112 | func cancelDrag() { | ||
| 113 | dragStart = nil | ||
| 114 | isDragSuspended = false | ||
| 115 | } | ||
| 116 | |||
| 117 | /// Called when a two-finger gesture begins. A drag in progress is suspended and its effect on | ||
| 118 | /// `t` undone. | ||
| 119 | func beginOrbit() { | ||
| 120 | orbits += 1 | ||
| 121 | if let dragStart { | ||
| 122 | t = dragStart | ||
| 123 | isDragSuspended = true | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | func endOrbit() { | ||
| 128 | orbits = max(orbits - 1, 0) | ||
| 129 | } | ||
| 130 | |||
| 91 | /// Turns the view of the model by `angles` radians, as `Camera.orbited(by:)` does. | 131 | /// Turns the view of the model by `angles` radians, as `Camera.orbited(by:)` does. |
| 92 | func orbit(by angles: SIMD2<Double>) { | 132 | func orbit(by angles: SIMD2<Double>) { |
| 93 | let turned = camera.orbited(by: angles) | 133 | let turned = camera.orbited(by: angles) |
App/Sources/OrbitGesture.swift +19 −4
| @@ -4,7 +4,8 @@ import UIKit | |||
| 4 | 4 | ||
| 5 | extension View { | 5 | extension View { |
| 6 | /// Lets a two-finger drag on the view orbit the session's camera around the model, and a pinch | 6 | /// Lets a two-finger drag on the view orbit the session's camera around the model, and a pinch |
| 7 | /// change its field of view. Neither changes `t` or the diagram. | 7 | /// change its field of view. Neither changes `t` or the diagram: either one beginning suspends |
| 8 | /// a one-finger drag in progress and undoes what it did to `t`. | ||
| 8 | func orbitGesture(_ session: FoldSession) -> some View { | 9 | func orbitGesture(_ session: FoldSession) -> some View { |
| 9 | modifier(OrbitGesture(session: session)) | 10 | modifier(OrbitGesture(session: session)) |
| 10 | } | 11 | } |
| @@ -17,14 +18,14 @@ struct OrbitGesture: ViewModifier { | |||
| 17 | 18 | ||
| 18 | func body(content: Content) -> some View { | 19 | func body(content: Content) -> some View { |
| 19 | content | 20 | content |
| 20 | .gesture(TwoFingerPan { translation in | 21 | .gesture(TwoFingerPan(session: session) { translation in |
| 21 | session.orbit(by: SIMD2(translation.x, translation.y) * Self.radiansPerPoint) | 22 | session.orbit(by: SIMD2(translation.x, translation.y) * Self.radiansPerPoint) |
| 22 | }) | 23 | }) |
| 23 | .gesture(Pinch { scale in session.zoom(by: scale) }) | 24 | .gesture(Pinch(session: session) { scale in session.zoom(by: scale) }) |
| 24 | } | 25 | } |
| 25 | } | 26 | } |
| 26 | 27 | ||
| 27 | /// The names of the two recognizers, which may recognize together but with nothing else. | 28 | /// The names of the two recognizers, which may recognize together. |
| 28 | private let orbitRecognizers: Set<String> = ["orbit.pan", "orbit.pinch"] | 29 | private let orbitRecognizers: Set<String> = ["orbit.pan", "orbit.pinch"] |
| 29 | 30 | ||
| 30 | private final class Simultaneous: NSObject, UIGestureRecognizerDelegate { | 31 | private final class Simultaneous: NSObject, UIGestureRecognizerDelegate { |
| @@ -34,8 +35,19 @@ private final class Simultaneous: NSObject, UIGestureRecognizerDelegate { | |||
| 34 | } | 35 | } |
| 35 | } | 36 | } |
| 36 | 37 | ||
| 38 | /// Tells the session when the recognizer begins and ends. | ||
| 39 | @MainActor | ||
| 40 | private func track(_ state: UIGestureRecognizer.State, _ session: FoldSession) { | ||
| 41 | switch state { | ||
| 42 | case .began: session.beginOrbit() | ||
| 43 | case .ended, .cancelled, .failed: session.endOrbit() | ||
| 44 | default: break | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 37 | /// Reports a two-finger drag's translation in points since the last report. | 48 | /// Reports a two-finger drag's translation in points since the last report. |
| 38 | private struct TwoFingerPan: UIGestureRecognizerRepresentable { | 49 | private struct TwoFingerPan: UIGestureRecognizerRepresentable { |
| 50 | let session: FoldSession | ||
| 39 | let changed: (CGPoint) -> Void | 51 | let changed: (CGPoint) -> Void |
| 40 | 52 | ||
| 41 | func makeCoordinator(converter: CoordinateSpaceConverter) -> Simultaneous { Simultaneous() } | 53 | func makeCoordinator(converter: CoordinateSpaceConverter) -> Simultaneous { Simultaneous() } |
| @@ -50,6 +62,7 @@ private struct TwoFingerPan: UIGestureRecognizerRepresentable { | |||
| 50 | } | 62 | } |
| 51 | 63 | ||
| 52 | func handleUIGestureRecognizerAction(_ recognizer: UIPanGestureRecognizer, context: Context) { | 64 | func handleUIGestureRecognizerAction(_ recognizer: UIPanGestureRecognizer, context: Context) { |
| 65 | track(recognizer.state, session) | ||
| 53 | guard recognizer.state == .began || recognizer.state == .changed else { return } | 66 | guard recognizer.state == .began || recognizer.state == .changed else { return } |
| 54 | let translation = recognizer.translation(in: recognizer.view) | 67 | let translation = recognizer.translation(in: recognizer.view) |
| 55 | recognizer.setTranslation(.zero, in: recognizer.view) | 68 | recognizer.setTranslation(.zero, in: recognizer.view) |
| @@ -59,6 +72,7 @@ private struct TwoFingerPan: UIGestureRecognizerRepresentable { | |||
| 59 | 72 | ||
| 60 | /// Reports a pinch's scale since the last report. | 73 | /// Reports a pinch's scale since the last report. |
| 61 | private struct Pinch: UIGestureRecognizerRepresentable { | 74 | private struct Pinch: UIGestureRecognizerRepresentable { |
| 75 | let session: FoldSession | ||
| 62 | let changed: (Double) -> Void | 76 | let changed: (Double) -> Void |
| 63 | 77 | ||
| 64 | func makeCoordinator(converter: CoordinateSpaceConverter) -> Simultaneous { Simultaneous() } | 78 | func makeCoordinator(converter: CoordinateSpaceConverter) -> Simultaneous { Simultaneous() } |
| @@ -71,6 +85,7 @@ private struct Pinch: UIGestureRecognizerRepresentable { | |||
| 71 | } | 85 | } |
| 72 | 86 | ||
| 73 | func handleUIGestureRecognizerAction(_ recognizer: UIPinchGestureRecognizer, context: Context) { | 87 | func handleUIGestureRecognizerAction(_ recognizer: UIPinchGestureRecognizer, context: Context) { |
| 88 | track(recognizer.state, session) | ||
| 74 | guard recognizer.state == .began || recognizer.state == .changed else { return } | 89 | guard recognizer.state == .began || recognizer.state == .changed else { return } |
| 75 | let scale = recognizer.scale | 90 | let scale = recognizer.scale |
| 76 | recognizer.scale = 1 | 91 | recognizer.scale = 1 |
App/Sources/ReorientGesture.swift +8 −3
| @@ -23,21 +23,26 @@ struct ReorientGesture: ViewModifier { | |||
| 23 | 23 | ||
| 24 | func body(content: Content) -> some View { | 24 | func body(content: Content) -> some View { |
| 25 | let reorients = session.step.kind == .turnOver || session.step.kind == .rotate | 25 | let reorients = session.step.kind == .turnOver || session.step.kind == .rotate |
| 26 | content.gesture(drag, isEnabled: reorients) | 26 | content |
| 27 | .gesture(drag, isEnabled: reorients) | ||
| 28 | .onChange(of: grab == nil) { _, ended in if ended { session.cancelDrag() } } | ||
| 27 | } | 29 | } |
| 28 | 30 | ||
| 29 | /// The width is measured on the frame the step starts from, which a vertical turn-over would | 31 | /// The width is measured on the frame the step starts from, which a vertical turn-over would |
| 30 | /// otherwise shrink to an edge midway. | 32 | /// otherwise shrink to an edge midway. A two-finger gesture during the drag suspends it |
| 33 | /// (`FoldSession.beginOrbit()`). | ||
| 31 | private var drag: some Gesture { | 34 | private var drag: some Gesture { |
| 32 | DragGesture() | 35 | DragGesture() |
| 33 | .updating($grab) { value, grab, _ in | 36 | .updating($grab) { value, grab, _ in |
| 37 | if grab == nil { session.beginDrag() } | ||
| 34 | let start = grab ?? Grab( | 38 | let start = grab ?? Grab( |
| 35 | mapping: ReorientMapping(positions: session.interpolator.positions(at: 0), camera: session.camera, | 39 | mapping: ReorientMapping(positions: session.interpolator.positions(at: 0), camera: session.camera, |
| 36 | viewportSize: session.viewportSize), | 40 | viewportSize: session.viewportSize), |
| 37 | t: session.t) | 41 | t: session.t) |
| 38 | grab = start | 42 | grab = start |
| 43 | guard !session.isDragSuspended else { return } | ||
| 39 | session.t = start.mapping.parameter(translation: value.translation.width, current: start.t) | 44 | session.t = start.mapping.parameter(translation: value.translation.width, current: start.t) |
| 40 | } | 45 | } |
| 41 | .onEnded { _ in session.completeStep() } | 46 | .onEnded { _ in session.endDrag() } |
| 42 | } | 47 | } |
| 43 | } | 48 | } |
App/Tests/FoldSessionTests.swift +46
| @@ -68,3 +68,49 @@ private func cup(step: Int = 1) throws -> FoldSession { | |||
| 68 | #expect(session.eyeDirection == SIMD3(0, 0, 1)) | 68 | #expect(session.eyeDirection == SIMD3(0, 0, 1)) |
| 69 | #expect(session.camera.fieldOfView == start.fieldOfView) | 69 | #expect(session.camera.fieldOfView == start.fieldOfView) |
| 70 | } | 70 | } |
| 71 | |||
| 72 | @MainActor @Test func anOrbitDuringADragRestoresTAndTheDragDoesNotComplete() throws { | ||
| 73 | let session = try cup() | ||
| 74 | session.t = 0.2 | ||
| 75 | session.beginDrag() | ||
| 76 | session.t = 1 | ||
| 77 | session.beginOrbit() | ||
| 78 | #expect(session.t == 0.2) | ||
| 79 | #expect(session.isDragSuspended) | ||
| 80 | session.endOrbit() | ||
| 81 | #expect(session.isDragSuspended) | ||
| 82 | session.endDrag() | ||
| 83 | #expect(session.step.index == 1) | ||
| 84 | #expect(session.t == 0.2) | ||
| 85 | #expect(!session.isDragSuspended) | ||
| 86 | } | ||
| 87 | |||
| 88 | @MainActor @Test func aDragThatStartsDuringAnOrbitIsSuspended() throws { | ||
| 89 | let session = try cup() | ||
| 90 | session.beginOrbit() | ||
| 91 | session.beginDrag() | ||
| 92 | #expect(session.isDragSuspended) | ||
| 93 | session.endOrbit() | ||
| 94 | session.endDrag() | ||
| 95 | #expect(session.step.index == 1) | ||
| 96 | } | ||
| 97 | |||
| 98 | @MainActor @Test func aDragWithoutAnOrbitCompletesAtTheEnd() throws { | ||
| 99 | let session = try cup() | ||
| 100 | session.beginDrag() | ||
| 101 | session.t = 1 | ||
| 102 | session.endDrag() | ||
| 103 | #expect(session.step.index == 2) | ||
| 104 | } | ||
| 105 | |||
| 106 | @MainActor @Test func anOrbitAfterACancelledDragLeavesTAlone() throws { | ||
| 107 | let session = try cup() | ||
| 108 | session.t = 0.2 | ||
| 109 | session.beginDrag() | ||
| 110 | session.t = 0.6 | ||
| 111 | session.cancelDrag() | ||
| 112 | session.beginOrbit() | ||
| 113 | #expect(session.t == 0.6) | ||
| 114 | #expect(!session.isDragSuspended) | ||
| 115 | session.endOrbit() | ||
| 116 | } | ||
App/UITests/AccessibilityAuditTests.swift +9 −3
| @@ -44,12 +44,16 @@ final class AccessibilityAuditTests: XCTestCase { | |||
| 44 | } | 44 | } |
| 45 | 45 | ||
| 46 | func testDiagramDoesNotChangeWhenTheModelViewChanges() { | 46 | func testDiagramDoesNotChangeWhenTheModelViewChanges() { |
| 47 | let app = XCUIApplication(); app.launch() | 47 | let app = XCUIApplication(); app.launchArguments = ["-UITesting"]; app.launch() |
| 48 | app.buttons["Cup"].tap() | 48 | app.buttons["Cup"].tap() |
| 49 | let diagram = app.descendants(matching: .any)["diagram"] | 49 | let diagram = app.descendants(matching: .any)["diagram"] |
| 50 | let camera = app.descendants(matching: .any)["paper.camera"] | ||
| 50 | XCTAssertTrue(diagram.waitForExistence(timeout: 5)) | 51 | XCTAssertTrue(diagram.waitForExistence(timeout: 5)) |
| 52 | XCTAssertTrue(camera.waitForExistence(timeout: 5)) | ||
| 51 | let before = diagram.screenshot().pngRepresentation | 53 | let before = diagram.screenshot().pngRepresentation |
| 54 | let cameraBefore = camera.value as? String | ||
| 52 | twoFingerGestures(app.otherElements["fold.paper"]) | 55 | twoFingerGestures(app.otherElements["fold.paper"]) |
| 56 | XCTAssertNotEqual(camera.value as? String, cameraBefore) | ||
| 53 | XCTAssertEqual(diagram.screenshot().pngRepresentation, before) | 57 | XCTAssertEqual(diagram.screenshot().pngRepresentation, before) |
| 54 | } | 58 | } |
| 55 | 59 | ||
| @@ -76,6 +80,8 @@ final class AccessibilityAuditTests: XCTestCase { | |||
| 76 | } | 80 | } |
| 77 | if index.label == step { break } | 81 | if index.label == step { break } |
| 78 | } | 82 | } |
| 83 | // The cup has six steps; stopping earlier means an advance failed. | ||
| 84 | XCTAssertEqual(index.label, "6") | ||
| 79 | } | 85 | } |
| 80 | 86 | ||
| 81 | private func audit(_ app: XCUIApplication, _ name: String) throws { | 87 | private func audit(_ app: XCUIApplication, _ name: String) throws { |
| @@ -117,8 +123,8 @@ final class AccessibilityAuditTests: XCTestCase { | |||
| 117 | if sizeName == "default", issue.auditType == .dynamicType { return true } | 123 | if sizeName == "default", issue.auditType == .dynamicType { return true } |
| 118 | // Contrast is measured from pixels, so text partly off screen measures wrong. | 124 | // Contrast is measured from pixels, so text partly off screen measures wrong. |
| 119 | if issue.auditType == .contrast, let element = issue.element, !screen.contains(element.frame) { return true } | 125 | if issue.auditType == .contrast, let element = issue.element, !screen.contains(element.frame) { return true } |
| 120 | // The same for text partly scrolled out of its scroll view, as a long caption is at the largest size. | 126 | // The same for the caption, which at the largest size runs past the bottom of its scroll view. |
| 121 | if issue.auditType == .contrast, let element = issue.element, | 127 | if issue.auditType == .contrast, let element = issue.element, element.identifier == "diagram.caption", |
| 122 | scrollFrames.contains(where: { $0.intersects(element.frame) && !$0.contains(element.frame) }) { | 128 | scrollFrames.contains(where: { $0.intersects(element.frame) && !$0.contains(element.frame) }) { |
| 123 | return true | 129 | return true |
| 124 | } | 130 | } |