@c9up/orbit 0.1.0 → 0.1.2
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/Cargo.lock +9 -16
- package/Cargo.toml +4 -0
- package/README.md +7 -1
- package/android/src/main/java/com/c9up/orbit/OrbitEffects.kt +74 -1
- package/android/src/main/java/com/c9up/orbit/OrbitHost.kt +67 -4
- package/android/src/main/java/com/c9up/orbit/OrbitInput.kt +90 -8
- package/android/src/main/java/com/c9up/orbit/OrbitViews.kt +23 -4
- package/crates/orbit-host/Cargo.toml +1 -1
- package/crates/orbit-host/src/lib.rs +580 -115
- package/crates/orbit-host/src/motion.rs +6 -0
- package/crates/orbit-host/src/vector.rs +312 -56
- package/dist/color.d.ts.map +1 -1
- package/dist/color.js +3 -3
- package/dist/color.js.map +1 -1
- package/dist/commit.d.ts +38 -0
- package/dist/commit.d.ts.map +1 -1
- package/dist/commit.js +154 -13
- package/dist/commit.js.map +1 -1
- package/dist/errors.d.ts +60 -26
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +85 -11
- package/dist/errors.js.map +1 -1
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +4 -2
- package/dist/host.js.map +1 -1
- package/dist/index.d.ts +5 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -1
- package/dist/index.js.map +1 -1
- package/dist/motion.d.ts.map +1 -1
- package/dist/motion.js +7 -6
- package/dist/motion.js.map +1 -1
- package/dist/pages.d.ts.map +1 -1
- package/dist/pages.js +3 -3
- package/dist/pages.js.map +1 -1
- package/dist/react-native/index.d.ts +9 -1
- package/dist/react-native/index.d.ts.map +1 -1
- package/dist/react-native/index.js +55 -6
- package/dist/react-native/index.js.map +1 -1
- package/dist/report.d.ts +13 -0
- package/dist/report.d.ts.map +1 -0
- package/dist/report.js +22 -0
- package/dist/report.js.map +1 -0
- package/dist/server.d.ts +27 -3
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +15 -7
- package/dist/server.js.map +1 -1
- package/dist/stack.d.ts.map +1 -1
- package/dist/stack.js +3 -3
- package/dist/stack.js.map +1 -1
- package/dist/styles.d.ts.map +1 -1
- package/dist/styles.js +2 -2
- package/dist/styles.js.map +1 -1
- package/dist/template.d.ts.map +1 -1
- package/dist/template.js +56 -12
- package/dist/template.js.map +1 -1
- package/dist/textDecoder.d.ts +29 -0
- package/dist/textDecoder.d.ts.map +1 -0
- package/dist/textDecoder.js +149 -0
- package/dist/textDecoder.js.map +1 -0
- package/dist/url.d.ts.map +1 -1
- package/dist/url.js +4 -3
- package/dist/url.js.map +1 -1
- package/package.json +13 -7
- package/scripts/build-android.sh +0 -0
- package/src/color.ts +5 -10
- package/src/commit.ts +197 -18
- package/src/errors.ts +174 -44
- package/src/host.ts +4 -5
- package/src/index.ts +10 -1
- package/src/motion.ts +8 -6
- package/src/pages.ts +3 -6
- package/src/react-native/index.ts +70 -8
- package/src/react-native/react-native.d.ts +8 -0
- package/src/report.ts +25 -0
- package/src/server.ts +50 -14
- package/src/stack.ts +3 -9
- package/src/styles.ts +2 -5
- package/src/template.ts +65 -41
- package/src/textDecoder.ts +162 -0
- package/src/url.ts +10 -3
|
@@ -10,6 +10,9 @@
|
|
|
10
10
|
//! a compact binary list of mount operations for the platform layer (Kotlin)
|
|
11
11
|
//! to apply to real views. Nodes no longer reachable from the root are dropped
|
|
12
12
|
//! and their tags reported back so JS can forget them.
|
|
13
|
+
//!
|
|
14
|
+
//! A batch the host refuses may have been applied in part; JS then sends what
|
|
15
|
+
//! it carried again, so `"c"` on a tag the host holds replaces that node.
|
|
13
16
|
|
|
14
17
|
use std::cell::RefCell;
|
|
15
18
|
use std::collections::hash_map::DefaultHasher;
|
|
@@ -29,6 +32,13 @@ use taffy::prelude::*;
|
|
|
29
32
|
pub type MeasureFn = extern "C" fn(spans: *const u8, len: usize, max_width: f32) -> u64;
|
|
30
33
|
|
|
31
34
|
// Mount operation codes, shared with OrbitHost.kt.
|
|
35
|
+
/// How deep views may nest under the root (the root is level 1). taffy lays
|
|
36
|
+
/// a tree out by recursing once per level on the caller's stack — React
|
|
37
|
+
/// Native's JS thread, about 1 MiB — at about 6 KB a level (x86_64, release):
|
|
38
|
+
/// some 170 levels overflow it, and a stack overflow is not a panic
|
|
39
|
+
/// `catch_unwind` could stop. Deeper is refused, coded, before any layout.
|
|
40
|
+
pub const MAX_DEPTH: usize = 100;
|
|
41
|
+
|
|
32
42
|
const OP_CREATE: u8 = 1;
|
|
33
43
|
const OP_DELETE: u8 = 2;
|
|
34
44
|
const OP_CHILDREN: u8 = 3;
|
|
@@ -157,6 +167,8 @@ pub struct CommitInfo {
|
|
|
157
167
|
pub diff_us: u128,
|
|
158
168
|
pub measures: u32,
|
|
159
169
|
pub nodes: usize,
|
|
170
|
+
/// Values of drawings left out, coded (`E_ORBIT_SVG_VALUE: …`).
|
|
171
|
+
pub warnings: Vec<String>,
|
|
160
172
|
}
|
|
161
173
|
|
|
162
174
|
pub struct Host {
|
|
@@ -238,25 +250,41 @@ fn length_percentage_auto(v: &Value) -> LengthPercentageAuto {
|
|
|
238
250
|
}
|
|
239
251
|
}
|
|
240
252
|
|
|
253
|
+
/// A min or max size. Unparseable means no bound, as it did when these took a
|
|
254
|
+
/// `Dimension`; a margin or inset parses `length_percentage_auto` instead,
|
|
255
|
+
/// where unparseable means zero.
|
|
256
|
+
fn size_bound(v: &Value) -> LengthPercentageAuto {
|
|
257
|
+
match v {
|
|
258
|
+
Value::String(s) if s.ends_with('%') => s
|
|
259
|
+
.trim_end_matches('%')
|
|
260
|
+
.parse::<f32>()
|
|
261
|
+
.map(|p| LengthPercentageAuto::percent(p / 100.0))
|
|
262
|
+
.unwrap_or(LengthPercentageAuto::auto()),
|
|
263
|
+
_ => num(v)
|
|
264
|
+
.map(LengthPercentageAuto::length)
|
|
265
|
+
.unwrap_or(LengthPercentageAuto::auto()),
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
241
269
|
fn align(v: &Value) -> Option<AlignItems> {
|
|
242
270
|
match v.as_str()? {
|
|
243
|
-
"flex-start" | "start" => Some(AlignItems::
|
|
244
|
-
"flex-end" | "end" => Some(AlignItems::
|
|
245
|
-
"center" => Some(AlignItems::
|
|
246
|
-
"stretch" => Some(AlignItems::
|
|
247
|
-
"baseline" => Some(AlignItems::
|
|
271
|
+
"flex-start" | "start" => Some(AlignItems::FLEX_START),
|
|
272
|
+
"flex-end" | "end" => Some(AlignItems::FLEX_END),
|
|
273
|
+
"center" => Some(AlignItems::CENTER),
|
|
274
|
+
"stretch" => Some(AlignItems::STRETCH),
|
|
275
|
+
"baseline" => Some(AlignItems::BASELINE),
|
|
248
276
|
_ => None,
|
|
249
277
|
}
|
|
250
278
|
}
|
|
251
279
|
|
|
252
280
|
fn justify(v: &Value) -> Option<JustifyContent> {
|
|
253
281
|
match v.as_str()? {
|
|
254
|
-
"flex-start" | "start" => Some(JustifyContent::
|
|
255
|
-
"flex-end" | "end" => Some(JustifyContent::
|
|
256
|
-
"center" => Some(JustifyContent::
|
|
257
|
-
"space-between" => Some(JustifyContent::
|
|
258
|
-
"space-around" => Some(JustifyContent::
|
|
259
|
-
"space-evenly" => Some(JustifyContent::
|
|
282
|
+
"flex-start" | "start" => Some(JustifyContent::FLEX_START),
|
|
283
|
+
"flex-end" | "end" => Some(JustifyContent::FLEX_END),
|
|
284
|
+
"center" => Some(JustifyContent::CENTER),
|
|
285
|
+
"space-between" => Some(JustifyContent::SPACE_BETWEEN),
|
|
286
|
+
"space-around" => Some(JustifyContent::SPACE_AROUND),
|
|
287
|
+
"space-evenly" => Some(JustifyContent::SPACE_EVENLY),
|
|
260
288
|
_ => None,
|
|
261
289
|
}
|
|
262
290
|
}
|
|
@@ -282,10 +310,14 @@ fn sides<T: Copy>(
|
|
|
282
310
|
}
|
|
283
311
|
|
|
284
312
|
fn style_of(props: &Map<String, Value>) -> Style {
|
|
285
|
-
// React Native's defaults, not CSS's: column, no shrink
|
|
313
|
+
// React Native's defaults, not CSS's: column, no shrink, and relative
|
|
314
|
+
// positioning — insets apply to every box and an absolute child is placed
|
|
315
|
+
// in its parent, not in the nearest positioned ancestor (taffy, like CSS,
|
|
316
|
+
// defaults to `static` since 0.15).
|
|
286
317
|
let mut s = Style {
|
|
287
318
|
flex_direction: FlexDirection::Column,
|
|
288
319
|
flex_shrink: 0.0,
|
|
320
|
+
position: Position::Relative,
|
|
289
321
|
..Style::default()
|
|
290
322
|
};
|
|
291
323
|
if let Some(v) = props.get("flexDirection").and_then(Value::as_str) {
|
|
@@ -315,19 +347,30 @@ fn style_of(props: &Map<String, Value>) -> Style {
|
|
|
315
347
|
if let Some(v) = props.get("flexBasis") {
|
|
316
348
|
s.flex_basis = dimension(v);
|
|
317
349
|
}
|
|
318
|
-
|
|
350
|
+
// `normal` is what an unset value meant: stretch for align-items, start
|
|
351
|
+
// for justify-content, in a flex container.
|
|
352
|
+
s.align_items = props
|
|
353
|
+
.get("alignItems")
|
|
354
|
+
.and_then(align)
|
|
355
|
+
.unwrap_or(AlignItems::NORMAL);
|
|
319
356
|
s.align_self = props.get("alignSelf").and_then(align);
|
|
320
|
-
s.justify_content = props
|
|
357
|
+
s.justify_content = props
|
|
358
|
+
.get("justifyContent")
|
|
359
|
+
.and_then(justify)
|
|
360
|
+
.unwrap_or(JustifyContent::NORMAL);
|
|
361
|
+
for (key, slot) in [("width", &mut s.size.width), ("height", &mut s.size.height)] {
|
|
362
|
+
if let Some(v) = props.get(key) {
|
|
363
|
+
*slot = dimension(v);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
321
366
|
for (key, slot) in [
|
|
322
|
-
("width", &mut s.size.width),
|
|
323
|
-
("height", &mut s.size.height),
|
|
324
367
|
("minWidth", &mut s.min_size.width),
|
|
325
368
|
("minHeight", &mut s.min_size.height),
|
|
326
369
|
("maxWidth", &mut s.max_size.width),
|
|
327
370
|
("maxHeight", &mut s.max_size.height),
|
|
328
371
|
] {
|
|
329
372
|
if let Some(v) = props.get(key) {
|
|
330
|
-
*slot =
|
|
373
|
+
*slot = size_bound(v);
|
|
331
374
|
}
|
|
332
375
|
}
|
|
333
376
|
s.padding = sides(
|
|
@@ -619,14 +662,17 @@ impl Host {
|
|
|
619
662
|
}
|
|
620
663
|
|
|
621
664
|
/// The Text node a text-bearing node belongs to (itself for a Text).
|
|
665
|
+
/// A path to the root is never longer than the tree is large: a longer
|
|
666
|
+
/// walk is going round a cycle a bad batch made.
|
|
622
667
|
fn owning_text(&self, mut tag: i32) -> Option<i32> {
|
|
623
|
-
|
|
668
|
+
for _ in 0..=self.nodes.len() {
|
|
624
669
|
let node = self.nodes.get(&tag)?;
|
|
625
670
|
if matches!(node.kind, Kind::Text | Kind::TextInput) {
|
|
626
671
|
return Some(tag);
|
|
627
672
|
}
|
|
628
673
|
tag = *self.parent.get(&tag)?;
|
|
629
674
|
}
|
|
675
|
+
None
|
|
630
676
|
}
|
|
631
677
|
|
|
632
678
|
/// Where nodes were laid out: `[x, y, width, height]` in the root's
|
|
@@ -639,7 +685,13 @@ impl Host {
|
|
|
639
685
|
let own = self.tree.layout(id).ok()?;
|
|
640
686
|
let (mut x, mut y) = (own.location.x, own.location.y);
|
|
641
687
|
let mut at = tag;
|
|
688
|
+
let mut steps = 0;
|
|
642
689
|
while let Some(&parent) = self.parent.get(&at) {
|
|
690
|
+
// A cycle a bad batch made (see `owning_text`).
|
|
691
|
+
steps += 1;
|
|
692
|
+
if steps > self.nodes.len() {
|
|
693
|
+
return None;
|
|
694
|
+
}
|
|
643
695
|
if let Some(id) = self.nodes.get(&parent).and_then(|n| n.taffy) {
|
|
644
696
|
let layout = self.tree.layout(id).ok()?;
|
|
645
697
|
x += layout.location.x;
|
|
@@ -652,6 +704,52 @@ impl Host {
|
|
|
652
704
|
.collect()
|
|
653
705
|
}
|
|
654
706
|
|
|
707
|
+
/// Refuse a tree taffy would recurse into deeper than `MAX_DEPTH`: the
|
|
708
|
+
/// paths taffy follows, walked without recursion. A node reached again
|
|
709
|
+
/// (two parents) is not walked twice; a cycle never ends, so it is
|
|
710
|
+
/// refused as too deep.
|
|
711
|
+
fn check_depth(&self, root: NodeId) -> Result<(), String> {
|
|
712
|
+
let too_deep = || {
|
|
713
|
+
format!(
|
|
714
|
+
"E_ORBIT_TREE_TOO_DEEP: the views nest more than {MAX_DEPTH} levels deep, \
|
|
715
|
+
more than the host lays out"
|
|
716
|
+
)
|
|
717
|
+
};
|
|
718
|
+
// The levels below and including each node walked whole.
|
|
719
|
+
let mut height: HashMap<NodeId, usize> = HashMap::new();
|
|
720
|
+
// The path being walked: each node, its children, the next one to visit,
|
|
721
|
+
// and the tallest child seen so far.
|
|
722
|
+
let children = |id: NodeId| self.tree.children(id).unwrap_or_default();
|
|
723
|
+
let mut path: Vec<(NodeId, Vec<NodeId>, usize, usize)> = vec![(root, children(root), 0, 0)];
|
|
724
|
+
while let Some(top) = path.last_mut() {
|
|
725
|
+
let (id, kids, next, tallest) = (top.0, &top.1, top.2, top.3);
|
|
726
|
+
if let Some(&child) = kids.get(next) {
|
|
727
|
+
top.2 += 1;
|
|
728
|
+
let level = path.len() + 1;
|
|
729
|
+
if let Some(&h) = height.get(&child) {
|
|
730
|
+
if level - 1 + h > MAX_DEPTH {
|
|
731
|
+
return Err(too_deep());
|
|
732
|
+
}
|
|
733
|
+
if let Some(top) = path.last_mut() {
|
|
734
|
+
top.3 = top.3.max(h);
|
|
735
|
+
}
|
|
736
|
+
continue;
|
|
737
|
+
}
|
|
738
|
+
if level > MAX_DEPTH {
|
|
739
|
+
return Err(too_deep());
|
|
740
|
+
}
|
|
741
|
+
path.push((child, children(child), 0, 0));
|
|
742
|
+
} else {
|
|
743
|
+
path.pop();
|
|
744
|
+
height.insert(id, tallest + 1);
|
|
745
|
+
if let Some(parent) = path.last_mut() {
|
|
746
|
+
parent.3 = parent.3.max(tallest + 1);
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
Ok(())
|
|
751
|
+
}
|
|
752
|
+
|
|
655
753
|
pub fn commit(&mut self, ops_json: &[u8], out: &mut Vec<u8>) -> Result<CommitInfo, String> {
|
|
656
754
|
let mut info = CommitInfo::default();
|
|
657
755
|
let t0 = Instant::now();
|
|
@@ -676,9 +774,15 @@ impl Host {
|
|
|
676
774
|
.unwrap_or("")
|
|
677
775
|
.to_owned();
|
|
678
776
|
let vector = match (kind, props.get("vector")) {
|
|
679
|
-
(Kind::Vector, Some(v)) => vector::encode(v)
|
|
777
|
+
(Kind::Vector, Some(v)) => vector::encode(v, &mut info.warnings),
|
|
680
778
|
_ => Vec::new(),
|
|
681
779
|
};
|
|
780
|
+
// Sent again after a refused batch the host had applied in part.
|
|
781
|
+
if let Some(old) = self.nodes.remove(&tag) {
|
|
782
|
+
if let Some(id) = old.taffy {
|
|
783
|
+
let _ = self.tree.remove(id);
|
|
784
|
+
}
|
|
785
|
+
}
|
|
682
786
|
if kind.is_layout() {
|
|
683
787
|
let flags = gesture_flags(&props);
|
|
684
788
|
gesture::with(|g| g.set_node(tag, flags, kind == Kind::Scroll));
|
|
@@ -721,7 +825,7 @@ impl Host {
|
|
|
721
825
|
node.text = t.to_owned();
|
|
722
826
|
}
|
|
723
827
|
if let (Kind::Vector, Some(v)) = (node.kind, patch.get("vector")) {
|
|
724
|
-
node.vector = vector::encode(v)
|
|
828
|
+
node.vector = vector::encode(v, &mut info.warnings);
|
|
725
829
|
}
|
|
726
830
|
node.props.extend(patch);
|
|
727
831
|
if let Some(id) = node.taffy {
|
|
@@ -768,6 +872,23 @@ impl Host {
|
|
|
768
872
|
}
|
|
769
873
|
}
|
|
770
874
|
"r" => self.root = Some(tag),
|
|
875
|
+
// The platform's views are gone (Android recreated the
|
|
876
|
+
// activity): forget what was mounted, so this commit creates,
|
|
877
|
+
// styles and places every view of the tree again. Texts are
|
|
878
|
+
// measured again — the new activity may have another density.
|
|
879
|
+
"m" => {
|
|
880
|
+
self.mounted.clear();
|
|
881
|
+
self.mounted_root = None;
|
|
882
|
+
self.measure_cache.clear();
|
|
883
|
+
for node in self.nodes.values() {
|
|
884
|
+
if matches!(node.kind, Kind::Text | Kind::TextInput) {
|
|
885
|
+
if let Some(id) = node.taffy {
|
|
886
|
+
self.tree.mark_dirty(id).map_err(|e| e.to_string())?;
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
motion::with(|m| m.resend());
|
|
891
|
+
}
|
|
771
892
|
_ => {}
|
|
772
893
|
}
|
|
773
894
|
}
|
|
@@ -793,6 +914,7 @@ impl Host {
|
|
|
793
914
|
let Some(root_id) = self.nodes.get(&root).and_then(|n| n.taffy) else {
|
|
794
915
|
return Ok(info);
|
|
795
916
|
};
|
|
917
|
+
self.check_depth(root_id)?;
|
|
796
918
|
let t1 = Instant::now();
|
|
797
919
|
let mut root_style = self.tree.style(root_id).map_err(|e| e.to_string())?.clone();
|
|
798
920
|
root_style.size = Size {
|
|
@@ -824,65 +946,76 @@ impl Host {
|
|
|
824
946
|
width: AvailableSpace::Definite(self.viewport.0),
|
|
825
947
|
height: AvailableSpace::Definite(self.viewport.1),
|
|
826
948
|
},
|
|
827
|
-
|
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
AvailableSpace::MinContent => 0.0,
|
|
843
|
-
AvailableSpace::MaxContent => f32::INFINITY,
|
|
844
|
-
});
|
|
845
|
-
let mut hasher = DefaultHasher::new();
|
|
846
|
-
encoded.hash(&mut hasher);
|
|
847
|
-
let hash = hasher.finish();
|
|
848
|
-
let mut call = |width: f32| {
|
|
849
|
-
measures += 1;
|
|
850
|
-
match measure {
|
|
851
|
-
Some(f) => {
|
|
852
|
-
let packed = f(encoded.as_ptr(), encoded.len(), width);
|
|
853
|
-
(
|
|
854
|
-
f32::from_bits((packed >> 32) as u32),
|
|
855
|
-
f32::from_bits(packed as u32),
|
|
856
|
-
)
|
|
949
|
+
|input, _id, ctx, style| {
|
|
950
|
+
// Since taffy 0.14 the leaf's box model (padding, border,
|
|
951
|
+
// min/max, aspect ratio) is the caller's: compute_leaf_layout
|
|
952
|
+
// applies it around the content measure, as taffy did itself.
|
|
953
|
+
let tag = ctx.map(|t| *t);
|
|
954
|
+
taffy::compute_leaf_layout(
|
|
955
|
+
input,
|
|
956
|
+
style,
|
|
957
|
+
|_, _| 0.0,
|
|
958
|
+
|known, available| {
|
|
959
|
+
if let (Some(w), Some(h)) = (known.width, known.height) {
|
|
960
|
+
return Size {
|
|
961
|
+
width: w,
|
|
962
|
+
height: h,
|
|
963
|
+
};
|
|
857
964
|
}
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
965
|
+
let Some(tag) = tag else {
|
|
966
|
+
return Size::ZERO;
|
|
967
|
+
};
|
|
968
|
+
let Some(encoded) = spans.get(&tag) else {
|
|
969
|
+
return Size::ZERO;
|
|
970
|
+
};
|
|
971
|
+
let max_width = known.width.unwrap_or(match available.width {
|
|
972
|
+
AvailableSpace::Definite(w) => w,
|
|
973
|
+
AvailableSpace::MinContent => 0.0,
|
|
974
|
+
AvailableSpace::MaxContent => f32::INFINITY,
|
|
975
|
+
});
|
|
976
|
+
let mut hasher = DefaultHasher::new();
|
|
977
|
+
encoded.hash(&mut hasher);
|
|
978
|
+
let hash = hasher.finish();
|
|
979
|
+
let mut call = |width: f32| {
|
|
980
|
+
measures += 1;
|
|
981
|
+
match measure {
|
|
982
|
+
Some(f) => {
|
|
983
|
+
let packed = f(encoded.as_ptr(), encoded.len(), width);
|
|
984
|
+
(
|
|
985
|
+
f32::from_bits((packed >> 32) as u32),
|
|
986
|
+
f32::from_bits(packed as u32),
|
|
987
|
+
)
|
|
988
|
+
}
|
|
989
|
+
// Desktop tests: 7 dp per byte, 16 dp per line.
|
|
990
|
+
None => ((encoded.len() as f32 * 7.0).min(width), 16.0),
|
|
991
|
+
}
|
|
992
|
+
};
|
|
993
|
+
// One platform call per text in the common case: its natural
|
|
994
|
+
// size answers every constraint at least that wide. Only a
|
|
995
|
+
// narrower constraint (wrapping) costs another call.
|
|
996
|
+
let natural = *measure_cache
|
|
997
|
+
.entry((hash, f32::INFINITY.to_bits()))
|
|
998
|
+
.or_insert_with(|| call(f32::INFINITY));
|
|
999
|
+
let (w, h) = if max_width >= natural.0 {
|
|
1000
|
+
natural
|
|
1001
|
+
} else {
|
|
1002
|
+
*measure_cache
|
|
1003
|
+
.entry((hash, max_width.to_bits()))
|
|
1004
|
+
.or_insert_with(|| call(max_width))
|
|
1005
|
+
};
|
|
1006
|
+
// A field is as wide as its box says, like an HTML input:
|
|
1007
|
+
// its text does not size it. 160 dp when nothing does.
|
|
1008
|
+
let w = if inputs.contains(&tag) {
|
|
1009
|
+
DEFAULT_INPUT_WIDTH
|
|
1010
|
+
} else {
|
|
1011
|
+
w
|
|
1012
|
+
};
|
|
1013
|
+
Size {
|
|
1014
|
+
width: known.width.unwrap_or(w),
|
|
1015
|
+
height: known.height.unwrap_or(h),
|
|
1016
|
+
}
|
|
1017
|
+
},
|
|
1018
|
+
)
|
|
886
1019
|
},
|
|
887
1020
|
)
|
|
888
1021
|
.map_err(|e| e.to_string())?;
|
|
@@ -1093,23 +1226,48 @@ impl OrbitBuf {
|
|
|
1093
1226
|
}
|
|
1094
1227
|
}
|
|
1095
1228
|
|
|
1229
|
+
/// Run an exported function's body. A panic must not unwind into the C++
|
|
1230
|
+
/// caller (that aborts the app): it becomes `fallback()` instead. This covers
|
|
1231
|
+
/// panics only — a stack overflow is not one, which is what `MAX_DEPTH` is for.
|
|
1232
|
+
fn guarded<T>(fallback: impl FnOnce() -> T, body: impl FnOnce() -> T) -> T {
|
|
1233
|
+
std::panic::catch_unwind(std::panic::AssertUnwindSafe(body)).unwrap_or_else(|_| fallback())
|
|
1234
|
+
}
|
|
1235
|
+
|
|
1236
|
+
/// `len` items at `ptr`, or none when there are none (a null pointer included).
|
|
1237
|
+
///
|
|
1238
|
+
/// # Safety
|
|
1239
|
+
/// When `len > 0`, `ptr` must point to `len` readable items.
|
|
1240
|
+
unsafe fn items<'a, T>(ptr: *const T, len: usize) -> &'a [T] {
|
|
1241
|
+
if ptr.is_null() || len == 0 {
|
|
1242
|
+
&[]
|
|
1243
|
+
} else {
|
|
1244
|
+
std::slice::from_raw_parts(ptr, len)
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1096
1248
|
/// # Safety
|
|
1097
1249
|
/// `buf` must come from this library and be freed once.
|
|
1098
1250
|
#[no_mangle]
|
|
1099
1251
|
pub unsafe extern "C" fn orbit_buf_free(buf: OrbitBuf) {
|
|
1100
1252
|
if !buf.ptr.is_null() {
|
|
1101
|
-
|
|
1253
|
+
guarded(
|
|
1254
|
+
|| (),
|
|
1255
|
+
|| drop(Vec::from_raw_parts(buf.ptr, buf.len, buf.cap)),
|
|
1256
|
+
);
|
|
1102
1257
|
}
|
|
1103
1258
|
}
|
|
1104
1259
|
|
|
1105
1260
|
#[no_mangle]
|
|
1106
1261
|
pub extern "C" fn orbit_set_measure(measure: MeasureFn) {
|
|
1107
|
-
HOST.with(|h| h.borrow_mut().set_measure(measure));
|
|
1262
|
+
guarded(|| (), || HOST.with(|h| h.borrow_mut().set_measure(measure)));
|
|
1108
1263
|
}
|
|
1109
1264
|
|
|
1110
1265
|
#[no_mangle]
|
|
1111
1266
|
pub extern "C" fn orbit_set_viewport(width: f32, height: f32) {
|
|
1112
|
-
|
|
1267
|
+
guarded(
|
|
1268
|
+
|| (),
|
|
1269
|
+
|| HOST.with(|h| h.borrow_mut().set_viewport(width, height)),
|
|
1270
|
+
);
|
|
1113
1271
|
}
|
|
1114
1272
|
|
|
1115
1273
|
fn panic_message(payload: Box<dyn std::any::Any + Send>) -> String {
|
|
@@ -1129,6 +1287,7 @@ fn info_json(info: &CommitInfo, ops_bytes: usize) -> String {
|
|
|
1129
1287
|
"measures": info.measures,
|
|
1130
1288
|
"nodes": info.nodes,
|
|
1131
1289
|
"opsBytes": ops_bytes,
|
|
1290
|
+
"warnings": info.warnings,
|
|
1132
1291
|
})
|
|
1133
1292
|
.to_string()
|
|
1134
1293
|
}
|
|
@@ -1147,8 +1306,11 @@ pub unsafe extern "C" fn orbit_touch(
|
|
|
1147
1306
|
hit: i32,
|
|
1148
1307
|
out: *mut OrbitBuf,
|
|
1149
1308
|
) {
|
|
1150
|
-
let
|
|
1151
|
-
|
|
1309
|
+
let commands = guarded(Vec::new, || {
|
|
1310
|
+
let mut commands = Vec::new();
|
|
1311
|
+
gesture::with(|g| g.touch(action, x, y, now_ns, hit, &mut commands));
|
|
1312
|
+
commands
|
|
1313
|
+
});
|
|
1152
1314
|
*out = OrbitBuf::from_vec(commands);
|
|
1153
1315
|
}
|
|
1154
1316
|
|
|
@@ -1158,27 +1320,37 @@ pub unsafe extern "C" fn orbit_touch(
|
|
|
1158
1320
|
/// `out` must be valid.
|
|
1159
1321
|
#[no_mangle]
|
|
1160
1322
|
pub unsafe extern "C" fn orbit_gesture_tick(now_ns: i64, out: *mut OrbitBuf) {
|
|
1161
|
-
let
|
|
1162
|
-
|
|
1323
|
+
let commands = guarded(Vec::new, || {
|
|
1324
|
+
let mut commands = Vec::new();
|
|
1325
|
+
gesture::with(|g| g.tick(now_ns, &mut commands));
|
|
1326
|
+
commands
|
|
1327
|
+
});
|
|
1163
1328
|
*out = OrbitBuf::from_vec(commands);
|
|
1164
1329
|
}
|
|
1165
1330
|
|
|
1166
1331
|
/// The frames of a JSON array of tags, as a JSON array of `[x, y, w, h]` or
|
|
1167
|
-
/// null (see `Host::frames`). Returns 0,
|
|
1332
|
+
/// null (see `Host::frames`). Returns 0, 1 with `{"error": …}`, or 2 when it
|
|
1333
|
+
/// panicked.
|
|
1168
1334
|
///
|
|
1169
1335
|
/// # Safety
|
|
1170
1336
|
/// `tags` must point to `len` readable bytes; `out` must be valid.
|
|
1171
1337
|
#[no_mangle]
|
|
1172
1338
|
pub unsafe extern "C" fn orbit_layout(tags: *const u8, len: usize, out: *mut OrbitBuf) -> i32 {
|
|
1173
|
-
let input =
|
|
1174
|
-
let
|
|
1339
|
+
let input = items(tags, len);
|
|
1340
|
+
let result = std::panic::catch_unwind(|| match serde_json::from_slice::<Vec<i32>>(input) {
|
|
1175
1341
|
Ok(tags) => (
|
|
1176
1342
|
0,
|
|
1177
1343
|
serde_json::to_string(&HOST.with(|h| h.borrow().frames(&tags)))
|
|
1178
1344
|
.unwrap_or_else(|_| "[]".to_owned()),
|
|
1179
1345
|
),
|
|
1180
1346
|
Err(e) => (1, serde_json::json!({ "error": e.to_string() }).to_string()),
|
|
1181
|
-
};
|
|
1347
|
+
});
|
|
1348
|
+
let (code, json) = result.unwrap_or_else(|p| {
|
|
1349
|
+
(
|
|
1350
|
+
2,
|
|
1351
|
+
serde_json::json!({ "error": panic_message(p) }).to_string(),
|
|
1352
|
+
)
|
|
1353
|
+
});
|
|
1182
1354
|
*out = OrbitBuf::from_vec(json.into_bytes());
|
|
1183
1355
|
code
|
|
1184
1356
|
}
|
|
@@ -1195,7 +1367,7 @@ pub unsafe extern "C" fn orbit_commit(
|
|
|
1195
1367
|
mount_ops: *mut OrbitBuf,
|
|
1196
1368
|
info: *mut OrbitBuf,
|
|
1197
1369
|
) -> i32 {
|
|
1198
|
-
let input =
|
|
1370
|
+
let input = items(ops, len);
|
|
1199
1371
|
let result = std::panic::catch_unwind(|| {
|
|
1200
1372
|
HOST.with(|h| {
|
|
1201
1373
|
let mut out = Vec::new();
|
|
@@ -1222,7 +1394,7 @@ pub unsafe extern "C" fn orbit_commit(
|
|
|
1222
1394
|
|
|
1223
1395
|
#[no_mangle]
|
|
1224
1396
|
pub extern "C" fn orbit_motion_value(tag: i32, initial: f64) {
|
|
1225
|
-
motion::with(|m| m.value(tag, initial));
|
|
1397
|
+
guarded(|| (), || motion::with(|m| m.value(tag, initial)));
|
|
1226
1398
|
}
|
|
1227
1399
|
|
|
1228
1400
|
/// # Safety
|
|
@@ -1235,19 +1407,22 @@ pub unsafe extern "C" fn orbit_motion_interpolate(
|
|
|
1235
1407
|
to: *const f64,
|
|
1236
1408
|
len: usize,
|
|
1237
1409
|
) {
|
|
1238
|
-
let from =
|
|
1239
|
-
let to =
|
|
1240
|
-
|
|
1410
|
+
let from = items(from, len).to_vec();
|
|
1411
|
+
let to = items(to, len).to_vec();
|
|
1412
|
+
guarded(
|
|
1413
|
+
|| (),
|
|
1414
|
+
|| motion::with(|m| m.interpolation(tag, input, from, to)),
|
|
1415
|
+
);
|
|
1241
1416
|
}
|
|
1242
1417
|
|
|
1243
1418
|
#[no_mangle]
|
|
1244
1419
|
pub extern "C" fn orbit_motion_set(tag: i32, value: f64) {
|
|
1245
|
-
motion::with(|m| m.set(tag, value));
|
|
1420
|
+
guarded(|| (), || motion::with(|m| m.set(tag, value)));
|
|
1246
1421
|
}
|
|
1247
1422
|
|
|
1248
1423
|
#[no_mangle]
|
|
1249
1424
|
pub extern "C" fn orbit_motion_stop(tag: i32) {
|
|
1250
|
-
motion::with(|m| m.stop(tag));
|
|
1425
|
+
guarded(|| (), || motion::with(|m| m.stop(tag)));
|
|
1251
1426
|
}
|
|
1252
1427
|
|
|
1253
1428
|
/// # Safety
|
|
@@ -1260,28 +1435,37 @@ pub unsafe extern "C" fn orbit_motion_frames(
|
|
|
1260
1435
|
to: f64,
|
|
1261
1436
|
looping: bool,
|
|
1262
1437
|
) {
|
|
1263
|
-
let frames =
|
|
1264
|
-
|
|
1438
|
+
let frames = items(frames, len).to_vec();
|
|
1439
|
+
guarded(
|
|
1440
|
+
|| (),
|
|
1441
|
+
|| motion::with(|m| m.frames(tag, frames, to, looping)),
|
|
1442
|
+
);
|
|
1265
1443
|
}
|
|
1266
1444
|
|
|
1267
1445
|
#[no_mangle]
|
|
1268
1446
|
pub extern "C" fn orbit_motion_spring(tag: i32, to: f64, stiffness: f64, damping: f64, mass: f64) {
|
|
1269
|
-
|
|
1447
|
+
guarded(
|
|
1448
|
+
|| (),
|
|
1449
|
+
|| motion::with(|m| m.spring(tag, to, stiffness, damping, mass)),
|
|
1450
|
+
);
|
|
1270
1451
|
}
|
|
1271
1452
|
|
|
1272
1453
|
#[no_mangle]
|
|
1273
1454
|
pub extern "C" fn orbit_motion_dispose(tag: i32) {
|
|
1274
|
-
motion::with(|m| m.dispose(tag));
|
|
1455
|
+
guarded(|| (), || motion::with(|m| m.dispose(tag)));
|
|
1275
1456
|
}
|
|
1276
1457
|
|
|
1277
1458
|
#[no_mangle]
|
|
1278
1459
|
pub extern "C" fn orbit_motion_bind(view: i32, prop: u8, node: i32) {
|
|
1279
|
-
motion::with(|m| m.bind(view, prop, node));
|
|
1460
|
+
guarded(|| (), || motion::with(|m| m.bind(view, prop, node)));
|
|
1280
1461
|
}
|
|
1281
1462
|
|
|
1282
1463
|
#[no_mangle]
|
|
1283
1464
|
pub extern "C" fn orbit_motion_bind_scroll(scroll_view: i32, value: i32) {
|
|
1284
|
-
|
|
1465
|
+
guarded(
|
|
1466
|
+
|| (),
|
|
1467
|
+
|| motion::with(|m| m.bind_scroll(scroll_view, value)),
|
|
1468
|
+
);
|
|
1285
1469
|
}
|
|
1286
1470
|
|
|
1287
1471
|
/// Advance every animation to `now_ns` (UI thread). `scroll_tags`/`scroll_y`
|
|
@@ -1298,19 +1482,17 @@ pub unsafe extern "C" fn orbit_motion_tick(
|
|
|
1298
1482
|
count: usize,
|
|
1299
1483
|
out: *mut OrbitBuf,
|
|
1300
1484
|
) -> i32 {
|
|
1301
|
-
let tags =
|
|
1302
|
-
|
|
1303
|
-
} else {
|
|
1304
|
-
std::slice::from_raw_parts(scroll_tags, count)
|
|
1305
|
-
};
|
|
1306
|
-
let ys = if count == 0 {
|
|
1307
|
-
&[][..]
|
|
1308
|
-
} else {
|
|
1309
|
-
std::slice::from_raw_parts(scroll_y, count)
|
|
1310
|
-
};
|
|
1485
|
+
let tags = items(scroll_tags, count);
|
|
1486
|
+
let ys = items(scroll_y, count);
|
|
1311
1487
|
let scrolls: Vec<(i32, f32)> = tags.iter().copied().zip(ys.iter().copied()).collect();
|
|
1312
|
-
let
|
|
1313
|
-
|
|
1488
|
+
let (active, buf) = guarded(
|
|
1489
|
+
|| (false, Vec::new()),
|
|
1490
|
+
|| {
|
|
1491
|
+
let mut buf = Vec::new();
|
|
1492
|
+
let active = motion::with(|m| m.tick(now_ns, &scrolls, &mut buf));
|
|
1493
|
+
(active, buf)
|
|
1494
|
+
},
|
|
1495
|
+
);
|
|
1314
1496
|
*out = OrbitBuf::from_vec(buf);
|
|
1315
1497
|
i32::from(active)
|
|
1316
1498
|
}
|
|
@@ -1468,7 +1650,9 @@ mod tests {
|
|
|
1468
1650
|
assert_eq!(codes(&third), vec![OP_VECTOR]);
|
|
1469
1651
|
|
|
1470
1652
|
let broken = r#"[["u",3,{"vector":{"root":{"t":"svg","a":{"viewBox":"0 0 1 1"},"c":[{"t":"path","a":{"d":"M0 0 Q"}}]}}}]]"#;
|
|
1471
|
-
|
|
1653
|
+
// Path data in error is drawn up to the error and reported, not refused.
|
|
1654
|
+
let (_, info) = commit(&mut host, broken);
|
|
1655
|
+
assert_eq!(info.warnings.len(), 1);
|
|
1472
1656
|
}
|
|
1473
1657
|
|
|
1474
1658
|
#[test]
|
|
@@ -1605,4 +1789,285 @@ mod tests {
|
|
|
1605
1789
|
assert_eq!(info.deleted, vec![3]);
|
|
1606
1790
|
assert_eq!(codes(&out), vec![OP_CHILDREN, OP_DELETE]);
|
|
1607
1791
|
}
|
|
1792
|
+
|
|
1793
|
+
#[test]
|
|
1794
|
+
fn mounting_again_sends_the_whole_tree_and_measures_its_texts_again() {
|
|
1795
|
+
let mut host = Host::default();
|
|
1796
|
+
let tree = r#"[["c",1,"RCTView",{}],["c",2,"RCTText",{}],["c",3,"RCTRawText",{"text":"hi"}],["k",2,[3]],["k",1,[2]],["r",1]]"#;
|
|
1797
|
+
let (first, _) = commit(&mut host, tree);
|
|
1798
|
+
let (idle, info) = commit(&mut host, "[]");
|
|
1799
|
+
assert!(idle.is_empty());
|
|
1800
|
+
assert_eq!(info.measures, 0);
|
|
1801
|
+
let (again, info) = commit(&mut host, r#"[["m",1]]"#);
|
|
1802
|
+
assert_eq!(again, first);
|
|
1803
|
+
assert!(info.deleted.is_empty());
|
|
1804
|
+
assert!(info.measures > 0);
|
|
1805
|
+
}
|
|
1806
|
+
|
|
1807
|
+
/// Frames of every tag in `tags`, in the root's coordinates.
|
|
1808
|
+
fn layout_of(ops: &str, tags: &[i32]) -> Vec<Option<[f32; 4]>> {
|
|
1809
|
+
let mut host = Host::default();
|
|
1810
|
+
host.set_viewport(300.0, 600.0);
|
|
1811
|
+
commit(&mut host, ops);
|
|
1812
|
+
host.frames(tags)
|
|
1813
|
+
}
|
|
1814
|
+
|
|
1815
|
+
/// The boxes React Native's rules give, checked against the frames taffy
|
|
1816
|
+
/// 0.9 produced before the move to 0.15 (identical on every scene): an
|
|
1817
|
+
/// absolute child placed in its parent, insets moving an in-flow box,
|
|
1818
|
+
/// alignment, min/max sizes (unparseable means no bound), wrapping, flex
|
|
1819
|
+
/// sizing, aspect ratio, a measured text in a padded box, a scroll view.
|
|
1820
|
+
#[test]
|
|
1821
|
+
fn lays_out_boxes_by_react_natives_rules() {
|
|
1822
|
+
type Frames = Vec<Option<[f32; 4]>>;
|
|
1823
|
+
let scenes: &[(&str, &[i32], Frames)] = &[
|
|
1824
|
+
// absolute child of a nested box: placed in its parent, insets from it
|
|
1825
|
+
(
|
|
1826
|
+
r#"[["c",1,"RCTView",{"padding":10}],["c",3,"RCTView",{"margin":20,"height":100,"padding":5}],
|
|
1827
|
+
["c",5,"RCTView",{"position":"absolute","top":4,"right":6,"width":30,"height":30}],
|
|
1828
|
+
["c",7,"RCTView",{"position":"absolute","left":"10%","bottom":0,"height":10,"right":0}],
|
|
1829
|
+
["k",3,[5,7]],["k",1,[3]],["r",1]]"#,
|
|
1830
|
+
&[3, 5, 7],
|
|
1831
|
+
vec![
|
|
1832
|
+
Some([30.0, 30.0, 240.0, 100.0]),
|
|
1833
|
+
Some([234.0, 34.0, 30.0, 30.0]),
|
|
1834
|
+
Some([54.0, 120.0, 216.0, 10.0]),
|
|
1835
|
+
],
|
|
1836
|
+
),
|
|
1837
|
+
// insets on an in-flow box move it (relative positioning)
|
|
1838
|
+
(
|
|
1839
|
+
r#"[["c",1,"RCTView",{}],["c",3,"RCTView",{"height":40,"top":7,"left":9}],["c",5,"RCTView",{"height":40}],
|
|
1840
|
+
["k",1,[3,5]],["r",1]]"#,
|
|
1841
|
+
&[3, 5],
|
|
1842
|
+
vec![
|
|
1843
|
+
Some([9.0, 7.0, 300.0, 40.0]),
|
|
1844
|
+
Some([0.0, 40.0, 300.0, 40.0]),
|
|
1845
|
+
],
|
|
1846
|
+
),
|
|
1847
|
+
// alignment: center, flex-end, space-between, alignSelf, stretch by default
|
|
1848
|
+
(
|
|
1849
|
+
r#"[["c",1,"RCTView",{"flexDirection":"row","justifyContent":"space-between","alignItems":"center","height":100}],
|
|
1850
|
+
["c",3,"RCTView",{"width":50,"height":20}],["c",5,"RCTView",{"width":50,"alignSelf":"stretch"}],
|
|
1851
|
+
["c",7,"RCTView",{"width":50,"height":30,"alignSelf":"flex-end"}],
|
|
1852
|
+
["c",9,"RCTView",{"height":60}],["c",11,"RCTView",{"height":10,"marginHorizontal":15}],
|
|
1853
|
+
["k",9,[11]],["k",1,[3,5,7]],["c",13,"RCTView",{}],["k",13,[1,9]],["r",13]]"#,
|
|
1854
|
+
&[1, 3, 5, 7, 9, 11],
|
|
1855
|
+
vec![
|
|
1856
|
+
Some([0.0, 0.0, 300.0, 100.0]),
|
|
1857
|
+
Some([0.0, 40.0, 50.0, 20.0]),
|
|
1858
|
+
Some([125.0, 0.0, 50.0, 100.0]),
|
|
1859
|
+
Some([250.0, 70.0, 50.0, 30.0]),
|
|
1860
|
+
Some([0.0, 100.0, 300.0, 60.0]),
|
|
1861
|
+
Some([15.0, 100.0, 270.0, 10.0]),
|
|
1862
|
+
],
|
|
1863
|
+
),
|
|
1864
|
+
// justify center / flex-end, space-around, space-evenly
|
|
1865
|
+
(
|
|
1866
|
+
r#"[["c",1,"RCTView",{}],["c",3,"RCTView",{"flexDirection":"row","justifyContent":"center","height":20}],
|
|
1867
|
+
["c",5,"RCTView",{"width":40,"height":10}],["c",7,"RCTView",{"flexDirection":"row","justifyContent":"space-evenly","height":20}],
|
|
1868
|
+
["c",9,"RCTView",{"width":40,"height":10}],["c",11,"RCTView",{"width":40,"height":10}],
|
|
1869
|
+
["k",3,[5]],["k",7,[9,11]],["k",1,[3,7]],["r",1]]"#,
|
|
1870
|
+
&[5, 9, 11],
|
|
1871
|
+
vec![
|
|
1872
|
+
Some([130.0, 0.0, 40.0, 10.0]),
|
|
1873
|
+
Some([73.0, 20.0, 40.0, 10.0]),
|
|
1874
|
+
Some([187.0, 20.0, 40.0, 10.0]),
|
|
1875
|
+
],
|
|
1876
|
+
),
|
|
1877
|
+
// min / max sizes: lengths, percentages, unparseable values
|
|
1878
|
+
(
|
|
1879
|
+
r#"[["c",1,"RCTView",{"alignItems":"flex-start"}],["c",3,"RCTView",{"minWidth":120,"height":10}],
|
|
1880
|
+
["c",5,"RCTView",{"width":250,"maxWidth":"50%","height":10}],["c",7,"RCTView",{"maxWidth":"wide","width":200,"height":10}],
|
|
1881
|
+
["c",9,"RCTView",{"minHeight":"nope","height":10,"width":10}],["c",11,"RCTView",{"flex":1,"maxHeight":50}],
|
|
1882
|
+
["k",1,[3,5,7,9,11]],["r",1]]"#,
|
|
1883
|
+
&[3, 5, 7, 9, 11],
|
|
1884
|
+
vec![
|
|
1885
|
+
Some([0.0, 0.0, 120.0, 10.0]),
|
|
1886
|
+
Some([0.0, 10.0, 150.0, 10.0]),
|
|
1887
|
+
Some([0.0, 20.0, 200.0, 10.0]),
|
|
1888
|
+
Some([0.0, 30.0, 10.0, 10.0]),
|
|
1889
|
+
Some([0.0, 40.0, 0.0, 50.0]),
|
|
1890
|
+
],
|
|
1891
|
+
),
|
|
1892
|
+
// wrap, flex grow / shrink / basis, aspect ratio, percentage widths
|
|
1893
|
+
(
|
|
1894
|
+
r#"[["c",1,"RCTView",{"flexDirection":"row","flexWrap":"wrap","padding":4}],
|
|
1895
|
+
["c",3,"RCTView",{"width":"40%","aspectRatio":2}],["c",5,"RCTView",{"width":"40%","height":15}],
|
|
1896
|
+
["c",7,"RCTView",{"width":"40%","height":25}],["c",9,"RCTView",{"flexGrow":1,"height":5}],
|
|
1897
|
+
["c",11,"RCTView",{"flexDirection":"row","height":30}],["c",13,"RCTView",{"flex":1}],["c",15,"RCTView",{"flex":2}],
|
|
1898
|
+
["c",17,"RCTView",{"flexBasis":60,"flexShrink":1}],["k",11,[13,15,17]],
|
|
1899
|
+
["c",19,"RCTView",{}],["k",1,[3,5,7,9]],["k",19,[1,11]],["r",19]]"#,
|
|
1900
|
+
&[1, 3, 5, 7, 9, 13, 15, 17],
|
|
1901
|
+
vec![
|
|
1902
|
+
Some([0.0, 0.0, 300.0, 91.0]),
|
|
1903
|
+
Some([4.0, 4.0, 117.0, 58.0]),
|
|
1904
|
+
Some([121.0, 4.0, 117.0, 15.0]),
|
|
1905
|
+
Some([4.0, 62.0, 117.0, 25.0]),
|
|
1906
|
+
Some([121.0, 62.0, 175.0, 5.0]),
|
|
1907
|
+
Some([0.0, 91.0, 80.0, 30.0]),
|
|
1908
|
+
Some([80.0, 91.0, 160.0, 30.0]),
|
|
1909
|
+
Some([240.0, 91.0, 60.0, 30.0]),
|
|
1910
|
+
],
|
|
1911
|
+
),
|
|
1912
|
+
// measured text in a padded, bordered box, wrapping under a narrow width
|
|
1913
|
+
(
|
|
1914
|
+
r#"[["c",1,"RCTView",{"alignItems":"flex-start"}],["c",3,"RCTText",{"fontSize":12,"padding":6,"borderWidth":2}],
|
|
1915
|
+
["c",5,"RCTRawText",{"text":"hello world"}],["c",7,"RCTText",{"fontSize":12,"width":40,"paddingHorizontal":3}],
|
|
1916
|
+
["c",9,"RCTRawText",{"text":"a long sentence that wraps"}],["k",3,[5]],["k",7,[9]],["k",1,[3,7]],["r",1]]"#,
|
|
1917
|
+
&[3, 7],
|
|
1918
|
+
vec![Some([0.0, 0.0, 268.0, 32.0]), Some([0.0, 32.0, 40.0, 16.0])],
|
|
1919
|
+
),
|
|
1920
|
+
// scroll view: content child grows past the frame
|
|
1921
|
+
(
|
|
1922
|
+
r#"[["c",1,"RCTView",{}],["c",3,"RCTScrollView",{"height":100}],["c",5,"RCTView",{}],
|
|
1923
|
+
["c",7,"RCTView",{"height":400}],["k",5,[7]],["k",3,[5]],["k",1,[3]],["r",1]]"#,
|
|
1924
|
+
&[3, 5, 7],
|
|
1925
|
+
vec![
|
|
1926
|
+
Some([0.0, 0.0, 300.0, 100.0]),
|
|
1927
|
+
Some([0.0, 0.0, 300.0, 400.0]),
|
|
1928
|
+
Some([0.0, 0.0, 300.0, 400.0]),
|
|
1929
|
+
],
|
|
1930
|
+
),
|
|
1931
|
+
];
|
|
1932
|
+
for (i, (ops, tags, expected)) in scenes.iter().enumerate() {
|
|
1933
|
+
assert_eq!(&layout_of(ops, tags), expected, "scene {i}");
|
|
1934
|
+
}
|
|
1935
|
+
}
|
|
1936
|
+
|
|
1937
|
+
/// A chain of `depth` nested views, `extra` ops appended before the root.
|
|
1938
|
+
fn chain(depth: usize, extra: &str) -> String {
|
|
1939
|
+
let mut ops = String::from("[");
|
|
1940
|
+
for i in 1..=depth {
|
|
1941
|
+
ops.push_str(&format!(r#"["c",{},"RCTView",{{"padding":1}}],"#, i * 2));
|
|
1942
|
+
}
|
|
1943
|
+
for i in 1..depth {
|
|
1944
|
+
ops.push_str(&format!(r#"["k",{},[{}]],"#, i * 2, i * 2 + 2));
|
|
1945
|
+
}
|
|
1946
|
+
ops.push_str(extra);
|
|
1947
|
+
ops.push_str(r#"["r",2]]"#);
|
|
1948
|
+
ops
|
|
1949
|
+
}
|
|
1950
|
+
|
|
1951
|
+
#[test]
|
|
1952
|
+
fn refuses_views_nested_deeper_than_the_bound_before_layout() {
|
|
1953
|
+
let mut host = Host::default();
|
|
1954
|
+
host.set_viewport(300.0, 600.0);
|
|
1955
|
+
let mut out = Vec::new();
|
|
1956
|
+
assert!(host
|
|
1957
|
+
.commit(chain(MAX_DEPTH, "").as_bytes(), &mut out)
|
|
1958
|
+
.is_ok());
|
|
1959
|
+
let mut host = Host::default();
|
|
1960
|
+
let mut out = Vec::new();
|
|
1961
|
+
let error = host
|
|
1962
|
+
.commit(chain(MAX_DEPTH + 1, "").as_bytes(), &mut out)
|
|
1963
|
+
.unwrap_err();
|
|
1964
|
+
assert!(error.starts_with("E_ORBIT_TREE_TOO_DEEP: "), "{error}");
|
|
1965
|
+
assert!(out.is_empty());
|
|
1966
|
+
// Text runs are not laid out: a deep chain of them does not count.
|
|
1967
|
+
let mut ops = chain(MAX_DEPTH - 1, "");
|
|
1968
|
+
ops.truncate(ops.len() - r#"["r",2]]"#.len());
|
|
1969
|
+
let last = MAX_DEPTH * 2 - 2;
|
|
1970
|
+
ops.push_str(&format!(
|
|
1971
|
+
r#"["c",9001,"RCTText",{{}}],["c",9003,"RCTVirtualText",{{}}],["c",9005,"RCTRawText",{{"text":"x"}}],["k",9003,[9005]],["k",9001,[9003]],["k",{last},[9001]],["r",2]]"#
|
|
1972
|
+
));
|
|
1973
|
+
let mut host = Host::default();
|
|
1974
|
+
assert!(host.commit(ops.as_bytes(), &mut Vec::new()).is_ok());
|
|
1975
|
+
}
|
|
1976
|
+
|
|
1977
|
+
#[test]
|
|
1978
|
+
fn refuses_a_cycle_and_counts_a_moved_view_where_it_is() {
|
|
1979
|
+
// 2 → 4 → 6 → 2: the walk taffy would do never ends.
|
|
1980
|
+
let mut host = Host::default();
|
|
1981
|
+
let error = host
|
|
1982
|
+
.commit(chain(3, r#"["k",6,[2]],"#).as_bytes(), &mut Vec::new())
|
|
1983
|
+
.unwrap_err();
|
|
1984
|
+
assert!(error.starts_with("E_ORBIT_TREE_TOO_DEEP: "), "{error}");
|
|
1985
|
+
// A view listed under a second parent moves there, as taffy moves it:
|
|
1986
|
+
// the deepest one of a chain of MAX_DEPTH, moved under the root.
|
|
1987
|
+
let mut host = Host::default();
|
|
1988
|
+
let moved = chain(MAX_DEPTH, &format!(r#"["k",2,[4,{}]],"#, MAX_DEPTH * 2));
|
|
1989
|
+
assert!(host.commit(moved.as_bytes(), &mut Vec::new()).is_ok());
|
|
1990
|
+
let deeper = chain(
|
|
1991
|
+
MAX_DEPTH + 1,
|
|
1992
|
+
&format!(r#"["k",2,[4,{}]],"#, MAX_DEPTH * 2 + 2),
|
|
1993
|
+
);
|
|
1994
|
+
assert!(Host::default()
|
|
1995
|
+
.commit(deeper.as_bytes(), &mut Vec::new())
|
|
1996
|
+
.is_ok());
|
|
1997
|
+
let error = Host::default()
|
|
1998
|
+
.commit(chain(MAX_DEPTH + 1, "").as_bytes(), &mut Vec::new())
|
|
1999
|
+
.unwrap_err();
|
|
2000
|
+
assert!(error.starts_with("E_ORBIT_TREE_TOO_DEEP: "), "{error}");
|
|
2001
|
+
}
|
|
2002
|
+
|
|
2003
|
+
#[test]
|
|
2004
|
+
fn creating_a_tag_it_holds_replaces_that_node() {
|
|
2005
|
+
let mut host = Host::default();
|
|
2006
|
+
host.set_viewport(300.0, 600.0);
|
|
2007
|
+
let ops = r#"[["c",1,"RCTView",{}],["c",3,"RCTView",{"height":20}],["k",1,[3]],["r",1]]"#;
|
|
2008
|
+
commit(&mut host, ops);
|
|
2009
|
+
let before = host.tree.total_node_count();
|
|
2010
|
+
// A refused batch sent again: the same creates and children.
|
|
2011
|
+
let (_, info) = commit(
|
|
2012
|
+
&mut host,
|
|
2013
|
+
r#"[["c",3,"RCTView",{"height":30}],["k",1,[3]]]"#,
|
|
2014
|
+
);
|
|
2015
|
+
assert_eq!(host.tree.total_node_count(), before);
|
|
2016
|
+
assert!(info.deleted.is_empty());
|
|
2017
|
+
assert_eq!(host.frames(&[3]), vec![Some([0.0, 0.0, 300.0, 30.0])]);
|
|
2018
|
+
}
|
|
2019
|
+
|
|
2020
|
+
#[test]
|
|
2021
|
+
fn a_drawing_value_it_cannot_read_costs_that_shape_not_the_commit() {
|
|
2022
|
+
let mut host = Host::default();
|
|
2023
|
+
host.set_viewport(300.0, 600.0);
|
|
2024
|
+
let ops = r#"[["c",1,"RCTView",{}],["c",3,"OrbitVector",{"width":100,"height":50,"vector":{"color":-16777216,"root":
|
|
2025
|
+
{"t":"svg","a":{"viewBox":"0 0 100 50"},"c":[
|
|
2026
|
+
{"t":"rect","a":{"width":"40","height":"NaN"},"c":[]},
|
|
2027
|
+
{"t":"g","a":{"transform":")("},"c":[]},
|
|
2028
|
+
{"t":"rect","a":{"width":"40","height":"20"},"c":[]}]}}}],["k",1,[3]],["r",1]]"#;
|
|
2029
|
+
let (out, info) = commit(&mut host, ops);
|
|
2030
|
+
assert!(codes(&out).contains(&OP_VECTOR));
|
|
2031
|
+
assert_eq!(info.warnings.len(), 1);
|
|
2032
|
+
assert!(info.warnings[0].starts_with("E_ORBIT_SVG_VALUE: "));
|
|
2033
|
+
// A patch too.
|
|
2034
|
+
let (_, info) = commit(
|
|
2035
|
+
&mut host,
|
|
2036
|
+
r#"[["u",3,{"vector":{"root":{"t":"svg","c":[{"t":"path","a":{"d":"M0 0 L1 NaN"}}]}}}]]"#,
|
|
2037
|
+
);
|
|
2038
|
+
assert_eq!(info.warnings.len(), 1);
|
|
2039
|
+
let json = info_json(&info, 0);
|
|
2040
|
+
assert!(
|
|
2041
|
+
json.contains(r#""warnings":["E_ORBIT_SVG_VALUE: "#),
|
|
2042
|
+
"{json}"
|
|
2043
|
+
);
|
|
2044
|
+
}
|
|
2045
|
+
|
|
2046
|
+
#[test]
|
|
2047
|
+
fn a_panic_never_leaves_an_exported_function() {
|
|
2048
|
+
assert_eq!(guarded(|| 7, || panic!("boom")), 7);
|
|
2049
|
+
assert_eq!(guarded(|| 7, || 3), 3);
|
|
2050
|
+
assert!(unsafe { items::<u8>(std::ptr::null(), 4) }.is_empty());
|
|
2051
|
+
// The C ABI with nothing to read and nothing to tick.
|
|
2052
|
+
let mut out = OrbitBuf {
|
|
2053
|
+
ptr: std::ptr::null_mut(),
|
|
2054
|
+
len: 0,
|
|
2055
|
+
cap: 0,
|
|
2056
|
+
};
|
|
2057
|
+
let code = unsafe { orbit_layout(std::ptr::null(), 0, &mut out) };
|
|
2058
|
+
assert_eq!(code, 1);
|
|
2059
|
+
unsafe { orbit_buf_free(out) };
|
|
2060
|
+
let mut out = OrbitBuf {
|
|
2061
|
+
ptr: std::ptr::null_mut(),
|
|
2062
|
+
len: 0,
|
|
2063
|
+
cap: 0,
|
|
2064
|
+
};
|
|
2065
|
+
let active =
|
|
2066
|
+
unsafe { orbit_motion_tick(0, std::ptr::null(), std::ptr::null(), 0, &mut out) };
|
|
2067
|
+
assert_eq!(active, 0);
|
|
2068
|
+
unsafe { orbit_buf_free(out) };
|
|
2069
|
+
unsafe { orbit_motion_interpolate(1, 2, std::ptr::null(), std::ptr::null(), 0) };
|
|
2070
|
+
unsafe { orbit_motion_frames(1, std::ptr::null(), 0, 1.0, false) };
|
|
2071
|
+
orbit_motion_dispose(1);
|
|
2072
|
+
}
|
|
1608
2073
|
}
|