Qlty config and code cleanup !12
7 files changed, +80 −100
Layout: unified · split
.qlty/.gitignore added +7
| @@ -0,0 +1,7 @@ | |||
| 1 | * | ||
| 2 | !configs | ||
| 3 | !configs/** | ||
| 4 | !hooks | ||
| 5 | !hooks/** | ||
| 6 | !qlty.toml | ||
| 7 | !.gitignore | ||
.qlty/qlty.toml added +54
| @@ -0,0 +1,54 @@ | |||
| 1 | config_version = "0" | ||
| 2 | |||
| 3 | exclude_patterns = [ | ||
| 4 | "**/vendor/**", | ||
| 5 | "**/node_modules/**", | ||
| 6 | "**/testdata/**", | ||
| 7 | "**/dist/**", | ||
| 8 | "**/.build/**", | ||
| 9 | "**/build/**", | ||
| 10 | "**/*.min.*", | ||
| 11 | "**/generated/**", | ||
| 12 | ".claude/**", | ||
| 13 | "**/Pods/**", | ||
| 14 | # Throwaway prototypes kept for reference. | ||
| 15 | "spikes/**", | ||
| 16 | ] | ||
| 17 | |||
| 18 | test_patterns = [ | ||
| 19 | "**/test/**", | ||
| 20 | "**/tests/**", | ||
| 21 | "**/Tests/**", | ||
| 22 | "**/*_test.*", | ||
| 23 | "**/test_*.*", | ||
| 24 | "**/*.test.*", | ||
| 25 | "**/*Tests.swift", | ||
| 26 | "e2e/**", | ||
| 27 | ] | ||
| 28 | |||
| 29 | [[source]] | ||
| 30 | name = "default" | ||
| 31 | default = true | ||
| 32 | |||
| 33 | [[plugin]] | ||
| 34 | name = "osv-scanner" | ||
| 35 | |||
| 36 | [[plugin]] | ||
| 37 | name = "trufflehog" | ||
| 38 | |||
| 39 | |||
| 40 | # Early returns are guard clauses, which keep the happy path unindented. | ||
| 41 | [smells.return_statements] | ||
| 42 | enabled = false | ||
| 43 | |||
| 44 | # Function and file complexity are left to review rather than counted. | ||
| 45 | [smells.function_complexity] | ||
| 46 | enabled = false | ||
| 47 | |||
| 48 | [smells.file_complexity] | ||
| 49 | enabled = false | ||
| 50 | |||
| 51 | # tree-sitter-swift nests each `else if` inside the previous branch, so a one-line `if` in the | ||
| 52 | # fourth branch of a flat chain reports level 5. | ||
| 53 | [language.swift.smells.nested_control_flow] | ||
| 54 | enabled = false | ||
App/Sources/FoldGesture.swift +7 −5
| @@ -6,18 +6,20 @@ extension View { | |||
| 6 | /// follow the finger along their screen path, and the step completes when the finger lifts at | 6 | /// follow the finger along their screen path, and the step completes when the finger lifts at |
| 7 | /// t = 1. Releasing below 1 leaves the paper where it is. | 7 | /// t = 1. Releasing below 1 leaves the paper where it is. |
| 8 | func foldGesture(_ session: FoldSession) -> some View { | 8 | func foldGesture(_ session: FoldSession) -> some View { |
| 9 | modifier(FoldGesture(session: session)) | 9 | modifier(StepDragGesture(session: session, kinds: [.fold, .foldAndUnfold], begin: session.beginFoldDrag)) |
| 10 | } | 10 | } |
| 11 | } | 11 | } |
| 12 | 12 | ||
| 13 | private struct FoldGesture: ViewModifier { | 13 | /// A one-finger drag that drives the session's step while it is one of `kinds`. |
| 14 | struct StepDragGesture: ViewModifier { | ||
| 14 | let session: FoldSession | 15 | let session: FoldSession |
| 16 | let kinds: Set<StepKind> | ||
| 17 | let begin: () -> Void | ||
| 15 | @GestureState private var isDragging = false | 18 | @GestureState private var isDragging = false |
| 16 | 19 | ||
| 17 | func body(content: Content) -> some View { | 20 | func body(content: Content) -> some View { |
| 18 | let folds = session.step.kind == .fold || session.step.kind == .foldAndUnfold | ||
| 19 | content | 21 | content |
| 20 | .gesture(drag, isEnabled: folds && !session.isLocked) | 22 | .gesture(drag, isEnabled: kinds.contains(session.step.kind) && !session.isLocked) |
| 21 | .onChange(of: isDragging) { _, dragging in if !dragging { session.cancelDrag() } } | 23 | .onChange(of: isDragging) { _, dragging in if !dragging { session.cancelDrag() } } |
| 22 | } | 24 | } |
| 23 | 25 | ||
| @@ -25,7 +27,7 @@ private struct FoldGesture: ViewModifier { | |||
| 25 | private var drag: some Gesture { | 27 | private var drag: some Gesture { |
| 26 | DragGesture() | 28 | DragGesture() |
| 27 | .updating($isDragging) { value, isDragging, _ in | 29 | .updating($isDragging) { value, isDragging, _ in |
| 28 | if !isDragging { session.beginFoldDrag() } | 30 | if !isDragging { begin() } |
| 29 | isDragging = true | 31 | isDragging = true |
| 30 | session.drag(translation: SIMD2(value.translation.width, value.translation.height)) | 32 | session.drag(translation: SIMD2(value.translation.width, value.translation.height)) |
| 31 | } | 33 | } |
App/Sources/ReorientGesture.swift +1 −25
| @@ -1,4 +1,3 @@ | |||
| 1 | import FoldFormat | ||
| 2 | import SwiftUI | 1 | import SwiftUI |
| 3 | 2 | ||
| 4 | extension View { | 3 | extension View { |
| @@ -6,29 +5,6 @@ extension View { | |||
| 6 | /// drag across the model's on-screen width turns it fully, a leftward drag turns it back, and | 5 | /// drag across the model's on-screen width turns it fully, a leftward drag turns it back, and |
| 7 | /// the step completes when the finger lifts at t = 1. Releasing below 1 leaves it where it is. | 6 | /// the step completes when the finger lifts at t = 1. Releasing below 1 leaves it where it is. |
| 8 | func reorientGesture(_ session: FoldSession) -> some View { | 7 | func reorientGesture(_ session: FoldSession) -> some View { |
| 9 | modifier(ReorientGesture(session: session)) | 8 | modifier(StepDragGesture(session: session, kinds: [.turnOver, .rotate], begin: session.beginReorientDrag)) |
| 10 | } | ||
| 11 | } | ||
| 12 | |||
| 13 | struct ReorientGesture: ViewModifier { | ||
| 14 | let session: FoldSession | ||
| 15 | @GestureState private var isDragging = false | ||
| 16 | |||
| 17 | func body(content: Content) -> some View { | ||
| 18 | let reorients = session.step.kind == .turnOver || session.step.kind == .rotate | ||
| 19 | content | ||
| 20 | .gesture(drag, isEnabled: reorients && !session.isLocked) | ||
| 21 | .onChange(of: isDragging) { _, dragging in if !dragging { session.cancelDrag() } } | ||
| 22 | } | ||
| 23 | |||
| 24 | /// A two-finger gesture during the drag suspends it (`FoldSession.beginOrbit()`). | ||
| 25 | private var drag: some Gesture { | ||
| 26 | DragGesture() | ||
| 27 | .updating($isDragging) { value, isDragging, _ in | ||
| 28 | if !isDragging { session.beginReorientDrag() } | ||
| 29 | isDragging = true | ||
| 30 | session.drag(translation: SIMD2(value.translation.width, value.translation.height)) | ||
| 31 | } | ||
| 32 | .onEnded { value in session.endDrag(translation: SIMD2(value.translation.width, value.translation.height)) } | ||
| 33 | } | 9 | } |
| 34 | } | 10 | } |
Packages/FoldKit/Sources/FoldCompiler/LayerOrderGuess.swift +1 −63
| @@ -69,7 +69,7 @@ func guessOrders(previous: FoldFrame, step: FoldStep, interpolator: FoldInterpol | |||
| 69 | let t = arrival(window) - arrivalLead * (window.upperBound - window.lowerBound) | 69 | let t = arrival(window) - arrivalLead * (window.upperBound - window.lowerBound) |
| 70 | let near = sampled[t] ?? interpolator.positions(at: t) | 70 | let near = sampled[t] ?? interpolator.positions(at: t) |
| 71 | sampled[t] = near | 71 | sampled[t] = near |
| 72 | let meeting = overlapCentre(a, b, positions, faces, normals) | 72 | let meeting = overlapCentre(a, b, inPlaneOf: a, positions, faces, normals) |
| 73 | let separation = dot(carry(meeting, on: faces[a], from: positions, to: near) | 73 | let separation = dot(carry(meeting, on: faces[a], from: positions, to: near) |
| 74 | - carry(meeting, on: faces[b], from: positions, to: near), normals[b]) | 74 | - carry(meeting, on: faces[b], from: positions, to: near), normals[b]) |
| 75 | if abs(separation) > size * separationTolerance { | 75 | if abs(separation) > size * separationTolerance { |
| @@ -128,23 +128,6 @@ private func overlappingPairs(_ positions: [SIMD3<Double>], _ faces: [[Int]], _ | |||
| 128 | return pairs | 128 | return pairs |
| 129 | } | 129 | } |
| 130 | 130 | ||
| 131 | /// The centroid of the overlap of two coplanar faces, in their plane. | ||
| 132 | private func overlapCentre(_ a: Int, _ b: Int, _ positions: [SIMD3<Double>], _ faces: [[Int]], | ||
| 133 | _ normals: [SIMD3<Double>]) -> SIMD3<Double> { | ||
| 134 | let n = normals[a] | ||
| 135 | let (u, v) = planeBasis(n) | ||
| 136 | let project = { (f: Int) in | ||
| 137 | counterclockwise(faces[f].map { SIMD2(dot(positions[$0], u), dot(positions[$0], v)) }) | ||
| 138 | } | ||
| 139 | var overlap = project(a) | ||
| 140 | let clip = project(b) | ||
| 141 | for i in clip.indices where !overlap.isEmpty { | ||
| 142 | overlap = keepLeft(of: clip[i], clip[(i + 1) % clip.count], overlap) | ||
| 143 | } | ||
| 144 | let c = centroid(overlap) | ||
| 145 | return c.x * u + c.y * v + dot(positions[faces[a][0]], n) * n | ||
| 146 | } | ||
| 147 | |||
| 148 | /// `point`, a point of the face with corners `corners` at `from`, where the same point of paper | 131 | /// `point`, a point of the face with corners `corners` at `from`, where the same point of paper |
| 149 | /// is at `to`. Both states must place the face rigidly. | 132 | /// is at `to`. Both states must place the face rigidly. |
| 150 | private func carry(_ point: SIMD3<Double>, on corners: [Int], from: [SIMD3<Double>], to: [SIMD3<Double>]) -> SIMD3<Double> { | 133 | private func carry(_ point: SIMD3<Double>, on corners: [Int], from: [SIMD3<Double>], to: [SIMD3<Double>]) -> SIMD3<Double> { |
| @@ -166,48 +149,3 @@ private func carry(_ point: SIMD3<Double>, on corners: [Int], from: [SIMD3<Doubl | |||
| 166 | let wr = (d11 * dot(d, e2) - d12 * dot(d, e1)) / denominator | 149 | let wr = (d11 * dot(d, e2) - d12 * dot(d, e1)) / denominator |
| 167 | return to[p] + wq * (to[q] - to[p]) + wr * (to[r] - to[p]) | 150 | return to[p] + wq * (to[q] - to[p]) + wr * (to[r] - to[p]) |
| 168 | } | 151 | } |
| 169 | |||
| 170 | /// Two unit vectors spanning the plane with unit normal `n`. | ||
| 171 | private func planeBasis(_ n: SIMD3<Double>) -> (SIMD3<Double>, SIMD3<Double>) { | ||
| 172 | let helper: SIMD3<Double> = abs(n.x) < 0.9 ? SIMD3(1, 0, 0) : SIMD3(0, 1, 0) | ||
| 173 | let u = normalize(cross(n, helper)) | ||
| 174 | return (u, cross(n, u)) | ||
| 175 | } | ||
| 176 | |||
| 177 | private func signedArea(_ p: [SIMD2<Double>]) -> Double { | ||
| 178 | p.indices.reduce(0) { sum, i in | ||
| 179 | let a = p[i], b = p[(i + 1) % p.count] | ||
| 180 | return sum + (a.x * b.y - b.x * a.y) / 2 | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | private func counterclockwise(_ p: [SIMD2<Double>]) -> [SIMD2<Double>] { | ||
| 185 | signedArea(p) < 0 ? p.reversed() : p | ||
| 186 | } | ||
| 187 | |||
| 188 | /// The area centroid of a polygon, or its corners' mean when it has no area. | ||
| 189 | private func centroid(_ p: [SIMD2<Double>]) -> SIMD2<Double> { | ||
| 190 | let area = signedArea(p) | ||
| 191 | guard abs(area) > 0 else { return p.reduce(.zero, +) / Double(max(p.count, 1)) } | ||
| 192 | var sum = SIMD2<Double>.zero | ||
| 193 | for i in p.indices { | ||
| 194 | let a = p[i], b = p[(i + 1) % p.count] | ||
| 195 | sum += (a + b) * (a.x * b.y - b.x * a.y) | ||
| 196 | } | ||
| 197 | return sum / (6 * area) | ||
| 198 | } | ||
| 199 | |||
| 200 | /// The part of convex polygon `p` to the left of the directed line from `a` to `b`. | ||
| 201 | private func keepLeft(of a: SIMD2<Double>, _ b: SIMD2<Double>, _ p: [SIMD2<Double>]) -> [SIMD2<Double>] { | ||
| 202 | let d = b - a | ||
| 203 | let side = p.map { d.x * ($0.y - a.y) - d.y * ($0.x - a.x) } | ||
| 204 | var result: [SIMD2<Double>] = [] | ||
| 205 | for i in p.indices { | ||
| 206 | let j = (i + 1) % p.count | ||
| 207 | if side[i] >= 0 { result.append(p[i]) } | ||
| 208 | if (side[i] > 0 && side[j] < 0) || (side[i] < 0 && side[j] > 0) { | ||
| 209 | result.append(p[i] + (p[j] - p[i]) * (side[i] / (side[i] - side[j]))) | ||
| 210 | } | ||
| 211 | } | ||
| 212 | return result | ||
| 213 | } | ||
Packages/FoldKit/Sources/Folding/LayerMotion.swift +5 −5
| @@ -94,7 +94,7 @@ private func crossings(_ frame: FoldFrame, axes: [FoldAxis], departure: Bool, st | |||
| 94 | for s in faces.indices where s != m && still(s) && normals[s] != .zero | 94 | for s in faces.indices where s != m && still(s) && normals[s] != .zero |
| 95 | && overlap(m, s, points, faces, normals, tolerance: tolerance) { | 95 | && overlap(m, s, points, faces, normals, tolerance: tolerance) { |
| 96 | guard let side = side(of: m, from: s, frame.faceOrders, normals) else { continue } | 96 | guard let side = side(of: m, from: s, frame.faceOrders, normals) else { continue } |
| 97 | let centre = overlapCentre(m, s, points, faces, normals) | 97 | let centre = overlapCentre(m, s, inPlaneOf: s, points, faces, normals) |
| 98 | let offset = dot(carry(centre, on: faces[m], from: points, to: near) - centre, normals[s]) | 98 | let offset = dot(carry(centre, on: faces[m], from: points, to: near) - centre, normals[s]) |
| 99 | if abs(offset) > size * separationTolerance && (offset > 0) != (side > 0) { | 99 | if abs(offset) > size * separationTolerance && (offset > 0) != (side > 0) { |
| 100 | result.append((m, s)) | 100 | result.append((m, s)) |
| @@ -125,10 +125,10 @@ private func overlap(_ a: Int, _ b: Int, _ points: [SIMD3<Double>], _ faces: [[I | |||
| 125 | return convexPolygonsOverlap(project(a), project(b), tolerance: tolerance) | 125 | return convexPolygonsOverlap(project(a), project(b), tolerance: tolerance) |
| 126 | } | 126 | } |
| 127 | 127 | ||
| 128 | /// The centroid of the overlap of two coplanar faces, in the plane of `b`. | 128 | /// The centroid of the overlap of coplanar faces `a` and `b`, in the plane of `plane`, one of them. |
| 129 | private func overlapCentre(_ a: Int, _ b: Int, _ points: [SIMD3<Double>], _ faces: [[Int]], | 129 | package func overlapCentre(_ a: Int, _ b: Int, inPlaneOf plane: Int, _ points: [SIMD3<Double>], _ faces: [[Int]], |
| 130 | _ normals: [SIMD3<Double>]) -> SIMD3<Double> { | 130 | _ normals: [SIMD3<Double>]) -> SIMD3<Double> { |
| 131 | let n = normals[b] | 131 | let n = normals[plane] |
| 132 | let (u, v) = planeBasis(n) | 132 | let (u, v) = planeBasis(n) |
| 133 | let project = { (f: Int) in counterclockwise(faces[f].map { SIMD2(dot(points[$0], u), dot(points[$0], v)) }) } | 133 | let project = { (f: Int) in counterclockwise(faces[f].map { SIMD2(dot(points[$0], u), dot(points[$0], v)) }) } |
| 134 | var common = project(a) | 134 | var common = project(a) |
| @@ -137,7 +137,7 @@ private func overlapCentre(_ a: Int, _ b: Int, _ points: [SIMD3<Double>], _ face | |||
| 137 | common = keepLeft(of: clip[i], clip[(i + 1) % clip.count], common) | 137 | common = keepLeft(of: clip[i], clip[(i + 1) % clip.count], common) |
| 138 | } | 138 | } |
| 139 | let c = centroid(common) | 139 | let c = centroid(common) |
| 140 | return c.x * u + c.y * v + dot(points[faces[b][0]], n) * n | 140 | return c.x * u + c.y * v + dot(points[faces[plane][0]], n) * n |
| 141 | } | 141 | } |
| 142 | 142 | ||
| 143 | /// `point`, a point of the face with corners `corners` at `from`, where the same point of paper | 143 | /// `point`, a point of the face with corners `corners` at `from`, where the same point of paper |
Packages/FoldKit/Sources/Folding/LayerOrder.swift +5 −2
| @@ -252,7 +252,7 @@ private func boundingBoxDiagonal(_ points: [SIMD3<Double>]) -> Double { | |||
| 252 | } | 252 | } |
| 253 | 253 | ||
| 254 | /// Two unit vectors spanning the plane with unit normal `n`. | 254 | /// Two unit vectors spanning the plane with unit normal `n`. |
| 255 | func planeBasis(_ n: SIMD3<Double>) -> (SIMD3<Double>, SIMD3<Double>) { | 255 | package func planeBasis(_ n: SIMD3<Double>) -> (SIMD3<Double>, SIMD3<Double>) { |
| 256 | let helper: SIMD3<Double> = abs(n.x) < 0.9 ? SIMD3(1, 0, 0) : SIMD3(0, 1, 0) | 256 | let helper: SIMD3<Double> = abs(n.x) < 0.9 ? SIMD3(1, 0, 0) : SIMD3(0, 1, 0) |
| 257 | let u = normalize(cross(n, helper)) | 257 | let u = normalize(cross(n, helper)) |
| 258 | return (u, cross(n, u)) | 258 | return (u, cross(n, u)) |
| @@ -271,13 +271,16 @@ func counterclockwise(_ p: [SIMD2<Double>]) -> [SIMD2<Double>] { | |||
| 271 | signedArea(p) < 0 ? p.reversed() : p | 271 | signedArea(p) < 0 ? p.reversed() : p |
| 272 | } | 272 | } |
| 273 | 273 | ||
| 274 | /// The area centroid of a polygon, or its corners' mean when it has no area. | ||
| 274 | func centroid(_ p: [SIMD2<Double>]) -> SIMD2<Double> { | 275 | func centroid(_ p: [SIMD2<Double>]) -> SIMD2<Double> { |
| 276 | let area = signedArea(p) | ||
| 277 | guard abs(area) > 0 else { return p.reduce(.zero, +) / Double(max(p.count, 1)) } | ||
| 275 | var sum = SIMD2<Double>.zero | 278 | var sum = SIMD2<Double>.zero |
| 276 | for i in p.indices { | 279 | for i in p.indices { |
| 277 | let a = p[i], b = p[(i + 1) % p.count] | 280 | let a = p[i], b = p[(i + 1) % p.count] |
| 278 | sum += (a + b) * (a.x * b.y - b.x * a.y) | 281 | sum += (a + b) * (a.x * b.y - b.x * a.y) |
| 279 | } | 282 | } |
| 280 | return sum / (6 * signedArea(p)) | 283 | return sum / (6 * area) |
| 281 | } | 284 | } |
| 282 | 285 | ||
| 283 | /// Whether `point` is in the open interior of the counterclockwise convex polygon `p`. | 286 | /// Whether `point` is in the open interior of the counterclockwise convex polygon `p`. |