Skip to content

Commit 9b3acf4

Browse files
committed
Support Circle, Ellipse, Capsule, and ContainerRelativeShape
1 parent 07b96f2 commit 9b3acf4

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

Sources/LiveViewNative/BuiltinRegistry.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ struct BuiltinRegistry {
4444
Shape(element: element, context: context, shape: Rectangle())
4545
case "roundedrectangle":
4646
Shape(element: element, context: context, shape: RoundedRectangle(from: element))
47+
case "circle":
48+
Shape(element: element, context: context, shape: Circle())
49+
case "ellipse":
50+
Shape(element: element, context: context, shape: Ellipse())
51+
case "capsule":
52+
Shape(element: element, context: context, shape: Capsule(from: element))
53+
case "containerrelativeshape":
54+
Shape(element: element, context: context, shape: ContainerRelativeShape())
4755
case "lvn-link":
4856
Link(element: element, context: context)
4957
case "toggle":

Sources/LiveViewNative/Views/Shapes/Shape.swift

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,33 @@ struct Shape<S: SwiftUI.Shape>: View {
2828

2929
extension RoundedRectangle {
3030
init(from element: ElementNode) {
31-
let radius: Double
32-
if let s = element.attributeValue(for: "corner-radius"), let d = Double(s) {
33-
radius = d
34-
} else {
35-
radius = 5
31+
let radius = element.attributeValue(for: "corner-radius").flatMap(Double.init) ?? 0
32+
self.init(
33+
cornerSize: .init(
34+
width: element.attributeValue(for: "corner-width").flatMap(Double.init) ?? radius,
35+
height: element.attributeValue(for: "corner-height").flatMap(Double.init) ?? radius
36+
),
37+
style: (element.attributeValue(for: "style").flatMap(RoundedCornerStyle.init) ?? .circular).style
38+
)
39+
}
40+
}
41+
42+
extension Capsule {
43+
init(from element: ElementNode) {
44+
self.init(
45+
style: (element.attributeValue(for: "style").flatMap(RoundedCornerStyle.init) ?? .circular).style
46+
)
47+
}
48+
}
49+
50+
private enum RoundedCornerStyle: String {
51+
case circular
52+
case continuous
53+
54+
var style: SwiftUI.RoundedCornerStyle {
55+
switch self {
56+
case .circular: return .circular
57+
case .continuous: return .continuous
3658
}
37-
self.init(cornerRadius: radius)
3859
}
3960
}

0 commit comments

Comments
 (0)