|
| 1 | +// |
| 2 | +// AppearanceModifiers.swift |
| 3 | +// AndroidSwiftUICore |
| 4 | +// |
| 5 | +// Visual modifiers that fold into the Compose Modifier chain: border, shadow, |
| 6 | +// and clipShape. |
| 7 | +// |
| 8 | + |
| 9 | +// MARK: - Border |
| 10 | + |
| 11 | +public struct _BorderModifier: RenderModifier { |
| 12 | + let color: Color |
| 13 | + let width: Double |
| 14 | + public var _modifierNode: ModifierNode { |
| 15 | + ModifierNode(kind: "border", args: ["color": color.propValue, "width": .double(width)]) |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +public extension View { |
| 20 | + func border(_ color: Color, width: Double = 1) -> ModifiedContent<Self, _BorderModifier> { |
| 21 | + modifier(_BorderModifier(color: color, width: width)) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +// MARK: - Shadow |
| 26 | + |
| 27 | +public struct _ShadowModifier: RenderModifier { |
| 28 | + let radius: Double |
| 29 | + public var _modifierNode: ModifierNode { |
| 30 | + ModifierNode(kind: "shadow", args: ["radius": .double(radius)]) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +public extension View { |
| 35 | + func shadow(radius: Double, x: Double = 0, y: Double = 0) -> ModifiedContent<Self, _ShadowModifier> { |
| 36 | + modifier(_ShadowModifier(radius: radius)) |
| 37 | + } |
| 38 | + func shadow(color: Color, radius: Double, x: Double = 0, y: Double = 0) -> ModifiedContent<Self, _ShadowModifier> { |
| 39 | + modifier(_ShadowModifier(radius: radius)) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// MARK: - Clip shape |
| 44 | + |
| 45 | +/// A shape reduced to the fields the interpreter needs to build a Compose shape. |
| 46 | +public protocol _ShapeKind { |
| 47 | + var _shapeKind: String { get } |
| 48 | + var _cornerRadius: Double? { get } |
| 49 | +} |
| 50 | + |
| 51 | +extension Rectangle: _ShapeKind { |
| 52 | + public var _shapeKind: String { "rectangle" } |
| 53 | + public var _cornerRadius: Double? { nil } |
| 54 | +} |
| 55 | +extension Circle: _ShapeKind { |
| 56 | + public var _shapeKind: String { "circle" } |
| 57 | + public var _cornerRadius: Double? { nil } |
| 58 | +} |
| 59 | +extension Capsule: _ShapeKind { |
| 60 | + public var _shapeKind: String { "capsule" } |
| 61 | + public var _cornerRadius: Double? { nil } |
| 62 | +} |
| 63 | +extension RoundedRectangle: _ShapeKind { |
| 64 | + public var _shapeKind: String { "roundedRectangle" } |
| 65 | + public var _cornerRadius: Double? { cornerRadius } |
| 66 | +} |
| 67 | + |
| 68 | +public struct _ClipShapeModifier: RenderModifier { |
| 69 | + let kind: String |
| 70 | + let cornerRadius: Double? |
| 71 | + public var _modifierNode: ModifierNode { |
| 72 | + var args: [String: PropValue] = ["shape": .string(kind)] |
| 73 | + if let cornerRadius { args["cornerRadius"] = .double(cornerRadius) } |
| 74 | + return ModifierNode(kind: "clipShape", args: args) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +public extension View { |
| 79 | + func clipShape<S: _ShapeKind>(_ shape: S) -> ModifiedContent<Self, _ClipShapeModifier> { |
| 80 | + modifier(_ClipShapeModifier(kind: shape._shapeKind, cornerRadius: shape._cornerRadius)) |
| 81 | + } |
| 82 | +} |
0 commit comments