|
| 1 | +// |
| 2 | +// Shapes.swift |
| 3 | +// AndroidSwiftUICore |
| 4 | +// |
| 5 | +// Shape views fill their frame with a fill color. A shape without an explicit |
| 6 | +// `.frame` renders at zero size (the layout engine's fill-the-parent behavior |
| 7 | +// is not modeled); the catalog always frames them. |
| 8 | +// |
| 9 | + |
| 10 | +private func shapeNode(_ kind: String, fill: Color?, cornerRadius: Double? = nil, context: ResolveContext) -> RenderNode { |
| 11 | + var props: [String: PropValue] = ["shape": .string(kind)] |
| 12 | + if let fill { props["fill"] = fill.propValue } |
| 13 | + if let cornerRadius { props["cornerRadius"] = .double(cornerRadius) } |
| 14 | + return RenderNode(type: "Shape", id: context.path, props: props) |
| 15 | +} |
| 16 | + |
| 17 | +public struct Rectangle: View { |
| 18 | + internal var fillColor: Color? |
| 19 | + public init() {} |
| 20 | + public func fill(_ color: Color) -> Rectangle { var copy = self; copy.fillColor = color; return copy } |
| 21 | + public typealias Body = Never |
| 22 | +} |
| 23 | + |
| 24 | +extension Rectangle: PrimitiveView { |
| 25 | + public func _render(in context: ResolveContext) -> RenderNode { |
| 26 | + shapeNode("rectangle", fill: fillColor, context: context) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +public struct Circle: View { |
| 31 | + internal var fillColor: Color? |
| 32 | + public init() {} |
| 33 | + public func fill(_ color: Color) -> Circle { var copy = self; copy.fillColor = color; return copy } |
| 34 | + public typealias Body = Never |
| 35 | +} |
| 36 | + |
| 37 | +extension Circle: PrimitiveView { |
| 38 | + public func _render(in context: ResolveContext) -> RenderNode { |
| 39 | + shapeNode("circle", fill: fillColor, context: context) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +public struct Capsule: View { |
| 44 | + internal var fillColor: Color? |
| 45 | + public init() {} |
| 46 | + public func fill(_ color: Color) -> Capsule { var copy = self; copy.fillColor = color; return copy } |
| 47 | + public typealias Body = Never |
| 48 | +} |
| 49 | + |
| 50 | +extension Capsule: PrimitiveView { |
| 51 | + public func _render(in context: ResolveContext) -> RenderNode { |
| 52 | + shapeNode("capsule", fill: fillColor, context: context) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +public struct RoundedRectangle: View { |
| 57 | + internal let cornerRadius: Double |
| 58 | + internal var fillColor: Color? |
| 59 | + public init(cornerRadius: Double) { self.cornerRadius = cornerRadius } |
| 60 | + public func fill(_ color: Color) -> RoundedRectangle { var copy = self; copy.fillColor = color; return copy } |
| 61 | + public typealias Body = Never |
| 62 | +} |
| 63 | + |
| 64 | +extension RoundedRectangle: PrimitiveView { |
| 65 | + public func _render(in context: ResolveContext) -> RenderNode { |
| 66 | + shapeNode("roundedRectangle", fill: fillColor, cornerRadius: cornerRadius, context: context) |
| 67 | + } |
| 68 | +} |
0 commit comments