orvyn-lang 0.3.0 → 0.4.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.
@@ -34,10 +34,30 @@ struct OrvynRootView: View {
34
34
  }
35
35
  } else if let root = engine.root {
36
36
  GeometryReader { geo in
37
- ScrollView {
38
- ONodeView(node: root)
39
- .frame(maxWidth: .infinity, alignment: .topLeading)
40
- .environment(\.orvynWidth, geo.size.width)
37
+ ScrollViewReader { proxy in
38
+ ScrollView {
39
+ ONodeView(node: root)
40
+ .frame(maxWidth: .infinity, alignment: .topLeading)
41
+ .environment(\.orvynWidth, geo.size.width)
42
+ .id("top")
43
+ }
44
+ .overlay(alignment: .top) {
45
+ if let sticky = OSticky.find(root) {
46
+ ONodeView(node: sticky, stretch: true, pinned: true)
47
+ .frame(maxWidth: .infinity)
48
+ .environment(\.orvynWidth, geo.size.width)
49
+ }
50
+ }
51
+ .onChange(of: engine.scrollTick) { _ in
52
+ guard let target = engine.scrollTarget else { return }
53
+ withAnimation(.easeInOut(duration: 0.5)) { proxy.scrollTo(target, anchor: .top) }
54
+ }
55
+ .onAppear {
56
+ // for screenshots and tests: start at a section
57
+ if let start = ProcessInfo.processInfo.environment["ORVYN_SCROLL_TO"] {
58
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) { proxy.scrollTo(start, anchor: .top) }
59
+ }
60
+ }
41
61
  }
42
62
  }
43
63
  } else {
@@ -70,6 +90,8 @@ struct ONodeView: View {
70
90
  let node: ONode
71
91
  // containers inside a Column fill the width, like on the web
72
92
  var stretch = false
93
+ /// the pinned copy of a sticky element; the one in the page keeps its space but is hidden
94
+ var pinned = false
73
95
  @EnvironmentObject private var engine: OrvynEngine
74
96
  @Environment(\.orvynWidth) private var parentWidth
75
97
 
@@ -77,7 +99,11 @@ struct ONodeView: View {
77
99
  let rtl = node.s["rtl"] as? Bool
78
100
  let content = coreView
79
101
  .environment(\.orvynWidth, innerWidth)
102
+ .modifier(OCenterSelf(enabled: (node.s["centerSelf"] as? Bool) == true))
103
+ .modifier(OStickyPlaceholder(hidden: !pinned && (node.s["position"] as? String) == "sticky"))
80
104
  .modifier(OLabel(text: node.a))
105
+ .modifier(OScrollAnchor(label: ["col", "box", "row"].contains(node.t) ? node.a : nil))
106
+ .modifier(OAppear(spec: node.s["appear"] as? String, from: node.s["appearFrom"] as? [String: Any], onShow: node.visible.map { handler in { engine.fire(handler) } }))
81
107
 
82
108
  if let rtl {
83
109
  content.environment(\.layoutDirection, rtl ? .rightToLeft : .leftToRight)
@@ -86,6 +112,35 @@ struct ONodeView: View {
86
112
  }
87
113
  }
88
114
 
115
+ // position: "absolute" children float over their parent, like on the web
116
+ private var flowChildren: [ONode] { node.c.filter { ($0.s["position"] as? String) != "absolute" } }
117
+ private var absoluteChildren: [ONode] { node.c.filter { ($0.s["position"] as? String) == "absolute" } }
118
+
119
+ @ViewBuilder
120
+ private var absoluteLayer: some View {
121
+ if !absoluteChildren.isEmpty {
122
+ GeometryReader { geo in
123
+ ZStack(alignment: .topLeading) {
124
+ ForEach(absoluteChildren) { child in
125
+ let left = OStyle.offset(child.s["left"], of: geo.size.width)
126
+ let right = OStyle.offset(child.s["right"], of: geo.size.width)
127
+ let top = OStyle.offset(child.s["top"], of: geo.size.height)
128
+ let bottom = OStyle.offset(child.s["bottom"], of: geo.size.height)
129
+ ONodeView(node: child)
130
+ .fixedSize()
131
+ .padding(.leading, left ?? 0)
132
+ .padding(.trailing, left == nil ? (right ?? 0) : 0)
133
+ .padding(.top, top ?? 0)
134
+ .padding(.bottom, top == nil ? (bottom ?? 0) : 0)
135
+ .frame(width: geo.size.width, height: geo.size.height,
136
+ alignment: Alignment(horizontal: left == nil && right != nil ? .trailing : .leading,
137
+ vertical: top == nil && bottom != nil ? .bottom : .top))
138
+ }
139
+ }
140
+ }
141
+ }
142
+ }
143
+
89
144
  @ViewBuilder
90
145
  private var coreView: some View {
91
146
  switch node.t {
@@ -107,15 +162,21 @@ struct ONodeView: View {
107
162
  let isScrollable = (scrollMode as? Bool) == true || (scrollMode as? String) == "horizontal" || (scrollMode as? String) == "both"
108
163
 
109
164
  Group {
110
- if isWrapped {
111
- OFlowLayout(spacing: gap, lineSpacing: gap) {
165
+ if let marquee = OMarqueeSpec(node.s["marquee"]) {
166
+ OMarquee(spec: marquee) { rowContent }
167
+ } else if isWrapped {
168
+ OFlowLayout(spacing: gap, lineSpacing: gap,
169
+ centered: (node.s["justify"] as? String) == "center" || (node.s["center"] as? Bool) == true,
170
+ spaced: (node.s["justify"] as? String) == "between",
171
+ alignEnd: (node.s["align"] as? String) == "end") {
112
172
  ForEach(node.c) { child in
113
173
  ONodeView(node: child)
174
+ .layoutValue(key: OFillShare.self, value: OFillShare.of(child))
114
175
  }
115
176
  }
116
177
  } else if isScrollable {
117
178
  ScrollView(.horizontal, showsIndicators: false) { rowContent }
118
- } else if !node.c.contains(where: { ["col", "box"].contains($0.t) }) {
179
+ } else if !node.c.contains(where: { ["col", "box", "text", "heading"].contains($0.t) }) {
119
180
  ViewThatFits(in: .horizontal) {
120
181
  rowContent
121
182
  ScrollView(.horizontal, showsIndicators: false) { rowContent }
@@ -135,11 +196,13 @@ struct ONodeView: View {
135
196
  if isCentered || justify == "center" || justify == "end" || justify == "around" || justify == "evenly" {
136
197
  Spacer(minLength: 0)
137
198
  }
138
- ForEach(Array(node.c.enumerated()), id: \.element.id) { idx, child in
199
+ ForEach(Array(flowChildren.enumerated()), id: \.element.id) { idx, child in
139
200
  if idx > 0 && (justify == "between" || justify == "around" || justify == "evenly") {
140
201
  Spacer(minLength: 0)
141
202
  }
142
- ONodeView(node: child, stretch: ["row", "col", "box"].contains(child.t))
203
+ // like a web column: boxes always fill it, buttons too unless the column aligns its items
204
+ ONodeView(node: child, stretch: ["row", "col", "box"].contains(child.t)
205
+ || (child.t == "button" && node.s["align"] == nil && (node.s["center"] as? Bool) != true))
143
206
  }
144
207
  if isCentered || justify == "center" || justify == "around" || justify == "evenly" {
145
208
  Spacer(minLength: 0)
@@ -150,9 +213,9 @@ struct ONodeView: View {
150
213
  let isScrollable = (scrollMode as? Bool) == true || (scrollMode as? String) == "vertical" || (scrollMode as? String) == "both"
151
214
  Group {
152
215
  if isScrollable {
153
- ScrollView(.vertical) { stack }
216
+ ScrollView(.vertical) { stack.overlay { absoluteLayer } }
154
217
  } else {
155
- stack
218
+ stack.overlay { absoluteLayer }
156
219
  }
157
220
  }
158
221
  .modifier(OBoxStyle(s: node.s, stretch: stretch))
@@ -191,7 +254,7 @@ struct ONodeView: View {
191
254
  .buttonStyle(OButtonStyle(pressed: pressedStyle))
192
255
  .disabled(isDisabled || isLoading)
193
256
  .opacity(isDisabled ? 0.5 : (isLoading ? 0.75 : 1))
194
- .fixedSize(horizontal: node.c.isEmpty && (node.x ?? "").count <= 24 && !isLoading, vertical: false)
257
+ .fixedSize(horizontal: !isWideButton && node.c.isEmpty && (node.x ?? "").count <= 24 && !isLoading, vertical: false)
195
258
 
196
259
  case "input":
197
260
  OInput(node: node)
@@ -247,12 +310,23 @@ struct ONodeView: View {
247
310
  .modifier(OBoxStyle(s: node.s, stretch: stretch))
248
311
 
249
312
  case "image":
250
- AsyncImage(url: URL(string: node.src ?? "")) { image in
251
- image.resizable().scaledToFit()
252
- } placeholder: {
253
- ProgressView()
313
+ if let src = node.src, src.hasPrefix("asset:"), let image = OBundledImage.load(src) {
314
+ Image(uiImage: image).resizable().scaledToFit()
315
+ .modifier(OBoxStyle(s: node.s))
316
+ } else if let src = node.src, src.hasPrefix("data:"),
317
+ let comma = src.firstIndex(of: ","),
318
+ let data = Data(base64Encoded: String(src[src.index(after: comma)...])),
319
+ let image = UIImage(data: data) {
320
+ Image(uiImage: image).resizable().scaledToFit()
321
+ .modifier(OBoxStyle(s: node.s))
322
+ } else {
323
+ AsyncImage(url: URL(string: node.src ?? "")) { image in
324
+ image.resizable().scaledToFit()
325
+ } placeholder: {
326
+ ProgressView()
327
+ }
328
+ .modifier(OBoxStyle(s: node.s))
254
329
  }
255
- .modifier(OBoxStyle(s: node.s))
256
330
 
257
331
  default:
258
332
  EmptyView()
@@ -289,9 +363,17 @@ struct ONodeView: View {
289
363
  }
290
364
  }
291
365
 
366
+ /// a button fills its column on the web (and with width: "100%"); its words are centred
367
+ private var isWideButton: Bool {
368
+ node.t == "button" && (stretch || (node.s["width"] as? String)?.trimmingCharacters(in: .whitespaces) == "100%")
369
+ }
370
+
292
371
  private var label: some View {
293
372
  let isHeading = node.t == "heading"
294
373
  let isButton = node.t == "button"
374
+ let boxStyle: [String: Any] = isButton && node.s["textAlign"] == nil
375
+ ? node.s.merging(["center": true]) { own, _ in own }
376
+ : node.s
295
377
  let baseSize = OStyle.number(node.s["size"]) ?? (isHeading ? 28 : 17)
296
378
  let weight = OStyle.weight(node.s["weight"] ?? node.s["fontWeight"]) ?? (isHeading ? .bold : (isButton ? .semibold : .regular))
297
379
  let fallbackColor: Color = isButton || node.t == "link" ? .accentColor : .primary
@@ -304,10 +386,11 @@ struct ONodeView: View {
304
386
  return Text(node.x ?? "")
305
387
  .font(OStyle.font(node.s["font"], size: baseSize, weight: weight))
306
388
  .lineSpacing(lineSpacing)
307
- .multilineTextAlignment(OStyle.textAlign(node.s["textAlign"] ?? ((node.s["center"] as? Bool) == true ? "center" : nil)))
389
+ .tracking(OStyle.letterSpacing(node.s["letterSpacing"], size: baseSize))
390
+ .multilineTextAlignment(OStyle.textAlign(boxStyle["textAlign"] ?? ((boxStyle["center"] as? Bool) == true ? "center" : nil)))
308
391
  .foregroundColor(OStyle.color(node.s["color"]) ?? fallbackColor)
309
392
  .fixedSize(horizontal: false, vertical: true)
310
- .modifier(OBoxStyle(s: node.s))
393
+ .modifier(OBoxStyle(s: boxStyle, stretch: isWideButton))
311
394
  }
312
395
  }
313
396
 
@@ -344,6 +427,9 @@ struct OButtonStyle: ButtonStyle {
344
427
  struct OFlowLayout: Layout {
345
428
  var spacing: CGFloat = 8
346
429
  var lineSpacing: CGFloat = 8
430
+ var centered = false
431
+ var spaced = false
432
+ var alignEnd = false
347
433
 
348
434
  func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
349
435
  let maxWidth = proposal.width ?? .infinity
@@ -353,7 +439,7 @@ struct OFlowLayout: Layout {
353
439
  var maxUsedWidth: CGFloat = 0
354
440
 
355
441
  for subview in subviews {
356
- let size = subview.sizeThatFits(.unspecified)
442
+ let size = Self.measure(subview, maxWidth: maxWidth)
357
443
  if x > 0 && x + size.width > maxWidth {
358
444
  x = 0
359
445
  y += rowHeight + lineSpacing
@@ -366,21 +452,50 @@ struct OFlowLayout: Layout {
366
452
  return CGSize(width: proposal.width ?? maxUsedWidth, height: y + rowHeight)
367
453
  }
368
454
 
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
455
+ /// an item is never wider than the row: long text wraps inside it, like on the web
456
+ static func measure(_ subview: LayoutSubview, maxWidth: CGFloat) -> CGSize {
457
+ let ideal = subview.sizeThatFits(.unspecified)
458
+ guard maxWidth.isFinite else { return ideal }
459
+ // width: "100%" (or grow) takes the whole row, like a wrapped item on a phone-size web page
460
+ if let share = subview[OFillShare.self] {
461
+ let width = maxWidth * share
462
+ let fitted = subview.sizeThatFits(ProposedViewSize(width: width, height: nil))
463
+ return CGSize(width: width, height: fitted.height)
464
+ }
465
+ // keep the item's own width (never wider than the row), then measure its
466
+ // height at that width so wrapped text gets the lines it needs
467
+ let width = min(ideal.width, maxWidth)
468
+ let fitted = subview.sizeThatFits(ProposedViewSize(width: width, height: nil))
469
+ return CGSize(width: width, height: fitted.height)
470
+ }
373
471
 
472
+ func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
473
+ // group items into lines first, so a centred row can centre each line
474
+ var lines: [[(LayoutSubview, CGSize)]] = [[]]
475
+ var x: CGFloat = 0
374
476
  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
477
+ let size = Self.measure(subview, maxWidth: bounds.width)
478
+ if x > 0 && x + size.width > bounds.width {
479
+ lines.append([])
480
+ x = 0
380
481
  }
381
- subview.place(at: CGPoint(x: x, y: y), proposal: ProposedViewSize(size))
482
+ lines[lines.count - 1].append((subview, size))
382
483
  x += size.width + spacing
383
- rowHeight = max(rowHeight, size.height)
484
+ }
485
+ var y = bounds.minY
486
+ for line in lines where !line.isEmpty {
487
+ let used = line.map { $0.1.width }.reduce(0, +) + spacing * CGFloat(line.count - 1)
488
+ var lineX = bounds.minX + (centered ? max(0, (bounds.width - used) / 2) : 0)
489
+ // justify: between spreads the items of a line to both edges
490
+ let extra = spaced && line.count > 1 ? max(0, (bounds.width - used) / CGFloat(line.count - 1)) : 0
491
+ let lineHeight = line.map { $0.1.height }.max() ?? 0
492
+ for (subview, size) in line {
493
+ // align: end puts shorter items on the line's bottom edge
494
+ let dy = alignEnd ? lineHeight - size.height : 0
495
+ subview.place(at: CGPoint(x: lineX, y: y + dy), proposal: ProposedViewSize(size))
496
+ lineX += size.width + spacing + extra
497
+ }
498
+ y += lineHeight + lineSpacing
384
499
  }
385
500
  }
386
501
  }
@@ -517,27 +632,43 @@ struct OBoxStyle: ViewModifier {
517
632
  let align = isCentered ? Alignment.center : (isRow ? .leading : OStyle.frameAlignment(s["align"]))
518
633
 
519
634
  return content
520
- .padding(.horizontal, OStyle.number(s["paddingX"]) ?? OStyle.number(s["padding"]) ?? 0)
521
- .padding(.vertical, OStyle.number(s["paddingY"]) ?? OStyle.number(s["padding"]) ?? 0)
635
+ .padding(.leading, OStyle.number(s["paddingLeft"]) ?? OStyle.number(s["paddingX"]) ?? OStyle.number(s["padding"]) ?? 0)
636
+ .padding(.trailing, OStyle.number(s["paddingRight"]) ?? OStyle.number(s["paddingX"]) ?? OStyle.number(s["padding"]) ?? 0)
637
+ .padding(.top, OStyle.number(s["paddingTop"]) ?? OStyle.number(s["paddingY"]) ?? OStyle.number(s["padding"]) ?? 0)
638
+ .padding(.bottom, OStyle.number(s["paddingBottom"]) ?? OStyle.number(s["paddingY"]) ?? OStyle.number(s["padding"]) ?? 0)
522
639
  .frame(width: isFullPercent ? nil : resolvedWidth,
523
640
  height: OStyle.number(s["height"]))
524
- .frame(maxWidth: isFullPercent || stretch ? .infinity : OStyle.number(s["maxWidth"]),
641
+ .frame(minWidth: OStyle.number(s["minWidth"]),
642
+ maxWidth: isFullPercent || stretch ? (OStyle.number(s["maxWidth"]) ?? .infinity) : OStyle.number(s["maxWidth"]),
525
643
  minHeight: OStyle.number(s["minHeight"]),
526
644
  maxHeight: OStyle.number(s["maxHeight"]),
527
645
  alignment: align)
528
- .background(OStyle.color(s["bg"]) ?? Color.clear)
646
+ .background(OBackground(s: s))
529
647
  .clipShape(RoundedRectangle(cornerRadius: radius))
648
+ .modifier(OClip(enabled: (s["overflow"] as? String) == "hidden"))
530
649
  .overlay(
531
650
  RoundedRectangle(cornerRadius: radius)
532
651
  .stroke(border?.color ?? .clear, lineWidth: border?.width ?? 0)
533
652
  )
534
653
  .shadow(color: .black.opacity(shadow > 0 ? 0.08 * shadow : 0), radius: 4 * shadow, y: 2 * shadow)
535
654
  .opacity(OStyle.number(s["opacity"]).map { Double($0) } ?? 1)
655
+ .animation(OStyle.animation(s["animate"]), value: String(describing: s))
536
656
  .padding(OStyle.number(s["margin"]) ?? 0)
537
657
  }
538
658
  }
539
659
 
540
660
  enum OStyle {
661
+ static func animation(_ value: Any?) -> Animation? {
662
+ guard let text = value as? String else { return nil }
663
+ let parts = text.split(separator: " ")
664
+ let seconds = Double(parts.last?.replacingOccurrences(of: "s", with: "") ?? "") ?? 0.3
665
+ switch parts.first {
666
+ case "linear": return .linear(duration: seconds)
667
+ case "spring": return .spring(response: seconds, dampingFraction: 0.8)
668
+ case "ease": return .easeInOut(duration: seconds)
669
+ default: return nil
670
+ }
671
+ }
541
672
  static func keyboard(_ kind: String?) -> UIKeyboardType {
542
673
  switch kind {
543
674
  case "email": return .emailAddress
@@ -547,6 +678,24 @@ enum OStyle {
547
678
  }
548
679
  }
549
680
 
681
+ /// letterSpacing: points, or "-0.04em" (a share of the text size)
682
+ static func letterSpacing(_ value: Any?, size: CGFloat) -> CGFloat {
683
+ if let string = (value as? String)?.trimmingCharacters(in: .whitespaces), string.hasSuffix("em"),
684
+ let em = Double(string.dropLast(2)) {
685
+ return size * CGFloat(em)
686
+ }
687
+ return number(value) ?? 0
688
+ }
689
+
690
+ /// top/left/right/bottom: points, or a percent of the parent
691
+ static func offset(_ value: Any?, of size: CGFloat) -> CGFloat? {
692
+ if let string = value as? String, string.hasSuffix("%"),
693
+ let pct = Double(string.dropLast().trimmingCharacters(in: .whitespaces)) {
694
+ return size * CGFloat(pct / 100)
695
+ }
696
+ return number(value)
697
+ }
698
+
550
699
  static func number(_ value: Any?) -> CGFloat? {
551
700
  if let number = value as? NSNumber, CFGetTypeID(number) != CFBooleanGetTypeID() {
552
701
  return CGFloat(truncating: number)
@@ -671,3 +820,187 @@ enum OStyle {
671
820
  return Color(.sRGB, red: r, green: g, blue: b, opacity: a)
672
821
  }
673
822
  }
823
+
824
+ // Images from public/ ship in the app's "public" folder. Each one is read
825
+ // from disk once; screen updates reuse the decoded picture.
826
+ enum OBundledImage {
827
+ private static let cache = NSCache<NSString, UIImage>()
828
+
829
+ static func load(_ src: String) -> UIImage? {
830
+ let key = src as NSString
831
+ if let image = cache.object(forKey: key) { return image }
832
+ let path = String(src.dropFirst("asset:/".count))
833
+ guard let folder = Bundle.main.resourceURL?.appendingPathComponent("public"),
834
+ let image = UIImage(contentsOfFile: folder.appendingPathComponent(path).path) else { return nil }
835
+ cache.setObject(image, forKey: key)
836
+ return image
837
+ }
838
+ }
839
+
840
+ // `appear: "fade-up 0.6s 0.2s"` plays once when the element is first shown;
841
+ // `onVisible:` runs at the same moment.
842
+ struct OAppear: ViewModifier {
843
+ let spec: String?
844
+ /// animation pop { opacity: 0, y: 30, scale: 0.8, rotate: -6 }: the start state
845
+ let from: [String: Any]?
846
+ let onShow: (() -> Void)?
847
+ @State private var shown = false
848
+ @Environment(\.accessibilityReduceMotion) private var reduceMotion
849
+
850
+ func body(content: Content) -> some View {
851
+ let parts = (spec ?? "").split(separator: " ").map(String.init)
852
+ let preset = parts.first ?? ""
853
+ let seconds = parts.count > 1 ? Double(parts[1].replacingOccurrences(of: "s", with: "")) ?? 0.5 : 0.5
854
+ let delay = parts.count > 2 ? Double(parts[2].replacingOccurrences(of: "s", with: "")) ?? 0 : 0
855
+ let hidden = !(spec ?? "").isEmpty && !shown && !reduceMotion
856
+ let distance: CGFloat = 24
857
+ func custom(_ key: String) -> CGFloat? { (from?[key] as? NSNumber).map { CGFloat($0.doubleValue) } }
858
+ let startOpacity = from != nil ? (custom("opacity") ?? 1) : 0
859
+ let startScale = from != nil ? (custom("scale") ?? 1) : (preset == "scale" ? 0.9 : 1)
860
+ let startX = from != nil ? (custom("x") ?? 0) : (preset == "slide-left" ? distance : preset == "slide-right" ? -distance : 0)
861
+ let startY = from != nil ? (custom("y") ?? 0) : (preset == "fade-up" ? distance : preset == "fade-down" ? -distance : 0)
862
+ return content
863
+ .opacity(hidden ? startOpacity : 1)
864
+ .scaleEffect(hidden ? startScale : 1)
865
+ .rotationEffect(.degrees(hidden ? Double(custom("rotate") ?? 0) : 0))
866
+ .offset(x: hidden ? startX : 0, y: hidden ? startY : 0)
867
+ // like the web: play once the element has scrolled onto the screen,
868
+ // not when the page is first built (that happens for the whole page at once)
869
+ .background(GeometryReader { geo in
870
+ let frame = geo.frame(in: .global)
871
+ Color.clear
872
+ .onAppear { reveal(frame, seconds: seconds, delay: delay) }
873
+ .onChange(of: frame.minY) { _ in reveal(geo.frame(in: .global), seconds: seconds, delay: delay) }
874
+ })
875
+ }
876
+
877
+ private func reveal(_ frame: CGRect, seconds: Double, delay: Double) {
878
+ guard !shown else { return }
879
+ let screen = UIScreen.main.bounds.height
880
+ // at least a little of it is inside the screen
881
+ guard frame.minY < screen * 0.9, frame.maxY > 0 else { return }
882
+ withAnimation(.timingCurve(0.2, 0.8, 0.2, 1, duration: seconds).delay(delay)) { shown = true }
883
+ onShow?()
884
+ }
885
+ }
886
+
887
+ /// lets scrollTo(label) find this element
888
+ struct OScrollAnchor: ViewModifier {
889
+ let label: String?
890
+ func body(content: Content) -> some View {
891
+ if let label, !label.isEmpty { content.id(label) } else { content }
892
+ }
893
+ }
894
+
895
+ /// overflow: "hidden" cuts off whatever sticks out of the box
896
+ struct OClip: ViewModifier {
897
+ let enabled: Bool
898
+ func body(content: Content) -> some View {
899
+ if enabled { content.clipped() } else { content }
900
+ }
901
+ }
902
+
903
+ /// bg: a colour, or a photo from `url(...)` filling the box (cover) with an optional dark tint
904
+ struct OBackground: View {
905
+ let s: [String: Any]
906
+ var body: some View {
907
+ ZStack {
908
+ OStyle.color(s["bg"]) ?? Color.clear
909
+ if let src = s["bgImage"] as? String {
910
+ Color.clear.overlay {
911
+ if src.hasPrefix("asset:"), let image = OBundledImage.load(src) {
912
+ Image(uiImage: image).resizable().scaledToFill()
913
+ } else if let url = URL(string: src) {
914
+ AsyncImage(url: url) { image in image.resizable().scaledToFill() } placeholder: { Color.clear }
915
+ }
916
+ }
917
+ .clipped()
918
+ Color.black.opacity((s["bgDim"] as? Double) ?? 0)
919
+ }
920
+ }
921
+ }
922
+ }
923
+
924
+ /// how much of a wrapping row an item asks for: 1 for width "100%" or grow, 0.5 for "50%"
925
+ struct OFillShare: LayoutValueKey {
926
+ static let defaultValue: CGFloat? = nil
927
+ static func of(_ node: ONode) -> CGFloat? {
928
+ if let width = (node.s["width"] as? String)?.trimmingCharacters(in: .whitespaces), width.hasSuffix("%"),
929
+ let pct = Double(width.dropLast()) {
930
+ return CGFloat(min(100, max(0, pct)) / 100)
931
+ }
932
+ if (node.s["centerSelf"] as? Bool) == true { return 1 }
933
+ if node.s["width"] == nil, let grow = OStyle.number(node.s["grow"]), grow > 0 { return 1 }
934
+ // a component's plain wrapper takes the size its one child asks for
935
+ if node.s.isEmpty, node.c.count == 1 { return of(node.c[0]) }
936
+ return nil
937
+ }
938
+ }
939
+
940
+ /// margin: "0 auto": centre the element in the space it is given
941
+ struct OCenterSelf: ViewModifier {
942
+ let enabled: Bool
943
+ func body(content: Content) -> some View {
944
+ if enabled { content.frame(maxWidth: .infinity, alignment: .center) } else { content }
945
+ }
946
+ }
947
+
948
+ /// position: "sticky" — the first sticky element stays pinned to the top while the page scrolls
949
+ enum OSticky {
950
+ static func find(_ node: ONode) -> ONode? {
951
+ if (node.s["position"] as? String) == "sticky" { return node }
952
+ for child in node.c { if let found = find(child) { return found } }
953
+ return nil
954
+ }
955
+ }
956
+
957
+ struct OStickyPlaceholder: ViewModifier {
958
+ let hidden: Bool
959
+ func body(content: Content) -> some View {
960
+ if hidden { content.opacity(0).allowsHitTesting(false).accessibilityHidden(true) } else { content }
961
+ }
962
+ }
963
+
964
+ /// marquee: "left 30s" — the row's items slide in an endless loop (two copies, so no gap)
965
+ struct OMarqueeSpec {
966
+ var left = true
967
+ var seconds: Double = 30
968
+ init?(_ value: Any?) {
969
+ if let flag = value as? Bool { if !flag { return nil }; return }
970
+ guard let text = value as? String, !text.isEmpty else { return nil }
971
+ for part in text.split(separator: " ") {
972
+ if part == "left" { left = true }
973
+ else if part == "right" { left = false }
974
+ else if part.hasSuffix("s"), let n = Double(part.dropLast()), n > 0 { seconds = n }
975
+ }
976
+ }
977
+ }
978
+
979
+ struct OMarquee<Content: View>: View {
980
+ let spec: OMarqueeSpec
981
+ @ViewBuilder let content: () -> Content
982
+ @State private var width: CGFloat = 0
983
+ @Environment(\.accessibilityReduceMotion) private var reduceMotion
984
+ @Environment(\.orvynWidth) private var available
985
+
986
+ var body: some View {
987
+ TimelineView(.animation(paused: reduceMotion)) { context in
988
+ let progress = CGFloat(context.date.timeIntervalSinceReferenceDate.truncatingRemainder(dividingBy: spec.seconds) / spec.seconds)
989
+ let shift = reduceMotion || width == 0 ? 0 : progress * width
990
+ HStack(spacing: 0) {
991
+ content()
992
+ .fixedSize()
993
+ .background(GeometryReader { geo in
994
+ Color.clear
995
+ .onAppear { width = geo.size.width }
996
+ .onChange(of: geo.size.width) { width = $0 }
997
+ })
998
+ content().fixedSize().accessibilityHidden(true)
999
+ }
1000
+ .offset(x: spec.left ? -shift : shift - width)
1001
+ }
1002
+ // only as wide as the space it is in: the long strip must not widen the page
1003
+ .frame(width: available > 0 ? available : nil, alignment: .leading)
1004
+ .clipped()
1005
+ }
1006
+ }
@@ -16,6 +16,8 @@ function generateIOSProject(appJs, config) {
16
16
  content: readFileSync(join(templateDir, name), "utf-8")
17
17
  })),
18
18
  { path: `${target}/app.js`, content: appJs },
19
+ // images from public/ are copied in here by the build; this keeps the folder
20
+ { path: `${target}/public/.orvyn`, content: "" },
19
21
  { path: `${target}-Info.plist`, content: infoPlist(config) },
20
22
  { path: `${target}/PrivacyInfo.xcprivacy`, content: PRIVACY },
21
23
  ...assets(target, config),
@@ -118,6 +120,7 @@ function pbxproj(target, config) {
118
120
  0A0000000000000000000004 /* app.js in Resources */ = {isa = PBXBuildFile; fileRef = 0B0000000000000000000004 /* app.js */; };
119
121
  0A0000000000000000000005 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0B0000000000000000000005 /* Assets.xcassets */; };
120
122
  0A0000000000000000000006 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 0B0000000000000000000006 /* PrivacyInfo.xcprivacy */; };
123
+ 0A0000000000000000000007 /* public in Resources */ = {isa = PBXBuildFile; fileRef = 0B0000000000000000000007 /* public */; };
121
124
  /* End PBXBuildFile section */
122
125
 
123
126
  /* Begin PBXFileReference section */
@@ -127,6 +130,7 @@ function pbxproj(target, config) {
127
130
  0B0000000000000000000004 /* app.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = app.js; sourceTree = "<group>"; };
128
131
  0B0000000000000000000005 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
129
132
  0B0000000000000000000006 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
133
+ 0B0000000000000000000007 /* public */ = {isa = PBXFileReference; lastKnownFileType = folder; path = public; sourceTree = "<group>"; };
130
134
  0B0000000000000000000010 /* ${target}.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ${q(`${target}.app`)}; sourceTree = BUILT_PRODUCTS_DIR; };
131
135
  /* End PBXFileReference section */
132
136
 
@@ -158,6 +162,7 @@ function pbxproj(target, config) {
158
162
  0B0000000000000000000004 /* app.js */,
159
163
  0B0000000000000000000005 /* Assets.xcassets */,
160
164
  0B0000000000000000000006 /* PrivacyInfo.xcprivacy */,
165
+ 0B0000000000000000000007 /* public */,
161
166
  );
162
167
  path = ${q(target)};
163
168
  sourceTree = "<group>";
@@ -226,6 +231,7 @@ function pbxproj(target, config) {
226
231
  0A0000000000000000000004 /* app.js in Resources */,
227
232
  0A0000000000000000000005 /* Assets.xcassets in Resources */,
228
233
  0A0000000000000000000006 /* PrivacyInfo.xcprivacy in Resources */,
234
+ 0A0000000000000000000007 /* public in Resources */,
229
235
  );
230
236
  runOnlyForDeploymentPostprocessing = 0;
231
237
  };