Commit 02dab02f24
Verified · cmc
Layout: unified · split
App/Resources/PrivacyInfo.xcprivacy added +14
| @@ -0,0 +1,14 @@ | ||
| 1 | <?xml version="1.0" encoding="UTF-8"?> | |
| 2 | <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
| 3 | <plist version="1.0"> | |
| 4 | <dict> | |
| 5 | <key>NSPrivacyTracking</key> | |
| 6 | <false/> | |
| 7 | <key>NSPrivacyTrackingDomains</key> | |
| 8 | <array/> | |
| 9 | <key>NSPrivacyCollectedDataTypes</key> | |
| 10 | <array/> | |
| 11 | <key>NSPrivacyAccessedAPITypes</key> | |
| 12 | <array/> | |
| 13 | </dict> | |
| 14 | </plist> | |
App/Sources/FoldSession.swift +32 −4
| @@ -27,6 +27,10 @@ final class FoldSession { | ||
| 27 | 27 | var up: SIMD3<Double> { |
| 28 | 28 | didSet { cachedMapping = nil } |
| 29 | 29 | } |
| 30 | /// The camera's vertical field of view in radians, which a pinch changes. | |
| 31 | var fieldOfView = FoldSession.defaultFieldOfView { | |
| 32 | didSet { cachedMapping = nil } | |
| 33 | } | |
| 30 | 34 | /// The paper view's size in points. |
| 31 | 35 | var viewportSize = SIMD2(1.0, 1.0) { |
| 32 | 36 | didSet { cachedMapping = nil } |
| @@ -44,7 +48,9 @@ final class FoldSession { | ||
| 44 | 48 | @ObservationIgnored private var cachedMapping: FoldParameterMapping? |
| 45 | 49 | /// Half the flat sheet's diagonal, the radius the camera keeps in view. |
| 46 | 50 | private let radius: Double |
| 47 | private static let fieldOfView = Double.pi / 6 | |
| 51 | private static let defaultFieldOfView = Double.pi / 6 | |
| 52 | /// The fields of view a pinch can reach, from about 2.5 times the default magnification to half of it. | |
| 53 | static let fieldOfViewRange = 0.21...0.98 | |
| 48 | 54 | |
| 49 | 55 | /// `step` counts from 1. The camera starts at the step's authored viewpoint. |
| 50 | 56 | init(model: FoldFile, step n: Int) throws { |
| @@ -77,10 +83,32 @@ final class FoldSession { | ||
| 77 | 83 | let eye = normalize(next.viewpoint.eye) |
| 78 | 84 | eyeDirection = eye |
| 79 | 85 | up = normalize(next.viewpoint.up - dot(next.viewpoint.up, eye) * eye) |
| 86 | fieldOfView = Self.defaultFieldOfView | |
| 80 | 87 | cachedMapping = nil |
| 81 | 88 | t = 0 |
| 82 | 89 | } |
| 83 | 90 | |
| 91 | /// Turns the view of the model by `angles` radians, as `Camera.orbited(by:)` does. | |
| 92 | func orbit(by angles: SIMD2<Double>) { | |
| 93 | let turned = camera.orbited(by: angles) | |
| 94 | eyeDirection = normalize(turned.eye - turned.target) | |
| 95 | up = turned.up | |
| 96 | } | |
| 97 | ||
| 98 | /// Magnifies the view of the model by `scale`, within `fieldOfViewRange`. | |
| 99 | func zoom(by scale: Double) { | |
| 100 | fieldOfView = camera.zoomed(by: scale, within: Self.fieldOfViewRange).fieldOfView | |
| 101 | } | |
| 102 | ||
| 103 | /// Moves `t` to the next multiple of 0.1 above it (`direction` 1) or below it (−1), within | |
| 104 | /// 0...1, for VoiceOver's adjustable action. Reaching 1 completes the step, as lifting the | |
| 105 | /// finger there does. | |
| 106 | func adjust(_ direction: Int) { | |
| 107 | let tenths = direction > 0 ? (t * 10 + 1e-9).rounded(.down) + 1 : (t * 10 - 1e-9).rounded(.up) - 1 | |
| 108 | t = min(max(tenths, 0), 10) / 10 | |
| 109 | if t == 1 { completeStep() } | |
| 110 | } | |
| 111 | ||
| 84 | 112 | /// Maps drags on the paper view to `t` for the current step, camera and view size. |
| 85 | 113 | var mapping: FoldParameterMapping { |
| 86 | 114 | if let cachedMapping { return cachedMapping } |
| @@ -90,12 +118,12 @@ final class FoldSession { | ||
| 90 | 118 | } |
| 91 | 119 | |
| 92 | 120 | /// Looks at the step's start frame from `eyeDirection`, far enough that a sphere of `radius` |
| 93 | /// about it fits the paper view at `aspect`. | |
| 121 | /// about it fits the paper view at `aspect` at the default field of view. | |
| 94 | 122 | var camera: Camera { |
| 95 | let half = Self.fieldOfView / 2 | |
| 123 | let half = Self.defaultFieldOfView / 2 | |
| 96 | 124 | let horizontalHalf = atan(tan(half) * aspect) |
| 97 | 125 | let distance = radius / sin(min(half, horizontalHalf)) |
| 98 | return Camera(eye: target + distance * eyeDirection, target: target, up: up, fieldOfView: Self.fieldOfView) | |
| 126 | return Camera(eye: target + distance * eyeDirection, target: target, up: up, fieldOfView: fieldOfView) | |
| 99 | 127 | } |
| 100 | 128 | |
| 101 | 129 | private func pose() { |
App/Sources/ModelListView.swift +7 −3
| @@ -5,13 +5,17 @@ import SwiftUI | ||
| 5 | 5 | /// The shipped models by title. Choosing one opens it at step 1. |
| 6 | 6 | struct ModelListView: View { |
| 7 | 7 | private let models: [FoldFile] |
| 8 | @State private var path: [Int] | |
| 8 | 9 | |
| 9 | 10 | init() { |
| 10 | models = (try? ModelLibrary.all()) ?? [] | |
| 11 | let models = (try? ModelLibrary.all()) ?? [] | |
| 12 | self.models = models | |
| 13 | let cup = ScreenshotScene.requested?.pose == nil ? nil : models.firstIndex { $0.fileTitle == "Cup" } | |
| 14 | _path = State(initialValue: cup.map { [$0] } ?? []) | |
| 11 | 15 | } |
| 12 | 16 | |
| 13 | 17 | var body: some View { |
| 14 | NavigationStack { | |
| 18 | NavigationStack(path: $path) { | |
| 15 | 19 | Group { |
| 16 | 20 | if models.isEmpty { |
| 17 | 21 | ContentUnavailableView("No Models", systemImage: "square.dashed") |
| @@ -23,7 +27,7 @@ struct ModelListView: View { | ||
| 23 | 27 | } |
| 24 | 28 | .navigationTitle("Models") |
| 25 | 29 | .navigationDestination(for: Int.self) { i in |
| 26 | StepView(model: models[i], step: 1) | |
| 30 | StepView(model: models[i], step: ScreenshotScene.requested?.pose?.step ?? 1) | |
| 27 | 31 | } |
| 28 | 32 | } |
| 29 | 33 | } |
App/Sources/OrbitGesture.swift added +79
| @@ -0,0 +1,79 @@ | ||
| 1 | import Folding | |
| 2 | import SwiftUI | |
| 3 | import UIKit | |
| 4 | ||
| 5 | extension View { | |
| 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. | |
| 8 | func orbitGesture(_ session: FoldSession) -> some View { | |
| 9 | modifier(OrbitGesture(session: session)) | |
| 10 | } | |
| 11 | } | |
| 12 | ||
| 13 | struct OrbitGesture: ViewModifier { | |
| 14 | let session: FoldSession | |
| 15 | /// How far the model turns per point of drag, in radians. | |
| 16 | static let radiansPerPoint = 0.01 | |
| 17 | ||
| 18 | func body(content: Content) -> some View { | |
| 19 | content | |
| 20 | .gesture(TwoFingerPan { translation in | |
| 21 | session.orbit(by: SIMD2(translation.x, translation.y) * Self.radiansPerPoint) | |
| 22 | }) | |
| 23 | .gesture(Pinch { scale in session.zoom(by: scale) }) | |
| 24 | } | |
| 25 | } | |
| 26 | ||
| 27 | /// The names of the two recognizers, which may recognize together but with nothing else. | |
| 28 | private let orbitRecognizers: Set<String> = ["orbit.pan", "orbit.pinch"] | |
| 29 | ||
| 30 | private final class Simultaneous: NSObject, UIGestureRecognizerDelegate { | |
| 31 | func gestureRecognizer(_ recognizer: UIGestureRecognizer, | |
| 32 | shouldRecognizeSimultaneouslyWith other: UIGestureRecognizer) -> Bool { | |
| 33 | orbitRecognizers.contains(other.name ?? "") | |
| 34 | } | |
| 35 | } | |
| 36 | ||
| 37 | /// Reports a two-finger drag's translation in points since the last report. | |
| 38 | private struct TwoFingerPan: UIGestureRecognizerRepresentable { | |
| 39 | let changed: (CGPoint) -> Void | |
| 40 | ||
| 41 | func makeCoordinator(converter: CoordinateSpaceConverter) -> Simultaneous { Simultaneous() } | |
| 42 | ||
| 43 | func makeUIGestureRecognizer(context: Context) -> UIPanGestureRecognizer { | |
| 44 | let pan = UIPanGestureRecognizer() | |
| 45 | pan.name = "orbit.pan" | |
| 46 | pan.minimumNumberOfTouches = 2 | |
| 47 | pan.maximumNumberOfTouches = 2 | |
| 48 | pan.delegate = context.coordinator | |
| 49 | return pan | |
| 50 | } | |
| 51 | ||
| 52 | func handleUIGestureRecognizerAction(_ recognizer: UIPanGestureRecognizer, context: Context) { | |
| 53 | guard recognizer.state == .began || recognizer.state == .changed else { return } | |
| 54 | let translation = recognizer.translation(in: recognizer.view) | |
| 55 | recognizer.setTranslation(.zero, in: recognizer.view) | |
| 56 | changed(translation) | |
| 57 | } | |
| 58 | } | |
| 59 | ||
| 60 | /// Reports a pinch's scale since the last report. | |
| 61 | private struct Pinch: UIGestureRecognizerRepresentable { | |
| 62 | let changed: (Double) -> Void | |
| 63 | ||
| 64 | func makeCoordinator(converter: CoordinateSpaceConverter) -> Simultaneous { Simultaneous() } | |
| 65 | ||
| 66 | func makeUIGestureRecognizer(context: Context) -> UIPinchGestureRecognizer { | |
| 67 | let pinch = UIPinchGestureRecognizer() | |
| 68 | pinch.name = "orbit.pinch" | |
| 69 | pinch.delegate = context.coordinator | |
| 70 | return pinch | |
| 71 | } | |
| 72 | ||
| 73 | func handleUIGestureRecognizerAction(_ recognizer: UIPinchGestureRecognizer, context: Context) { | |
| 74 | guard recognizer.state == .began || recognizer.state == .changed else { return } | |
| 75 | let scale = recognizer.scale | |
| 76 | recognizer.scale = 1 | |
| 77 | changed(scale) | |
| 78 | } | |
| 79 | } | |
App/Sources/ScreenshotScene.swift added +32
| @@ -0,0 +1,32 @@ | ||
| 1 | import Foundation | |
| 2 | ||
| 3 | /// App Store screens, opened by `scripts/take-screenshots.sh` with `-UITesting -screenshot <name>`. | |
| 4 | /// Every scene but `models` opens the cup at a step, posed and turned to show the paper in 3D. | |
| 5 | enum ScreenshotScene: String { | |
| 6 | case models, fold, flap, cup | |
| 7 | ||
| 8 | static let requested: ScreenshotScene? = { | |
| 9 | let arguments = ProcessInfo.processInfo.arguments | |
| 10 | guard arguments.contains("-UITesting"), let i = arguments.firstIndex(of: "-screenshot"), | |
| 11 | i + 1 < arguments.count else { return nil } | |
| 12 | return ScreenshotScene(rawValue: arguments[i + 1]) | |
| 13 | }() | |
| 14 | ||
| 15 | /// The cup's step, its fold parameter, the orbit from the step's viewpoint in radians, and | |
| 16 | /// the zoom. | |
| 17 | var pose: (step: Int, t: Double, orbit: SIMD2<Double>, zoom: Double)? { | |
| 18 | switch self { | |
| 19 | case .models: nil | |
| 20 | case .fold: (1, 0.55, SIMD2(0.45, -0.5), 1.2) | |
| 21 | case .flap: (4, 0.6, SIMD2(0.6, 0.3), 1.6) | |
| 22 | case .cup: (6, 1, SIMD2(0.5, 0.4), 2.2) | |
| 23 | } | |
| 24 | } | |
| 25 | ||
| 26 | @MainActor func apply(to session: FoldSession) { | |
| 27 | guard let pose else { return } | |
| 28 | session.t = pose.t | |
| 29 | session.orbit(by: pose.orbit) | |
| 30 | session.zoom(by: pose.zoom) | |
| 31 | } | |
| 32 | } | |
App/Sources/StepView.swift +37 −16
| @@ -13,7 +13,9 @@ struct StepView: View { | ||
| 13 | 13 | /// `step` counts from 1. |
| 14 | 14 | init(model: FoldFile, step: Int) { |
| 15 | 15 | title = model.fileTitle ?? "Untitled" |
| 16 | _session = State(initialValue: try? FoldSession(model: model, step: step)) | |
| 16 | let session = try? FoldSession(model: model, step: step) | |
| 17 | if let session { ScreenshotScene.requested?.apply(to: session) } | |
| 18 | _session = State(initialValue: session) | |
| 17 | 19 | } |
| 18 | 20 | |
| 19 | 21 | var body: some View { |
| @@ -33,24 +35,35 @@ struct StepView: View { | ||
| 33 | 35 | .navigationBarTitleDisplayMode(.inline) |
| 34 | 36 | } |
| 35 | 37 | |
| 38 | /// The caption takes the height it needs up to `captionShare` of the pane, and scrolls beyond | |
| 39 | /// that, so the diagram stays in view at large text sizes. | |
| 36 | 40 | private func diagram(_ session: FoldSession) -> some View { |
| 37 | VStack(alignment: .leading, spacing: 0) { | |
| 38 | DiagramCanvas(diagram: session.diagram, style: .standard) | |
| 39 | .accessibilityLabel("Diagram of step \(session.step.index)") | |
| 40 | HStack(alignment: .firstTextBaseline, spacing: 12) { | |
| 41 | Text("\(session.step.index)") | |
| 42 | .font(.title.bold()) | |
| 43 | .monospacedDigit() | |
| 44 | .accessibilityIdentifier("step.index") | |
| 45 | Text(session.step.instruction) | |
| 46 | .fixedSize(horizontal: false, vertical: true) | |
| 47 | .accessibilityIdentifier("diagram.caption") | |
| 41 | GeometryReader { pane in | |
| 42 | VStack(alignment: .leading, spacing: 0) { | |
| 43 | DiagramCanvas(diagram: session.diagram, style: .standard) | |
| 44 | .accessibilityLabel("Diagram of step \(session.step.index)") | |
| 45 | ScrollView { | |
| 46 | HStack(alignment: .firstTextBaseline, spacing: 12) { | |
| 47 | Text("\(session.step.index)") | |
| 48 | .font(.title.bold()) | |
| 49 | .monospacedDigit() | |
| 50 | .accessibilityIdentifier("step.index") | |
| 51 | Text(session.step.instruction) | |
| 52 | .fixedSize(horizontal: false, vertical: true) | |
| 53 | .accessibilityIdentifier("diagram.caption") | |
| 54 | } | |
| 55 | .frame(maxWidth: .infinity, alignment: .leading) | |
| 56 | .padding() | |
| 57 | } | |
| 58 | .scrollBounceBehavior(.basedOnSize) | |
| 59 | .frame(maxHeight: pane.size.height * Self.captionShare) | |
| 60 | .fixedSize(horizontal: false, vertical: true) | |
| 48 | 61 | } |
| 49 | .frame(maxWidth: .infinity, alignment: .leading) | |
| 50 | .padding() | |
| 51 | 62 | } |
| 52 | 63 | } |
| 53 | 64 | |
| 65 | private static let captionShare = 0.45 | |
| 66 | ||
| 54 | 67 | private func paper(_ session: FoldSession) -> some View { |
| 55 | 68 | PaperView(positions: session.meshPositions, mesh: session.interpolator.mesh, clusters: session.clusters, |
| 56 | 69 | camera: session.camera) |
| @@ -59,9 +72,17 @@ struct StepView: View { | ||
| 59 | 72 | } |
| 60 | 73 | .foldGesture(session) |
| 61 | 74 | .reorientGesture(session) |
| 75 | .orbitGesture(session) | |
| 62 | 76 | .accessibilityElement() |
| 63 | 77 | .accessibilityLabel(session.step.instruction) |
| 64 | 78 | .accessibilityValue(session.t.formatted(.percent.precision(.fractionLength(0)))) |
| 79 | .accessibilityAdjustableAction { direction in | |
| 80 | switch direction { | |
| 81 | case .increment: session.adjust(1) | |
| 82 | case .decrement: session.adjust(-1) | |
| 83 | @unknown default: break | |
| 84 | } | |
| 85 | } | |
| 65 | 86 | .accessibilityIdentifier("fold.paper") |
| 66 | 87 | .overlay(alignment: .topLeading) { |
| 67 | 88 | if Self.isUITesting { |
| @@ -87,12 +108,12 @@ struct StepView: View { | ||
| 87 | 108 | .accessibilityIdentifier("fold.path") |
| 88 | 109 | } |
| 89 | 110 | |
| 90 | /// For UI tests: the camera's eye and up, to 3 decimals. | |
| 111 | /// For UI tests: the camera's eye, up and field of view, to 3 decimals. | |
| 91 | 112 | private func cameraValue(_ session: FoldSession) -> some View { |
| 92 | 113 | let camera = session.camera |
| 93 | 114 | let value = [camera.eye, camera.up].map { v in |
| 94 | 115 | String(format: "%.3f,%.3f,%.3f", v.x, v.y, v.z) |
| 95 | }.joined(separator: " ") | |
| 116 | }.joined(separator: " ") + String(format: " %.3f", camera.fieldOfView) | |
| 96 | 117 | return Color.clear |
| 97 | 118 | .frame(width: 1, height: 1) |
| 98 | 119 | .allowsHitTesting(false) |
App/Tests/FoldSessionTests.swift added +70
| @@ -0,0 +1,70 @@ | ||
| 1 | import FoldFormat | |
| 2 | import Models | |
| 3 | import Testing | |
| 4 | @testable import Fold | |
| 5 | ||
| 6 | @MainActor | |
| 7 | private func cup(step: Int = 1) throws -> FoldSession { | |
| 8 | let model = try #require(try ModelLibrary.all().first { $0.fileTitle == "Cup" }) | |
| 9 | return try FoldSession(model: model, step: step) | |
| 10 | } | |
| 11 | ||
| 12 | @MainActor @Test func incrementingMovesToTheNextTenth() throws { | |
| 13 | let session = try cup() | |
| 14 | session.adjust(1) | |
| 15 | #expect(session.t == 0.1) | |
| 16 | session.adjust(1); session.adjust(1) | |
| 17 | #expect(session.t == 0.3) | |
| 18 | session.t = 0.37 | |
| 19 | session.adjust(1) | |
| 20 | #expect(session.t == 0.4) | |
| 21 | } | |
| 22 | ||
| 23 | @MainActor @Test func decrementingMovesToTheTenthBelowAndStopsAtZero() throws { | |
| 24 | let session = try cup() | |
| 25 | session.adjust(-1) | |
| 26 | #expect(session.t == 0) | |
| 27 | session.t = 0.37 | |
| 28 | session.adjust(-1) | |
| 29 | #expect(session.t == 0.3) | |
| 30 | } | |
| 31 | ||
| 32 | @MainActor @Test func incrementingToTheEndCompletesTheStep() throws { | |
| 33 | let session = try cup() | |
| 34 | for _ in 0..<9 { session.adjust(1) } | |
| 35 | #expect(session.step.index == 1 && session.t == 0.9) | |
| 36 | session.adjust(1) | |
| 37 | #expect(session.step.index == 2) | |
| 38 | #expect(session.t == 0) | |
| 39 | } | |
| 40 | ||
| 41 | @MainActor @Test func incrementingOnTheLastStepStaysAtTheEnd() throws { | |
| 42 | let session = try cup(step: 6) | |
| 43 | for _ in 0..<12 { session.adjust(1) } | |
| 44 | #expect(session.step.index == 6) | |
| 45 | #expect(session.t == 1) | |
| 46 | } | |
| 47 | ||
| 48 | @MainActor @Test func zoomIsClampedAndOrbitAndZoomLeaveTheFoldAlone() throws { | |
| 49 | let session = try cup() | |
| 50 | session.t = 0.5 | |
| 51 | session.zoom(by: 100) | |
| 52 | #expect(session.fieldOfView == FoldSession.fieldOfViewRange.lowerBound) | |
| 53 | session.zoom(by: 0.001) | |
| 54 | #expect(session.fieldOfView == FoldSession.fieldOfViewRange.upperBound) | |
| 55 | session.orbit(by: SIMD2(0.4, -0.7)) | |
| 56 | #expect(session.t == 0.5) | |
| 57 | #expect(session.step.index == 1) | |
| 58 | } | |
| 59 | ||
| 60 | @MainActor @Test func completingAStepResetsTheCameraToTheNextViewpoint() throws { | |
| 61 | let session = try cup() | |
| 62 | let start = session.camera | |
| 63 | session.orbit(by: SIMD2(0.4, -0.7)) | |
| 64 | session.zoom(by: 2) | |
| 65 | session.t = 1 | |
| 66 | session.completeStep() | |
| 67 | #expect(session.step.index == 2) | |
| 68 | #expect(session.eyeDirection == SIMD3(0, 0, 1)) | |
| 69 | #expect(session.camera.fieldOfView == start.fieldOfView) | |
| 70 | } | |
App/UITests/AccessibilityAuditTests.swift added +139
| @@ -0,0 +1,139 @@ | ||
| 1 | import XCTest | |
| 2 | ||
| 3 | /// Opens every step of the cup by the labels VoiceOver sees and runs Xcode's accessibility audit | |
| 4 | /// on each screen, at the default text size and at the largest accessibility size. Also checks | |
| 5 | /// that two-finger gestures on the paper change the view without advancing the fold or touching | |
| 6 | /// the diagram. XCUITest has no two-finger drag, so a pinch and a rotation stand in for it. | |
| 7 | final class AccessibilityAuditTests: XCTestCase { | |
| 8 | static let largestText = "UICTContentSizeCategoryAccessibilityXXXL" | |
| 9 | /// Elements that exist only under `-UITesting`. | |
| 10 | static let testHooks: Set<String> = ["fold.path", "paper.camera"] | |
| 11 | ||
| 12 | private var sizeName = "" | |
| 13 | ||
| 14 | override func setUp() { | |
| 15 | continueAfterFailure = true | |
| 16 | } | |
| 17 | ||
| 18 | func testEveryScreenPassesTheAccessibilityAuditAtDefaultTextSize() throws { | |
| 19 | try everyScreen(textSize: nil) | |
| 20 | } | |
| 21 | ||
| 22 | func testEveryScreenPassesTheAuditAtTheLargestTextSize() throws { | |
| 23 | try everyScreen(textSize: Self.largestText) | |
| 24 | } | |
| 25 | ||
| 26 | func testPaperViewDescribesTheCurrentStepInWords() { | |
| 27 | let app = XCUIApplication(); app.launch() | |
| 28 | app.buttons["Cup"].tap() | |
| 29 | XCTAssertTrue(app.otherElements["fold.paper"].label.hasPrefix("Valley fold")) | |
| 30 | } | |
| 31 | ||
| 32 | func testTwoFingerGesturesChangeTheViewWithoutAdvancingTheFold() { | |
| 33 | let app = XCUIApplication(); app.launchArguments = ["-UITesting"]; app.launch() | |
| 34 | app.buttons["Cup"].tap() | |
| 35 | let paper = app.otherElements["fold.paper"] | |
| 36 | let camera = app.descendants(matching: .any)["paper.camera"] | |
| 37 | XCTAssertTrue(camera.waitForExistence(timeout: 5)) | |
| 38 | let before = paper.value as? String | |
| 39 | let cameraBefore = camera.value as? String | |
| 40 | twoFingerGestures(paper) | |
| 41 | XCTAssertEqual(app.staticTexts["step.index"].label, "1") | |
| 42 | XCTAssertEqual(paper.value as? String, before) | |
| 43 | XCTAssertNotEqual(camera.value as? String, cameraBefore) | |
| 44 | } | |
| 45 | ||
| 46 | func testDiagramDoesNotChangeWhenTheModelViewChanges() { | |
| 47 | let app = XCUIApplication(); app.launch() | |
| 48 | app.buttons["Cup"].tap() | |
| 49 | let diagram = app.descendants(matching: .any)["diagram"] | |
| 50 | XCTAssertTrue(diagram.waitForExistence(timeout: 5)) | |
| 51 | let before = diagram.screenshot().pngRepresentation | |
| 52 | twoFingerGestures(app.otherElements["fold.paper"]) | |
| 53 | XCTAssertEqual(diagram.screenshot().pngRepresentation, before) | |
| 54 | } | |
| 55 | ||
| 56 | // MARK: - Helpers | |
| 57 | ||
| 58 | /// The model list, then every step of the cup, each advanced by the user's own drag. | |
| 59 | private func everyScreen(textSize: String?) throws { | |
| 60 | sizeName = textSize == nil ? "default" : "largest" | |
| 61 | let app = XCUIApplication() | |
| 62 | app.launchArguments = ["-UITesting"] | |
| 63 | if let textSize { app.launchArguments += ["-UIPreferredContentSizeCategoryName", textSize] } | |
| 64 | app.launch() | |
| 65 | try audit(app, "models") | |
| 66 | app.buttons["Cup"].tap() | |
| 67 | let index = app.staticTexts["step.index"] | |
| 68 | XCTAssertTrue(index.waitForExistence(timeout: 5)) | |
| 69 | while true { | |
| 70 | let step = index.label | |
| 71 | try audit(app, "step \(step)") | |
| 72 | if app.staticTexts["diagram.caption"].label.hasPrefix("Turn over") { | |
| 73 | dragAcross(app.otherElements["fold.paper"]) | |
| 74 | } else { | |
| 75 | completeCurrentFold(app) | |
| 76 | } | |
| 77 | if index.label == step { break } | |
| 78 | } | |
| 79 | } | |
| 80 | ||
| 81 | private func audit(_ app: XCUIApplication, _ name: String) throws { | |
| 82 | sleep(1) | |
| 83 | let shot = XCTAttachment(screenshot: app.screenshot()) | |
| 84 | shot.name = "\(sizeName) \(name)" | |
| 85 | shot.lifetime = .keepAlways | |
| 86 | add(shot) | |
| 87 | let screen = app.windows.firstMatch.frame | |
| 88 | let navigationBar = app.navigationBars.firstMatch | |
| 89 | let barFrame = navigationBar.exists ? navigationBar.frame : .null | |
| 90 | let scrollFrames = app.scrollViews.allElementsBoundByIndex.map(\.frame) | |
| 91 | var clipped: [String] = [] | |
| 92 | try app.performAccessibilityAudit { issue in | |
| 93 | if self.isExpected(issue, screen: screen, barFrame: barFrame, scrollFrames: scrollFrames) { return true } | |
| 94 | // Clipping is re-checked below: it is reported now and then for text that is not clipped. | |
| 95 | if issue.auditType == .textClipped { | |
| 96 | clipped.append(issue.element?.label ?? "") | |
| 97 | return true | |
| 98 | } | |
| 99 | self.report(issue, name) | |
| 100 | return false | |
| 101 | } | |
| 102 | guard !clipped.isEmpty else { return } | |
| 103 | sleep(1) | |
| 104 | try app.performAccessibilityAudit(for: .textClipped) { issue in | |
| 105 | guard clipped.contains(issue.element?.label ?? "") else { return true } | |
| 106 | self.report(issue, name) | |
| 107 | return false | |
| 108 | } | |
| 109 | } | |
| 110 | ||
| 111 | private func isExpected(_ issue: XCUIAccessibilityAuditIssue, screen: CGRect, barFrame: CGRect, | |
| 112 | scrollFrames: [CGRect]) -> Bool { | |
| 113 | if let element = issue.element, Self.testHooks.contains(element.identifier) { return true } | |
| 114 | // Navigation bar buttons are drawn by the system. | |
| 115 | if let element = issue.element, barFrame.intersects(element.frame) { return true } | |
| 116 | // Text growth is checked by the largest-text run, where clipping shows. | |
| 117 | if sizeName == "default", issue.auditType == .dynamicType { return true } | |
| 118 | // 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 } | |
| 120 | // The same for text partly scrolled out of its scroll view, as a long caption is at the largest size. | |
| 121 | if issue.auditType == .contrast, let element = issue.element, | |
| 122 | scrollFrames.contains(where: { $0.intersects(element.frame) && !$0.contains(element.frame) }) { | |
| 123 | return true | |
| 124 | } | |
| 125 | return false | |
| 126 | } | |
| 127 | ||
| 128 | private func report(_ issue: XCUIAccessibilityAuditIssue, _ name: String) { | |
| 129 | print("AUDIT [\(sizeName)] [\(name)] \(issue.auditType.rawValue) \(issue.compactDescription) :: \(issue.element?.debugDescription.prefix(200) ?? "-")") | |
| 130 | } | |
| 131 | } | |
| 132 | ||
| 133 | /// The two-finger gestures XCUITest can make on the element: a pinch out, a pinch in and a | |
| 134 | /// rotation. | |
| 135 | func twoFingerGestures(_ element: XCUIElement) { | |
| 136 | element.pinch(withScale: 1.6, velocity: 1) | |
| 137 | element.pinch(withScale: 0.8, velocity: -1) | |
| 138 | element.rotate(0.6, withVelocity: 1) | |
| 139 | } | |
App/project.yml +16 −1
| @@ -11,9 +11,10 @@ targets: | ||
| 11 | 11 | type: application |
| 12 | 12 | platform: iOS |
| 13 | 13 | scheme: |
| 14 | testTargets: [FoldUITests] | |
| 14 | testTargets: [FoldTests, FoldUITests] | |
| 15 | 15 | sources: |
| 16 | 16 | - Sources |
| 17 | - Resources | |
| 17 | 18 | dependencies: |
| 18 | 19 | - package: FoldKit |
| 19 | 20 | product: FoldFormat |
| @@ -46,6 +47,20 @@ targets: | ||
| 46 | 47 | INFOPLIST_KEY_UIApplicationSceneManifest_Generation: YES |
| 47 | 48 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone: UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight |
| 48 | 49 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad: UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight |
| 50 | FoldTests: | |
| 51 | type: bundle.unit-test | |
| 52 | platform: iOS | |
| 53 | sources: [Tests] | |
| 54 | dependencies: | |
| 55 | - target: Fold | |
| 56 | settings: | |
| 57 | base: | |
| 58 | PRODUCT_BUNDLE_IDENTIFIER: sh.krz.fold.tests | |
| 59 | DEVELOPMENT_TEAM: ZCNAX3VL9D | |
| 60 | CODE_SIGN_STYLE: Automatic | |
| 61 | SWIFT_VERSION: "6.0" | |
| 62 | TARGETED_DEVICE_FAMILY: "1,2" | |
| 63 | GENERATE_INFOPLIST_FILE: YES | |
| 49 | 64 | FoldUITests: |
| 50 | 65 | type: bundle.ui-testing |
| 51 | 66 | platform: iOS |
AppStore/rights-basis.md added +18
| @@ -0,0 +1,18 @@ | ||
| 1 | # Rights basis | |
| 2 | ||
| 3 | Every model the app ships is a traditional origami model: the cup, the crane, | |
| 4 | the sampan and the box (the masu). Traditional models have no known designer | |
| 5 | and are in the public domain, so the app may fold them and publish their | |
| 6 | diagrams. | |
| 7 | ||
| 8 | Each shipped `.fold` file has `file_author` exactly `"Traditional"`, and the | |
| 9 | validator rejects any other value. | |
| 10 | ||
| 11 | Models designed in the modern era are their designers' intellectual property. | |
| 12 | Diagramming one without the designer's permission is a copyright problem and, | |
| 13 | in the origami community, a serious breach. No designed model ships without | |
| 14 | the designer's written permission, and adding one starts with that permission, | |
| 15 | not with a commit. | |
| 16 | ||
| 17 | The app generates its diagrams from each model's data. They are not copied or | |
| 18 | traced from any published diagram. | |
Packages/FoldKit/Sources/Folding/Camera.swift +26
| @@ -44,3 +44,29 @@ public struct Camera: Sendable { | ||
| 44 | 44 | return projection * view |
| 45 | 45 | } |
| 46 | 46 | } |
| 47 | ||
| 48 | extension Camera { | |
| 49 | /// The camera moved around `target` at its distance so that the scene appears to turn by | |
| 50 | /// `length(angles)` radians: `angles.x` about the camera's up axis, carrying the near side | |
| 51 | /// rightward on screen, and `angles.y` about its right axis, carrying the near side downward. | |
| 52 | /// The returned `up` is a unit vector perpendicular to the view direction, at any angle. | |
| 53 | public func orbited(by angles: SIMD2<Double>) -> Camera { | |
| 54 | let f = viewDirection | |
| 55 | let s = normalize(cross(f, up)) | |
| 56 | let u = cross(s, f) | |
| 57 | let angle = length(angles) | |
| 58 | guard angle > 0 else { return Camera(eye: eye, target: target, up: u, fieldOfView: fieldOfView) } | |
| 59 | // The camera turns the opposite way to the scene. | |
| 60 | let turn = simd_quatd(angle: -angle, axis: (angles.x * u + angles.y * s) / angle) | |
| 61 | let turnedF = turn.act(f), turnedU = turn.act(u) | |
| 62 | return Camera(eye: target + turn.act(eye - target), target: target, | |
| 63 | up: normalize(turnedU - dot(turnedU, turnedF) * turnedF), fieldOfView: fieldOfView) | |
| 64 | } | |
| 65 | ||
| 66 | /// The camera with its field of view narrowed by `scale`, as a pinch of that scale magnifies | |
| 67 | /// the image, and clamped to `range`. | |
| 68 | public func zoomed(by scale: Double, within range: ClosedRange<Double>) -> Camera { | |
| 69 | let fieldOfView = min(max(2 * atan(tan(self.fieldOfView / 2) / scale), range.lowerBound), range.upperBound) | |
| 70 | return Camera(eye: eye, target: target, up: up, fieldOfView: fieldOfView) | |
| 71 | } | |
| 72 | } | |
Packages/FoldKit/Tests/FoldingTests/CameraOrbitTests.swift added +85
| @@ -0,0 +1,85 @@ | ||
| 1 | import Testing | |
| 2 | import simd | |
| 3 | import Folding | |
| 4 | ||
| 5 | private let front = Camera(eye: SIMD3(0, 0, 5), target: .zero, up: SIMD3(0, 1, 0), fieldOfView: .pi / 6) | |
| 6 | ||
| 7 | /// The point's position in clip space, divided by w. | |
| 8 | private func screen(_ p: SIMD3<Double>, _ camera: Camera) -> SIMD2<Double> { | |
| 9 | let clip = camera.viewProjection(aspect: 1) * SIMD4(p, 1) | |
| 10 | return SIMD2(clip.x, clip.y) / clip.w | |
| 11 | } | |
| 12 | ||
| 13 | private func isPerpendicularUnitUp(_ camera: Camera) -> Bool { | |
| 14 | let up = camera.up, f = camera.viewDirection | |
| 15 | return up.x.isFinite && up.y.isFinite && up.z.isFinite | |
| 16 | && abs(length(up) - 1) < 1e-12 && abs(dot(up, f)) < 1e-12 | |
| 17 | } | |
| 18 | ||
| 19 | @Test func orbitingByPositiveXCarriesTheNearSideRight() { | |
| 20 | let near = SIMD3(0.0, 0, 1) | |
| 21 | #expect(screen(near, front.orbited(by: SIMD2(0.1, 0))).x > 0.01) | |
| 22 | } | |
| 23 | ||
| 24 | @Test func orbitingByPositiveYCarriesTheNearSideDown() { | |
| 25 | let near = SIMD3(0.0, 0, 1) | |
| 26 | #expect(screen(near, front.orbited(by: SIMD2(0, 0.1))).y < -0.01) | |
| 27 | } | |
| 28 | ||
| 29 | @Test func aQuarterTurnAboutUpMovesTheEyeToTheLeftOfTheScene() { | |
| 30 | let c = front.orbited(by: SIMD2(.pi / 2, 0)) | |
| 31 | #expect(length(c.eye - SIMD3(-5, 0, 0)) < 1e-12) | |
| 32 | #expect(length(c.up - SIMD3(0, 1, 0)) < 1e-12) | |
| 33 | } | |
| 34 | ||
| 35 | @Test func orbitingKeepsTheTargetDistanceAndFieldOfView() { | |
| 36 | let c = front.orbited(by: SIMD2(0.7, -1.3)) | |
| 37 | #expect(c.target == front.target) | |
| 38 | #expect(abs(length(c.eye - c.target) - 5) < 1e-12) | |
| 39 | #expect(c.fieldOfView == front.fieldOfView) | |
| 40 | } | |
| 41 | ||
| 42 | @Test func upStaysPerpendicularToTheViewDirectionOverThePoles() { | |
| 43 | var c = front | |
| 44 | for _ in 0..<200 { | |
| 45 | c = c.orbited(by: SIMD2(0, 0.1)) | |
| 46 | #expect(isPerpendicularUnitUp(c)) | |
| 47 | let m = c.viewProjection(aspect: 1) | |
| 48 | #expect((0..<4).allSatisfy { i in (0..<4).allSatisfy { j in m[i][j].isFinite } }) | |
| 49 | } | |
| 50 | #expect(abs(length(c.eye - c.target) - 5) < 1e-9) | |
| 51 | } | |
| 52 | ||
| 53 | @Test func orbitingExactlyOntoThePoleKeepsAUsableUp() { | |
| 54 | let c = front.orbited(by: SIMD2(0, .pi / 2)) | |
| 55 | #expect(length(c.eye - SIMD3(0, 5, 0)) < 1e-12) | |
| 56 | #expect(isPerpendicularUnitUp(c)) | |
| 57 | #expect(length(c.up - SIMD3(0, 0, -1)) < 1e-12) | |
| 58 | } | |
| 59 | ||
| 60 | @Test func manyDiagonalOrbitsDoNotDrift() { | |
| 61 | var c = front | |
| 62 | for k in 0..<1000 { | |
| 63 | c = c.orbited(by: SIMD2(0.031 * Double(k % 7 - 3), 0.017 * Double(k % 5 - 2))) | |
| 64 | } | |
| 65 | #expect(isPerpendicularUnitUp(c)) | |
| 66 | #expect(abs(length(c.eye - c.target) - 5) < 1e-9) | |
| 67 | } | |
| 68 | ||
| 69 | @Test func orbitingMakesAnObliqueUpPerpendicular() { | |
| 70 | let oblique = Camera(eye: SIMD3(0, 0, 5), target: .zero, up: SIMD3(0, 1, 1), fieldOfView: .pi / 6) | |
| 71 | #expect(isPerpendicularUnitUp(oblique.orbited(by: .zero))) | |
| 72 | #expect(isPerpendicularUnitUp(oblique.orbited(by: SIMD2(0.2, 0.3)))) | |
| 73 | } | |
| 74 | ||
| 75 | @Test func zoomingByTwoHalvesTheTangentOfTheHalfAngle() { | |
| 76 | let c = front.zoomed(by: 2, within: 0.1...2) | |
| 77 | #expect(abs(tan(c.fieldOfView / 2) - tan(front.fieldOfView / 2) / 2) < 1e-12) | |
| 78 | #expect(c.eye == front.eye && c.up == front.up) | |
| 79 | #expect(front.zoomed(by: 1, within: 0.1...2).fieldOfView == front.fieldOfView) | |
| 80 | } | |
| 81 | ||
| 82 | @Test func zoomingIsClampedToTheRange() { | |
| 83 | #expect(front.zoomed(by: 100, within: 0.2...1).fieldOfView == 0.2) | |
| 84 | #expect(front.zoomed(by: 0.01, within: 0.2...1).fieldOfView == 1) | |
| 85 | } | |
scripts/take-screenshots.sh added +76
| @@ -0,0 +1,76 @@ | ||
| 1 | #!/bin/sh | |
| 2 | # Takes the App Store screenshots on the iPhone 18 Pro Max (6.9") and iPad Pro 13-inch (M5) | |
| 3 | # simulators, in light mode at 9:41 with a full status bar. Each screen opens through the | |
| 4 | # -UITesting -screenshot launch arguments (App/Sources/ScreenshotScene.swift). The 6.3" set is | |
| 5 | # resized from the 6.9" one; every PNG is flattened to RGB. | |
| 6 | # Usage: scripts/take-screenshots.sh [scene ...] (default: every scene, in App Store order) | |
| 7 | # TEXT_SIZE=accessibility-extra-extra-extra-large (any simctl content_size) shoots at that text | |
| 8 | # size into AppStore/screenshots-<size> instead. | |
| 9 | set -eu | |
| 10 | ||
| 11 | cd "$(dirname "$0")/.." | |
| 12 | all="fold flap cup models" | |
| 13 | scenes="${*:-$all}" | |
| 14 | size="${TEXT_SIZE:-}" | |
| 15 | out=AppStore/screenshots${size:+-$size} | |
| 16 | derived=.build/screenshots | |
| 17 | app="$derived/Build/Products/Debug-iphonesimulator/Fold.app" | |
| 18 | bundle=sh.krz.fold | |
| 19 | ||
| 20 | xcodegen generate --spec App/project.yml --quiet | |
| 21 | xcodebuild build -project App/Fold.xcodeproj -scheme Fold -configuration Debug \ | |
| 22 | -destination 'generic/platform=iOS Simulator' -derivedDataPath "$derived" -quiet | |
| 23 | ||
| 24 | udid() { | |
| 25 | xcrun simctl list devices available | grep -F " $1 (" | head -1 | sed 's/.*(\([0-9A-F-]\{36\}\)).*/\1/' | |
| 26 | } | |
| 27 | ||
| 28 | flatten() { | |
| 29 | # Drops the alpha channel, which App Store Connect rejects. | |
| 30 | sips -s format jpeg -s formatOptions 100 "$1" --out "$1.jpg" >/dev/null | |
| 31 | sips -s format png "$1.jpg" --out "$1" >/dev/null | |
| 32 | rm "$1.jpg" | |
| 33 | } | |
| 34 | ||
| 35 | shoot() { # device name, output folder | |
| 36 | device="$(udid "$1")" | |
| 37 | [ -n "$device" ] || { echo "No simulator named $1." >&2; exit 1; } | |
| 38 | xcrun simctl boot "$device" 2>/dev/null || true | |
| 39 | xcrun simctl bootstatus "$device" -b >/dev/null | |
| 40 | xcrun simctl ui "$device" appearance light | |
| 41 | xcrun simctl ui "$device" content_size "${size:-large}" | |
| 42 | xcrun simctl install "$device" "$app" | |
| 43 | mkdir -p "$out/$2" | |
| 44 | for scene in $scenes; do | |
| 45 | n="$(printf '%s\n' $all | grep -nx "$scene" | cut -d: -f1)" | |
| 46 | [ -n "$n" ] || { echo "Unknown scene $scene; one of: $all" >&2; exit 1; } | |
| 47 | xcrun simctl terminate "$device" "$bundle" 2>/dev/null || true | |
| 48 | # Set per launch: changing content_size restarts the status bar and drops the override. | |
| 49 | xcrun simctl status_bar "$device" override --time 9:41 --dataNetwork wifi --wifiMode active \ | |
| 50 | --wifiBars 3 --cellularMode active --cellularBars 4 --batteryState charged --batteryLevel 100 | |
| 51 | xcrun simctl launch "$device" "$bundle" -UITesting -screenshot "$scene" >/dev/null | |
| 52 | file="$out/$2/$(printf %02d "$n")-$scene.png" | |
| 53 | # A screen that has not drawn yet is blank white, which compresses to a few KB. | |
| 54 | tries=0 | |
| 55 | while :; do | |
| 56 | sleep 3 | |
| 57 | xcrun simctl io "$device" screenshot "$file" >/dev/null 2>&1 | |
| 58 | [ "$(stat -f%z "$file")" -gt 30000 ] && break | |
| 59 | tries=$((tries + 1)) | |
| 60 | [ "$tries" -lt 10 ] || { echo "$scene stayed blank on $1." >&2; exit 1; } | |
| 61 | done | |
| 62 | flatten "$file" | |
| 63 | echo "$file" | |
| 64 | done | |
| 65 | xcrun simctl terminate "$device" "$bundle" 2>/dev/null || true | |
| 66 | xcrun simctl status_bar "$device" clear | |
| 67 | xcrun simctl ui "$device" content_size large | |
| 68 | } | |
| 69 | ||
| 70 | shoot "iPhone 18 Pro Max" iphone-6.9 | |
| 71 | shoot "iPad Pro 13-inch (M5)" ipad | |
| 72 | ||
| 73 | mkdir -p "$out/iphone-6.3" | |
| 74 | for file in "$out"/iphone-6.9/*.png; do | |
| 75 | sips -z 2622 1206 "$file" --out "$out/iphone-6.3/$(basename "$file")" >/dev/null | |
| 76 | done | |