|
| 1 | +// |
| 2 | +// Gradient.swift |
| 3 | +// AndroidSwiftUICore |
| 4 | +// |
| 5 | + |
| 6 | +/// A location in a view, normalized to its size (0…1 on each axis). |
| 7 | +public struct UnitPoint: Equatable, Sendable { |
| 8 | + public var x: Double |
| 9 | + public var y: Double |
| 10 | + public init(x: Double, y: Double) { self.x = x; self.y = y } |
| 11 | + |
| 12 | + public static let leading = UnitPoint(x: 0, y: 0.5) |
| 13 | + public static let trailing = UnitPoint(x: 1, y: 0.5) |
| 14 | + public static let top = UnitPoint(x: 0.5, y: 0) |
| 15 | + public static let bottom = UnitPoint(x: 0.5, y: 1) |
| 16 | + public static let topLeading = UnitPoint(x: 0, y: 0) |
| 17 | + public static let topTrailing = UnitPoint(x: 1, y: 0) |
| 18 | + public static let bottomLeading = UnitPoint(x: 0, y: 1) |
| 19 | + public static let bottomTrailing = UnitPoint(x: 1, y: 1) |
| 20 | + public static let center = UnitPoint(x: 0.5, y: 0.5) |
| 21 | +} |
| 22 | + |
| 23 | +/// An ordered list of colors for a gradient. |
| 24 | +public struct Gradient: Equatable, Sendable { |
| 25 | + public var colors: [Color] |
| 26 | + public init(colors: [Color]) { self.colors = colors } |
| 27 | +} |
| 28 | + |
| 29 | +/// A linear gradient fill, usable as a view. |
| 30 | +public struct LinearGradient: View { |
| 31 | + |
| 32 | + internal let colors: [Color] |
| 33 | + internal let startPoint: UnitPoint |
| 34 | + internal let endPoint: UnitPoint |
| 35 | + |
| 36 | + public init(colors: [Color], startPoint: UnitPoint, endPoint: UnitPoint) { |
| 37 | + self.colors = colors |
| 38 | + self.startPoint = startPoint |
| 39 | + self.endPoint = endPoint |
| 40 | + } |
| 41 | + |
| 42 | + public init(gradient: Gradient, startPoint: UnitPoint, endPoint: UnitPoint) { |
| 43 | + self.init(colors: gradient.colors, startPoint: startPoint, endPoint: endPoint) |
| 44 | + } |
| 45 | + |
| 46 | + public typealias Body = Never |
| 47 | +} |
| 48 | + |
| 49 | +extension LinearGradient: PrimitiveView { |
| 50 | + public func _render(in context: ResolveContext) -> RenderNode { |
| 51 | + RenderNode(type: "LinearGradient", id: context.path, props: [ |
| 52 | + "colors": .array(colors.map { $0.propValue }), |
| 53 | + "startX": .double(startPoint.x), "startY": .double(startPoint.y), |
| 54 | + "endX": .double(endPoint.x), "endY": .double(endPoint.y), |
| 55 | + ]) |
| 56 | + } |
| 57 | +} |
0 commit comments