-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViews.swift
More file actions
210 lines (169 loc) · 5.98 KB
/
Copy pathViews.swift
File metadata and controls
210 lines (169 loc) · 5.98 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//
// Views.swift
// AndroidSwiftUICore
//
// Primitives added for the first gallery wave.
//
/// A scrollable container.
public struct ScrollView<Content: View>: View {
internal let axis: Axis
internal let content: Content
public enum Axis: String, Sendable { case vertical, horizontal }
public init(_ axis: Axis = .vertical, @ViewBuilder content: () -> Content) {
self.axis = axis
self.content = content()
}
public typealias Body = Never
}
extension ScrollView: PrimitiveView {
public func _render(in context: ResolveContext) -> RenderNode {
RenderNode(
type: "ScrollView",
id: context.path,
props: ["axis": .string(axis.rawValue)],
children: Evaluator.resolveChildren(content, context.descending("content"))
)
}
}
/// A color used as a view fills its proposed space.
extension Color: View {
public typealias Body = Never
}
extension Color: PrimitiveView {
public func _render(in context: ResolveContext) -> RenderNode {
RenderNode(type: "Color", id: context.path, props: ["color": propValue])
}
}
/// An image. `Image(systemName:)` maps a curated set of SF Symbol names to
/// Material icons in the interpreter; a named asset stays placeholder-level
/// until an asset pipeline exists.
public struct Image: View {
internal let name: String
internal let systemName: String?
public init(_ name: String) {
self.name = name
self.systemName = nil
}
public init(systemName: String) {
self.name = systemName
self.systemName = systemName
}
public typealias Body = Never
}
extension Image: PrimitiveView {
public func _render(in context: ResolveContext) -> RenderNode {
var props: [String: PropValue] = ["name": .string(name)]
if let systemName { props["systemName"] = .string(systemName) }
return RenderNode(type: "Image", id: context.path, props: props)
}
}
/// Progress: indeterminate (spinner) or fractional (bar).
public struct ProgressView: View {
internal let value: Double?
public init() {
self.value = nil
}
public init<V: BinaryFloatingPoint>(value: V?, total: V = 1.0) {
self.value = value.map { Double($0 / total) }
}
public typealias Body = Never
}
extension ProgressView: PrimitiveView {
public func _render(in context: ResolveContext) -> RenderNode {
var props: [String: PropValue] = [:]
if let value { props["value"] = .double(value) }
return RenderNode(type: "ProgressView", id: context.path, props: props)
}
}
/// A control for selecting a value from a bounded range.
public struct Slider: View {
internal let value: Binding<Double>
internal let minimum: Double
internal let maximum: Double
public init<V: BinaryFloatingPoint>(value: Binding<V>, in bounds: ClosedRange<V> = 0...1) {
self.value = Binding(
get: { Double(value.wrappedValue) },
set: { value.wrappedValue = V($0) }
)
self.minimum = Double(bounds.lowerBound)
self.maximum = Double(bounds.upperBound)
}
public typealias Body = Never
}
extension Slider: PrimitiveView {
public func _render(in context: ResolveContext) -> RenderNode {
let binding = value
let callbackID = context.callbacks.register(.double { binding.wrappedValue = $0 })
return RenderNode(
type: "Slider",
id: context.path,
props: [
"value": .double(value.wrappedValue),
"min": .double(minimum),
"max": .double(maximum),
"onChange": .int(Int(callbackID)),
]
)
}
}
/// An editable text field bound to a string.
public struct TextField: View {
internal let placeholder: String
internal let text: Binding<String>
public init<S: StringProtocol>(_ placeholder: S, text: Binding<String>) {
self.placeholder = String(placeholder)
self.text = text
}
public typealias Body = Never
}
extension TextField: PrimitiveView {
public func _render(in context: ResolveContext) -> RenderNode {
let binding = text
let callbackID = context.callbacks.register(.string { binding.wrappedValue = $0 })
return RenderNode(
type: "TextField",
id: context.path,
props: [
"text": .string(text.wrappedValue),
"placeholder": .string(placeholder),
"onChange": .int(Int(callbackID)),
]
)
}
}
/// A text field that obscures its contents. Shares the TextField node with a
/// `secure` flag; the interpreter applies a password transformation.
public struct SecureField: View {
internal let placeholder: String
internal let text: Binding<String>
public init<S: StringProtocol>(_ placeholder: S, text: Binding<String>) {
self.placeholder = String(placeholder)
self.text = text
}
public typealias Body = Never
}
extension SecureField: PrimitiveView {
public func _render(in context: ResolveContext) -> RenderNode {
let binding = text
let callbackID = context.callbacks.register(.string { binding.wrappedValue = $0 })
return RenderNode(
type: "TextField",
id: context.path,
props: [
"text": .string(text.wrappedValue),
"placeholder": .string(placeholder),
"onChange": .int(Int(callbackID)),
"secure": .bool(true),
]
)
}
}
/// Eager stand-ins: laziness is an optimization the lazy container path (R7)
/// provides; semantics match the eager stacks.
/// An angle in degrees or radians.
public struct Angle: Equatable, Sendable {
public var degrees: Double
public init(degrees: Double) { self.degrees = degrees }
public static func degrees(_ value: Double) -> Angle { Angle(degrees: value) }
public static func radians(_ value: Double) -> Angle { Angle(degrees: value * 180 / .pi) }
}