Qlty config and code cleanup !12

merged merged by cmc on 2026-10-30 06:20 UTC · krz/fold:cleanup into main

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 @@
1config_version = "0"
2
3exclude_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
18test_patterns = [
19 "**/test/**",
20 "**/tests/**",
21 "**/Tests/**",
22 "**/*_test.*",
23 "**/test_*.*",
24 "**/*.test.*",
25 "**/*Tests.swift",
26 "e2e/**",
27]
28
29[[source]]
30name = "default"
31default = true
32
33[[plugin]]
34name = "osv-scanner"
35
36[[plugin]]
37name = "trufflehog"
38
39
40# Early returns are guard clauses, which keep the happy path unindented.
41[smells.return_statements]
42enabled = false
43
44# Function and file complexity are left to review rather than counted.
45[smells.function_complexity]
46enabled = false
47
48[smells.file_complexity]
49enabled = 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]
54enabled = false
App/Sources/FoldGesture.swift +7 −5
@@ -6,18 +6,20 @@ extension View {
66 /// follow the finger along their screen path, and the step completes when the finger lifts at
77 /// t = 1. Releasing below 1 leaves the paper where it is.
88 func foldGesture(_ session: FoldSession) -> some View {
9 modifier(FoldGesture(session: session))
9 modifier(StepDragGesture(session: session, kinds: [.fold, .foldAndUnfold], begin: session.beginFoldDrag))
1010 }
1111}
1212
13private struct FoldGesture: ViewModifier {
13/// A one-finger drag that drives the session's step while it is one of `kinds`.
14struct StepDragGesture: ViewModifier {
1415 let session: FoldSession
16 let kinds: Set<StepKind>
17 let begin: () -> Void
1518 @GestureState private var isDragging = false
1619
1720 func body(content: Content) -> some View {
18 let folds = session.step.kind == .fold || session.step.kind == .foldAndUnfold
1921 content
20 .gesture(drag, isEnabled: folds && !session.isLocked)
22 .gesture(drag, isEnabled: kinds.contains(session.step.kind) && !session.isLocked)
2123 .onChange(of: isDragging) { _, dragging in if !dragging { session.cancelDrag() } }
2224 }
2325
@@ -25,7 +27,7 @@ private struct FoldGesture: ViewModifier {
2527 private var drag: some Gesture {
2628 DragGesture()
2729 .updating($isDragging) { value, isDragging, _ in
28 if !isDragging { session.beginFoldDrag() }
30 if !isDragging { begin() }
2931 isDragging = true
3032 session.drag(translation: SIMD2(value.translation.width, value.translation.height))
3133 }
App/Sources/ReorientGesture.swift +1 −25
@@ -1,4 +1,3 @@
1import FoldFormat
21import SwiftUI
32
43extension View {
@@ -6,29 +5,6 @@ extension View {
65 /// drag across the model's on-screen width turns it fully, a leftward drag turns it back, and
76 /// the step completes when the finger lifts at t = 1. Releasing below 1 leaves it where it is.
87 func reorientGesture(_ session: FoldSession) -> some View {
9 modifier(ReorientGesture(session: session))
10 }
11}
12
13struct 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)) }
8 modifier(StepDragGesture(session: session, kinds: [.turnOver, .rotate], begin: session.beginReorientDrag))
339 }
3410}
Packages/FoldKit/Sources/FoldCompiler/LayerOrderGuess.swift +1 −63
@@ -69,7 +69,7 @@ func guessOrders(previous: FoldFrame, step: FoldStep, interpolator: FoldInterpol
6969 let t = arrival(window) - arrivalLead * (window.upperBound - window.lowerBound)
7070 let near = sampled[t] ?? interpolator.positions(at: t)
7171 sampled[t] = near
72 let meeting = overlapCentre(a, b, positions, faces, normals)
72 let meeting = overlapCentre(a, b, inPlaneOf: a, positions, faces, normals)
7373 let separation = dot(carry(meeting, on: faces[a], from: positions, to: near)
7474 - carry(meeting, on: faces[b], from: positions, to: near), normals[b])
7575 if abs(separation) > size * separationTolerance {
@@ -128,23 +128,6 @@ private func overlappingPairs(_ positions: [SIMD3<Double>], _ faces: [[Int]], _
128128 return pairs
129129}
130130
131/// The centroid of the overlap of two coplanar faces, in their plane.
132private 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
148131/// `point`, a point of the face with corners `corners` at `from`, where the same point of paper
149132/// is at `to`. Both states must place the face rigidly.
150133private 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
166149 let wr = (d11 * dot(d, e2) - d12 * dot(d, e1)) / denominator
167150 return to[p] + wq * (to[q] - to[p]) + wr * (to[r] - to[p])
168151}
169
170/// Two unit vectors spanning the plane with unit normal `n`.
171private 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
177private 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
184private 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.
189private 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`.
201private 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
9494 for s in faces.indices where s != m && still(s) && normals[s] != .zero
9595 && overlap(m, s, points, faces, normals, tolerance: tolerance) {
9696 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)
9898 let offset = dot(carry(centre, on: faces[m], from: points, to: near) - centre, normals[s])
9999 if abs(offset) > size * separationTolerance && (offset > 0) != (side > 0) {
100100 result.append((m, s))
@@ -125,10 +125,10 @@ private func overlap(_ a: Int, _ b: Int, _ points: [SIMD3<Double>], _ faces: [[I
125125 return convexPolygonsOverlap(project(a), project(b), tolerance: tolerance)
126126}
127127
128/// The centroid of the overlap of two coplanar faces, in the plane of `b`.
129private func overlapCentre(_ a: Int, _ b: Int, _ points: [SIMD3<Double>], _ faces: [[Int]],
128/// The centroid of the overlap of coplanar faces `a` and `b`, in the plane of `plane`, one of them.
129package func overlapCentre(_ a: Int, _ b: Int, inPlaneOf plane: Int, _ points: [SIMD3<Double>], _ faces: [[Int]],
130130 _ normals: [SIMD3<Double>]) -> SIMD3<Double> {
131 let n = normals[b]
131 let n = normals[plane]
132132 let (u, v) = planeBasis(n)
133133 let project = { (f: Int) in counterclockwise(faces[f].map { SIMD2(dot(points[$0], u), dot(points[$0], v)) }) }
134134 var common = project(a)
@@ -137,7 +137,7 @@ private func overlapCentre(_ a: Int, _ b: Int, _ points: [SIMD3<Double>], _ face
137137 common = keepLeft(of: clip[i], clip[(i + 1) % clip.count], common)
138138 }
139139 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
141141}
142142
143143/// `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 {
252252}
253253
254254/// Two unit vectors spanning the plane with unit normal `n`.
255func planeBasis(_ n: SIMD3<Double>) -> (SIMD3<Double>, SIMD3<Double>) {
255package func planeBasis(_ n: SIMD3<Double>) -> (SIMD3<Double>, SIMD3<Double>) {
256256 let helper: SIMD3<Double> = abs(n.x) < 0.9 ? SIMD3(1, 0, 0) : SIMD3(0, 1, 0)
257257 let u = normalize(cross(n, helper))
258258 return (u, cross(n, u))
@@ -271,13 +271,16 @@ func counterclockwise(_ p: [SIMD2<Double>]) -> [SIMD2<Double>] {
271271 signedArea(p) < 0 ? p.reversed() : p
272272}
273273
274/// The area centroid of a polygon, or its corners' mean when it has no area.
274275func 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)) }
275278 var sum = SIMD2<Double>.zero
276279 for i in p.indices {
277280 let a = p[i], b = p[(i + 1) % p.count]
278281 sum += (a + b) * (a.x * b.y - b.x * a.y)
279282 }
280 return sum / (6 * signedArea(p))
283 return sum / (6 * area)
281284}
282285
283286/// Whether `point` is in the open interior of the counterclockwise convex polygon `p`.