orvyn-lang 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/GUIDE.md +149 -0
- package/LANGUAGE.md +744 -27
- package/README.md +211 -250
- package/brand/orvyn-sunset-animated.svg +15 -0
- package/package.json +3 -1
- package/src/cli/orvyn.js +186 -10
- package/src/core/signal.js +104 -6
- package/src/devtools/runtime.js +25 -1
- package/src/lang/a11y.js +102 -0
- package/src/lang/auth.js +239 -0
- package/src/lang/bundle-native.js +10 -6
- package/src/lang/codegen.js +428 -57
- package/src/lang/device.js +41 -0
- package/src/lang/doctor.js +60 -0
- package/src/lang/env.js +27 -0
- package/src/lang/errors.js +513 -0
- package/src/lang/form.js +69 -8
- package/src/lang/index.js +2 -1
- package/src/lang/lexer.js +2 -0
- package/src/lang/list.js +44 -0
- package/src/lang/native/android/MainActivity.kt +43 -4
- package/src/lang/native/android/OrvynDevice.kt +163 -0
- package/src/lang/native/android/OrvynEngine.kt +154 -7
- package/src/lang/native/android/OrvynRenderer.kt +518 -44
- package/src/lang/native/android.js +74 -11
- package/src/lang/native/build.js +130 -60
- package/src/lang/native/config.js +108 -0
- package/src/lang/native/ios/OrvynApp.swift +6 -0
- package/src/lang/native/ios/OrvynEngine.swift +266 -4
- package/src/lang/native/ios/OrvynViews.swift +429 -34
- package/src/lang/native/ios.js +75 -2
- package/src/lang/native-runtime.js +406 -56
- package/src/lang/page.js +52 -0
- package/src/lang/parser.js +243 -15
- package/src/lang/prerender.js +300 -0
- package/src/lang/read-import.js +11 -0
- package/src/lang/router.js +266 -0
- package/src/lang/runtime-shared.js +792 -10
- package/src/lang/runtime.js +362 -52
- package/src/lang/select.js +13 -0
- package/src/lang/server-code.js +233 -0
- package/src/lang/server-export.js +78 -0
- package/src/lang/server-libs.js +53 -0
- package/src/lang/server.js +409 -0
- package/src/lang/test-runner.js +134 -0
- package/src/lang/types.js +312 -0
- package/src/lang/vite-plugin.js +82 -3
- package/src/lang/web-overlay.js +578 -0
- package/src/lang/widgets.js +94 -0
- package/src/lang/words.js +38 -0
- package/src/runtime/mount.js +116 -27
- package/src/runtime/patch.js +90 -11
- package/src/runtime/style.js +11 -1
- package/src/styling/fluid.js +91 -4
- package/src/styling/spec.js +306 -0
|
@@ -2,12 +2,28 @@
|
|
|
2
2
|
//
|
|
3
3
|
// Turns the Orvyn UI tree into native SwiftUI views.
|
|
4
4
|
|
|
5
|
+
import MapKit
|
|
5
6
|
import SwiftUI
|
|
6
7
|
|
|
8
|
+
private struct OrvynWidthKey: EnvironmentKey {
|
|
9
|
+
static let defaultValue: CGFloat = 0
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
private extension EnvironmentValues {
|
|
13
|
+
var orvynWidth: CGFloat {
|
|
14
|
+
get { self[OrvynWidthKey.self] }
|
|
15
|
+
set { self[OrvynWidthKey.self] = newValue }
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
7
19
|
struct OrvynRootView: View {
|
|
8
20
|
@StateObject private var engine = OrvynEngine()
|
|
9
21
|
|
|
10
22
|
var body: some View {
|
|
23
|
+
let rootTheme = engine.root?.theme
|
|
24
|
+
let isDark = (rootTheme?["dark"] as? Bool) ?? false
|
|
25
|
+
let bgColor = OStyle.color(rootTheme?["bg"]) ?? Color(.systemBackground)
|
|
26
|
+
|
|
11
27
|
Group {
|
|
12
28
|
if let error = engine.error {
|
|
13
29
|
ScrollView {
|
|
@@ -17,14 +33,28 @@ struct OrvynRootView: View {
|
|
|
17
33
|
.padding()
|
|
18
34
|
}
|
|
19
35
|
} else if let root = engine.root {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
36
|
+
GeometryReader { geo in
|
|
37
|
+
ScrollView {
|
|
38
|
+
ONodeView(node: root)
|
|
39
|
+
.frame(maxWidth: .infinity, alignment: .topLeading)
|
|
40
|
+
.environment(\.orvynWidth, geo.size.width)
|
|
41
|
+
}
|
|
23
42
|
}
|
|
24
43
|
} else {
|
|
25
44
|
ProgressView()
|
|
26
45
|
}
|
|
27
46
|
}
|
|
47
|
+
.background(bgColor.ignoresSafeArea())
|
|
48
|
+
.preferredColorScheme(rootTheme != nil ? (isDark ? .dark : .light) : nil)
|
|
49
|
+
.onOpenURL { url in engine.openURL(url.absoluteString) }
|
|
50
|
+
.simultaneousGesture(
|
|
51
|
+
DragGesture(minimumDistance: 24)
|
|
52
|
+
.onEnded { value in
|
|
53
|
+
if engine.root?.canGoBack == true && value.startLocation.x < 28 && value.translation.width > 64 {
|
|
54
|
+
engine.goBack()
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
)
|
|
28
58
|
.environmentObject(engine)
|
|
29
59
|
// Cmd+R in the simulator reloads the app while `orvyn dev ios` runs
|
|
30
60
|
.background(
|
|
@@ -41,15 +71,51 @@ struct ONodeView: View {
|
|
|
41
71
|
// containers inside a Column fill the width, like on the web
|
|
42
72
|
var stretch = false
|
|
43
73
|
@EnvironmentObject private var engine: OrvynEngine
|
|
74
|
+
@Environment(\.orvynWidth) private var parentWidth
|
|
44
75
|
|
|
45
76
|
var body: some View {
|
|
77
|
+
let rtl = node.s["rtl"] as? Bool
|
|
78
|
+
let content = coreView
|
|
79
|
+
.environment(\.orvynWidth, innerWidth)
|
|
80
|
+
.modifier(OLabel(text: node.a))
|
|
81
|
+
|
|
82
|
+
if let rtl {
|
|
83
|
+
content.environment(\.layoutDirection, rtl ? .rightToLeft : .leftToRight)
|
|
84
|
+
} else {
|
|
85
|
+
content
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
@ViewBuilder
|
|
90
|
+
private var coreView: some View {
|
|
46
91
|
switch node.t {
|
|
92
|
+
case "list":
|
|
93
|
+
// LazyVStack only builds the rows near the screen
|
|
94
|
+
ScrollView(.vertical) {
|
|
95
|
+
LazyVStack(spacing: 0) {
|
|
96
|
+
ForEach(node.c) { child in
|
|
97
|
+
ONodeView(node: child, stretch: true)
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
.modifier(OBoxStyle(s: node.s, stretch: true))
|
|
102
|
+
|
|
47
103
|
case "row":
|
|
48
|
-
|
|
104
|
+
let gap = OStyle.number(node.s["gap"]) ?? 0
|
|
105
|
+
let isWrapped = (node.s["wrap"] as? Bool) == true
|
|
106
|
+
let scrollMode = node.s["scroll"]
|
|
107
|
+
let isScrollable = (scrollMode as? Bool) == true || (scrollMode as? String) == "horizontal" || (scrollMode as? String) == "both"
|
|
108
|
+
|
|
49
109
|
Group {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
110
|
+
if isWrapped {
|
|
111
|
+
OFlowLayout(spacing: gap, lineSpacing: gap) {
|
|
112
|
+
ForEach(node.c) { child in
|
|
113
|
+
ONodeView(node: child)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
} else if isScrollable {
|
|
117
|
+
ScrollView(.horizontal, showsIndicators: false) { rowContent }
|
|
118
|
+
} else if !node.c.contains(where: { ["col", "box"].contains($0.t) }) {
|
|
53
119
|
ViewThatFits(in: .horizontal) {
|
|
54
120
|
rowContent
|
|
55
121
|
ScrollView(.horizontal, showsIndicators: false) { rowContent }
|
|
@@ -61,8 +127,33 @@ struct ONodeView: View {
|
|
|
61
127
|
.modifier(OBoxStyle(s: node.s, stretch: stretch, isRow: true))
|
|
62
128
|
|
|
63
129
|
case "col", "box":
|
|
64
|
-
|
|
65
|
-
|
|
130
|
+
let gap = OStyle.number(node.s["gap"]) ?? 0
|
|
131
|
+
let justify = (node.s["justify"] as? String) ?? ""
|
|
132
|
+
let isCentered = (node.s["center"] as? Bool) == true
|
|
133
|
+
let alignment: HorizontalAlignment = isCentered ? .center : OStyle.horizontalAlignment(node.s["align"])
|
|
134
|
+
let stack = VStack(alignment: alignment, spacing: gap) {
|
|
135
|
+
if isCentered || justify == "center" || justify == "end" || justify == "around" || justify == "evenly" {
|
|
136
|
+
Spacer(minLength: 0)
|
|
137
|
+
}
|
|
138
|
+
ForEach(Array(node.c.enumerated()), id: \.element.id) { idx, child in
|
|
139
|
+
if idx > 0 && (justify == "between" || justify == "around" || justify == "evenly") {
|
|
140
|
+
Spacer(minLength: 0)
|
|
141
|
+
}
|
|
142
|
+
ONodeView(node: child, stretch: ["row", "col", "box"].contains(child.t))
|
|
143
|
+
}
|
|
144
|
+
if isCentered || justify == "center" || justify == "around" || justify == "evenly" {
|
|
145
|
+
Spacer(minLength: 0)
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
let scrollMode = node.s["scroll"]
|
|
150
|
+
let isScrollable = (scrollMode as? Bool) == true || (scrollMode as? String) == "vertical" || (scrollMode as? String) == "both"
|
|
151
|
+
Group {
|
|
152
|
+
if isScrollable {
|
|
153
|
+
ScrollView(.vertical) { stack }
|
|
154
|
+
} else {
|
|
155
|
+
stack
|
|
156
|
+
}
|
|
66
157
|
}
|
|
67
158
|
.modifier(OBoxStyle(s: node.s, stretch: stretch))
|
|
68
159
|
|
|
@@ -80,17 +171,81 @@ struct ONodeView: View {
|
|
|
80
171
|
.buttonStyle(.plain)
|
|
81
172
|
|
|
82
173
|
case "button":
|
|
174
|
+
let isDisabled = (node.s["disabled"] as? Bool) == true
|
|
175
|
+
let isLoading = (node.s["loading"] as? Bool) == true
|
|
176
|
+
let pressedStyle = node.s["pressed"] as? [String: Any] ?? node.s["active"] as? [String: Any]
|
|
177
|
+
|
|
83
178
|
Button {
|
|
84
|
-
|
|
179
|
+
if !isDisabled && !isLoading {
|
|
180
|
+
engine.fire(node.tap)
|
|
181
|
+
}
|
|
85
182
|
} label: {
|
|
86
|
-
|
|
183
|
+
HStack(spacing: 6) {
|
|
184
|
+
if isLoading {
|
|
185
|
+
ProgressView()
|
|
186
|
+
.scaleEffect(0.8)
|
|
187
|
+
}
|
|
188
|
+
label
|
|
189
|
+
}
|
|
87
190
|
}
|
|
88
|
-
.buttonStyle(
|
|
89
|
-
.
|
|
191
|
+
.buttonStyle(OButtonStyle(pressed: pressedStyle))
|
|
192
|
+
.disabled(isDisabled || isLoading)
|
|
193
|
+
.opacity(isDisabled ? 0.5 : (isLoading ? 0.75 : 1))
|
|
194
|
+
.fixedSize(horizontal: node.c.isEmpty && (node.x ?? "").count <= 24 && !isLoading, vertical: false)
|
|
90
195
|
|
|
91
196
|
case "input":
|
|
92
197
|
OInput(node: node)
|
|
93
198
|
|
|
199
|
+
case "toggle":
|
|
200
|
+
let isOn = node.v == "true"
|
|
201
|
+
let flip = Binding(get: { isOn }, set: { engine.fire(node.change, $0 ? "true" : "false") })
|
|
202
|
+
Group {
|
|
203
|
+
if node.inputType == "switch" {
|
|
204
|
+
Toggle(node.x ?? "", isOn: flip)
|
|
205
|
+
} else {
|
|
206
|
+
Button { flip.wrappedValue = !isOn } label: {
|
|
207
|
+
HStack(spacing: 8) {
|
|
208
|
+
Image(systemName: isOn ? "checkmark.square.fill" : "square")
|
|
209
|
+
Text(node.x ?? "").foregroundColor(.primary)
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
.accessibilityAddTraits(isOn ? .isSelected : [])
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
.disabled((node.s["disabled"] as? Bool) == true)
|
|
216
|
+
.modifier(OBoxStyle(s: node.s, stretch: stretch))
|
|
217
|
+
|
|
218
|
+
case "modal":
|
|
219
|
+
// the card is the only child; the dimmed background is the sheet itself
|
|
220
|
+
Color.clear
|
|
221
|
+
.frame(width: 0, height: 0)
|
|
222
|
+
.sheet(isPresented: Binding(get: { true }, set: { open in if !open { engine.fire(node.tap) } })) {
|
|
223
|
+
ScrollView {
|
|
224
|
+
ForEach(node.c) { child in
|
|
225
|
+
ONodeView(node: child, stretch: true)
|
|
226
|
+
}
|
|
227
|
+
.padding()
|
|
228
|
+
}
|
|
229
|
+
.environmentObject(engine)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
case "map":
|
|
233
|
+
OMap(place: node.v ?? "")
|
|
234
|
+
.frame(height: OStyle.number(node.s["height"]) ?? 240)
|
|
235
|
+
.modifier(OBoxStyle(s: node.s.filter { $0.key != "height" }, stretch: true))
|
|
236
|
+
|
|
237
|
+
case "select":
|
|
238
|
+
let choice = Binding(get: { node.v ?? "" }, set: { engine.fire(node.change, $0) })
|
|
239
|
+
Picker(node.ph ?? "", selection: choice) {
|
|
240
|
+
Text(node.ph ?? "").tag("")
|
|
241
|
+
ForEach(node.o, id: \.self) { item in
|
|
242
|
+
Text(item).tag(item)
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
.pickerStyle(.menu)
|
|
246
|
+
.disabled((node.s["disabled"] as? Bool) == true)
|
|
247
|
+
.modifier(OBoxStyle(s: node.s, stretch: stretch))
|
|
248
|
+
|
|
94
249
|
case "image":
|
|
95
250
|
AsyncImage(url: URL(string: node.src ?? "")) { image in
|
|
96
251
|
image.resizable().scaledToFit()
|
|
@@ -104,49 +259,182 @@ struct ONodeView: View {
|
|
|
104
259
|
}
|
|
105
260
|
}
|
|
106
261
|
|
|
262
|
+
private var innerWidth: CGFloat {
|
|
263
|
+
let base = OStyle.resolvedWidth(node.s["width"], parentWidth: parentWidth) ?? parentWidth
|
|
264
|
+
let capped = OStyle.number(node.s["maxWidth"]).map { min(base > 0 ? base : $0, $0) } ?? base
|
|
265
|
+
let px = OStyle.number(node.s["paddingX"]) ?? OStyle.number(node.s["padding"]) ?? 0
|
|
266
|
+
let margin = OStyle.number(node.s["margin"]) ?? 0
|
|
267
|
+
return max(0, capped - (px * 2) - (margin * 2))
|
|
268
|
+
}
|
|
269
|
+
|
|
107
270
|
private var rowContent: some View {
|
|
108
|
-
|
|
109
|
-
|
|
271
|
+
let gap = OStyle.number(node.s["gap"]) ?? 0
|
|
272
|
+
let justify = (node.s["justify"] as? String) ?? ""
|
|
273
|
+
let isCentered = (node.s["center"] as? Bool) == true
|
|
274
|
+
let verticalAlign = isCentered ? VerticalAlignment.center : OStyle.verticalAlignment(node.s["align"])
|
|
275
|
+
|
|
276
|
+
return HStack(alignment: verticalAlign, spacing: gap) {
|
|
277
|
+
if isCentered || justify == "center" || justify == "end" || justify == "around" || justify == "evenly" {
|
|
278
|
+
Spacer(minLength: 0)
|
|
279
|
+
}
|
|
280
|
+
ForEach(Array(node.c.enumerated()), id: \.element.id) { idx, child in
|
|
281
|
+
if idx > 0 && (justify == "between" || justify == "around" || justify == "evenly") {
|
|
282
|
+
Spacer(minLength: 0)
|
|
283
|
+
}
|
|
110
284
|
ONodeView(node: child)
|
|
111
285
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
private var children: some View {
|
|
116
|
-
ForEach(node.c) { child in
|
|
117
|
-
ONodeView(node: child, stretch: ["row", "col", "box"].contains(child.t))
|
|
286
|
+
if isCentered || justify == "center" || justify == "around" || justify == "evenly" {
|
|
287
|
+
Spacer(minLength: 0)
|
|
288
|
+
}
|
|
118
289
|
}
|
|
119
290
|
}
|
|
120
291
|
|
|
121
292
|
private var label: some View {
|
|
122
293
|
let isHeading = node.t == "heading"
|
|
123
294
|
let isButton = node.t == "button"
|
|
124
|
-
let
|
|
125
|
-
let weight = OStyle.weight(node.s["weight"]) ?? (isHeading ? .bold : (isButton ? .semibold : .regular))
|
|
295
|
+
let baseSize = OStyle.number(node.s["size"]) ?? (isHeading ? 28 : 17)
|
|
296
|
+
let weight = OStyle.weight(node.s["weight"] ?? node.s["fontWeight"]) ?? (isHeading ? .bold : (isButton ? .semibold : .regular))
|
|
126
297
|
let fallbackColor: Color = isButton || node.t == "link" ? .accentColor : .primary
|
|
298
|
+
let lineHeight = OStyle.number(node.s["lineHeight"])
|
|
299
|
+
let lineSpacing: CGFloat = {
|
|
300
|
+
guard let lh = lineHeight else { return 0 }
|
|
301
|
+
return lh <= 4 ? max(0, baseSize * (lh - 1)) : max(0, lh - baseSize)
|
|
302
|
+
}()
|
|
127
303
|
|
|
128
304
|
return Text(node.x ?? "")
|
|
129
|
-
.font(.
|
|
305
|
+
.font(OStyle.font(node.s["font"], size: baseSize, weight: weight))
|
|
306
|
+
.lineSpacing(lineSpacing)
|
|
307
|
+
.multilineTextAlignment(OStyle.textAlign(node.s["textAlign"] ?? ((node.s["center"] as? Bool) == true ? "center" : nil)))
|
|
130
308
|
.foregroundColor(OStyle.color(node.s["color"]) ?? fallbackColor)
|
|
309
|
+
.fixedSize(horizontal: false, vertical: true)
|
|
131
310
|
.modifier(OBoxStyle(s: node.s))
|
|
132
311
|
}
|
|
133
312
|
}
|
|
134
313
|
|
|
314
|
+
// `label:` is what VoiceOver says; label: "" hides a decorative element
|
|
315
|
+
struct OLabel: ViewModifier {
|
|
316
|
+
let text: String?
|
|
317
|
+
|
|
318
|
+
func body(content: Content) -> some View {
|
|
319
|
+
if let text {
|
|
320
|
+
if text.isEmpty {
|
|
321
|
+
content.accessibilityHidden(true)
|
|
322
|
+
} else {
|
|
323
|
+
content.accessibilityLabel(text)
|
|
324
|
+
}
|
|
325
|
+
} else {
|
|
326
|
+
content
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
struct OButtonStyle: ButtonStyle {
|
|
332
|
+
let pressed: [String: Any]?
|
|
333
|
+
@Environment(\.accessibilityReduceMotion) private var reduceMotion
|
|
334
|
+
|
|
335
|
+
func makeBody(configuration: Configuration) -> some View {
|
|
336
|
+
let pressedOpacity = OStyle.number(pressed?["opacity"]).map { Double($0) } ?? 0.72
|
|
337
|
+
return configuration.label
|
|
338
|
+
.opacity(configuration.isPressed ? pressedOpacity : 1)
|
|
339
|
+
.scaleEffect(configuration.isPressed && !reduceMotion ? 0.98 : 1)
|
|
340
|
+
.animation(reduceMotion ? nil : .easeOut(duration: 0.12), value: configuration.isPressed)
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
struct OFlowLayout: Layout {
|
|
345
|
+
var spacing: CGFloat = 8
|
|
346
|
+
var lineSpacing: CGFloat = 8
|
|
347
|
+
|
|
348
|
+
func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
|
|
349
|
+
let maxWidth = proposal.width ?? .infinity
|
|
350
|
+
var x: CGFloat = 0
|
|
351
|
+
var y: CGFloat = 0
|
|
352
|
+
var rowHeight: CGFloat = 0
|
|
353
|
+
var maxUsedWidth: CGFloat = 0
|
|
354
|
+
|
|
355
|
+
for subview in subviews {
|
|
356
|
+
let size = subview.sizeThatFits(.unspecified)
|
|
357
|
+
if x > 0 && x + size.width > maxWidth {
|
|
358
|
+
x = 0
|
|
359
|
+
y += rowHeight + lineSpacing
|
|
360
|
+
rowHeight = 0
|
|
361
|
+
}
|
|
362
|
+
x += size.width + (x > 0 ? spacing : 0)
|
|
363
|
+
rowHeight = max(rowHeight, size.height)
|
|
364
|
+
maxUsedWidth = max(maxUsedWidth, x)
|
|
365
|
+
}
|
|
366
|
+
return CGSize(width: proposal.width ?? maxUsedWidth, height: y + rowHeight)
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
|
|
370
|
+
var x = bounds.minX
|
|
371
|
+
var y = bounds.minY
|
|
372
|
+
var rowHeight: CGFloat = 0
|
|
373
|
+
|
|
374
|
+
for subview in subviews {
|
|
375
|
+
let size = subview.sizeThatFits(.unspecified)
|
|
376
|
+
if x > bounds.minX && x + size.width > bounds.maxX {
|
|
377
|
+
x = bounds.minX
|
|
378
|
+
y += rowHeight + lineSpacing
|
|
379
|
+
rowHeight = 0
|
|
380
|
+
}
|
|
381
|
+
subview.place(at: CGPoint(x: x, y: y), proposal: ProposedViewSize(size))
|
|
382
|
+
x += size.width + spacing
|
|
383
|
+
rowHeight = max(rowHeight, size.height)
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
135
388
|
struct OInput: View {
|
|
136
389
|
let node: ONode
|
|
137
390
|
@EnvironmentObject private var engine: OrvynEngine
|
|
138
391
|
@State private var text = ""
|
|
392
|
+
@FocusState private var focused: Bool
|
|
139
393
|
|
|
140
394
|
var body: some View {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
395
|
+
let isDisabled = (node.s["disabled"] as? Bool) == true
|
|
396
|
+
let hasError = node.s["error"] != nil && (node.s["error"] as? Bool) != false
|
|
397
|
+
let border = OStyle.border(node.s["border"])
|
|
398
|
+
let strokeColor: Color = hasError
|
|
399
|
+
? .red
|
|
400
|
+
: (focused ? .accentColor : (border?.color ?? Color.gray.opacity(0.4)))
|
|
401
|
+
let strokeWidth: CGFloat = hasError || focused ? max(1.5, border?.width ?? 1.5) : (border?.width ?? 1)
|
|
402
|
+
|
|
403
|
+
if node.inputType == "date" {
|
|
404
|
+
ODate(node: node)
|
|
405
|
+
} else {
|
|
406
|
+
field(isDisabled: isDisabled, strokeColor: strokeColor, strokeWidth: strokeWidth)
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
@ViewBuilder
|
|
411
|
+
private func field(isDisabled: Bool, strokeColor: Color, strokeWidth: CGFloat) -> some View {
|
|
412
|
+
Group {
|
|
413
|
+
if node.inputType == "password" {
|
|
414
|
+
// never show what is typed
|
|
415
|
+
SecureField(node.ph ?? "", text: $text)
|
|
416
|
+
.textContentType(.password)
|
|
417
|
+
} else if node.inputType == "multiline" {
|
|
418
|
+
TextField(node.ph ?? "", text: $text, axis: .vertical)
|
|
419
|
+
.lineLimit(4...10)
|
|
420
|
+
} else {
|
|
421
|
+
TextField(node.ph ?? "", text: $text)
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
.focused($focused)
|
|
425
|
+
.disabled(isDisabled)
|
|
426
|
+
.opacity(isDisabled ? 0.5 : 1)
|
|
427
|
+
.keyboardType(OStyle.keyboard(node.inputType))
|
|
428
|
+
.textInputAutocapitalization(node.inputType == "email" || node.inputType == "password" ? .never : .sentences)
|
|
429
|
+
.disableAutocorrection(node.inputType == "email" || node.inputType == "password")
|
|
145
430
|
.padding(OStyle.number(node.s["padding"]) ?? 10)
|
|
146
431
|
.background(
|
|
147
432
|
RoundedRectangle(cornerRadius: OStyle.number(node.s["radius"]) ?? 8)
|
|
148
|
-
.
|
|
149
|
-
|
|
433
|
+
.fill(OStyle.color(node.s["bg"]) ?? Color.clear)
|
|
434
|
+
)
|
|
435
|
+
.overlay(
|
|
436
|
+
RoundedRectangle(cornerRadius: OStyle.number(node.s["radius"]) ?? 8)
|
|
437
|
+
.stroke(strokeColor, lineWidth: strokeWidth)
|
|
150
438
|
)
|
|
151
439
|
.onAppear { text = node.v ?? "" }
|
|
152
440
|
.onChange(of: text) { value in
|
|
@@ -158,6 +446,57 @@ struct OInput: View {
|
|
|
158
446
|
}
|
|
159
447
|
}
|
|
160
448
|
|
|
449
|
+
// the value is text like "2000-01-31"; empty until a date is chosen
|
|
450
|
+
struct ODate: View {
|
|
451
|
+
let node: ONode
|
|
452
|
+
@EnvironmentObject private var engine: OrvynEngine
|
|
453
|
+
|
|
454
|
+
private static let format: DateFormatter = {
|
|
455
|
+
let formatter = DateFormatter()
|
|
456
|
+
formatter.locale = Locale(identifier: "en_US_POSIX")
|
|
457
|
+
formatter.dateFormat = "yyyy-MM-dd"
|
|
458
|
+
return formatter
|
|
459
|
+
}()
|
|
460
|
+
|
|
461
|
+
var body: some View {
|
|
462
|
+
let chosen = Self.format.date(from: node.v ?? "")
|
|
463
|
+
if let chosen {
|
|
464
|
+
DatePicker(node.ph ?? "Date", selection: Binding(
|
|
465
|
+
get: { chosen },
|
|
466
|
+
set: { engine.fire(node.change, Self.format.string(from: $0)) }
|
|
467
|
+
), displayedComponents: .date)
|
|
468
|
+
} else {
|
|
469
|
+
Button("\(node.ph ?? "Date"): choose") {
|
|
470
|
+
engine.fire(node.change, Self.format.string(from: Date()))
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
private struct OPin: Identifiable {
|
|
477
|
+
let id = 0
|
|
478
|
+
let coordinate: CLLocationCoordinate2D
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
// "latitude,longitude,zoom"
|
|
482
|
+
struct OMap: View {
|
|
483
|
+
let place: String
|
|
484
|
+
|
|
485
|
+
var body: some View {
|
|
486
|
+
let parts = place.split(separator: ",").compactMap { Double($0) }
|
|
487
|
+
if parts.count == 3 {
|
|
488
|
+
let center = CLLocationCoordinate2D(latitude: parts[0], longitude: parts[1])
|
|
489
|
+
let span = 360 / pow(2, parts[2])
|
|
490
|
+
Map(
|
|
491
|
+
coordinateRegion: .constant(MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: span, longitudeDelta: span))),
|
|
492
|
+
annotationItems: [OPin(coordinate: center)]
|
|
493
|
+
) { pin in
|
|
494
|
+
MapMarker(coordinate: pin.coordinate)
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
161
500
|
// MARK: - Styles
|
|
162
501
|
|
|
163
502
|
struct OBoxStyle: ViewModifier {
|
|
@@ -165,21 +504,27 @@ struct OBoxStyle: ViewModifier {
|
|
|
165
504
|
var stretch = false
|
|
166
505
|
// in a row, `align` means vertical alignment, so the row itself stays left
|
|
167
506
|
var isRow = false
|
|
507
|
+
@Environment(\.orvynWidth) private var parentWidth
|
|
168
508
|
|
|
169
509
|
func body(content: Content) -> some View {
|
|
170
510
|
let radius = OStyle.number(s["radius"]) ?? 0
|
|
171
511
|
let border = OStyle.border(s["border"])
|
|
172
512
|
let shadow = OStyle.number(s["shadow"]) ?? 0
|
|
173
|
-
let
|
|
513
|
+
let widthString = s["width"] as? String
|
|
514
|
+
let isFullPercent = widthString?.trimmingCharacters(in: .whitespaces) == "100%"
|
|
515
|
+
let resolvedWidth = OStyle.resolvedWidth(s["width"], parentWidth: parentWidth)
|
|
516
|
+
let isCentered = (s["center"] as? Bool) == true
|
|
517
|
+
let align = isCentered ? Alignment.center : (isRow ? .leading : OStyle.frameAlignment(s["align"]))
|
|
174
518
|
|
|
175
519
|
return content
|
|
176
520
|
.padding(.horizontal, OStyle.number(s["paddingX"]) ?? OStyle.number(s["padding"]) ?? 0)
|
|
177
521
|
.padding(.vertical, OStyle.number(s["paddingY"]) ?? OStyle.number(s["padding"]) ?? 0)
|
|
178
|
-
.frame(width:
|
|
522
|
+
.frame(width: isFullPercent ? nil : resolvedWidth,
|
|
179
523
|
height: OStyle.number(s["height"]))
|
|
180
|
-
.frame(maxWidth:
|
|
524
|
+
.frame(maxWidth: isFullPercent || stretch ? .infinity : OStyle.number(s["maxWidth"]),
|
|
181
525
|
minHeight: OStyle.number(s["minHeight"]),
|
|
182
|
-
|
|
526
|
+
maxHeight: OStyle.number(s["maxHeight"]),
|
|
527
|
+
alignment: align)
|
|
183
528
|
.background(OStyle.color(s["bg"]) ?? Color.clear)
|
|
184
529
|
.clipShape(RoundedRectangle(cornerRadius: radius))
|
|
185
530
|
.overlay(
|
|
@@ -193,17 +538,51 @@ struct OBoxStyle: ViewModifier {
|
|
|
193
538
|
}
|
|
194
539
|
|
|
195
540
|
enum OStyle {
|
|
541
|
+
static func keyboard(_ kind: String?) -> UIKeyboardType {
|
|
542
|
+
switch kind {
|
|
543
|
+
case "email": return .emailAddress
|
|
544
|
+
case "number": return .numbersAndPunctuation
|
|
545
|
+
case "tel": return .phonePad
|
|
546
|
+
default: return .default
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
|
|
196
550
|
static func number(_ value: Any?) -> CGFloat? {
|
|
197
551
|
if let number = value as? NSNumber, CFGetTypeID(number) != CFBooleanGetTypeID() {
|
|
198
552
|
return CGFloat(truncating: number)
|
|
199
553
|
}
|
|
200
554
|
if let string = value as? String {
|
|
555
|
+
if string.contains("%") { return nil }
|
|
201
556
|
let cleaned = string.replacingOccurrences(of: "px", with: "").trimmingCharacters(in: .whitespaces)
|
|
202
557
|
if let double = Double(cleaned) { return CGFloat(double) }
|
|
203
558
|
}
|
|
204
559
|
return nil
|
|
205
560
|
}
|
|
206
561
|
|
|
562
|
+
static func resolvedWidth(_ value: Any?, parentWidth: CGFloat) -> CGFloat? {
|
|
563
|
+
if let string = value as? String {
|
|
564
|
+
let trimmed = string.trimmingCharacters(in: .whitespaces)
|
|
565
|
+
if trimmed.hasSuffix("%"), let pct = Double(trimmed.dropLast()), parentWidth > 0 {
|
|
566
|
+
return max(0, parentWidth * CGFloat(pct / 100.0))
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
return number(value)
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
static func font(_ value: Any?, size: CGFloat, weight: Font.Weight) -> Font {
|
|
573
|
+
let scaled = UIFontMetrics.default.scaledValue(for: size)
|
|
574
|
+
switch (value as? String)?.trimmingCharacters(in: .whitespaces) {
|
|
575
|
+
case "mono":
|
|
576
|
+
return .system(size: scaled, weight: weight, design: .monospaced)
|
|
577
|
+
case "serif":
|
|
578
|
+
return .system(size: scaled, weight: weight, design: .serif)
|
|
579
|
+
case "sans", nil, "":
|
|
580
|
+
return .system(size: scaled, weight: weight)
|
|
581
|
+
case let custom?:
|
|
582
|
+
return .custom(custom, size: size, relativeTo: .body)
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
207
586
|
static func weight(_ value: Any?) -> Font.Weight? {
|
|
208
587
|
if let n = number(value) {
|
|
209
588
|
switch n {
|
|
@@ -233,6 +612,14 @@ enum OStyle {
|
|
|
233
612
|
}
|
|
234
613
|
}
|
|
235
614
|
|
|
615
|
+
static func verticalAlignment(_ value: Any?) -> VerticalAlignment {
|
|
616
|
+
switch value as? String {
|
|
617
|
+
case "start", "flex-start", "top": return .top
|
|
618
|
+
case "end", "flex-end", "bottom": return .bottom
|
|
619
|
+
default: return .center
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
|
|
236
623
|
static func frameAlignment(_ value: Any?) -> Alignment {
|
|
237
624
|
switch value as? String {
|
|
238
625
|
case "center": return .center
|
|
@@ -241,6 +628,14 @@ enum OStyle {
|
|
|
241
628
|
}
|
|
242
629
|
}
|
|
243
630
|
|
|
631
|
+
static func textAlign(_ value: Any?) -> TextAlignment {
|
|
632
|
+
switch value as? String {
|
|
633
|
+
case "center": return .center
|
|
634
|
+
case "right", "end": return .trailing
|
|
635
|
+
default: return .leading
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
|
|
244
639
|
static func border(_ value: Any?) -> (width: CGFloat, color: Color)? {
|
|
245
640
|
guard let string = value as? String else { return nil }
|
|
246
641
|
let parts = string.split(separator: " ").map(String.init)
|