Skip to content

Commit f65b366

Browse files
committed
Code formatting, comments, and rounded rectangle
1 parent e2164a7 commit f65b366

File tree

6 files changed

+535
-106
lines changed

6 files changed

+535
-106
lines changed

Sources/SwiftCrossUI/Path.swift

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ public struct AffineTransform: Equatable, CustomDebugStringConvertible {
171171
}
172172

173173
public struct Path {
174+
/// A rectangle in 2-D space.
175+
///
176+
/// This type is inspired by `CGRect`.
174177
public struct Rect: Equatable {
175178
public var origin: SIMD2<Double>
176179
public var size: SIMD2<Double>
@@ -186,6 +189,8 @@ public struct Path {
186189
public var height: Double { size.y }
187190

188191
public var center: SIMD2<Double> { size * 0.5 + origin }
192+
public var maxX: Double { size.x + origin.x }
193+
public var maxY: Double { size.y + origin.y }
189194

190195
public init(x: Double, y: Double, width: Double, height: Double) {
191196
origin = SIMD2(x: x, y: y)
@@ -209,7 +214,6 @@ public struct Path {
209214
clockwise: Bool
210215
)
211216
case transform(AffineTransform)
212-
case subpath([Action], FillRule)
213217
}
214218

215219
/// A list of every action that has been performed on this path.
@@ -286,10 +290,14 @@ public struct Path {
286290
}
287291

288292
public consuming func addSubpath(_ subpath: Path) -> Path {
289-
actions.append(.subpath(subpath.actions, subpath.fillRule))
293+
actions.append(contentsOf: subpath.actions)
290294
return self
291295
}
292296

297+
/// Set the default stroke style for the path.
298+
///
299+
/// This is not necessarily respected; it can be overridden by ``Shape/stroke(_:style:)``,
300+
/// and is lost when the path is passed to ``addSubpath(_:)``.
293301
public consuming func stroke(style: StrokeStyle) -> Path {
294302
strokeStyle = style
295303
return self
@@ -300,3 +308,18 @@ public struct Path {
300308
return self
301309
}
302310
}
311+
312+
extension Path {
313+
@inlinable
314+
public consuming func `if`(
315+
_ condition: Bool,
316+
then ifTrue: (consuming Path) -> Path,
317+
else ifFalse: (consuming Path) -> Path = { $0 }
318+
) -> Path {
319+
if condition {
320+
ifTrue(self)
321+
} else {
322+
ifFalse(self)
323+
}
324+
}
325+
}
Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,9 @@
1+
/// A rounded rectangle whose corner radius is equal to half the length of its shortest side.
12
public struct Capsule: Shape {
23
public init() {}
34

45
public func path(in bounds: Path.Rect) -> Path {
5-
if bounds.width > bounds.height {
6-
let radius = bounds.height / 2.0
7-
8-
return Path()
9-
.move(to: SIMD2(x: bounds.width + bounds.x - radius, y: bounds.y))
10-
.addArc(
11-
center: SIMD2(x: bounds.width + bounds.x - radius, y: bounds.center.y),
12-
radius: radius,
13-
startAngle: .pi * 1.5,
14-
endAngle: .pi * 0.5,
15-
clockwise: true
16-
)
17-
.addLine(to: SIMD2(x: radius + bounds.x, y: bounds.height + bounds.y))
18-
.addArc(
19-
center: SIMD2(x: radius + bounds.x, y: bounds.center.y),
20-
radius: radius,
21-
startAngle: .pi * 0.5,
22-
endAngle: .pi * 1.5,
23-
clockwise: true
24-
)
25-
.addLine(to: SIMD2(x: bounds.width + bounds.x - radius, y: bounds.y))
26-
} else if bounds.width < bounds.height {
27-
let radius = bounds.width / 2.0
28-
29-
return Path()
30-
.move(to: SIMD2(x: bounds.x, y: bounds.height + bounds.y - radius))
31-
.addArc(
32-
center: SIMD2(x: bounds.center.x, y: bounds.height + bounds.y - radius),
33-
radius: radius,
34-
startAngle: .pi,
35-
endAngle: 0.0,
36-
clockwise: false
37-
)
38-
.addLine(to: SIMD2(x: bounds.width + bounds.x, y: radius + bounds.y))
39-
.addArc(
40-
center: SIMD2(x: bounds.center.x, y: radius + bounds.y),
41-
radius: radius,
42-
startAngle: 0.0,
43-
endAngle: .pi,
44-
clockwise: false
45-
)
46-
.addLine(to: SIMD2(x: bounds.x, y: bounds.height + bounds.y - radius))
47-
} else {
48-
return Circle().path(in: bounds)
49-
}
6+
let radius = min(bounds.width, bounds.height) / 2.0
7+
return RoundedRectangle(cornerRadius: radius).path(in: bounds)
508
}
519
}

0 commit comments

Comments
 (0)