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