use std::cell::Cell;
use super::*;
use crate::{
Color, EventKind, IMAGE_SCALES, ImageScales, StyleValue,
animation::{AnimationOp, AnimationProp},
geometry::{point, px},
};
#[derive(Default)]
struct FakeEngine {
measures: Cell<u32>,
layouts: Cell<u32>,
}
struct FakeLayout {
chars: usize,
points: Vec<Option<Point<Points>>>,
}
impl TextEngine for FakeEngine {
type Prepared = (usize, Vec<u32>);
fn prepare(&self, text: &AttributedText) -> (usize, Vec<u32>) {
(
text.text.chars().count(),
text.attachments.iter().map(|a| a.index).collect(),
)
}
fn measure(&self, &(chars, _): &(usize, Vec<u32>), max_width: Option<Points>) -> Size<Points> {
self.measures.set(self.measures.get() + 1);
let w = chars as f32 * 8.0;
match max_width.map(|m| m.0) {
Some(max) if w > max && max > 0.0 => size_pt(max, (w / max).ceil() * 16.0),
_ => size_pt(w, 16.0),
}
}
fn layout(&self, (chars, attachments): &(usize, Vec<u32>), _: Size<Points>) -> TextLayout {
self.layouts.set(self.layouts.get() + 1);
let points: Vec<Option<Point<Points>>> = attachments
.iter()
.enumerate()
.map(|(i, &at)| {
(i < 2).then_some(crate::geometry::point(Points(at as f32 * 8.0), Points(2.0)))
})
.collect();
TextLayout(Arc::new(FakeLayout {
chars: *chars,
points,
}))
}
fn attachments(&self, layout: &TextLayout) -> Vec<Option<Point<Points>>> {
layout
.downcast::<FakeLayout>()
.map(|l| l.points.clone())
.unwrap_or_default()
}
}
fn js_events<M: TextEngine>(c: &mut Core<M>) -> Vec<(NodeId, EventKind, EventPayload)> {
use crate::ui::{JsEvent, json_host};
let mut events = Vec::new();
c.take_events(&mut events);
let native = |id, name: &str, data| {
(
id,
EventKind::NativeEvent,
EventPayload::Native {
name: name.into(),
data,
},
)
};
events
.into_iter()
.map(|e| match e {
JsEvent::Event(id, kind, payload) => (id, kind, payload),
JsEvent::Component { host, event } => native(
host,
event.name(),
json_host::event_data(&*event).unwrap_or(crate::JsonValue::Null),
),
JsEvent::Children { host, wanted } => {
native(host, "$children", crate::JsonValue::Bool(wanted))
}
JsEvent::Render {
host,
prop,
key,
value,
} => native(
host,
"$render",
serde_json::json!({
"prop": prop,
"key": key.as_str(),
"value": json_host::render_value(&*value),
}),
),
JsEvent::Unrender { host, prop, key } => native(
host,
"$unrender",
serde_json::json!({ "prop": prop, "key": key.as_str() }),
),
JsEvent::Reply { call, result } => native(
ROOT_ID,
"$return",
match json_host::result(result) {
Ok(v) => serde_json::json!({ "c": call, "v": v }),
Err(e) => serde_json::json!({ "c": call, "e": e }),
},
),
})
.collect()
}
fn rust_prop<P: crate::ui::Props>(name: &'static str, value: Option<crate::JsonValue>) -> Prop {
let specs = P::spec(&mut crate::ui::TsTypes::default());
let decode = specs
.iter()
.find(|p| p.name == name)
.and_then(|p| p.decode)
.expect("a value prop");
Prop::RustProp(
name,
value.map(|v| crate::ui::json_host::decode(decode, v).expect("the value converts")),
)
}
#[test]
fn calls_into_rust_settle_their_promises_whatever_happens() {
use crate::ui::{RustCall, RustOp, ToJs};
let op = |call, run| Op::Rust(RustOp { call, run });
let mut c = core();
c.apply(op(
1,
RustCall::Op(Box::new(|_| Ok(Box::new(42.0f64) as Box<dyn ToJs>))),
))
.unwrap();
c.apply(op(2, RustCall::Op(Box::new(|_| panic!("op boom")))))
.unwrap();
c.apply(op(3, RustCall::Method(40, Box::new(|_| None))))
.unwrap();
c.discard_ops(vec![op(
4,
RustCall::Op(Box::new(|_| Ok(Box::new(1.0f64) as Box<dyn ToJs>))),
)]);
c.apply(op(
0,
RustCall::Op(Box::new(|_| Ok(Box::new(()) as Box<dyn ToJs>))),
))
.unwrap();
c.apply(op(0, RustCall::Op(Box::new(|_| panic!("quiet boom")))))
.unwrap();
c.apply(op(0, RustCall::Method(40, Box::new(|_| None))))
.unwrap();
c.discard_ops(vec![op(
0,
RustCall::Op(Box::new(|_| Ok(Box::new(()) as Box<dyn ToJs>))),
)]);
let replies: Vec<_> = js_events(&mut c)
.into_iter()
.map(|(_, _, payload)| match payload {
EventPayload::Native { data, .. } => data,
other => panic!("{other:?}"),
})
.collect();
assert_eq!(
replies,
[
serde_json::json!({ "c": 1, "v": 42 }),
serde_json::json!({ "c": 2, "e": "op boom" }),
serde_json::json!({ "c": 3, "e": "node 40 hosts no Rust component" }),
serde_json::json!({ "c": 4, "e": "the script stopped before the call ran" }),
]
);
}
#[test]
fn values_of_calls_into_rust_that_panic_as_they_drop_stay_in_the_core() {
use crate::ui::{JsHost, JsOwned, JsValue, PropError, RustCall, RustOp, ToJs, TsTypes};
struct Grenade;
impl Drop for Grenade {
fn drop(&mut self) {
panic!("grenade");
}
}
impl JsValue for Grenade {
fn ts_type(_: &mut TsTypes) -> String {
"null".into()
}
fn from_js(_: crate::ui::JsIn<'_>) -> Result<Self, PropError> {
Ok(Grenade)
}
fn to_js<'a>(&self, host: &'a dyn JsHost) -> Result<JsOwned<'a>, PropError> {
Ok(JsOwned::new(host, host.null()))
}
}
let mut c = core();
c.apply(Op::Rust(RustOp {
call: 0,
run: RustCall::Op(Box::new(|_| Ok(Box::new(Grenade) as Box<dyn ToJs>))),
}))
.unwrap();
let grenade = Grenade;
c.apply(Op::Rust(RustOp {
call: 0,
run: RustCall::Method(
40,
Box::new(move |_| {
let _ = &grenade;
None
}),
),
}))
.unwrap();
let grenade = Grenade;
c.discard_ops(vec![Op::Rust(RustOp {
call: 0,
run: RustCall::Op(Box::new(move |_| {
let _ = &grenade;
Err(String::new().into())
})),
})]);
assert!(js_events(&mut c).is_empty());
}
fn core() -> Core<FakeEngine> {
let mut c = Core::new(FakeEngine::default(), size_pt(320.0, 480.0));
c.set_tokens(crate::ui::tokens::sample());
c
}
fn test_tokens(json: Option<&str>) -> Result<crate::ui::Tokens, String> {
let mut tokens = crate::ui::tokens::sample();
let Some(json) = json else {
return Ok(tokens);
};
let value: serde_json::Value = serde_json::from_str(json).map_err(|e| e.to_string())?;
for (key, value) in value.as_object().ok_or("not an object")? {
let color = value.as_str().and_then(Color::parse).ok_or("not a color")?;
tokens.set_color(key, color.into());
}
Ok(tokens)
}
fn tokens_with(colors: &[(&str, crate::ui::AdaptiveColor)]) -> crate::ui::Tokens {
let mut tokens = crate::ui::Tokens::default();
for (key, color) in colors {
tokens.set_color(key, *color);
}
tokens
}
fn token(key: &str) -> crate::ui::AdaptiveColor {
crate::ui::tokens::sample().color(key).unwrap()
}
fn style(props: &[(&str, StyleValue<'_>)]) -> Box<Style> {
let mut s = Style::default();
for &(k, v) in props {
s.set(k, v).unwrap();
}
Box::new(s)
}
fn style_op(id: NodeId, props: &[(&str, StyleValue<'_>)]) -> Op {
let mut style = Box::<Style>::default();
let mut patch = StylePatch::default();
for &(k, v) in props {
patch.set(&mut style, k, v).unwrap();
}
Op::SetStyle { id, style, patch }
}
fn apply(core: &mut Core<FakeEngine>, ops: Vec<Op>) {
for op in ops {
core.apply(op).unwrap();
}
}
fn frame_of(batch: &MountBatch, id: NodeId) -> Option<Bounds<Points>> {
batch.items.iter().rev().find_map(|i| match i {
MountItem::Frame { id: i, frame } if *i == id => Some(*frame),
_ => None,
})
}
#[test]
fn mounts_subtree_built_detached() {
let mut c = core();
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::Create {
id: 3,
tag: Tag::Text,
},
Op::CreateText {
id: 4,
text: "hello".into(),
},
Op::Insert {
parent: 3,
child: 4,
before: None,
},
Op::Insert {
parent: 2,
child: 3,
before: None,
},
style_op(2, &[("padding", StyleValue::Number(10.0))]),
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
let batch = c.commit().unwrap();
let creates: Vec<_> = batch
.items
.iter()
.filter_map(|i| match i {
MountItem::Create { id, kind } => Some((*id, *kind)),
_ => None,
})
.collect();
assert_eq!(creates, vec![(2, ViewKind::View), (3, ViewKind::Text)]);
let text = batch.items.iter().find_map(|i| match i {
MountItem::Text { id: 3, text, .. } => Some(text.clone()),
_ => None,
});
assert_eq!(text.unwrap().text, "hello");
assert_eq!(frame_of(&batch, 2), Some(rect(0.0, 0.0, 320.0, 36.0)));
assert_eq!(frame_of(&batch, 3), Some(rect(10.0, 10.0, 300.0, 16.0)));
assert!(c.commit().is_none(), "second commit is a no-op");
}
#[test]
fn text_update_emits_only_text_and_frames() {
let mut c = core();
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::Text,
},
Op::CreateText {
id: 3,
text: "a".into(),
},
Op::Insert {
parent: 2,
child: 3,
before: None,
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
c.commit().unwrap();
c.apply(Op::SetText {
id: 3,
text: "abc".into(),
})
.unwrap();
let batch = c.commit().unwrap();
assert!(matches!(&batch.items[0], MountItem::Text { id: 2, text, .. } if text.text == "abc"));
assert_eq!(batch.items.len(), 1);
}
#[test]
fn text_out_of_the_layout_shows_no_glyphs() {
let mut c = core();
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::Create {
id: 4,
tag: Tag::Text,
},
Op::CreateText {
id: 5,
text: "✓".into(),
},
Op::Insert {
parent: 4,
child: 5,
before: None,
},
class_op(4, "hidden"),
Op::Insert {
parent: 2,
child: 4,
before: None,
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
let text_of = |batch: &MountBatch| {
batch.items.iter().find_map(|i| match i {
MountItem::Text {
id: 4,
text,
layout,
..
} => Some((
text.text.clone(),
layout.downcast::<FakeLayout>().unwrap().chars,
)),
_ => None,
})
};
let hidden = Some(("✓".to_owned(), 0));
let shown = Some(("✓".to_owned(), 1));
assert_eq!(text_of(&c.commit().unwrap()), hidden);
let mut toggle = |id, classes, expected: &Option<(String, usize)>| {
c.apply(class_op(id, classes)).unwrap();
assert_eq!(&text_of(&c.commit().unwrap()), expected, "{id}: {classes}");
};
toggle(4, "flex", &shown);
toggle(4, "hidden", &hidden);
toggle(4, "flex", &shown);
toggle(2, "hidden", &hidden);
toggle(2, "flex", &shown);
apply(
&mut c,
vec![
class_op(2, "hidden"),
Op::SetText {
id: 5,
text: "–".into(),
},
],
);
assert_eq!(text_of(&c.commit().unwrap()), Some(("–".to_owned(), 0)));
}
#[test]
fn flex_text_in_a_row_shrinks_and_wraps() {
let mut c = core();
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
style_op(2, &[("flexDirection", StyleValue::Str("row"))]),
Op::Create {
id: 3,
tag: Tag::View,
},
style_op(
3,
&[
("width", StyleValue::Number(120.0)),
("height", StyleValue::Number(10.0)),
],
),
Op::Create {
id: 4,
tag: Tag::Text,
},
style_op(4, &[("flex", StyleValue::Number(1.0))]),
Op::CreateText {
id: 5,
text: "x".repeat(50),
},
Op::Insert {
parent: 4,
child: 5,
before: None,
},
Op::Insert {
parent: 2,
child: 3,
before: None,
},
Op::Insert {
parent: 2,
child: 4,
before: None,
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
let batch = c.commit().unwrap();
assert_eq!(frame_of(&batch, 4), Some(rect(120.0, 0.0, 200.0, 32.0)));
}
#[test]
fn nested_spans_merge_with_styles() {
let mut c = core();
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::Text,
},
Op::CreateText {
id: 3,
text: "Count: ".into(),
},
Op::Create {
id: 4,
tag: Tag::Text,
},
style_op(4, &[("fontWeight", StyleValue::Str("bold"))]),
Op::CreateText {
id: 5,
text: "1".into(),
},
Op::Insert {
parent: 4,
child: 5,
before: None,
},
Op::Insert {
parent: 2,
child: 3,
before: None,
},
Op::Insert {
parent: 2,
child: 4,
before: None,
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
let batch = c.commit().unwrap();
let text = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Text { text, .. } => Some(text.clone()),
_ => None,
})
.unwrap();
assert_eq!(text.text, "Count: 1");
assert_eq!(text.spans.len(), 2);
assert_eq!(text.spans[1].style.font_weight, 700);
assert!(
!batch
.items
.iter()
.any(|i| matches!(i, MountItem::Create { id: 4, .. }))
);
}
fn text_item(batch: &MountBatch) -> Option<Arc<AttributedText>> {
batch.items.iter().find_map(|i| match i {
MountItem::Text { text, .. } => Some(text.clone()),
_ => None,
})
}
#[test]
fn span_backgrounds_style_their_characters() {
let mut c = core();
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::Text,
},
style_op(2, &[("backgroundColor", StyleValue::Str("#00ff00"))]),
Op::CreateText {
id: 3,
text: "a ".into(),
},
Op::Create {
id: 4,
tag: Tag::Text,
},
style_op(
4,
&[
("backgroundColor", StyleValue::Str("#ff0000")),
("borderRadius", StyleValue::Number(4.0)),
],
),
Op::CreateText {
id: 5,
text: "b".into(),
},
Op::Create {
id: 6,
tag: Tag::Text,
},
style_op(6, &[("fontWeight", StyleValue::Str("bold"))]),
Op::CreateText {
id: 7,
text: "c".into(),
},
Op::CreateText {
id: 8,
text: " d".into(),
},
Op::Insert {
parent: 6,
child: 7,
before: None,
},
Op::Insert {
parent: 4,
child: 5,
before: None,
},
Op::Insert {
parent: 4,
child: 6,
before: None,
},
Op::Insert {
parent: 2,
child: 3,
before: None,
},
Op::Insert {
parent: 2,
child: 4,
before: None,
},
Op::Insert {
parent: 2,
child: 8,
before: None,
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
let batch = c.commit().unwrap();
let text = text_item(&batch).unwrap();
assert_eq!(text.text, "a bc d");
let backgrounds: Vec<_> = text.spans.iter().map(|s| s.style.background).collect();
let red = Some(crate::text::TextBackground {
color: Color(0xff00_00ff),
radius: Points(4.0),
node: 4,
});
assert_eq!(backgrounds, [None, red, red, None]);
assert!(text.has_backgrounds());
assert_eq!(
view_style_of(&batch, 2).unwrap().background_color,
Color(0x00ff_00ff)
);
apply(
&mut c,
vec![style_op(
4,
&[("backgroundColor", StyleValue::Str("#0000ff"))],
)],
);
let batch = c.commit().unwrap();
let text = text_item(&batch).unwrap();
assert_eq!(
text.spans[1].style.background.map(|b| (b.color, b.radius)),
Some((Color(0x0000_ffff), Points(0.0)))
);
assert!(frame_of(&batch, 2).is_none());
apply(
&mut c,
vec![style_op(
4,
&[("backgroundColor", StyleValue::Str("transparent"))],
)],
);
let text = text_item(&c.commit().unwrap()).unwrap();
assert!(!text.has_backgrounds());
}
#[test]
fn background_boxes_join_pieces_of_one_span() {
let mut t = AttributedText::default();
let plain = ResolvedTextStyle::default();
let bg = |node| crate::text::TextBackground {
color: Color(0xff00_00ff),
radius: Points(3.0),
node,
};
let code = ResolvedTextStyle {
background: Some(bg(4)),
..plain
};
let bold = ResolvedTextStyle {
font_weight: 700,
..code
};
t.push("a ", plain);
t.push("b", code);
t.push("c", bold);
t.push(
"d",
ResolvedTextStyle {
background: Some(bg(6)),
..plain
},
);
t.push(" e ", plain);
let piece = |i: u32, ascent: f32| crate::text::LinePiece {
x: Points(i as f32 * 10.0),
width: Points(10.0),
background: t.background_at(i),
ascent: Points(ascent),
descent: Points(2.0),
};
let pieces = (0..7).map(|i| piece(i, if i == 3 { 12.0 } else { 10.0 }));
let mut boxes = Vec::new();
crate::text::BackgroundBox::join(pieces, (Points(0.0), Points(70.0)), |b| boxes.push(b));
assert_eq!(boxes.len(), 2);
assert_eq!((boxes[0].x.0, boxes[0].width.0), (20.0, 20.0));
assert_eq!((boxes[0].ascent.0, boxes[0].descent.0), (12.0, 2.0));
assert_eq!(boxes[0].background.node, 4);
assert_eq!((boxes[1].x.0, boxes[1].width.0), (40.0, 10.0));
assert_eq!(boxes[1].background.node, 6);
assert_eq!(boxes[0].radius(), Points(3.0));
assert_eq!(
crate::text::BackgroundBox {
ascent: Points(2.0),
descent: Points(1.0),
..boxes[0]
}
.radius(),
Points(1.5)
);
let mut boxes = Vec::new();
crate::text::BackgroundBox::join(
(0..7).map(|i| piece(i, 10.0)),
(Points(0.0), Points(25.0)),
|b| boxes.push(b),
);
assert_eq!(boxes.len(), 1);
assert_eq!((boxes[0].x.0, boxes[0].width.0), (20.0, 5.0));
}
#[test]
fn placeholder_text_outside_text_is_ignored() {
let mut c = core();
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
Op::CreateText {
id: 3,
text: String::new(),
},
Op::Insert {
parent: ROOT_ID,
child: 3,
before: Some(2),
},
Op::Create {
id: 4,
tag: Tag::View,
},
Op::Insert {
parent: ROOT_ID,
child: 4,
before: None,
},
],
);
let batch = c.commit().unwrap();
let inserts: Vec<_> = batch
.items
.iter()
.filter_map(|i| match i {
MountItem::Insert { child, before, .. } => Some((*child, *before)),
_ => None,
})
.collect();
assert_eq!(inserts, vec![(2, None), (4, None)]);
}
#[test]
fn remove_reinsert_and_release() {
let mut c = core();
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::Create {
id: 3,
tag: Tag::View,
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
Op::Insert {
parent: ROOT_ID,
child: 3,
before: None,
},
],
);
c.commit().unwrap();
c.apply(Op::Insert {
parent: ROOT_ID,
child: 3,
before: Some(2),
})
.unwrap();
let batch = c.commit().unwrap();
assert!(matches!(
batch.items[0],
MountItem::Insert {
child: 3,
before: Some(2),
..
}
));
assert!(
!batch
.items
.iter()
.any(|i| matches!(i, MountItem::Remove { .. }))
);
c.apply(Op::Remove {
parent: ROOT_ID,
child: 2,
})
.unwrap();
let batch = c.commit().unwrap();
assert!(matches!(batch.items[0], MountItem::Remove { id: 2 }));
c.apply(Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
})
.unwrap();
let batch = c.commit().unwrap();
assert!(
!batch
.items
.iter()
.any(|i| matches!(i, MountItem::Create { .. }))
);
c.apply(Op::Release { id: 2 }).unwrap();
let batch = c.commit().unwrap();
assert!(
batch
.items
.iter()
.any(|i| matches!(i, MountItem::Delete { id: 2 }))
);
assert_eq!(c.node_count(), 2);
assert_eq!(
c.apply(Op::SetText {
id: 2,
text: String::new()
}),
Err(ApplyError::UnknownNode(2))
);
}
#[test]
fn events_and_errors() {
let mut c = core();
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
Op::Listen {
id: 2,
event: EventKind::Press,
enabled: true,
},
],
);
let batch = c.commit().unwrap();
assert!(batch.items.iter().any(
|i| matches!(i, MountItem::Events { id: 2, events } if events.contains(EventKind::Press))
));
assert_eq!(
c.apply(Op::Create {
id: 2,
tag: Tag::View
}),
Err(ApplyError::DuplicateNode(2))
);
assert_eq!(
c.apply(Op::Insert {
parent: 2,
child: ROOT_ID,
before: None
}),
Err(ApplyError::Cycle(1))
);
}
#[test]
fn color_change_skips_layout_and_identical_texts_share_measurements() {
let mut c = core();
let mut ops = Vec::new();
for i in 0..3u32 {
let (block, raw) = (10 + i * 2, 11 + i * 2);
ops.extend([
Op::Create {
id: block,
tag: Tag::Text,
},
Op::CreateText {
id: raw,
text: "same".into(),
},
Op::Insert {
parent: block,
child: raw,
before: None,
},
Op::Insert {
parent: ROOT_ID,
child: block,
before: None,
},
]);
}
apply(&mut c, ops);
let batch = c.commit().unwrap();
let first_measures = c.text_engine.measures.get();
assert!(first_measures <= 2, "measured {first_measures} times");
assert_eq!(c.text_engine.layouts.get(), 3);
let content = batch.items.iter().find_map(|i| match i {
MountItem::Text {
id: 10, content, ..
} => Some(*content),
_ => None,
});
assert_eq!(content, Some(rect(0.0, 0.0, 320.0, 16.0)));
c.apply(style_op(10, &[("color", StyleValue::Str("red"))]))
.unwrap();
let batch = c.commit().unwrap();
assert_eq!(
batch.items.len(),
1,
"only the new text layout: {:?}",
batch.items
);
assert!(matches!(&batch.items[0], MountItem::Text { id: 10, .. }));
assert_eq!(
c.text_engine.measures.get(),
first_measures,
"no re-measure"
);
assert_eq!(c.text_engine.layouts.get(), 4);
}
#[test]
fn image_uses_natural_size_and_aspect_ratio() {
let mut c = core();
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::Image,
},
Op::SetProp {
id: 2,
prop: Prop::Source(Some(ImageSource {
uri: "asset:/logo.png".into(),
size: Some(size_pt(200.0, 100.0)),
scales: ImageScales::NONE,
})),
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
Op::Create {
id: 3,
tag: Tag::Image,
},
style_op(
3,
&[
("width", StyleValue::Number(50.0)),
("alignSelf", StyleValue::Str("flex-start")),
],
),
Op::SetProp {
id: 3,
prop: Prop::Source(Some(ImageSource {
uri: "asset:/logo.png".into(),
size: Some(size_pt(200.0, 100.0)),
scales: ImageScales::NONE,
})),
},
Op::Insert {
parent: ROOT_ID,
child: 3,
before: None,
},
],
);
let batch = c.commit().unwrap();
assert!(batch.items.iter().any(|i| matches!(
i,
MountItem::Create {
id: 2,
kind: ViewKind::Image
}
)));
assert!(batch.items.iter().any(|i| matches!(
i,
MountItem::Image {
id: 2,
source: Some(_),
..
}
)));
assert_eq!(
frame_of(&batch, 2).map(|f| (f.size.width.0, f.size.height.0)),
Some((320.0, 160.0))
);
assert_eq!(
frame_of(&batch, 3).map(|f| (f.size.width.0, f.size.height.0)),
Some((50.0, 25.0))
);
}
#[test]
fn native_components_get_their_name_props_and_size() {
let mut c = core();
let prop = |name: &str, value: Option<crate::JsonValue>| Op::SetProp {
id: 2,
prop: Prop::ComponentProp(name.into(), value),
};
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::Native,
},
Op::SetProp {
id: 2,
prop: Prop::Component("com.example.map".into()),
},
prop("zoom", Some(serde_json::json!(3))),
prop("center", Some(serde_json::json!({ "lat": 1.5, "lng": 2 }))),
style_op(2, &[("alignSelf", StyleValue::Str("flex-start"))]),
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
let batch = c.commit().unwrap();
let at = |pred: &dyn Fn(&MountItem) -> bool| batch.items.iter().position(pred);
let create = at(&|i| {
matches!(
i,
MountItem::Create {
id: 2,
kind: ViewKind::Native
}
)
})
.unwrap();
let component =
at(&|i| matches!(i, MountItem::Component { id: 2, name } if &**name == "com.example.map"))
.unwrap();
let props = batch
.items
.iter()
.position(|i| matches!(i, MountItem::ComponentProps { id: 2, .. }))
.unwrap();
assert!(create < component && component < props);
let MountItem::ComponentProps { props: all, .. } = &batch.items[props] else {
unreachable!()
};
assert_eq!(all.len(), 2);
assert!(all.contains(&(
"center".into(),
Some(serde_json::json!({ "lat": 1.5, "lng": 2 }))
)));
assert_eq!(
frame_of(&batch, 2).map(|f| (f.size.width.0, f.size.height.0)),
Some((0.0, 0.0))
);
apply(
&mut c,
vec![
prop("zoom", Some(serde_json::json!(4))),
prop("center", None),
],
);
let batch = c.commit().unwrap();
let changed: Vec<_> = batch
.items
.iter()
.filter_map(|i| match i {
MountItem::ComponentProps { id: 2, props } => Some(props.clone()),
_ => None,
})
.collect();
assert_eq!(
changed,
[vec![
("zoom".into(), Some(serde_json::json!(4))),
("center".into(), None)
]]
);
c.set_component_size(2, size_pt(120.0, 40.0));
let batch = c.commit().unwrap();
assert_eq!(
frame_of(&batch, 2).map(|f| (f.size.width.0, f.size.height.0)),
Some((120.0, 40.0))
);
}
#[test]
fn scroll_reports_content_size() {
let mut c = core();
let mut ops = vec![
Op::Create {
id: 2,
tag: Tag::Scroll,
},
style_op(2, &[("height", StyleValue::Number(200.0))]),
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
];
for i in 0..5u32 {
ops.push(Op::Create {
id: 10 + i,
tag: Tag::View,
});
ops.push(style_op(10 + i, &[("height", StyleValue::Number(100.0))]));
ops.push(Op::Insert {
parent: 2,
child: 10 + i,
before: None,
});
}
apply(&mut c, ops);
let batch = c.commit().unwrap();
let content = batch.items.iter().find_map(|i| match i {
MountItem::ScrollContent { id: 2, size } => Some(*size),
_ => None,
});
assert_eq!(content, Some(size_pt(320.0, 500.0)));
assert_eq!(
frame_of(&batch, 14).map(|f| (f.origin.y.0, f.size.height.0)),
Some((400.0, 100.0))
);
assert!(batch.items.iter().any(|i| matches!(
i,
MountItem::ScrollConfig {
id: 2,
horizontal: false,
..
}
)));
}
#[test]
fn clipped_overflow_does_not_grow_scroll_content() {
fn content_width(clips: bool) -> f32 {
let mut c = core();
let mut boxed = vec![
("width", StyleValue::Number(300.0)),
("height", StyleValue::Number(100.0)),
];
if clips {
boxed.push(("overflow", StyleValue::Str("hidden")));
}
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::Scroll,
},
style_op(2, &[("height", StyleValue::Number(200.0))]),
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
Op::Create {
id: 3,
tag: Tag::View,
},
style_op(3, &boxed),
Op::Insert {
parent: 2,
child: 3,
before: None,
},
Op::Create {
id: 4,
tag: Tag::View,
},
style_op(
4,
&[
("position", StyleValue::Str("absolute")),
("left", StyleValue::Number(0.0)),
("top", StyleValue::Number(0.0)),
("width", StyleValue::Number(400.0)),
("height", StyleValue::Number(50.0)),
],
),
Op::Insert {
parent: 3,
child: 4,
before: None,
},
],
);
let batch = c.commit().unwrap();
batch
.items
.iter()
.find_map(|i| match i {
MountItem::ScrollContent { id: 2, size } => Some(size.width.0),
_ => None,
})
.expect("scroll content size")
}
assert_eq!(content_width(false), 400.0);
assert_eq!(content_width(true), 320.0);
}
#[test]
fn scroll_content_is_sent_once_per_batch() {
let mut c = core();
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::Scroll,
},
style_op(2, &[("height", StyleValue::Number(200.0))]),
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
c.commit();
let key = c.key(2).unwrap();
c.set_scroll_content(key, size_pt(320.0, 277.0));
c.set_scroll_content(key, size_pt(320.0, 909.0));
let sizes: Vec<_> = c
.mount
.iter()
.filter_map(|i| match i {
MountItem::ScrollContent { id: 2, size } => Some(size.height.0),
_ => None,
})
.collect();
assert_eq!(sizes, [909.0]);
c.mount.clear();
c.batches += 1;
c.set_scroll_content(key, size_pt(320.0, 300.0));
assert!(matches!(
c.mount[..],
[MountItem::ScrollContent { id: 2, .. }]
));
}
fn render_binds(
c: &mut Core<FakeEngine>,
next_id: &mut u32,
cells: &mut Vec<u32>,
row_height: f32,
) -> usize {
let mut binds = Vec::new();
c.take_list_binds(&mut binds);
let mut count = 0;
for bind in binds {
for (slot, index) in bind.binds {
count += 1;
while cells.len() <= slot as usize {
let id = *next_id;
*next_id += 1;
apply(
c,
vec![
Op::Create { id, tag: Tag::Cell },
Op::SetProp {
id,
prop: Prop::Slot(CellSlot::Item(cells.len() as u32)),
},
Op::Insert {
parent: bind.list,
child: id,
before: None,
},
],
);
cells.push(id);
}
let cell = cells[slot as usize];
c.apply(style_op(
cell,
&[("height", StyleValue::Number(row_height))],
))
.unwrap();
c.apply(Op::SetProp {
id: cell,
prop: Prop::CellIndex(index),
})
.unwrap();
}
}
count
}
#[test]
fn list_renders_window_and_recycles_cells() {
let mut c = core();
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::List,
},
style_op(2, &[("height", StyleValue::Number(400.0))]),
Op::SetProp {
id: 2,
prop: Prop::EstimatedItemSize(px(50.0)),
},
Op::SetProp {
id: 2,
prop: Prop::Count(1000),
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
c.commit();
let (mut next_id, mut cells) = (100, Vec::new());
assert_eq!(render_binds(&mut c, &mut next_id, &mut cells, 100.0), 17);
let batch = c.commit().unwrap();
assert_eq!(
frame_of(&batch, cells[3]),
Some(rect(0.0, 300.0, 320.0, 100.0))
);
let content = batch.items.iter().rev().find_map(|i| match i {
MountItem::ScrollContent { id: 2, size } => Some(size.height.0),
_ => None,
});
assert_eq!(content, Some(17.0 * 100.0 + 983.0 * 50.0));
c.set_scroll_offset(2, point(px(0.0), px(10_000.0)), u32::MAX);
c.commit();
assert_eq!(render_binds(&mut c, &mut next_id, &mut cells, 50.0), 25);
assert_eq!(cells.len(), 25);
c.commit();
c.set_scroll_offset(2, point(px(0.0), px(20_000.0)), u32::MAX);
c.commit();
assert_eq!(render_binds(&mut c, &mut next_id, &mut cells, 50.0), 25);
assert_eq!(cells.len(), 25);
c.commit();
assert_eq!(c.node_count(), 2 + cells.len());
}
#[test]
fn list_keeps_visible_content_anchored() {
let mut c = core();
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::List,
},
style_op(2, &[("height", StyleValue::Number(400.0))]),
Op::SetProp {
id: 2,
prop: Prop::EstimatedItemSize(px(50.0)),
},
Op::SetProp {
id: 2,
prop: Prop::Count(100),
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
c.commit();
c.set_scroll_offset(2, point(px(0.0), px(2000.0)), u32::MAX);
c.commit();
let (mut next_id, mut cells) = (100, Vec::new());
render_binds(&mut c, &mut next_id, &mut cells, 50.0);
c.commit();
c.set_scroll_offset(2, point(px(0.0), px(1200.0)), u32::MAX);
c.commit();
render_binds(&mut c, &mut next_id, &mut cells, 80.0);
let batch = c.commit().unwrap();
let shift: f32 = batch
.items
.iter()
.filter_map(|i| match i {
MountItem::ScrollBy { id: 2, dy, .. } => Some(dy.0),
_ => None,
})
.sum();
assert!(
shift > 0.0,
"rows above the viewport grew; content must be shifted down"
);
}
fn estimated_list(c: &mut Core<FakeEngine>) {
apply(
c,
vec![
Op::Create {
id: 2,
tag: Tag::List,
},
style_op(2, &[("height", StyleValue::Number(400.0))]),
Op::SetProp {
id: 2,
prop: Prop::EstimatedItemSize(px(50.0)),
},
Op::SetProp {
id: 2,
prop: Prop::Count(100),
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
}
fn scrolled(batch: &MountBatch, start: f32) -> f32 {
batch.items.iter().fold(start, |at, item| match item {
MountItem::Command {
id: 2,
command: Command::ScrollTo { y, .. },
} => y.0,
MountItem::ScrollBy { id: 2, dy, .. } => at + dy.0,
_ => at,
})
}
#[test]
fn scroll_commands_follow_their_target_as_rows_are_measured() {
let mut c = core();
estimated_list(&mut c);
c.commit();
let (mut next_id, mut cells) = (100, Vec::new());
render_binds(&mut c, &mut next_id, &mut cells, 50.0);
c.commit();
c.apply(Op::Command {
id: 2,
command: Command::ScrollToEnd { animated: false },
})
.unwrap();
let mut at = scrolled(&c.commit().unwrap(), 0.0);
for _ in 0..4 {
render_binds(&mut c, &mut next_id, &mut cells, 80.0);
if let Some(batch) = c.commit() {
at = scrolled(&batch, at);
}
}
let key = c.key(2).unwrap();
let content = c.nodes[key as usize]
.scroll
.as_ref()
.unwrap()
.content
.unwrap();
assert_eq!(at, (content.height - Points(400.0)).0);
c.apply(Op::Command {
id: 2,
command: Command::ScrollToIndex {
index: 50,
animated: false,
view_position: crate::geometry::Percentage(0.5),
view_offset: px(0.0),
},
})
.unwrap();
at = scrolled(&c.commit().unwrap(), at);
for _ in 0..4 {
render_binds(&mut c, &mut next_id, &mut cells, 80.0);
if let Some(batch) = c.commit() {
at = scrolled(&batch, at);
}
}
let list = c.nodes[key as usize].list.as_ref().unwrap();
assert_eq!(list.item_at(50) + 40.0 - at, 200.0);
assert!(!list.pinned(), "settled");
c.set_scroll_offset(2, point(px(0.0), px(at - 600.0)), u32::MAX);
c.commit();
assert!(render_binds(&mut c, &mut next_id, &mut cells, 120.0) > 0);
let batch = c.commit().unwrap();
assert!(
!batch
.items
.iter()
.any(|i| matches!(i, MountItem::Command { id: 2, .. })),
"no scroll command after the user scrolled"
);
}
#[test]
fn animated_scroll_commands_retarget_while_rows_are_measured() {
let mut c = core();
estimated_list(&mut c);
c.commit();
let (mut next_id, mut cells) = (100, Vec::new());
render_binds(&mut c, &mut next_id, &mut cells, 80.0);
c.commit();
c.apply(Op::Command {
id: 2,
command: Command::ScrollToEnd { animated: true },
})
.unwrap();
let first = scrolled(&c.commit().unwrap(), 0.0);
let mut target = first;
c.set_scroll_offset(2, point(px(0.0), px(1000.0)), 1);
c.commit();
c.set_scroll_offset(2, point(px(0.0), px(first - 10.0)), 1);
c.commit();
let mut issued = 1;
for _ in 0..4 {
render_binds(&mut c, &mut next_id, &mut cells, 80.0);
if let Some(batch) = c.commit() {
for item in &batch.items {
if let MountItem::Command {
id: 2,
command: Command::ScrollTo { y, animated, .. },
} = item
{
assert!(animated);
target = y.0;
issued += 1;
}
}
}
}
assert!(issued > 1 && target > first, "retargeted");
let key = c.key(2).unwrap();
let content = c.nodes[key as usize]
.scroll
.as_ref()
.unwrap()
.content
.unwrap();
assert_eq!(target, (content.height - Points(400.0)).0);
}
#[test]
fn animated_background_colors_replace_gradients() {
let mut c = core();
let mut s = Style::default();
s.set_background_images(vec![crate::effects::Gradient {
kind: crate::effects::GradientKind::Linear {
angle: crate::geometry::deg(180.0),
},
stops: Vec::new(),
repeating: false,
}]);
s.animated.push((AnimationProp::BackgroundColor, 7));
apply(
&mut c,
vec![
Op::Animation(AnimationOp::CreateValue {
node: 7,
value: 1.0,
}),
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetStyle {
id: 2,
style: Box::new(s),
patch: StylePatch::default(),
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
let batch = c.commit().unwrap();
assert!(view_style_of(&batch, 2).is_none_or(|s| s.effects.is_none()));
}
#[test]
fn animation_ops_and_bindings_are_forwarded_in_order() {
let mut c = core();
let mut s = Style::default();
s.animated.push((AnimationProp::Opacity, 7));
apply(
&mut c,
vec![
Op::Animation(AnimationOp::CreateValue {
node: 7,
value: 1.0,
}),
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetStyle {
id: 2,
style: Box::new(s),
patch: StylePatch::default(),
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
let batch = c.commit().unwrap();
let animation = batch.items.iter().position(|i| {
matches!(
i,
MountItem::Animation(AnimationOp::CreateValue { node: 7, .. })
)
});
let bind = batch
.items
.iter()
.position(|i| matches!(i, MountItem::AnimationBindings { id: 2, .. }));
assert!(
animation.unwrap() < bind.unwrap(),
"values exist before views bind them"
);
}
#[test]
fn leaf_styles_are_resolved() {
let mut c = core();
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::Text,
},
style_op(2, &[("padding", StyleValue::Number(10.0))]),
Op::CreateText {
id: 3,
text: "abc".into(),
},
Op::Insert {
parent: 2,
child: 3,
before: None,
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
Op::Create {
id: 4,
tag: Tag::View,
},
style_op(
4,
&[
("width", StyleValue::Number(100.0)),
("aspectRatio", StyleValue::Number(2.0)),
("alignSelf", StyleValue::Str("flex-start")),
],
),
Op::Insert {
parent: ROOT_ID,
child: 4,
before: None,
},
Op::Create {
id: 5,
tag: Tag::View,
},
style_op(5, &[("minHeight", StyleValue::Number(80.0))]),
Op::Insert {
parent: ROOT_ID,
child: 5,
before: None,
},
],
);
let batch = c.commit().unwrap();
assert_eq!(
frame_of(&batch, 2).map(|f| f.size.height.0),
Some(16.0 + 20.0)
);
let content = batch.items.iter().find_map(|i| match i {
MountItem::Text { id: 2, content, .. } => Some(*content),
_ => None,
});
assert_eq!(content, Some(rect(10.0, 10.0, 300.0, 16.0)));
assert_eq!(
frame_of(&batch, 4).map(|f| (f.size.width.0, f.size.height.0)),
Some((100.0, 50.0))
);
assert_eq!(frame_of(&batch, 5).map(|f| f.size.height.0), Some(80.0));
}
#[test]
#[ignore]
fn bench_row_relayout() {
let mut c = core();
let row = |s: &[(&str, StyleValue<'_>)]| style(s);
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetStyle {
id: 2,
style: row(&[
("flexDirection", StyleValue::Str("row")),
("alignItems", StyleValue::Str("center")),
("gap", StyleValue::Number(12.0)),
("padding", StyleValue::Number(8.0)),
]),
patch: StylePatch::default(),
},
Op::Create {
id: 3,
tag: Tag::Image,
},
Op::SetStyle {
id: 3,
style: row(&[
("width", StyleValue::Number(48.0)),
("height", StyleValue::Number(48.0)),
]),
patch: StylePatch::default(),
},
Op::Insert {
parent: 2,
child: 3,
before: None,
},
Op::Create {
id: 4,
tag: Tag::View,
},
Op::SetStyle {
id: 4,
style: row(&[("flex", StyleValue::Number(1.0))]),
patch: StylePatch::default(),
},
Op::Insert {
parent: 2,
child: 4,
before: None,
},
Op::Create {
id: 5,
tag: Tag::Text,
},
Op::CreateText {
id: 6,
text: "Photo #1".into(),
},
Op::Insert {
parent: 5,
child: 6,
before: None,
},
Op::Insert {
parent: 4,
child: 5,
before: None,
},
Op::Create {
id: 7,
tag: Tag::Text,
},
Op::CreateText {
id: 8,
text: "by Ada".into(),
},
Op::Insert {
parent: 7,
child: 8,
before: None,
},
Op::Insert {
parent: 4,
child: 7,
before: None,
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
c.commit();
let n = 2000;
let start = std::time::Instant::now();
for i in 0..n {
c.apply(Op::SetText {
id: 6,
text: format!("Photo #{i}"),
})
.unwrap();
c.apply(Op::SetText {
id: 8,
text: format!("by {}", ["Ada", "Ken"][i % 2]),
})
.unwrap();
c.commit();
}
eprintln!("row relayout: {:?} per commit", start.elapsed() / n as u32);
}
#[test]
#[ignore]
fn bench_cell_relayout() {
let mut c = Core::new(crate::ApproxTextEngine, size_pt(390.0, 800.0));
let s = |p: &[(&str, StyleValue<'_>)]| style(p);
let mut ops = vec![
Op::Create {
id: 2,
tag: Tag::List,
},
Op::SetStyle {
id: 2,
style: s(&[("flex", StyleValue::Number(1.0))]),
patch: StylePatch::default(),
},
Op::SetProp {
id: 2,
prop: Prop::EstimatedItemSize(px(64.0)),
},
Op::SetProp {
id: 2,
prop: Prop::Count(1000),
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
];
for op in std::mem::take(&mut ops) {
c.apply(op).unwrap();
}
c.commit();
let mut binds = Vec::new();
c.take_list_binds(&mut binds);
for op in [
Op::Create {
id: 10,
tag: Tag::Cell,
},
Op::SetProp {
id: 10,
prop: Prop::Slot(CellSlot::Item(0)),
},
Op::Insert {
parent: 2,
child: 10,
before: None,
},
Op::Create {
id: 11,
tag: Tag::View,
},
Op::SetStyle {
id: 11,
style: s(&[
("flexDirection", StyleValue::Str("row")),
("alignItems", StyleValue::Str("center")),
("gap", StyleValue::Number(12.0)),
("paddingHorizontal", StyleValue::Number(16.0)),
("paddingVertical", StyleValue::Number(8.0)),
]),
patch: StylePatch::default(),
},
Op::Insert {
parent: 10,
child: 11,
before: None,
},
Op::Create {
id: 12,
tag: Tag::Image,
},
Op::SetStyle {
id: 12,
style: s(&[
("width", StyleValue::Number(48.0)),
("height", StyleValue::Number(48.0)),
]),
patch: StylePatch::default(),
},
Op::Insert {
parent: 11,
child: 12,
before: None,
},
Op::Create {
id: 13,
tag: Tag::View,
},
Op::SetStyle {
id: 13,
style: s(&[
("flex", StyleValue::Number(1.0)),
("gap", StyleValue::Number(2.0)),
]),
patch: StylePatch::default(),
},
Op::Insert {
parent: 11,
child: 13,
before: None,
},
Op::Create {
id: 14,
tag: Tag::Text,
},
Op::CreateText {
id: 15,
text: "Photo #1".into(),
},
Op::Insert {
parent: 14,
child: 15,
before: None,
},
Op::Insert {
parent: 13,
child: 14,
before: None,
},
Op::Create {
id: 16,
tag: Tag::Text,
},
Op::CreateText {
id: 17,
text: "by Ada".into(),
},
Op::Insert {
parent: 16,
child: 17,
before: None,
},
Op::Insert {
parent: 13,
child: 16,
before: None,
},
Op::SetProp {
id: 10,
prop: Prop::CellIndex(0),
},
] {
c.apply(op).unwrap();
}
c.commit();
for idle in [0u64, 10] {
let n = if idle == 0 { 2000 } else { 100 };
let mut total = std::time::Duration::ZERO;
for i in 0..n {
std::thread::sleep(std::time::Duration::from_millis(idle));
let start = std::time::Instant::now();
c.apply(Op::SetText {
id: 15,
text: format!("Photo #{i}"),
})
.unwrap();
c.apply(Op::SetText {
id: 17,
text: format!("by {}", ["Ada", "Ken"][i % 2]),
})
.unwrap();
c.commit();
total += start.elapsed();
}
eprintln!(
"cell relayout ({idle} ms idle between frames): {:?} per commit",
total / n as u32
);
}
}
fn input_core() -> Core<FakeEngine> {
let mut c = core();
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::Input,
},
Op::SetProp {
id: 2,
prop: Prop::Placeholder("Name".into()),
},
style_op(2, &[("padding", StyleValue::Number(6.0))]),
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
c
}
#[test]
fn input_measures_its_placeholder_and_reports_its_content_box() {
let mut c = input_core();
let batch = c.commit().unwrap();
assert_eq!(frame_of(&batch, 2).map(|f| f.size.height.0), Some(28.0));
assert!(batch.items.iter().any(|i| matches!(i,
MountItem::Input { id: 2, config } if config.placeholder == "Name" && !config.multiline)));
assert!(batch.items.iter().any(|i| matches!(i,
MountItem::InputContent { id: 2, content } if *content == rect(6.0, 6.0, 308.0, 16.0))));
assert!(
!batch
.items
.iter()
.any(|i| matches!(i, MountItem::InputValue { .. }))
);
}
#[test]
fn inputs_resolve_what_return_does() {
use crate::{SubmitBehavior as S, mount::InputConfig};
let config = |multiline, blur_on_submit, submit_behavior| InputConfig {
multiline,
blur_on_submit,
submit_behavior,
..InputConfig::default()
};
assert_eq!(config(false, true, None).submit(), S::BlurAndSubmit);
assert_eq!(config(false, false, None).submit(), S::Submit);
assert_eq!(config(true, true, None).submit(), S::Newline);
assert_eq!(config(true, true, Some(S::Submit)).submit(), S::Submit);
assert_eq!(
config(false, false, Some(S::BlurAndSubmit)).submit(),
S::BlurAndSubmit
);
assert_eq!(
config(false, true, Some(S::Newline)).submit(),
S::BlurAndSubmit
);
}
#[test]
fn inputs_take_their_submit_behavior() {
let mut c = input_core();
c.commit().unwrap();
apply(
&mut c,
vec![
Op::SetProp {
id: 2,
prop: Prop::Multiline(true),
},
Op::SetProp {
id: 2,
prop: Prop::SubmitBehavior(Some(crate::SubmitBehavior::Submit)),
},
],
);
let batch = c.commit().unwrap();
assert!(batch.items.iter().any(|i| matches!(i,
MountItem::Input { id: 2, config } if config.submit() == crate::SubmitBehavior::Submit)));
}
#[test]
fn input_values_from_js_and_native_edits() {
let mut c = input_core();
c.commit();
c.input_changed(2, "ab", 2);
c.apply(Op::SetProp {
id: 2,
prop: Prop::Value("ab".into()),
})
.unwrap();
assert!(c.commit().is_none());
c.apply(Op::SetProp {
id: 2,
prop: Prop::Value("AB".into()),
})
.unwrap();
let batch = c.commit().unwrap();
assert!(batch.items.iter().any(|i| matches!(i,
MountItem::InputValue { id: 2, text, count: 2 } if text == "AB")));
c.apply(Op::SetProp {
id: 2,
prop: Prop::DefaultValue("x".into()),
})
.unwrap();
assert!(c.commit().is_none());
}
#[test]
fn multiline_inputs_grow_with_their_text() {
let mut c = input_core();
c.apply(Op::SetProp {
id: 2,
prop: Prop::Multiline(true),
})
.unwrap();
c.commit();
c.input_changed(2, &"x".repeat(50), 1);
let batch = c.commit().unwrap();
assert_eq!(frame_of(&batch, 2).map(|f| f.size.height.0), Some(44.0));
c.input_changed(2, "x\n", 2);
let batch = c.commit().unwrap();
assert_eq!(frame_of(&batch, 2).map(|f| f.size.height.0), Some(28.0));
}
#[test]
fn commands_reach_created_views_only() {
let mut c = input_core();
c.apply(Op::Command {
id: 2,
command: crate::Command::Focus,
})
.unwrap();
let batch = c.commit().unwrap();
assert!(matches!(
batch.items.last(),
Some(MountItem::Command {
id: 2,
command: crate::Command::Focus
})
));
c.apply(Op::Create {
id: 3,
tag: Tag::Input,
})
.unwrap();
c.apply(Op::Command {
id: 3,
command: crate::Command::Focus,
})
.unwrap();
assert!(c.commit().is_none());
}
fn insert_items(batch: &MountBatch) -> Vec<(NodeId, NodeId, Option<NodeId>)> {
batch
.items
.iter()
.filter_map(|i| match i {
MountItem::Insert {
parent,
child,
before,
} => Some((*parent, *child, *before)),
_ => None,
})
.collect()
}
#[test]
fn z_index_orders_native_views() {
let mut c = core();
let z = |v: f32| style(&[("zIndex", StyleValue::Number(v))]);
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::Create {
id: 3,
tag: Tag::View,
},
Op::Create {
id: 4,
tag: Tag::View,
},
Op::SetStyle {
id: 2,
style: z(5.0),
patch: StylePatch::default(),
},
Op::Insert {
parent: 1,
child: 2,
before: None,
},
Op::Insert {
parent: 1,
child: 3,
before: None,
},
],
);
let batch = c.commit().unwrap();
assert_eq!(insert_items(&batch), vec![(1, 2, None), (1, 3, Some(2))]);
apply(
&mut c,
vec![Op::SetStyle {
id: 3,
style: z(9.0),
patch: StylePatch::default(),
}],
);
let batch = c.commit().unwrap();
assert_eq!(insert_items(&batch), vec![(1, 3, None)]);
apply(
&mut c,
vec![Op::Insert {
parent: 1,
child: 4,
before: None,
}],
);
let batch = c.commit().unwrap();
assert_eq!(insert_items(&batch), vec![(1, 4, Some(2))]);
}
#[test]
fn layout_events_carry_frames_and_late_listeners_get_one() {
let mut c = core();
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
style_op(2, &[("height", StyleValue::Number(40.0))]),
Op::Listen {
id: 2,
event: EventKind::Layout,
enabled: true,
},
Op::Insert {
parent: 1,
child: 2,
before: None,
},
],
);
c.commit().unwrap();
let events = js_events(&mut c);
assert_eq!(
events,
vec![(
2,
EventKind::Layout,
EventPayload::Layout(rect(0.0, 0.0, 320.0, 40.0))
)]
);
apply(
&mut c,
vec![
Op::Listen {
id: 2,
event: EventKind::Layout,
enabled: false,
},
Op::Listen {
id: 2,
event: EventKind::Layout,
enabled: true,
},
],
);
c.commit();
let events = js_events(&mut c);
assert_eq!(events.len(), 1);
}
#[test]
fn font_scale_scales_text_that_allows_it() {
let mut c = core();
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::Text,
},
Op::CreateText {
id: 3,
text: "abc".into(),
},
Op::Insert {
parent: 2,
child: 3,
before: None,
},
Op::Insert {
parent: 1,
child: 2,
before: None,
},
Op::Create {
id: 4,
tag: Tag::Text,
},
Op::CreateText {
id: 5,
text: "abc".into(),
},
Op::SetProp {
id: 4,
prop: Prop::AllowFontScaling(false),
},
Op::Insert {
parent: 4,
child: 5,
before: None,
},
Op::Insert {
parent: 1,
child: 4,
before: None,
},
],
);
c.commit().unwrap();
c.set_font_scale(2.0);
let batch = c.commit().unwrap();
let size = |id| {
batch.items.iter().find_map(|i| match i {
MountItem::Text { id: i, text, .. } if *i == id => {
Some(text.spans[0].style.font_size.0)
}
_ => None,
})
};
assert_eq!(size(2), Some(28.0));
assert_eq!(size(4), None, "text without font scaling is unchanged");
}
#[test]
fn layout_animation_leads_the_batch() {
let mut c = core();
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::Insert {
parent: 1,
child: 2,
before: None,
},
Op::Command {
id: 1,
command: Command::LayoutAnimation(crate::LayoutAnimation {
create: None,
update: None,
delete: None,
}),
},
],
);
let batch = c.commit().unwrap();
assert!(
matches!(batch.items[0], MountItem::LayoutAnimation(_)),
"{:?}",
batch.items
);
}
#[test]
fn view_props_and_gestures_reach_the_platform() {
let mut c = core();
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetProp {
id: 2,
prop: Prop::HitSlop(crate::EdgeInsets::all(px(10.0))),
},
Op::SetProp {
id: 2,
prop: Prop::PinchValue(Some(7)),
},
Op::Insert {
parent: 1,
child: 2,
before: None,
},
],
);
let batch = c.commit().unwrap();
assert!(batch.items.iter().any(
|i| matches!(i, MountItem::ViewProps { id: 2, props } if props.hit_slop.top == px(10.0))
));
assert!(
batch
.items
.iter()
.any(|i| matches!(i, MountItem::Gestures { id: 2, values } if values.pinch == Some(7)))
);
}
#[test]
fn a_changing_label_is_sent_alone() {
use crate::props::AccessibilityProp;
let label = |v: &str| Op::SetProp {
id: 2,
prop: Prop::Accessibility(AccessibilityProp::Label(Some(v.into()))),
};
let mut c = core();
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
label("0 items"),
Op::Insert {
parent: 1,
child: 2,
before: None,
},
],
);
let batch = c.commit().unwrap();
assert!(batch.items.iter().any(|i| matches!(i, MountItem::ViewProps { id: 2, props } if props.accessibility.label.as_deref() == Some("0 items"))));
apply(&mut c, vec![label("1 item")]);
let batch = c.commit().unwrap();
assert_eq!(batch.items.len(), 1, "{:?}", batch.items);
assert!(
matches!(&batch.items[0], MountItem::AccessibilityLabel { id: 2, label: Some(l) } if &**l == "1 item")
);
apply(
&mut c,
vec![
label("2 items"),
Op::SetProp {
id: 2,
prop: Prop::TestId(Some("counter".into())),
},
],
);
let batch = c.commit().unwrap();
assert_eq!(batch.items.len(), 1, "{:?}", batch.items);
assert!(
matches!(&batch.items[0], MountItem::ViewProps { id: 2, props } if props.accessibility.label.as_deref() == Some("2 items"))
);
}
#[test]
fn screens_fill_their_stack_below_the_header() {
let mut c = core();
c.set_safe_area(crate::EdgeInsets {
top: px(20.0),
..Default::default()
});
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::Stack,
},
style_op(2, &[("flex", StyleValue::Number(1.0))]),
Op::Create {
id: 3,
tag: Tag::Screen,
},
Op::Create {
id: 4,
tag: Tag::View,
},
style_op(4, &[("flex", StyleValue::Number(1.0))]),
Op::Insert {
parent: 3,
child: 4,
before: None,
},
Op::Insert {
parent: 2,
child: 3,
before: None,
},
Op::Insert {
parent: 1,
child: 2,
before: None,
},
],
);
let batch = c.commit().unwrap();
assert_eq!(frame_of(&batch, 3), Some(rect(0.0, 0.0, 320.0, 480.0)));
assert_eq!(frame_of(&batch, 4), Some(rect(0.0, 64.0, 320.0, 416.0)));
assert!(batch.items.iter().any(
|i| matches!(i, MountItem::ScreenConfig { id: 3, config } if config.header_height == px(64.0))
));
c.set_screen_header(3, Some(px(52.0)));
let batch = c.commit().unwrap();
assert_eq!(frame_of(&batch, 4), Some(rect(0.0, 52.0, 320.0, 428.0)));
assert!(batch.items.iter().any(
|i| matches!(i, MountItem::ScreenConfig { id: 3, config } if config.header_height == px(52.0))
));
apply(
&mut c,
vec![Op::SetProp {
id: 3,
prop: Prop::Screen(crate::ScreenProp::HeaderVisible(false)),
}],
);
let batch = c.commit().unwrap();
assert_eq!(frame_of(&batch, 4), Some(rect(0.0, 0.0, 320.0, 480.0)));
}
#[test]
fn modals_lay_out_at_the_reported_size() {
let mut c = core();
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::Modal,
},
Op::Create {
id: 3,
tag: Tag::View,
},
style_op(3, &[("flex", StyleValue::Number(1.0))]),
Op::Insert {
parent: 2,
child: 3,
before: None,
},
Op::Insert {
parent: 1,
child: 2,
before: None,
},
],
);
let batch = c.commit().unwrap();
assert_eq!(frame_of(&batch, 3).map(|f| f.size.height.0), Some(480.0));
c.set_root_size(2, size_pt(320.0, 400.0), crate::EdgeInsets::default());
let batch = c.commit().unwrap();
assert_eq!(frame_of(&batch, 3).map(|f| f.size.height.0), Some(400.0));
}
#[test]
fn right_to_left_layouts() {
let mut c = core();
let n = |v: f32| StyleValue::Number(v);
let s = |v: &'static str| StyleValue::Str(v);
let mut ops = vec![
Op::Create {
id: 2,
tag: Tag::View,
},
style_op(
2,
&[
("flexDirection", s("row")),
("width", n(300.0)),
("height", n(50.0)),
],
),
];
for (id, key) in [(3, "marginStart"), (4, "marginLeft")] {
ops.push(Op::Create { id, tag: Tag::View });
ops.push(Op::SetStyle {
id,
style: style(&[("width", n(50.0)), ("height", n(50.0)), (key, n(10.0))]),
patch: StylePatch::default(),
});
ops.push(Op::Insert {
parent: 2,
child: id,
before: None,
});
}
ops.push(Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
});
apply(&mut c, ops);
let batch = c.commit().unwrap();
assert_eq!(frame_of(&batch, 3).map(|f| f.origin.x.0), Some(10.0));
assert_eq!(frame_of(&batch, 4).map(|f| f.origin.x.0), Some(70.0));
apply(
&mut c,
vec![Op::SetProp {
id: ROOT_ID,
prop: Prop::LayoutDirection(true),
}],
);
let batch = c.commit().unwrap();
assert!(
batch
.items
.iter()
.any(|i| matches!(i, MountItem::LayoutDirection { rtl: true }))
);
assert_eq!(
frame_of(&batch, 3).map(|f| f.origin.x.0),
Some(300.0 - 10.0 - 50.0)
);
assert_eq!(
frame_of(&batch, 4).map(|f| f.origin.x.0),
Some(300.0 - 60.0 - 10.0 - 50.0)
);
apply(
&mut c,
vec![
Op::Create {
id: 5,
tag: Tag::View,
},
style_op(
5,
&[
("flexDirection", s("row")),
("direction", s("ltr")),
("height", n(20.0)),
],
),
Op::Create {
id: 6,
tag: Tag::View,
},
style_op(
6,
&[
("width", n(20.0)),
("start", n(5.0)),
("position", s("absolute")),
],
),
Op::Insert {
parent: 5,
child: 6,
before: None,
},
Op::Insert {
parent: ROOT_ID,
child: 5,
before: None,
},
],
);
let batch = c.commit().unwrap();
assert_eq!(frame_of(&batch, 6).map(|f| f.origin.x.0), Some(5.0));
apply(
&mut c,
vec![Op::SetProp {
id: ROOT_ID,
prop: Prop::LayoutDirection(false),
}],
);
let batch = c.commit().unwrap();
assert_eq!(frame_of(&batch, 3).map(|f| f.origin.x.0), Some(10.0));
assert_eq!(frame_of(&batch, 4).map(|f| f.origin.x.0), Some(70.0));
}
#[test]
fn right_to_left_horizontal_scrolls() {
let mut c = core();
let n = |v: f32| StyleValue::Number(v);
let mut ops = vec![
Op::SetProp {
id: ROOT_ID,
prop: Prop::LayoutDirection(true),
},
Op::Create {
id: 2,
tag: Tag::Scroll,
},
Op::SetProp {
id: 2,
prop: Prop::Horizontal(true),
},
style_op(
2,
&[
("width", n(150.0)),
("height", n(50.0)),
("paddingStart", n(10.0)),
],
),
];
for id in [3, 4] {
ops.push(Op::Create { id, tag: Tag::View });
ops.push(Op::SetStyle {
id,
style: style(&[("width", n(100.0)), ("height", n(50.0))]),
patch: StylePatch::default(),
});
ops.push(Op::Insert {
parent: 2,
child: id,
before: None,
});
}
ops.push(Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
});
apply(&mut c, ops);
let batch = c.commit().unwrap();
assert_eq!(
frame_of(&batch, 3).map(|f| f.origin.x.0),
Some(210.0 - 10.0 - 100.0)
);
assert_eq!(frame_of(&batch, 4).map(|f| f.origin.x.0), Some(0.0));
assert!(batch.items.iter().any(|i| matches!(
i,
MountItem::Command {
id: 2,
command: Command::ScrollTo { x, .. }
} if x.0 == 60.0
)));
}
fn class_op(id: NodeId, classes: &str) -> Op {
Op::SetProp {
id,
prop: Prop::Class(classes.into()),
}
}
fn view_style_of(batch: &MountBatch, id: NodeId) -> Option<ViewStyle> {
batch.items.iter().rev().find_map(|i| match i {
MountItem::ViewStyle { id: i, style } if *i == id => Some((**style).clone()),
_ => None,
})
}
fn view_with_classes(c: &mut Core<FakeEngine>, id: NodeId, classes: &str) {
apply(
c,
vec![
Op::Create { id, tag: Tag::View },
class_op(id, classes),
Op::Insert {
parent: ROOT_ID,
child: id,
before: None,
},
],
);
}
#[test]
fn classes_style_views_and_inline_styles_win() {
let mut c = core();
view_with_classes(&mut c, 2, "w-20 h-10 p-2 bg-blue-500 rounded-lg");
let batch = c.commit().unwrap();
assert_eq!(frame_of(&batch, 2).unwrap().size.width.0, 80.0);
let v = view_style_of(&batch, 2).unwrap();
assert_eq!(v.background_color, Color(0x2b7f_ffff));
assert_eq!(v.border_radius, px(8.0));
c.apply(style_op(
2,
&[("backgroundColor", StyleValue::Str("#ff0000"))],
))
.unwrap();
let batch = c.commit().unwrap();
assert_eq!(
view_style_of(&batch, 2).unwrap().background_color,
Color(0xff00_00ff)
);
c.apply(class_op(2, "w-24 h-10 bg-green-500")).unwrap();
let batch = c.commit().unwrap();
assert_eq!(frame_of(&batch, 2).unwrap().size.width.0, 96.0);
let v = view_style_of(&batch, 2).unwrap();
assert_eq!(v.background_color, Color(0xff00_00ff));
assert_eq!(v.border_radius, px(0.0));
c.apply(class_op(2, "w-24 h-10 bg-green-500")).unwrap();
assert!(c.commit().is_none());
}
#[test]
fn important_classes_win_over_inline_styles() {
let mut c = core();
view_with_classes(
&mut c,
2,
"!bg-blue-500 translate-x-2 !scale-150 opacity-50",
);
c.apply(style_op(
2,
&[
("backgroundColor", StyleValue::Str("#ff0000")),
("opacity", StyleValue::Number(0.8)),
],
))
.unwrap();
let v = view_style_of(&c.commit().unwrap(), 2).unwrap();
assert_eq!(v.background_color, Color(0x2b7f_ffff));
assert_eq!(v.opacity, 0.8);
let mut expected = crate::Transform::default();
let _ = expected.push(crate::TransformKind::TranslateX, 8.0);
let _ = expected.push(crate::TransformKind::Scale, 1.5);
assert_eq!(v.transform, expected);
}
#[test]
fn inline_styles_resolve_longhands_whatever_their_order() {
use crate::effects::Keyframes;
let mut c = core();
view_with_classes(&mut c, 2, "border-t-4 outline-2 animate-spin");
c.apply(style_op(
2,
&[
("borderWidth", StyleValue::Number(1.0)),
("outlineStyle", StyleValue::Str("none")),
("animationDuration", StyleValue::Number(500.0)),
],
))
.unwrap();
let v = view_style_of(&c.commit().unwrap(), 2).unwrap();
assert_eq!(v.borders.widths.to_array().map(|w| w.0), [1.0; 4]);
assert!(!v.outline.is_visible());
let a = v.animation.unwrap();
assert_eq!((a.keyframes, a.duration), (Keyframes::Spin, 500.0));
c.apply(style_op(
2,
&[
("borderTopWidth", StyleValue::Number(5.0)),
("borderWidth", StyleValue::Number(2.0)),
("animationIterationCount", StyleValue::Number(2.0)),
("animationName", StyleValue::Str("pulse")),
],
))
.unwrap();
let v = view_style_of(&c.commit().unwrap(), 2).unwrap();
assert_eq!(
v.borders.widths.to_array().map(|w| w.0),
[5.0, 2.0, 2.0, 2.0]
);
assert_eq!(v.outline.width, px(2.0));
c.apply(class_op(2, "border-t-8")).unwrap();
let v = view_style_of(&c.commit().unwrap(), 2).unwrap();
assert_eq!(
v.borders.widths.to_array().map(|w| w.0),
[5.0, 2.0, 2.0, 2.0]
);
assert!(!v.outline.is_visible());
let a = v.animation.unwrap();
assert_eq!((a.keyframes, a.iterations), (Keyframes::Pulse, Some(2)));
c.apply(style_op(2, &[("borderWidth", StyleValue::Number(2.0))]))
.unwrap();
let v = view_style_of(&c.commit().unwrap(), 2).unwrap();
assert_eq!(v.borders.widths.to_array().map(|w| w.0), [2.0; 4]);
assert_eq!(v.animation, None);
}
#[test]
fn the_apps_appearance_wins_over_the_systems() {
use crate::platform::Appearance;
let mut c = core();
view_with_classes(&mut c, 2, "bg-white dark:bg-black");
let background = |batch: &MountBatch| view_style_of(batch, 2).map(|s| s.background_color);
assert_eq!(background(&c.commit().unwrap()), Some(Color(0xffff_ffff)));
let appearance = |c: &mut Core<FakeEngine>, a| {
apply(
c,
vec![Op::SetProp {
id: ROOT_ID,
prop: Prop::Appearance(a),
}],
)
};
appearance(&mut c, Appearance::Dark);
let batch = c.commit().unwrap();
assert_eq!(background(&batch), Some(Color(0x0000_00ff)));
assert!(batch.items.iter().any(|i| matches!(
i,
MountItem::Appearance {
appearance: Appearance::Dark
}
)));
c.set_class_environment(crate::tailwind::ClassEnvironment::default());
assert!(c.commit().is_none_or(|b| background(&b).is_none()));
c.set_class_environment(crate::tailwind::ClassEnvironment {
dark: true,
..Default::default()
});
appearance(&mut c, Appearance::Light);
assert_eq!(background(&c.commit().unwrap()), Some(Color(0xffff_ffff)));
appearance(&mut c, Appearance::System);
assert_eq!(background(&c.commit().unwrap()), Some(Color(0x0000_00ff)));
}
#[test]
fn dark_mode_and_breakpoints_restyle_natively() {
let mut c = core();
view_with_classes(&mut c, 2, "bg-white dark:bg-black p-2 md:p-8");
let batch = c.commit().unwrap();
assert_eq!(
view_style_of(&batch, 2).unwrap().background_color,
Color(0xffff_ffff)
);
c.set_class_environment(crate::tailwind::ClassEnvironment {
dark: true,
..Default::default()
});
let batch = c.commit().unwrap();
assert_eq!(
view_style_of(&batch, 2).unwrap().background_color,
Color(0x0000_00ff)
);
apply(
&mut c,
vec![
Op::Create {
id: 3,
tag: Tag::View,
},
style_op(3, &[("height", StyleValue::Number(5.0))]),
Op::Insert {
parent: 2,
child: 3,
before: None,
},
],
);
let batch = c.commit().unwrap();
assert_eq!(frame_of(&batch, 3).unwrap().origin.x.0, 8.0);
c.set_viewport(size_pt(1024.0, 768.0));
let batch = c.commit().unwrap();
assert_eq!(frame_of(&batch, 3).unwrap().origin.x.0, 32.0);
}
#[test]
fn press_and_group_states() {
let mut c = core();
view_with_classes(&mut c, 2, "group bg-white active:bg-zinc-200");
apply(
&mut c,
vec![
Op::Create {
id: 3,
tag: Tag::View,
},
class_op(3, "h-4 opacity-100 group-active:opacity-50"),
Op::Insert {
parent: 2,
child: 3,
before: None,
},
],
);
let batch = c.commit().unwrap();
assert!(batch.items.iter().any(|i| matches!(i, MountItem::TrackStates { id: 2, states } if states & crate::tailwind::state::PRESSED != 0)));
c.set_states(2, crate::tailwind::state::PRESSED);
let batch = c.commit().unwrap();
assert_eq!(
view_style_of(&batch, 2).unwrap().background_color,
Color(0xe4e4_e7ff)
);
assert_eq!(view_style_of(&batch, 3).unwrap().opacity, 0.5);
c.set_states(2, 0);
let batch = c.commit().unwrap();
assert_eq!(view_style_of(&batch, 3).unwrap().opacity, 1.0);
}
#[test]
fn a_node_joining_a_group_in_a_state_reads_it() {
let mut c = core();
view_with_classes(&mut c, 2, "group");
apply(
&mut c,
vec![Op::SetProp {
id: 2,
prop: Prop::Native(NativeProp::Disabled(true)),
}],
);
c.commit().unwrap();
apply(
&mut c,
vec![
Op::Create {
id: 3,
tag: Tag::View,
},
class_op(3, "h-4 opacity-100 group-disabled:opacity-40"),
Op::Insert {
parent: 2,
child: 3,
before: None,
},
],
);
let batch = c.commit().unwrap();
assert_eq!(view_style_of(&batch, 3).unwrap().opacity, 0.4);
apply(
&mut c,
vec![Op::Insert {
parent: ROOT_ID,
child: 3,
before: None,
}],
);
let batch = c.commit().unwrap();
assert_eq!(view_style_of(&batch, 3).unwrap().opacity, 1.0);
}
#[test]
fn untracked_states_are_forgotten() {
use crate::tailwind::state::{HOVERED, PRESSED};
let mut c = core();
view_with_classes(&mut c, 2, "bg-white hover:bg-zinc-200");
c.commit().unwrap();
c.set_states(2, HOVERED | PRESSED);
c.commit().unwrap();
apply(&mut c, vec![class_op(2, "bg-zinc-500")]);
let batch = c.commit().unwrap();
assert!(
batch
.items
.iter()
.any(|i| matches!(i, MountItem::TrackStates { id: 2, states: 0 }))
);
apply(&mut c, vec![class_op(2, "bg-white hover:bg-zinc-200")]);
let batch = c.commit().unwrap();
assert_eq!(
view_style_of(&batch, 2).unwrap().background_color,
Color(0xffff_ffff)
);
}
#[test]
fn text_inherits_from_views_and_classes_set_props() {
let mut c = core();
view_with_classes(&mut c, 2, "text-white text-lg text-center");
apply(
&mut c,
vec![
Op::Create {
id: 3,
tag: Tag::Text,
},
class_op(3, "line-clamp-2 font-bold"),
Op::CreateText {
id: 4,
text: "hello".into(),
},
Op::Insert {
parent: 3,
child: 4,
before: None,
},
Op::Insert {
parent: 2,
child: 3,
before: None,
},
],
);
let batch = c.commit().unwrap();
let text = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Text { id: 3, text, .. } => Some(text.clone()),
_ => None,
})
.unwrap();
assert_eq!(text.spans[0].style.color, Color(0xffff_ffff));
assert_eq!(text.spans[0].style.font_size, Points(18.0));
assert_eq!(text.spans[0].style.font_weight, 700);
assert_eq!(text.align, TextAlign::Center);
assert_eq!(text.max_lines, 2);
c.apply(class_op(2, "text-black text-lg")).unwrap();
let batch = c.commit().unwrap();
assert!(batch.items.iter().any(|i| matches!(i, MountItem::Text { id: 3, text, .. } if text.spans[0].style.color == Color(0x0000_00ff))));
}
#[test]
fn sibling_positions() {
let mut c = core();
view_with_classes(&mut c, 2, "flex-row");
for id in 3..6 {
apply(
&mut c,
vec![
Op::Create { id, tag: Tag::View },
class_op(id, "w-2 h-2 bg-red-500 first:bg-green-500 last:bg-blue-500"),
Op::Insert {
parent: 2,
child: id,
before: None,
},
],
);
}
let batch = c.commit().unwrap();
assert_eq!(
view_style_of(&batch, 3).unwrap().background_color,
Color(0x00c9_50ff)
);
assert_eq!(
view_style_of(&batch, 4).unwrap().background_color,
Color(0xfb2c_36ff)
);
assert_eq!(
view_style_of(&batch, 5).unwrap().background_color,
Color(0x2b7f_ffff)
);
c.apply(Op::Remove {
parent: 2,
child: 5,
})
.unwrap();
let batch = c.commit().unwrap();
assert_eq!(
view_style_of(&batch, 4).unwrap().background_color,
Color(0x2b7f_ffff)
);
}
#[test]
fn text_inherits_a_rows_positional_classes() {
let mut c = core();
view_with_classes(&mut c, 2, "");
let add_row = |c: &mut Core<FakeEngine>, row: NodeId| {
apply(
c,
vec![
Op::Create {
id: row,
tag: Tag::View,
},
class_op(row, "text-black last:text-white"),
Op::Create {
id: row + 1,
tag: Tag::Text,
},
Op::CreateText {
id: row + 2,
text: "label".into(),
},
Op::Insert {
parent: row + 1,
child: row + 2,
before: None,
},
Op::Insert {
parent: row,
child: row + 1,
before: None,
},
Op::Insert {
parent: 2,
child: row,
before: None,
},
],
);
};
let color = |batch: &MountBatch, id: NodeId| {
batch.items.iter().find_map(|i| match i {
MountItem::Text { id: t, text, .. } if *t == id => Some(text.spans[0].style.color),
_ => None,
})
};
add_row(&mut c, 10);
add_row(&mut c, 20);
let batch = c.commit().unwrap();
assert_eq!(color(&batch, 11), Some(Color(0x0000_00ff)));
assert_eq!(color(&batch, 21), Some(Color(0xffff_ffff)));
add_row(&mut c, 30);
let batch = c.commit().unwrap();
assert_eq!(color(&batch, 21), Some(Color(0x0000_00ff)));
assert_eq!(color(&batch, 31), Some(Color(0xffff_ffff)));
}
#[test]
fn fractional_translations_follow_the_size() {
let mut c = core();
view_with_classes(&mut c, 2, "w-20 h-10 -translate-x-1/2 translate-y-full");
let batch = c.commit().unwrap();
let v = view_style_of(&batch, 2).unwrap();
let ops = v.transform.ops();
assert_eq!(
(ops[0].kind, ops[0].value),
(crate::TransformKind::TranslateX, -40.0)
);
assert_eq!(
(ops[1].kind, ops[1].value),
(crate::TransformKind::TranslateY, 40.0)
);
c.apply(class_op(2, "w-40 h-10 -translate-x-1/2 translate-y-full"))
.unwrap();
let batch = c.commit().unwrap();
assert_eq!(
view_style_of(&batch, 2).unwrap().transform.ops()[0].value,
-80.0
);
}
#[test]
fn disabled_views_take_no_presses() {
let mut c = core();
view_with_classes(&mut c, 2, "disabled:opacity-40");
let press = |enabled| Op::Listen {
id: 2,
event: EventKind::Press,
enabled,
};
let disabled = |d| Op::SetProp {
id: 2,
prop: Prop::Native(NativeProp::Disabled(d)),
};
let events_of = |batch: &MountBatch| {
batch.items.iter().rev().find_map(|i| match i {
MountItem::Events { id: 2, events } => Some(*events),
_ => None,
})
};
apply(&mut c, vec![press(true), disabled(true)]);
let batch = c.commit().unwrap();
assert!(!events_of(&batch).unwrap().contains(EventKind::Press));
assert_eq!(view_style_of(&batch, 2).unwrap().opacity, 0.4);
apply(&mut c, vec![disabled(false)]);
let batch = c.commit().unwrap();
assert!(events_of(&batch).unwrap().contains(EventKind::Press));
assert_eq!(view_style_of(&batch, 2).unwrap().opacity, 1.0);
}
#[test]
fn disabled_views_hear_no_context_menus_or_file_drops() {
let mut c = core();
view_with_classes(&mut c, 2, "disabled:opacity-40");
let disabled = |d| Op::SetProp {
id: 2,
prop: Prop::Native(NativeProp::Disabled(d)),
};
let events_of = |batch: &MountBatch| {
batch.items.iter().rev().find_map(|i| match i {
MountItem::Events { id: 2, events } => Some(*events),
_ => None,
})
};
apply(
&mut c,
[EventKind::ContextMenu, EventKind::FileDrop]
.into_iter()
.map(|event| Op::Listen {
id: 2,
event,
enabled: true,
})
.collect(),
);
let batch = c.commit().unwrap();
let events = events_of(&batch).unwrap();
assert!(events.contains(EventKind::ContextMenu) && events.contains(EventKind::FileDrop));
apply(&mut c, vec![disabled(true)]);
let batch = c.commit().unwrap();
assert!(events_of(&batch).unwrap().is_empty());
apply(&mut c, vec![disabled(false)]);
let batch = c.commit().unwrap();
let events = events_of(&batch).unwrap();
assert!(events.contains(EventKind::ContextMenu) && events.contains(EventKind::FileDrop));
}
fn patch(decls: &[(&str, crate::StyleValue<'_>)]) -> Option<Arc<crate::StylePatch>> {
let mut style = crate::Style::default();
let mut patch = crate::StylePatch::default();
for (key, value) in decls {
patch.set(&mut style, key, *value).unwrap();
}
Some(Arc::new(patch))
}
#[test]
fn disabled_views_are_disabled_for_screen_readers() {
let mut c = core();
view_with_classes(&mut c, 2, "");
let disabled = |d| Op::SetProp {
id: 2,
prop: Prop::Native(NativeProp::Disabled(d)),
};
let state_of = |batch: &MountBatch| {
batch.items.iter().rev().find_map(|i| match i {
MountItem::ViewProps { id: 2, props } => Some(props.accessibility.state.disabled),
_ => None,
})
};
apply(&mut c, vec![disabled(true)]);
assert_eq!(state_of(&c.commit().unwrap()), Some(true));
apply(&mut c, vec![disabled(false)]);
assert_eq!(state_of(&c.commit().unwrap()), Some(false));
apply(
&mut c,
vec![Op::SetProp {
id: 2,
prop: Prop::Accessibility(AccessibilityProp::State(crate::AccessibilityState {
disabled: true,
..Default::default()
})),
}],
);
assert_eq!(state_of(&c.commit().unwrap()), Some(true));
}
#[test]
fn active_style_applies_while_pressed_and_not_disabled() {
use crate::{StyleValue as V, tailwind::state};
let mut c = core();
view_with_classes(&mut c, 2, "bg-white");
apply(
&mut c,
vec![
Op::SetStyle {
id: 2,
style: Box::default(),
patch: {
let mut s = crate::Style::default();
let mut p = crate::StylePatch::default();
p.set(&mut s, "opacity", V::Number(0.9)).unwrap();
p
},
},
Op::SetProp {
id: 2,
prop: Prop::ActiveStyle(patch(&[
("backgroundColor", V::Str("#dddddd")),
("opacity", V::Number(0.5)),
])),
},
Op::SetProp {
id: 2,
prop: Prop::DisabledStyle(patch(&[("opacity", V::Number(0.3))])),
},
],
);
let batch = c.commit().unwrap();
assert!(batch.items.iter().any(|i| matches!(
i,
MountItem::TrackStates { id: 2, states } if *states == state::PRESSED
)));
assert_eq!(view_style_of(&batch, 2).unwrap().opacity, 0.9);
c.set_states(2, state::PRESSED);
let batch = c.commit().unwrap();
let style = view_style_of(&batch, 2).unwrap();
assert_eq!(style.background_color, Color(0xdddd_ddff));
assert_eq!(style.opacity, 0.5);
c.set_states(2, 0);
let batch = c.commit().unwrap();
let style = view_style_of(&batch, 2).unwrap();
assert_eq!(style.background_color, Color(0xffff_ffff));
assert_eq!(style.opacity, 0.9);
apply(
&mut c,
vec![Op::SetProp {
id: 2,
prop: Prop::Native(NativeProp::Disabled(true)),
}],
);
c.set_states(2, state::PRESSED);
let batch = c.commit().unwrap();
let style = view_style_of(&batch, 2).unwrap();
assert_eq!(style.background_color, Color(0xffff_ffff));
assert_eq!(style.opacity, 0.3);
apply(
&mut c,
vec![
Op::SetProp {
id: 2,
prop: Prop::Native(NativeProp::Disabled(false)),
},
Op::SetProp {
id: 2,
prop: Prop::ActiveStyle(None),
},
],
);
let batch = c.commit().unwrap();
assert_eq!(
view_style_of(&batch, 2).unwrap().background_color,
Color(0xffff_ffff)
);
}
#[test]
fn selected_style_follows_the_accessibility_state() {
use crate::{AccessibilityProp, AccessibilityState, StyleValue as V};
let mut c = core();
view_with_classes(&mut c, 2, "bg-white selected:bg-black");
let selected = |on| Op::SetProp {
id: 2,
prop: Prop::Accessibility(AccessibilityProp::State(AccessibilityState {
selected: on,
..AccessibilityState::default()
})),
};
apply(
&mut c,
vec![
Op::SetProp {
id: 2,
prop: Prop::SelectedStyle(patch(&[("opacity", V::Number(0.7))])),
},
selected(true),
],
);
let batch = c.commit().unwrap();
let style = view_style_of(&batch, 2).unwrap();
assert_eq!(style.background_color, Color(0x0000_00ff));
assert_eq!(style.opacity, 0.7);
assert!(
!batch
.items
.iter()
.any(|i| matches!(i, MountItem::TrackStates { .. }))
);
apply(&mut c, vec![selected(false)]);
let batch = c.commit().unwrap();
let style = view_style_of(&batch, 2).unwrap();
assert_eq!(style.background_color, Color(0xffff_ffff));
assert_eq!(style.opacity, 1.0);
}
#[test]
fn checked_style_follows_the_accessibility_state() {
use crate::{AccessibilityProp, AccessibilityState, Checked, StyleValue as V};
let mut c = core();
view_with_classes(
&mut c,
2,
"group bg-zinc-200 checked:bg-emerald-500 selected:opacity-50",
);
apply(
&mut c,
vec![
Op::Create {
id: 3,
tag: Tag::View,
},
class_op(3, "h-4 w-4 group-checked:translate-x-4"),
Op::Insert {
parent: 2,
child: 3,
before: None,
},
],
);
let state = |checked, selected| Op::SetProp {
id: 2,
prop: Prop::Accessibility(AccessibilityProp::State(AccessibilityState {
checked,
selected,
..AccessibilityState::default()
})),
};
apply(
&mut c,
vec![
Op::SetProp {
id: 2,
prop: Prop::CheckedStyle(patch(&[("opacity", V::Number(0.7))])),
},
state(Some(Checked::True), false),
],
);
let batch = c.commit().unwrap();
let track = view_style_of(&batch, 2).unwrap();
assert_eq!(track.background_color, Color(0x00bc_7dff));
assert_eq!(track.opacity, 0.7);
assert_eq!(
view_style_of(&batch, 3).unwrap().transform.ops()[0].value,
16.0
);
assert!(!batch.items.iter().any(|i| matches!(
i,
MountItem::TrackStates { id: 2, states } if states & crate::tailwind::state::CHECKED != 0
)));
apply(&mut c, vec![state(Some(Checked::Mixed), true)]);
let batch = c.commit().unwrap();
let track = view_style_of(&batch, 2).unwrap();
assert_eq!(track.background_color, Color(0xe4e4_e7ff));
assert_eq!(track.opacity, 0.5);
assert!(view_style_of(&batch, 3).unwrap().transform.ops().is_empty());
}
#[test]
fn indeterminate_style_follows_the_accessibility_state() {
use crate::{AccessibilityProp, AccessibilityState, Checked, StyleValue as V};
let mut c = core();
view_with_classes(
&mut c,
2,
"group bg-zinc-200 checked:bg-emerald-500 indeterminate:bg-black",
);
apply(
&mut c,
vec![
Op::Create {
id: 3,
tag: Tag::View,
},
class_op(3, "h-4 w-4 group-indeterminate:opacity-50"),
Op::Insert {
parent: 2,
child: 3,
before: None,
},
],
);
let state = |checked| Op::SetProp {
id: 2,
prop: Prop::Accessibility(AccessibilityProp::State(AccessibilityState {
checked,
..AccessibilityState::default()
})),
};
apply(
&mut c,
vec![
Op::SetProp {
id: 2,
prop: Prop::IndeterminateStyle(patch(&[("opacity", V::Number(0.7))])),
},
state(Some(Checked::Mixed)),
],
);
let batch = c.commit().unwrap();
let mixed = view_style_of(&batch, 2).unwrap();
assert_eq!(mixed.background_color, Color(0x0000_00ff));
assert_eq!(mixed.opacity, 0.7);
assert_eq!(view_style_of(&batch, 3).unwrap().opacity, 0.5);
assert!(!batch.items.iter().any(|i| matches!(
i,
MountItem::TrackStates { id: 2, states }
if states & crate::tailwind::state::INDETERMINATE != 0
)));
apply(&mut c, vec![state(Some(Checked::True))]);
let batch = c.commit().unwrap();
let checked = view_style_of(&batch, 2).unwrap();
assert_eq!(checked.background_color, Color(0x00bc_7dff));
assert_eq!(checked.opacity, 1.0);
assert_eq!(view_style_of(&batch, 3).unwrap().opacity, 1.0);
}
#[test]
fn projected_states_style_a_part_without_its_accessibility() {
use crate::tailwind::state;
let mut c = core();
view_with_classes(
&mut c,
2,
"h-4 w-4 checked:translate-x-4 disabled:opacity-50",
);
let projected = |states| Op::SetProp {
id: 2,
prop: Prop::ProjectedStates(states),
};
apply(&mut c, vec![projected(state::CHECKED | state::DISABLED)]);
let batch = c.commit().unwrap();
let thumb = view_style_of(&batch, 2).unwrap();
assert_eq!(thumb.transform.ops()[0].value, 16.0);
assert_eq!(thumb.opacity, 0.5);
assert!(
!batch.items.iter().any(|i| matches!(
i,
MountItem::ViewProps { id: 2, .. } | MountItem::Events { id: 2, .. }
)),
"{:?}",
batch.items
);
apply(&mut c, vec![projected(0)]);
let batch = c.commit().unwrap();
let thumb = view_style_of(&batch, 2).unwrap();
assert!(thumb.transform.ops().is_empty());
assert_eq!(thumb.opacity, 1.0);
}
#[test]
fn input_accessories_are_found_by_native_id() {
let mut c = core();
let accessory = |batch: &MountBatch| {
batch.items.iter().rev().find_map(|i| match i {
MountItem::Input { id: 2, config } => Some(config.accessory),
_ => None,
})
};
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::Input,
},
Op::SetProp {
id: 2,
prop: Prop::InputAccessoryViewId(Some("bar".into())),
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
let batch = c.commit().unwrap();
assert_eq!(accessory(&batch), Some(None));
apply(
&mut c,
vec![
Op::Create {
id: 3,
tag: Tag::InputAccessory,
},
Op::SetProp {
id: 3,
prop: Prop::NativeId(Some("bar".into())),
},
Op::Create {
id: 4,
tag: Tag::View,
},
Op::SetStyle {
id: 4,
style: style(&[("height", StyleValue::Number(44.0))]),
patch: Default::default(),
},
Op::Insert {
parent: 3,
child: 4,
before: None,
},
Op::Insert {
parent: ROOT_ID,
child: 3,
before: None,
},
],
);
let batch = c.commit().unwrap();
assert_eq!(accessory(&batch), Some(Some(3)));
let frame = frame_of(&batch, 3).unwrap();
assert_eq!((frame.size.width.0, frame.size.height.0), (320.0, 44.0));
apply(
&mut c,
vec![Op::Remove {
parent: ROOT_ID,
child: 3,
}],
);
let batch = c.commit().unwrap();
assert_eq!(accessory(&batch), Some(None));
}
#[test]
fn inputs_report_their_content_size() {
let mut c = core();
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::Input,
},
Op::SetProp {
id: 2,
prop: Prop::Multiline(true),
},
Op::SetStyle {
id: 2,
style: style(&[("width", StyleValue::Number(100.0))]),
patch: Default::default(),
},
Op::Listen {
id: 2,
event: EventKind::ContentSizeChange,
enabled: true,
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
let sizes = |c: &mut Core<FakeEngine>| {
js_events(c)
.into_iter()
.filter(|(id, kind, _)| *id == 2 && *kind == EventKind::ContentSizeChange)
.count()
};
c.commit().unwrap();
assert_eq!(sizes(&mut c), 1);
c.input_changed(2, &"x".repeat(80), 1);
c.commit();
assert_eq!(sizes(&mut c), 1);
c.input_changed(2, &"x".repeat(80), 2);
c.commit();
assert_eq!(sizes(&mut c), 0);
}
#[test]
fn fixed_size_inputs_report_their_content_size() {
let mut c = core();
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::Input,
},
Op::SetProp {
id: 2,
prop: Prop::Placeholder("A much longer placeholder".into()),
},
Op::SetStyle {
id: 2,
style: style(&[
("width", StyleValue::Number(200.0)),
("height", StyleValue::Number(40.0)),
]),
patch: Default::default(),
},
Op::Listen {
id: 2,
event: EventKind::ContentSizeChange,
enabled: true,
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
let sizes = |c: &mut Core<FakeEngine>| {
js_events(c)
.into_iter()
.filter(|(id, kind, _)| *id == 2 && *kind == EventKind::ContentSizeChange)
.map(|(_, _, p)| p)
.collect::<Vec<_>>()
};
let size = |width: f64, height: f64| {
vec![EventPayload::Map(vec![
("width", PayloadValue::Number(width)),
("height", PayloadValue::Number(height)),
])]
};
let frame = |batch: &MountBatch| {
batch
.items
.iter()
.any(|i| matches!(i, MountItem::Frame { id: 2, .. }))
};
let batch = c.commit().unwrap();
assert!(frame(&batch));
assert_eq!(sizes(&mut c), size(8.0, 16.0));
for (count, (text, width)) in [("ab", 16.0), ("abc", 24.0)].into_iter().enumerate() {
c.input_changed(2, text, count as u32 + 1);
assert!(c.commit().is_none_or(|b| !frame(&b)));
assert_eq!(sizes(&mut c), size(width, 16.0), "{text:?}");
}
c.input_changed(2, "A much longer placeholder", 3);
c.commit();
assert_eq!(sizes(&mut c), size(200.0, 16.0));
c.input_changed(2, "", 4);
c.commit();
assert_eq!(sizes(&mut c), size(8.0, 16.0));
c.input_changed(2, "", 5);
assert!(c.commit().is_none());
assert_eq!(sizes(&mut c), Vec::new());
}
#[test]
fn text_shrinks_to_fit_its_lines() {
let mut c = Core::new(crate::text::ApproxTextEngine, size_pt(320.0, 480.0));
let ops = vec![
Op::Create {
id: 2,
tag: Tag::Text,
},
Op::SetStyle {
id: 2,
style: style(&[
("width", StyleValue::Number(60.0)),
("fontSize", StyleValue::Number(20.0)),
]),
patch: Default::default(),
},
Op::SetProp {
id: 2,
prop: Prop::NumberOfLines(1),
},
Op::SetProp {
id: 2,
prop: Prop::AdjustsFontSizeToFit(true),
},
Op::CreateText {
id: 3,
text: "Hello world".into(),
},
Op::Insert {
parent: 2,
child: 3,
before: None,
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
];
for op in ops {
c.apply(op).unwrap();
}
let batch = c.commit().unwrap();
let size = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Text { id: 2, text, .. } => Some(text.spans[0].style.font_size.0),
_ => None,
})
.unwrap();
assert!(size < 10.0 && size > 9.0, "{size}");
}
#[test]
fn views_inside_text_are_attachments() {
let mut c = core();
let raw = |id, text: &str| Op::CreateText {
id,
text: text.into(),
};
let insert = |parent, child| Op::Insert {
parent,
child,
before: None,
};
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::Text,
},
raw(3, "ab"),
Op::Create {
id: 4,
tag: Tag::View,
},
style_op(
4,
&[
("width", StyleValue::Number(20.0)),
("height", StyleValue::Number(10.0)),
("padding", StyleValue::Number(2.0)),
],
),
Op::Create {
id: 5,
tag: Tag::View,
},
style_op(5, &[("height", StyleValue::Number(3.0))]),
insert(4, 5),
Op::Create {
id: 6,
tag: Tag::Text,
},
raw(7, "cd"),
Op::Create {
id: 8,
tag: Tag::Image,
},
style_op(
8,
&[
("width", StyleValue::Number(12.0)),
("height", StyleValue::Number(12.0)),
],
),
insert(6, 7),
insert(6, 8),
insert(2, 3),
insert(2, 4),
insert(2, 6),
style_op(2, &[("padding", StyleValue::Number(5.0))]),
insert(ROOT_ID, 2),
],
);
let batch = c.commit().unwrap();
let text = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Text { id: 2, text, .. } => Some(text.clone()),
_ => None,
})
.unwrap();
assert_eq!(text.text, "ab\u{FFFC}cd\u{FFFC}");
assert_eq!(
text.attachments,
vec![
crate::TextAttachment {
index: 2,
width: Points(20.0),
height: Points(10.0),
},
crate::TextAttachment {
index: 5,
width: Points(12.0),
height: Points(12.0),
},
]
);
let inserts: Vec<_> = batch
.items
.iter()
.filter_map(|i| match i {
MountItem::Insert {
parent: 2, child, ..
} => Some(*child),
_ => None,
})
.collect();
assert_eq!(inserts, vec![4, 8]);
assert_eq!(
frame_of(&batch, 4),
Some(rect(5.0 + 16.0, 5.0 + 2.0, 20.0, 10.0))
);
assert_eq!(frame_of(&batch, 8).map(|f| f.origin.x.0), Some(5.0 + 40.0));
assert_eq!(frame_of(&batch, 5), Some(rect(2.0, 2.0, 16.0, 3.0)));
apply(
&mut c,
vec![style_op(
4,
&[
("width", StyleValue::Number(30.0)),
("height", StyleValue::Number(10.0)),
],
)],
);
let batch = c.commit().unwrap();
let text = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Text { id: 2, text, .. } => Some(text.clone()),
_ => None,
})
.unwrap();
assert_eq!(text.attachments[0].width, Points(30.0));
assert_eq!(frame_of(&batch, 4).map(|f| f.size.width.0), Some(30.0));
let align_of = |c: &mut Core<FakeEngine>, align: &str| {
apply(
c,
vec![style_op(4, &[("verticalAlign", StyleValue::Str(align))])],
);
let batch = c.commit()?;
if !batch
.items
.iter()
.any(|i| matches!(i, MountItem::Text { id: 2, .. }))
{
return None;
}
let layout = c.nodes[c.key(2).unwrap() as usize].layout.unwrap();
let cx = c.text_cx(layout);
Some(cx.alignments.get(cx.text.attachments[0].index))
};
assert_eq!(
align_of(&mut c, "text-top"),
Some(crate::VerticalAlign::TextTop)
);
assert_eq!(
align_of(&mut c, "middle"),
Some(crate::VerticalAlign::Middle)
);
assert_eq!(align_of(&mut c, "middle"), None);
assert_eq!(
align_of(&mut c, "baseline"),
Some(crate::VerticalAlign::Baseline)
);
apply(
&mut c,
vec![Op::Remove {
parent: 2,
child: 4,
}],
);
let batch = c.commit().unwrap();
assert!(
batch
.items
.iter()
.any(|i| matches!(i, MountItem::Remove { id: 4 }))
);
let text = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Text { id: 2, text, .. } => Some(text.clone()),
_ => None,
})
.unwrap();
assert_eq!(text.text, "abcd\u{FFFC}");
assert_eq!(frame_of(&batch, 8).map(|f| f.origin.x.0), Some(5.0 + 32.0));
}
#[test]
fn node_quota_and_dense_ids() {
let mut c = core();
c.set_max_nodes(3);
for id in 2..4 {
c.apply(Op::Create { id, tag: Tag::View }).unwrap();
}
assert!(matches!(
c.apply(Op::Create {
id: 4,
tag: Tag::View
}),
Err(ApplyError::TooManyNodes(3))
));
let mut c = core();
c.set_max_nodes(1000);
let err = c
.apply(Op::Create {
id: 4_000_000_000,
tag: Tag::View,
})
.unwrap_err();
assert!(matches!(err, ApplyError::IdOutOfRange(_)) && err.is_fatal());
}
#[test]
fn refused_urls_fail_and_send_nothing() {
let mut c = core();
c.set_access(Arc::new(crate::access::Access {
declared: crate::access::Capabilities::from_json(
&serde_json::json!({ "network": { "hosts": ["cdn.example.com"] } }),
)
.unwrap(),
granted: None,
}));
let image = |id, uri: &str| {
vec![
Op::Create {
id,
tag: Tag::Image,
},
Op::Listen {
id,
event: EventKind::Error,
enabled: true,
},
Op::SetProp {
id,
prop: Prop::Source(Some(ImageSource {
uri: uri.into(),
size: None,
scales: ImageScales::NONE,
})),
},
Op::Insert {
parent: ROOT_ID,
child: id,
before: None,
},
]
};
let mut ops = image(2, "https://cdn.example.com/a.png");
ops.extend(image(3, "https://tracker.io/pixel.gif"));
ops.push(Op::Command {
id: ROOT_ID,
command: Command::StatusBar(Default::default()),
});
apply(&mut c, ops);
let batch = c.commit().unwrap();
let sources: Vec<_> = batch
.items
.iter()
.filter_map(|i| match i {
MountItem::Image { id, source, .. } => {
Some((*id, source.as_ref().map(|s| s.uri.to_string())))
}
_ => None,
})
.collect();
assert_eq!(
sources,
[
(2, Some("https://cdn.example.com/a.png".to_owned())),
(3, None)
]
);
assert!(
!batch
.items
.iter()
.any(|i| matches!(i, MountItem::Command { .. }))
);
let events = js_events(&mut c);
assert!(matches!(
&events[..],
[(3, EventKind::Error, EventPayload::Error { message })] if message.contains("`tracker.io`")
));
}
mod components {
use serde_json::json;
use super::*;
use crate::{
Prop,
ui::{Children, Event, Props, Registry, Slot, prelude::*},
};
#[derive(Clone, PartialEq, Props)]
struct TallyProps {
#[prop(default = "Taps".into())]
label: crate::ui::Prop<SharedString>,
children: Children,
}
#[derive(Event)]
enum TallyEvent {
Tap { count: u32 },
}
#[component]
fn Tally(props: TallyProps, cx: Scope<TallyEvent>) -> impl IntoElement {
let taps = signal(0);
view()
.p_2()
.on_press(move |_: &()| {
taps.update(|t| *t += 1);
cx.emit(TallyEvent::Tap {
count: taps.get_untracked(),
});
})
.child(text(move || {
format!("{} {}", props.label.get(), taps.get())
}))
.child(view().gap_1().child(props.children))
}
fn texts(batch: &MountBatch) -> Vec<String> {
batch
.items
.iter()
.filter_map(|i| match i {
MountItem::Text { text, .. } => Some(text.text.clone()),
_ => None,
})
.collect()
}
#[derive(Default)]
struct Knob;
#[derive(Clone, PartialEq, Props)]
struct KnobProps {}
#[derive(Clone, Copy)]
struct KnobApi {
at: crate::ui::Signal<i32>,
}
#[crate::ui::methods]
impl KnobApi {
pub(crate) fn turn(&self, by: i32) -> i32 {
self.at.update(|at| *at += by);
self.at.get_untracked()
}
pub(crate) fn stuck(&self) -> Result<(), String> {
Err(format!("stuck at {}", self.at.get_untracked()))
}
}
impl Component for Knob {
type Props = KnobProps;
type Event = crate::ui::NoEvent;
const METHODS: crate::ui::Methods = crate::ui::Methods::of::<KnobApi>();
fn view(_: KnobProps, cx: Scope<crate::ui::NoEvent>) -> impl IntoElement {
let at = signal(0);
cx.methods(KnobApi { at });
text(move || format!("at {}", at.get()))
}
}
#[test]
fn methods_take_their_arguments_typed_and_answer_in_the_events() {
use crate::ui::{RustCall, RustOp, json_host::JsonHost};
let mut c = core();
let mut registry = Registry::new();
registry.register::<Knob>();
c.set_components(registry);
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetProp {
id: 2,
prop: Prop::Component(std::any::type_name::<Knob>().into()),
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
assert_eq!(texts(&c.commit().unwrap()), ["at 0"]);
let methods = <KnobApi as crate::ui::Api>::METHODS;
let call = |method: usize, args| {
let host = JsonHost::with_args(args);
(methods[method].call)(&host)
};
assert!(call(0, vec![json!("x")]).is_err());
for (n, (method, args, host)) in [
(0, vec![json!(3)], 2),
(1, vec![], 2),
(0, vec![json!(1)], 40),
]
.into_iter()
.enumerate()
{
let Ok(run) = call(method, args) else {
panic!("converts")
};
c.apply(Op::Rust(RustOp {
call: n as u32 + 1,
run: RustCall::Method(host, run),
}))
.unwrap();
}
assert_eq!(texts(&c.commit().unwrap()), ["at 3"]);
let replies: Vec<_> = js_events(&mut c)
.into_iter()
.map(|(_, _, payload)| match payload {
EventPayload::Native { data, .. } => data,
other => panic!("{other:?}"),
})
.collect();
assert_eq!(
replies,
[
json!({ "c": 1, "v": 3 }),
json!({ "c": 2, "e": "stuck at 3" }),
json!({ "c": 3, "e": "node 40 hosts no Rust component" }),
]
);
}
thread_local! {
static ECHOES: Cell<usize> = const { Cell::new(0) };
}
#[derive(Default)]
struct Echo;
#[derive(Clone, PartialEq, Props)]
struct EchoProps {
#[prop(default)]
label: crate::ui::Prop<SharedString>,
}
impl Component for Echo {
type Props = EchoProps;
type Event = crate::ui::NoEvent;
fn view(props: EchoProps, _: Scope<crate::ui::NoEvent>) -> impl IntoElement {
text(move || {
ECHOES.with(|e| e.set(e.get() + 1));
props.label.get()
})
}
}
#[test]
fn a_prop_set_to_an_equal_value_runs_nothing() {
let mut c = core();
let mut registry = Registry::new();
registry.register::<Echo>();
c.set_components(registry);
let label = |v: &str| Op::SetProp {
id: 2,
prop: rust_prop::<EchoProps>("label", Some(json!(v))),
};
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetProp {
id: 2,
prop: Prop::Component(std::any::type_name::<Echo>().into()),
},
label("a"),
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
assert_eq!(texts(&c.commit().unwrap()), ["a"]);
let runs = ECHOES.with(Cell::get);
apply(&mut c, vec![label("a")]);
assert!(c.commit().is_none());
assert_eq!(ECHOES.with(Cell::get), runs);
apply(&mut c, vec![label("b")]);
assert_eq!(texts(&c.commit().unwrap()), ["b"]);
assert_eq!(ECHOES.with(Cell::get), runs + 1);
}
fn core_with_tally() -> Core<FakeEngine> {
let mut c = core();
let mut registry = Registry::new();
registry.register::<Tally>();
c.set_components(registry);
c
}
#[test]
fn a_rust_root_mounts_renders_and_handles_events() {
let mut c = core();
c.mount_root(&crate::ui::Root::new::<Tally>(|| TallyProps {
label: "Root".into(),
children: Children::default(),
}));
let batch = c.commit().unwrap();
assert_eq!(texts(&batch), ["Root 0"]);
let host = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Insert {
parent: ROOT_ID,
child,
..
} => Some(*child),
_ => None,
})
.expect("the root's host is in the root view");
assert_eq!(host % 2, 1, "the core's own id");
assert!(c.component_event(host, EventKind::Press, &EventPayload::None));
assert_eq!(texts(&c.commit().unwrap()), ["Root 1"]);
}
#[test]
fn components_render_handle_events_and_place_children() {
let mut c = core_with_tally();
let prop = |name: &'static str, value| Op::SetProp {
id: 2,
prop: rust_prop::<TallyProps>(name, Some(value)),
};
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetProp {
id: 2,
prop: Prop::Component(std::any::type_name::<Tally>().into()),
},
prop("label", json!("Hits")),
Op::Create {
id: 4,
tag: Tag::View,
},
Op::Insert {
parent: 2,
child: 4,
before: None,
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
let batch = c.commit().unwrap();
assert_eq!(texts(&batch), ["Hits 0"]);
let holder = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Insert {
parent, child: 4, ..
} => Some(*parent),
_ => None,
})
.expect("JS child placed");
assert!(holder % 2 == 1 && holder != 2);
assert!(
batch
.items
.iter()
.any(|i| matches!(i, MountItem::Create { id: 2, .. }))
);
assert!(c.component_event(2, EventKind::Press, &EventPayload::None));
let events = js_events(&mut c);
assert_eq!(
events,
[(
2,
EventKind::NativeEvent,
EventPayload::Native {
name: "tap".into(),
data: json!({ "count": 1 })
}
)]
);
let batch = c.commit().unwrap();
assert_eq!(texts(&batch), ["Hits 1"]);
apply(
&mut c,
vec![Op::SetProp {
id: 2,
prop: rust_prop::<TallyProps>("label", None),
}],
);
let batch = c.commit().unwrap();
assert_eq!(texts(&batch), ["Taps 1"]);
assert!(
!batch
.items
.iter()
.any(|i| matches!(i, MountItem::Create { .. }))
);
let specs = <TallyProps as Props>::spec(&mut crate::ui::TsTypes::default());
let decode = specs
.iter()
.find(|p| p.name == "label")
.unwrap()
.decode
.unwrap();
assert!(crate::ui::json_host::decode(decode, json!(3)).is_err());
apply(
&mut c,
vec![Op::SetProp {
id: 2,
prop: Prop::RustProp("label", Some(crate::ui::AnyValue::new(3.0f64))),
}],
);
assert!(c.commit().is_none());
assert!(!c.component_event(2, EventKind::Layout, &EventPayload::None));
apply(
&mut c,
vec![
Op::Remove {
parent: ROOT_ID,
child: 2,
},
Op::Release { id: 2 },
],
);
let batch = c.commit().unwrap();
let deleted: Vec<NodeId> = batch
.items
.iter()
.filter_map(|i| match i {
MountItem::Delete { id } => Some(*id),
_ => None,
})
.collect();
assert!(deleted.contains(&2) && deleted.contains(&holder));
assert!(!deleted.contains(&4));
assert!(c.key(4).is_ok());
}
#[derive(Clone, Copy, PartialEq, Props)]
struct CardProps {
header: crate::ui::Prop<Option<Slot>>,
children: Children,
}
#[component]
fn Card(props: CardProps) -> impl IntoElement {
let filled = move || props.header.with(Option::is_some);
view().child(props.header.place(|h| h.h(px(44.0)))).child(
view()
.mt_0()
.style_when(filled, |v| v.mt_2())
.child(props.children),
)
}
#[derive(Clone, Copy, PartialEq, Props)]
struct PanelProps {
footer: crate::ui::Prop<Option<Slot>>,
children: Children,
}
#[component]
fn Panel(props: PanelProps) -> impl IntoElement {
view()
.child(props.children)
.child(props.footer.place(|f| f))
}
#[test]
fn children_go_before_a_region_that_fills_later() {
let mut c = core();
let mut registry = Registry::new();
registry.register::<Panel>();
c.set_components(registry);
let child = |id| {
vec![
Op::Create { id, tag: Tag::View },
Op::Insert {
parent: 2,
child: id,
before: None,
},
]
};
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetProp {
id: 2,
prop: Prop::Component(std::any::type_name::<Panel>().into()),
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
c.commit().unwrap();
apply(&mut c, child(8));
let batch = c.commit().expect("a child before an empty region");
assert!(batch.items.iter().any(|i| matches!(
i,
MountItem::Insert {
parent: 2,
child: 8,
before: None
}
)));
apply(
&mut c,
vec![
Op::Create {
id: 4,
tag: Tag::View,
},
Op::SetProp {
id: 4,
prop: Prop::ComponentSlot("footer".into()),
},
Op::Insert {
parent: 2,
child: 4,
before: None,
},
Op::Create {
id: 6,
tag: Tag::View,
},
Op::Insert {
parent: 4,
child: 6,
before: None,
},
],
);
c.commit().expect("the footer filled");
apply(&mut c, child(10));
let batch = c.commit().unwrap();
assert!(
batch.items.iter().any(|i| matches!(
i,
MountItem::Insert {
parent: 2,
child: 10,
before: Some(4)
}
)),
"{:?}",
batch.items
);
}
#[component]
fn Ledger(props: PanelProps) -> impl IntoElement {
view()
.child(dynamic(move || {
vec![props.children.into_element(), props.footer.place(|f| f)]
}))
.child(view())
}
fn child_ids(c: &Core<FakeEngine>, parent: NodeId) -> Vec<NodeId> {
let key = c.key(parent).unwrap();
c.nodes[key as usize]
.children
.iter()
.map(|&k| c.nodes[k as usize].id)
.collect()
}
fn mount_host<C: Component>() -> Core<FakeEngine> {
let mut c = core();
let mut registry = Registry::new();
registry.register::<C>();
c.set_components(registry);
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetProp {
id: 2,
prop: Prop::Component(std::any::type_name::<C>().into()),
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
c.commit().unwrap();
c
}
fn js_child(id: NodeId, before: Option<NodeId>) -> Vec<Op> {
vec![
Op::Create { id, tag: Tag::View },
Op::Insert {
parent: 2,
child: id,
before,
},
]
}
fn fill_footer() -> Vec<Op> {
vec![
Op::Create {
id: 4,
tag: Tag::View,
},
Op::SetProp {
id: 4,
prop: Prop::ComponentSlot("footer".into()),
},
Op::Insert {
parent: 2,
child: 4,
before: None,
},
Op::Create {
id: 6,
tag: Tag::View,
},
Op::Insert {
parent: 4,
child: 6,
before: None,
},
]
}
#[test]
fn children_in_a_region_go_before_the_node_after_it() {
let mut c = mount_host::<Ledger>();
let ids = child_ids(&c, 2);
assert_eq!(ids.len(), 1, "{ids:?}");
let tail = ids[0];
apply(&mut c, js_child(8, None));
c.commit().expect("a child before an empty region");
assert_eq!(child_ids(&c, 2), [8, tail]);
apply(&mut c, fill_footer());
c.commit().expect("the footer filled");
assert_eq!(child_ids(&c, 2), [8, 4, tail]);
apply(&mut c, js_child(10, None));
c.commit().unwrap();
assert_eq!(child_ids(&c, 2), [8, 10, 4, tail]);
}
#[test]
fn children_stay_in_place_as_a_region_fills_and_empties() {
let mut c = mount_host::<Ledger>();
let tail = child_ids(&c, 2)[0];
apply(&mut c, js_child(8, None));
apply(&mut c, fill_footer());
c.commit().unwrap();
assert_eq!(child_ids(&c, 2), [8, 4, tail]);
apply(
&mut c,
vec![Op::Remove {
parent: 2,
child: 4,
}],
);
c.commit().expect("the footer emptied");
assert_eq!(child_ids(&c, 2), [8, tail]);
apply(&mut c, js_child(10, None));
c.commit().unwrap();
assert_eq!(child_ids(&c, 2), [8, 10, tail]);
apply(
&mut c,
vec![Op::Insert {
parent: 2,
child: 4,
before: None,
}],
);
c.commit().expect("the footer filled again");
assert_eq!(child_ids(&c, 2), [8, 10, 4, tail]);
}
#[test]
fn children_go_before_the_sibling_js_names() {
let mut c = mount_host::<Panel>();
apply(&mut c, js_child(8, None));
apply(&mut c, fill_footer());
c.commit().unwrap();
assert_eq!(child_ids(&c, 2), [8, 4]);
apply(&mut c, js_child(10, Some(8)));
c.commit().unwrap();
assert_eq!(child_ids(&c, 2), [10, 8, 4]);
apply(&mut c, js_child(12, None));
c.commit().unwrap();
assert_eq!(child_ids(&c, 2), [10, 8, 12, 4]);
}
#[component]
fn Toggler(props: PanelProps) -> impl IntoElement {
let shown = signal(false);
view()
.child(
props
.footer
.place(move |f| f.on_press(move |_: &()| shown.update(|s| *s = !*s))),
)
.child(when(shown, || view().h(px(10.0))))
}
#[test]
fn a_slots_handlers_outlive_the_views_regions() {
let mut c = mount_host::<Toggler>();
apply(&mut c, fill_footer());
c.commit().unwrap();
assert_eq!(child_ids(&c, 2), [4]);
let press = |c: &mut Core<FakeEngine>| {
c.component_event(4, EventKind::Press, &EventPayload::None);
let _ = c.commit();
};
press(&mut c);
assert_eq!(child_ids(&c, 2).len(), 2, "the note shows");
press(&mut c);
assert_eq!(child_ids(&c, 2), [4], "the note is gone");
press(&mut c);
assert_eq!(child_ids(&c, 2).len(), 2, "and shows again");
}
#[test]
fn slots_are_the_glues_nodes_placed_by_the_render() {
let mut c = core();
let mut registry = Registry::new();
registry.register::<Card>();
c.set_components(registry);
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetProp {
id: 2,
prop: Prop::Component(std::any::type_name::<Card>().into()),
},
Op::Create {
id: 4,
tag: Tag::View,
},
Op::SetProp {
id: 4,
prop: Prop::ComponentSlot("header".into()),
},
Op::Insert {
parent: 2,
child: 4,
before: None,
},
Op::Create {
id: 6,
tag: Tag::View,
},
Op::Insert {
parent: 4,
child: 6,
before: None,
},
Op::Create {
id: 8,
tag: Tag::View,
},
style_op(8, &[("height", StyleValue::Number(10.0))]),
Op::Insert {
parent: 2,
child: 8,
before: Some(4),
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
let batch = c.commit().unwrap();
assert!(batch.items.iter().any(|i| matches!(
i,
MountItem::Insert {
parent: 2,
child: 4,
..
}
)));
assert_eq!(frame_of(&batch, 4).map(|f| f.size.height.0), Some(44.0));
let holder = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Insert {
parent, child: 8, ..
} => Some(*parent),
_ => None,
})
.unwrap();
assert_eq!(frame_of(&batch, holder).map(|f| f.origin.y.0), Some(52.0));
apply(
&mut c,
vec![Op::Remove {
parent: 4,
child: 6,
}],
);
let batch = c.commit().unwrap();
assert!(
batch
.items
.iter()
.any(|i| matches!(i, MountItem::Remove { id: 4 }))
);
assert_eq!(frame_of(&batch, holder).map(|f| f.origin.y.0), Some(0.0));
}
#[derive(Clone, PartialEq, Props)]
struct OuterProps {}
#[derive(Event)]
enum OuterEvent {
InnerTap(u32),
}
#[component]
fn Outer(_props: OuterProps, cx: Scope<OuterEvent>) -> impl IntoElement {
let inner_taps = signal(0);
view()
.child(text(move || format!("outer {}", inner_taps.get())))
.child(tally().label("In").on_tap(move |_: &TallyEvent| {
inner_taps.update(|t| *t += 1);
cx.emit(OuterEvent::InnerTap(inner_taps.get_untracked()));
}))
}
#[test]
fn nested_components_render_and_report_to_their_parent() {
let mut c = core();
let mut registry = Registry::new();
registry.register::<Tally>().register::<Outer>();
c.set_components(registry);
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetProp {
id: 2,
prop: Prop::Component(std::any::type_name::<Outer>().into()),
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
let batch = c.commit().unwrap();
let mut shown = texts(&batch);
shown.sort();
assert_eq!(shown, ["In 0", "outer 0"]);
let inner = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Events { id, events } if events.contains(EventKind::Press) => Some(*id),
_ => None,
})
.unwrap();
assert!(c.component_event(inner, EventKind::Press, &EventPayload::None));
let events = js_events(&mut c);
assert_eq!(
events,
[(
2,
EventKind::NativeEvent,
EventPayload::Native {
name: "innerTap".into(),
data: json!(1)
}
)]
);
let mut shown = texts(&c.commit().unwrap());
shown.sort();
assert_eq!(shown, ["In 1", "outer 1"]);
}
#[test]
fn the_apps_class_goes_last_and_unknown_components_render_nothing() {
let mut c = core_with_tally();
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetProp {
id: 2,
prop: Prop::Component(std::any::type_name::<Tally>().into()),
},
Op::SetProp {
id: 2,
prop: Prop::Class("p-8".into()),
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
Op::Create {
id: 6,
tag: Tag::View,
},
Op::SetProp {
id: 6,
prop: Prop::Component("test.missing".into()),
},
Op::Insert {
parent: ROOT_ID,
child: 6,
before: None,
},
],
);
let batch = c.commit().unwrap();
let text_id = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Text { id, .. } => Some(*id),
_ => None,
})
.unwrap();
let frame = frame_of(&batch, text_id).unwrap();
assert_eq!((frame.origin.x.0, frame.origin.y.0), (32.0, 32.0));
assert!(
batch
.items
.iter()
.any(|i| matches!(i, MountItem::Create { id: 6, .. }))
);
}
#[derive(Clone, PartialEq, Props)]
struct SwatchProps {}
#[component]
fn Swatch(_props: SwatchProps) -> impl IntoElement {
view()
.size_10()
.bg(move || tokens().color("primary").unwrap())
.active(|s| s.bg(rgb(0xff0000)))
}
#[test]
fn theme_colors_and_press_styles_switch_natively() {
let mut c = core();
let mut registry = Registry::new();
registry.register::<Swatch>().tokens_from(test_tokens);
c.set_components(registry);
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetProp {
id: 2,
prop: Prop::Component(std::any::type_name::<Swatch>().into()),
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
let background = |batch: &MountBatch| {
batch.items.iter().find_map(|i| match i {
MountItem::ViewStyle { id: 2, style } => Some(style.background_color),
_ => None,
})
};
let primary = token("primary");
let batch = c.commit().unwrap();
assert_eq!(background(&batch), Some(Color::from(primary.light)));
assert_eq!(frame_of(&batch, 2).map(|f| f.size.width.0), Some(40.0));
assert!(batch.items.iter().any(|i| matches!(
i,
MountItem::TrackStates { id: 2, states } if states & crate::tailwind::state::PRESSED != 0
)));
c.set_class_environment(crate::tailwind::ClassEnvironment {
dark: true,
..Default::default()
});
let batch = c.commit().unwrap();
assert_eq!(background(&batch), Some(Color::from(primary.dark)));
c.set_states(2, crate::tailwind::state::PRESSED);
let batch = c.commit().unwrap();
assert_eq!(background(&batch), Some(Color(0xff0000ff)));
}
#[derive(Clone, PartialEq, Props)]
struct PillProps {
#[prop(default)]
selected: crate::ui::Prop<bool>,
#[prop(default)]
selected_style: crate::ui::Prop<crate::ui::Style>,
}
#[component]
fn Pill(props: PillProps) -> impl IntoElement {
view()
.size_10()
.accessibility_state(move || crate::AccessibilityState {
selected: props.selected.get(),
..crate::AccessibilityState::default()
})
.selected_style(props.selected_style)
}
#[component]
fn Pills() -> impl IntoElement {
let dim = signal(false);
view()
.on_press(move || dim.set(true))
.child(pill().selected(true).selected_style(move |s| {
s.bg(move || tokens().color("primary").unwrap())
.opacity(move || if dim.get() { 0.5_f32 } else { 1.0 })
}))
}
#[test]
fn a_style_prop_set_with_a_closure_follows_the_theme_and_its_signals() {
let mut c = core();
let mut registry = Registry::new();
registry.register::<Pills>().tokens_from(test_tokens);
c.set_components(registry);
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetProp {
id: 2,
prop: Prop::Component(std::any::type_name::<Pills>().into()),
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
let styled = |batch: &MountBatch| {
batch.items.iter().find_map(|i| match i {
MountItem::ViewStyle { id, style } if *id != 2 => {
Some((style.background_color, style.opacity))
}
_ => None,
})
};
let primary = token("primary");
let batch = c.commit().unwrap();
assert_eq!(styled(&batch), Some((Color::from(primary.light), 1.0)));
c.set_class_environment(crate::tailwind::ClassEnvironment {
dark: true,
..Default::default()
});
let batch = c.commit().unwrap();
assert_eq!(styled(&batch), Some((Color::from(primary.dark), 1.0)));
assert!(c.component_event(2, EventKind::Press, &EventPayload::None));
let batch = c.commit().unwrap();
assert_eq!(styled(&batch), Some((Color::from(primary.dark), 0.5)));
}
#[test]
fn the_host_theme_wins_over_the_apps_until_cleared() {
let mut c = core();
let mut registry = Registry::new();
registry.register::<Swatch>().tokens_from(test_tokens);
c.set_components(registry);
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetProp {
id: 2,
prop: Prop::Component(std::any::type_name::<Swatch>().into()),
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
let background = |batch: &MountBatch| {
batch.items.iter().find_map(|i| match i {
MountItem::ViewStyle { id: 2, style } => Some(style.background_color),
_ => None,
})
};
let themed = |hex: u32| {
tokens_with(&[("primary", rgb(hex).into())]).over(&test_tokens(None).unwrap())
};
c.commit().unwrap();
c.set_host_tokens(Some(themed(0x00ff00)));
assert_eq!(
c.commit().as_ref().and_then(background),
Some(Color::from(rgb(0x00ff00)))
);
apply(
&mut c,
vec![Op::SetProp {
id: ROOT_ID,
prop: Prop::UiTheme(r##"{ "primary": "#0000ff" }"##.into()),
}],
);
assert_eq!(c.commit().as_ref().and_then(background), None);
c.set_host_tokens(None);
assert_eq!(
c.commit().as_ref().and_then(background),
Some(Color::from(rgb(0x0000ff)))
);
c.set_tokens(themed(0x0000ff));
assert_eq!(c.commit().as_ref().and_then(background), None);
}
#[test]
fn classes_naming_theme_tokens_follow_the_theme() {
let mut c = core();
view_with_classes(&mut c, 2, "w-10 h-10 bg-primary");
view_with_classes(&mut c, 4, "w-10 h-10 bg-chart-up");
view_with_classes(&mut c, 6, "w-10 h-10 bg-red-500");
let batch = c.commit().unwrap();
assert_eq!(
view_style_of(&batch, 2).unwrap().background_color,
Color::from(token("primary").light)
);
assert!(view_style_of(&batch, 4).is_none());
let host = tokens_with(&[
("primary", AdaptiveColor::rgb(0x00ff00, 0x004400)),
("chart-up", rgb(0x0000ff).into()),
]);
c.set_host_tokens(Some(host.clone()));
let batch = c.commit().unwrap();
assert_eq!(
view_style_of(&batch, 2).unwrap().background_color,
Color::from(rgb(0x00ff00))
);
assert_eq!(
view_style_of(&batch, 4).unwrap().background_color,
Color::from(rgb(0x0000ff))
);
assert!(view_style_of(&batch, 6).is_none());
c.set_class_environment(crate::tailwind::ClassEnvironment {
dark: true,
..Default::default()
});
let batch = c.commit().unwrap();
assert_eq!(
view_style_of(&batch, 2).unwrap().background_color,
Color::from(rgb(0x004400))
);
let mut changed = host;
changed.set_color("primary", rgb(0xff00ff).into());
c.set_host_tokens(Some(changed));
let batch = c.commit().unwrap();
assert_eq!(
view_style_of(&batch, 2).unwrap().background_color,
Color::from(rgb(0xff00ff))
);
assert!(view_style_of(&batch, 4).is_none());
assert!(view_style_of(&batch, 6).is_none());
}
#[test]
fn the_apps_token_classes_on_a_component_follow_the_theme() {
let mut c = core();
let mut registry = Registry::new();
registry.register::<Swatch>().tokens_from(test_tokens);
c.set_components(registry);
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetProp {
id: 2,
prop: Prop::Component(std::any::type_name::<Swatch>().into()),
},
Op::SetProp {
id: 2,
prop: Prop::Class("bg-chart-up".into()),
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
c.commit().unwrap();
c.set_host_tokens(Some(tokens_with(&[("chart-up", rgb(0x0000ff).into())])));
let batch = c.commit().unwrap();
assert_eq!(
view_style_of(&batch, 2).unwrap().background_color,
Color::from(rgb(0x0000ff))
);
}
#[test]
fn a_host_theme_keeps_the_apps_own_tokens() {
let mut c = core();
let mut registry = Registry::new();
registry.tokens_from(test_tokens);
c.set_components(registry);
apply(
&mut c,
vec![Op::SetProp {
id: ROOT_ID,
prop: Prop::UiTheme(r##"{ "chart-up": "#0000ff" }"##.into()),
}],
);
view_with_classes(&mut c, 2, "w-10 h-10 bg-chart-up");
c.set_host_tokens(Some(crate::ui::tokens::sample()));
let batch = c.commit().unwrap();
assert_eq!(
view_style_of(&batch, 2).unwrap().background_color,
Color::from(rgb(0x0000ff))
);
}
#[derive(Clone, PartialEq, Props)]
struct RowsProps {
children: Children,
}
#[component]
fn Rows(props: RowsProps) -> impl IntoElement {
view().children_each(props.children, |child, at| {
view()
.p_1()
.style_when(move || !at.is_first(), |v| v.mt_2())
.style_when(move || at.is_last(), |v| v.pb_2())
.child(child)
})
}
#[test]
fn children_each_wraps_every_js_child_and_follows_changes() {
let mut c = core();
let mut registry = Registry::new();
registry.register::<Rows>();
c.set_components(registry);
let child = |id: NodeId| {
vec![
Op::Create { id, tag: Tag::View },
style_op(id, &[("height", StyleValue::Number(10.0))]),
]
};
let mut ops = vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetProp {
id: 2,
prop: Prop::Component(std::any::type_name::<Rows>().into()),
},
];
ops.extend(child(4));
ops.extend(child(6));
ops.extend([
Op::Insert {
parent: 2,
child: 4,
before: None,
},
Op::Insert {
parent: 2,
child: 6,
before: None,
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
]);
apply(&mut c, ops);
let batch = c.commit().unwrap();
let wrapper_of = |batch: &MountBatch, id: NodeId| {
batch.items.iter().find_map(|i| match i {
MountItem::Insert { parent, child, .. } if *child == id => Some(*parent),
_ => None,
})
};
let (w4, w6) = (
wrapper_of(&batch, 4).unwrap(),
wrapper_of(&batch, 6).unwrap(),
);
assert!(w4 % 2 == 1 && w6 % 2 == 1 && w4 != w6);
assert_eq!(
frame_of(&batch, w4).map(|f| (f.origin.y.0, f.size.height.0)),
Some((0.0, 18.0))
);
assert_eq!(
frame_of(&batch, w6).map(|f| (f.origin.y.0, f.size.height.0)),
Some((26.0, 22.0))
);
let mut ops = child(8);
ops.push(Op::Insert {
parent: 2,
child: 8,
before: Some(4),
});
apply(&mut c, ops);
let batch = c.commit().unwrap();
let w8 = wrapper_of(&batch, 8).unwrap();
assert!(![w4, w6].contains(&w8));
assert_eq!(frame_of(&batch, w8).map(|f| f.origin.y.0), Some(0.0));
assert_eq!(frame_of(&batch, w4).map(|f| f.origin.y.0), Some(26.0));
assert!(wrapper_of(&batch, 4).is_none() && wrapper_of(&batch, 6).is_none());
apply(
&mut c,
vec![Op::Remove {
parent: 2,
child: 4,
}],
);
let batch = c.commit().unwrap();
assert!(
batch
.items
.iter()
.any(|i| matches!(i, MountItem::Delete { id } if *id == w4))
);
assert!(c.key(4).is_ok());
assert_eq!(frame_of(&batch, w6).map(|f| f.origin.y.0), Some(26.0));
}
#[derive(Clone, Copy, PartialEq, Props)]
struct ClockProps {
#[prop(default)]
start: crate::ui::Prop<u32>,
}
#[derive(Event)]
enum ClockEvent {
Done,
}
#[component]
fn Clock(props: ClockProps, cx: Scope<ClockEvent>) -> impl IntoElement {
let ticks = signal(props.start.get_untracked());
effect(move || ticks.set(props.start.get()));
let timer = stored(None::<Spawned<()>>);
timer.replace(Some(set_interval(
std::time::Duration::from_millis(20),
move || {
ticks.update(|t| *t += 1);
if ticks.get_untracked() == 3 {
cx.emit(ClockEvent::Done);
if let Some(timer) = timer.replace(None) {
timer.cancel();
}
}
},
)));
view().child(text(move || {
let width = if window().width > Points(500.0) {
"wide"
} else {
"narrow"
};
format!("{} {width}", ticks.get())
}))
}
#[derive(Clone, PartialEq, Props)]
struct LoaderProps {}
#[component]
fn Loader(_props: LoaderProps) -> impl IntoElement {
let value = signal(None::<u64>);
spawn(async move {
let loaded = background(async { (1..=10u64).product::<u64>() })
.await
.unwrap();
tokio::time::sleep(std::time::Duration::from_millis(5)).await;
value.set(Some(loaded));
});
view().child(text(move || match value.get() {
Some(v) => format!("loaded {v}"),
None => "loading".to_owned(),
}))
}
#[derive(Clone, PartialEq, Props)]
struct ProfileProps {}
#[component]
fn Profile(_props: ProfileProps) -> impl IntoElement {
let id = signal(Some(1u32));
let profile = resource(
move || id.get(),
|id, info: crate::ui::ResourceInfo| async move {
let ms = if id == 1 { 80 } else { 5 };
timer(std::time::Duration::from_millis(ms)).await;
if id == 3 {
Err(format!("no user {id}"))
} else {
Ok(format!(
"user {id}{}",
if info.refetching { " again" } else { "" }
))
}
},
);
spawn(async move {
let settled = || async {
while profile.loading() {
timer(std::time::Duration::from_millis(2)).await;
}
};
timer(std::time::Duration::from_millis(10)).await;
id.set(Some(2));
settled().await;
profile.refetch();
settled().await;
id.set(Some(3));
settled().await;
profile.mutate("local".into());
id.set(None);
});
view().child(text(move || {
format!(
"{:?}|{}|{}",
profile.state(),
profile.get().unwrap_or_default(),
profile.error().unwrap_or_default()
)
}))
}
#[test]
fn resources_load_refresh_fail_and_count_the_latest_load_only() {
let runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(1)
.enable_time()
.build()
.unwrap();
let mut c = core();
let mut registry = Registry::new();
registry.register::<Profile>();
c.set_components(registry);
c.set_task_runtime(runtime.handle().clone());
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetProp {
id: 2,
prop: Prop::Component(std::any::type_name::<Profile>().into()),
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
let mut shown = texts(&c.commit().unwrap());
shown.extend(run_until(&mut c, |s| s.iter().any(|t| t == "Ready|local|")));
shown.dedup();
assert_eq!(
shown,
[
"Pending||",
"Ready|user 2|",
"Refreshing|user 2|",
"Ready|user 2 again|",
"Refreshing|user 2 again|",
"Errored|user 2 again|no user 3",
"Ready|local|",
]
);
assert_eq!(c.components.executor.task_count(), 0);
}
fn run_until(c: &mut Core<FakeEngine>, done: impl Fn(&[String]) -> bool) -> Vec<String> {
let mut shown = Vec::new();
let end = std::time::Instant::now() + std::time::Duration::from_secs(5);
while std::time::Instant::now() < end {
c.run_component_tasks();
if let Some(batch) = c.commit() {
shown.extend(texts(&batch));
if done(&shown) {
return shown;
}
}
std::thread::sleep(std::time::Duration::from_millis(2));
}
panic!("not done: {shown:?}");
}
#[test]
fn state_tasks_props_and_the_environment() {
let runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(1)
.enable_time()
.build()
.unwrap();
let mut c = core();
let mut registry = Registry::new();
registry.register::<Clock>().register::<Loader>();
c.set_components(registry);
c.set_task_runtime(runtime.handle().clone());
let mount = |c: &mut Core<FakeEngine>, id: NodeId, name: &str| {
apply(
c,
vec![
Op::Create { id, tag: Tag::View },
Op::SetProp {
id,
prop: Prop::Component(name.into()),
},
Op::Insert {
parent: ROOT_ID,
child: id,
before: None,
},
],
)
};
mount(&mut c, 2, std::any::type_name::<Clock>());
assert_eq!(texts(&c.commit().unwrap()), ["0 narrow"]);
let shown = run_until(&mut c, |s| s.iter().any(|t| t == "3 narrow"));
assert!(shown.starts_with(&["1 narrow".to_owned()]));
let events = js_events(&mut c);
assert!(
matches!(&events[..], [(2, EventKind::NativeEvent, EventPayload::Native { name, .. })] if &**name == "done")
);
std::thread::sleep(std::time::Duration::from_millis(60));
c.run_component_tasks();
assert!(c.commit().is_none(), "stopped");
apply(
&mut c,
vec![Op::SetProp {
id: 2,
prop: rust_prop::<ClockProps>("start", Some(json!(10))),
}],
);
assert_eq!(texts(&c.commit().unwrap()), ["10 narrow"]);
let env = crate::ui::Environment {
window: size_pt(390.0, 844.0),
..Default::default()
};
c.set_environment(crate::ui::Environment {
keyboard_height: px(300.0),
..env
});
assert!(c.commit().is_none());
c.set_viewport(size_pt(800.0, 600.0));
assert!(texts(&c.commit().unwrap()).contains(&"10 wide".to_owned()));
mount(&mut c, 4, std::any::type_name::<Loader>());
assert!(texts(&c.commit().unwrap()).contains(&"loading".to_owned()));
run_until(&mut c, |s| s.iter().any(|t| t == "loaded 3628800"));
mount(&mut c, 6, std::any::type_name::<Clock>());
c.commit();
assert_eq!(c.components.executor.task_count(), 1);
apply(
&mut c,
vec![
Op::Remove {
parent: ROOT_ID,
child: 6,
},
Op::Release { id: 6 },
],
);
c.commit();
assert_eq!(c.components.executor.task_count(), 0);
}
#[component]
fn Form(_props: FormProps) -> impl IntoElement {
let name = signal(String::new());
let focused = signal(false);
let field = element_ref();
view()
.child(
scroll().bounces(false).child(
input()
.element_ref(&field)
.value(move || name.get())
.placeholder(move || {
if name.with(String::is_empty) {
"Name"
} else {
""
}
})
.on_change(move |text: &str| name.set(text.to_owned()))
.on_focus(move |_: &()| focused.set(true)),
),
)
.child(canvas(move || {
let len = name.with(String::len) as f32;
Painter::new().fill_rect(0.0, 0.0, len, 4.0)
}))
.child(when(
move || name.with(String::len) > 2,
#[allow(deprecated)]
move || {
modal()
.close_on_swipe(false)
.child(text(move || format!("Hi {}", name.get())))
},
))
.child(text(move || if focused.get() { "focused" } else { "idle" }))
.child(view().on_press(move |_: &()| {
field.focus();
field.set_selection(3, 3);
}))
}
#[derive(Clone, PartialEq, Props)]
struct FormProps {}
#[test]
fn host_elements_take_typed_props_and_events() {
let mut c = core();
let mut registry = Registry::new();
registry.register::<Form>();
c.set_components(registry);
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetProp {
id: 2,
prop: Prop::Component(std::any::type_name::<Form>().into()),
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
let batch = c.commit().unwrap();
let created = |batch: &MountBatch, wanted: ViewKind| {
batch.items.iter().find_map(|i| match i {
MountItem::Create { id, kind } if *kind == wanted => Some(*id),
_ => None,
})
};
let input = created(&batch, ViewKind::Input).expect("an input");
let button = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Events { id, events } if events.contains(EventKind::Press) => Some(*id),
_ => None,
})
.expect("the button listens to presses");
assert!(created(&batch, ViewKind::Scroll).is_some());
assert!(created(&batch, ViewKind::Modal).is_none(), "no modal yet");
let placeholder = |batch: &MountBatch| {
batch.items.iter().find_map(|i| match i {
MountItem::Input { config, .. } => Some(config.placeholder.clone()),
_ => None,
})
};
assert_eq!(placeholder(&batch).as_deref(), Some("Name"));
assert!(
batch
.items
.iter()
.any(|i| matches!(i, MountItem::Canvas { .. }))
);
assert_eq!(texts(&batch), ["idle"]);
c.input_changed(input, "Ada", 1);
assert!(c.component_event(
input,
EventKind::ChangeText,
&EventPayload::Text {
text: "Ada".into(),
count: 1
}
));
assert!(c.component_event(input, EventKind::Focus, &EventPayload::None));
let batch = c.commit().unwrap();
assert_eq!(placeholder(&batch).as_deref(), Some(""));
assert!(created(&batch, ViewKind::Modal).is_some());
assert!(
batch
.items
.iter()
.any(|i| matches!(i, MountItem::Canvas { .. }))
);
assert!(
!batch
.items
.iter()
.any(|i| matches!(i, MountItem::InputValue { .. }))
);
assert_eq!(texts(&batch), ["Hi Ada", "focused"]);
assert!(c.component_event(button, EventKind::Press, &EventPayload::None));
let batch = c.commit().unwrap();
let commands: Vec<_> = batch
.items
.iter()
.filter_map(|i| match i {
MountItem::Command { id, command } => Some((*id, command.clone())),
_ => None,
})
.collect();
assert_eq!(
commands,
[
(input, Command::Focus),
(input, Command::SetSelection { start: 3, end: 3 })
]
);
}
#[component]
fn Ruler() -> impl IntoElement {
let target = element_ref();
let unused = element_ref();
let shown = signal(String::new());
view()
.p_4()
.child(
view()
.pl(px(10.0))
.child(view().element_ref(&target).w(px(30.0)).h(px(20.0))),
)
.child(text(move || shown.get()))
.child(view().w(px(5.0)).h(px(5.0)).on_press(move |_: &()| {
let (target, unused) = (target.measure(), unused.measure());
spawn(async move {
let (m, none) = (target.await, unused.await);
let m = m.expect("laid out");
shown.set(format!(
"{},{} {}x{} at {},{} {}",
m.frame.origin.x.0,
m.frame.origin.y.0,
m.frame.size.width.0,
m.frame.size.height.0,
m.page.x.0,
m.page.y.0,
none.is_none()
));
});
}))
}
#[test]
fn element_refs_measure_with_the_commits_layout() {
let mut c = core();
let mut registry = Registry::new();
registry.register::<Ruler>();
c.set_components(registry);
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetProp {
id: 2,
prop: Prop::Component(std::any::type_name::<Ruler>().into()),
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
let batch = c.commit().unwrap();
let button = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Events { id, events } if events.contains(EventKind::Press) => Some(*id),
_ => None,
})
.expect("the button listens to presses");
assert!(c.component_event(button, EventKind::Press, &EventPayload::None));
let shown = run_until(&mut c, |s| s.iter().any(|t| t.contains(" at ")));
assert_eq!(shown.last().unwrap(), "10,0 30x20 at 26,16 true");
}
#[derive(Clone, PartialEq, Debug)]
struct Accent(u32);
#[derive(Default, Clone)]
struct Clicks(u32);
#[derive(Clone, PartialEq, Props)]
struct WithChildren {
children: Children,
}
#[component]
fn Theming(props: WithChildren) -> impl IntoElement {
let accent = signal(Accent(0));
provide_context(accent);
view()
.on_press(move |_: &()| accent.update(|a| a.0 += 1))
.child(props.children)
}
#[component]
fn AccentSwatch(_props: FormProps) -> impl IntoElement {
let accent = use_context::<Signal<Accent>>();
let clicks = global::<Clicks>();
view()
.on_press(move |_: &()| clicks.update(|c| c.0 += 1))
.child(text(move || {
let accent = accent.map_or(0, |a| a.with(|a| a.0));
format!("accent {accent} clicks {}", clicks.with(|c| c.0))
}))
}
#[test]
fn context_flows_down_and_globals_reach_their_readers() {
let mut c = core();
let mut registry = Registry::new();
registry.register::<Theming>().register::<AccentSwatch>();
c.set_components(registry);
let host = |id, name: &str| {
vec![
Op::Create { id, tag: Tag::View },
Op::SetProp {
id,
prop: Prop::Component(name.into()),
},
]
};
let mut ops = host(2, std::any::type_name::<Theming>());
ops.push(Op::Create {
id: 4,
tag: Tag::View,
});
ops.extend(host(6, std::any::type_name::<AccentSwatch>()));
ops.extend(host(8, std::any::type_name::<AccentSwatch>()));
for (parent, child) in [(4, 6), (2, 4), (ROOT_ID, 2), (ROOT_ID, 8)] {
ops.push(Op::Insert {
parent,
child,
before: None,
});
}
apply(&mut c, ops);
let batch = c.commit().unwrap();
let mut shown = texts(&batch);
shown.sort();
assert_eq!(shown, ["accent 0 clicks 0", "accent 0 clicks 0"]);
assert!(c.component_event(2, EventKind::Press, &EventPayload::None));
assert_eq!(texts(&c.commit().unwrap()), ["accent 1 clicks 0"]);
assert!(c.component_event(8, EventKind::Press, &EventPayload::None));
let mut shown = texts(&c.commit().unwrap());
shown.sort();
assert_eq!(shown, ["accent 0 clicks 1", "accent 1 clicks 1"]);
}
#[component]
fn Sheet(_props: FormProps) -> impl IntoElement {
let offset = animated_value(0.0);
let state = signal("resting");
view()
.bind_translate_y(&offset)
.bind_opacity(&offset.interpolate(&[0.0, 300.0], &[1.0, 0.2]))
.pan_values(None, Some(&offset))
.on_pan_end(move |e: &PanEvent| {
assert_eq!(e.value_y, Some(120.0));
let end = offset.spring(0.0, Spring::default());
state.set("springing");
spawn(async move {
let finished = end.await;
state.set(if finished { "back" } else { "stopped" });
});
})
.child(text(move || state.get()))
}
#[test]
fn animated_values_bind_animate_and_end() {
let runtime = tokio::runtime::Builder::new_current_thread()
.build()
.unwrap();
let mut c = core();
let mut registry = Registry::new();
registry.register::<Sheet>();
c.set_components(registry);
c.set_task_runtime(runtime.handle().clone());
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetProp {
id: 2,
prop: Prop::Component(std::any::type_name::<Sheet>().into()),
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
let batch = c.commit().unwrap();
let animations: Vec<_> = batch
.items
.iter()
.filter_map(|i| match i {
MountItem::Animation(op) => Some(op.clone()),
_ => None,
})
.collect();
let value = match animations[0] {
crate::animation::AnimationOp::CreateValue { node, value: 0.0 } => node,
ref op => panic!("{op:?}"),
};
assert_eq!(value % 2, 1, "the core's ids are odd");
assert!(
matches!(animations[1], crate::animation::AnimationOp::CreateInterpolate { input, .. } if input == value)
);
assert_eq!(texts(&batch), ["resting"]);
let payload = EventPayload::Pan {
translation: point(px(0.0), px(120.0)),
velocity_x: 0.0,
velocity_y: 300.0,
value_x: None,
value_y: Some(px(120.0)),
};
assert!(c.component_event(2, EventKind::PanEnd, &payload));
let batch = c.commit().unwrap();
let animation = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Animation(crate::animation::AnimationOp::Start {
node,
animation,
..
}) if *node == value => Some(*animation),
_ => None,
})
.expect("a spring");
assert_eq!(texts(&batch), ["springing"]);
let mut finished = vec![
crate::animation::Finished::new(animation, true),
crate::animation::Finished::new(8, true),
];
c.animations_finished(&mut finished);
assert_eq!(finished.len(), 1);
let _guard = runtime.enter();
assert_eq!(
run_until(&mut c, |s| s.contains(&"back".to_owned())),
["back"]
);
apply(
&mut c,
vec![
Op::Remove {
parent: ROOT_ID,
child: 2,
},
Op::Release { id: 2 },
],
);
let batch = [c.commit(), c.commit()];
let released = batch
.iter()
.flatten()
.flat_map(|b| &b.items)
.filter(|i| {
matches!(
i,
MountItem::Animation(crate::animation::AnimationOp::Release { .. })
)
})
.count();
assert_eq!(released, 2, "{batch:?}");
}
#[component]
fn Outlived() -> impl IntoElement {
let (owner, (fade, field)) =
crate::ui::create_root(|| (animated_value(1.0), element_ref()));
let _ = fade.timing(0.0, 300.0, Easing::Linear);
let shown = signal(String::new());
view()
.bind_opacity(&fade)
.child(input().element_ref(&field))
.child(view().on_press(move |_: &()| {
field.focus();
fade.set(0.5);
}))
.child(view().on_press(move |_: &()| {
owner.dispose();
fade.set(0.25);
fade.stop();
let _ = fade.spring(1.0, Spring::default());
field.focus();
let gone = fade.interpolate(&[0.0, 1.0], &[0.0, 1.0]);
shown.set(format!("{:?} {:?}", field.node(), gone.live().is_none()));
}))
.child(text(move || shown.get()))
}
#[test]
fn handles_outlived_by_what_binds_them_do_nothing() {
let mut c = core();
let mut registry = Registry::new();
registry.register::<Outlived>();
c.set_components(registry);
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetProp {
id: 2,
prop: Prop::Component(std::any::type_name::<Outlived>().into()),
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
let animations = |batch: &MountBatch| -> Vec<crate::animation::AnimationOp> {
batch
.items
.iter()
.filter_map(|i| match i {
MountItem::Animation(op) => Some(op.clone()),
_ => None,
})
.collect()
};
let commands = |batch: &MountBatch| -> Vec<(NodeId, Command)> {
batch
.items
.iter()
.filter_map(|i| match i {
MountItem::Command { id, command } => Some((*id, command.clone())),
_ => None,
})
.collect()
};
let batch = c.commit().unwrap();
let ops = animations(&batch);
let fade = match ops[..] {
[
crate::animation::AnimationOp::CreateValue { node, value: 1.0 },
crate::animation::AnimationOp::Start { node: started, .. },
] if started == node => node,
_ => panic!("{ops:?}"),
};
let input = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Create {
id,
kind: ViewKind::Input,
} => Some(*id),
_ => None,
})
.expect("an input");
let buttons: Vec<_> = batch
.items
.iter()
.filter_map(|i| match i {
MountItem::Events { id, events } if events.contains(EventKind::Press) => Some(*id),
_ => None,
})
.collect();
let [first, second] = buttons[..] else {
panic!("{buttons:?}");
};
assert!(c.component_event(first, EventKind::Press, &EventPayload::None));
let batch = c.commit().unwrap();
assert_eq!(
animations(&batch),
[crate::animation::AnimationOp::Set {
node: fade,
value: 0.5
}]
);
assert_eq!(commands(&batch), [(input, Command::Focus)]);
assert!(c.component_event(second, EventKind::Press, &EventPayload::None));
let batch = c.commit().unwrap();
assert_eq!(animations(&batch), []);
assert_eq!(commands(&batch), []);
assert_eq!(texts(&batch), ["None true"]);
assert!(c.component_event(first, EventKind::Press, &EventPayload::None));
assert!(
c.commit()
.is_none_or(|b| animations(&b).is_empty() && commands(&b).is_empty())
);
apply(
&mut c,
vec![
Op::Remove {
parent: ROOT_ID,
child: 2,
},
Op::Release { id: 2 },
],
);
let released: Vec<_> = [c.commit(), c.commit()]
.iter()
.flatten()
.flat_map(animations)
.collect();
assert_eq!(
released,
[crate::animation::AnimationOp::Release { node: fade }]
);
}
#[component]
fn Repeated() -> impl IntoElement {
let v = animated_value(0.0);
let presses = signal(0);
let colors = [AdaptiveColor {
light: Color::BLACK.into(),
dark: Color::TRANSPARENT.into(),
}; 2];
view()
.bind_opacity(&v)
.child(view().on_press(move |_: &()| {
let half = v.mul(0.5);
let _ = half.interpolate(&[0.0, 1.0], &[0.0, 1.0]);
let _ = v.interpolate_colors(&[0.0, 1.0], &colors);
presses.update(|n| *n += 1);
}))
.child(view().on_press(move |_: &()| {
let _ = (signal(0), animated_value(1.0), element_ref(), stored(0));
spawn(async move {
let _ = (signal(1), animated_value(2.0));
});
}))
.child(text(move || presses.get().to_string()))
}
#[test]
fn repeated_presses_do_not_grow_the_graph() {
let mut c = core();
let mut registry = Registry::new();
registry.register::<Repeated>();
c.set_components(registry);
let mount = |c: &mut Core<FakeEngine>, id| {
apply(
c,
vec![
Op::Create { id, tag: Tag::View },
Op::SetProp {
id,
prop: Prop::Component(std::any::type_name::<Repeated>().into()),
},
Op::Insert {
parent: ROOT_ID,
child: id,
before: None,
},
],
);
let batch = c.commit().unwrap();
let buttons: Vec<_> = batch
.items
.iter()
.filter_map(|i| match i {
MountItem::Events { id, events } if events.contains(EventKind::Press) => {
Some(*id)
}
_ => None,
})
.collect();
[buttons[0], buttons[1]]
};
let unmount = |c: &mut Core<FakeEngine>, id| {
apply(
c,
vec![
Op::Remove {
parent: ROOT_ID,
child: id,
},
Op::Release { id },
],
);
let _ = (c.commit(), c.commit());
};
let press = |c: &mut Core<FakeEngine>, button| {
assert!(c.component_event(button, EventKind::Press, &EventPayload::None));
c.run_component_tasks();
let _ = c.commit();
};
let slots = |c: &Core<FakeEngine>| c.components.runtime.slots_in_use();
let _ = mount(&mut c, 2);
unmount(&mut c, 2);
let empty = slots(&c);
let [derive, make] = mount(&mut c, 4);
for _ in 0..5 {
press(&mut c, derive);
}
let derived = slots(&c);
for _ in 0..500 {
press(&mut c, derive);
}
assert_eq!(slots(&c), derived, "deriving again adds nothing");
for _ in 0..50 {
press(&mut c, make);
}
assert!(slots(&c) > derived);
unmount(&mut c, 4);
assert_eq!(slots(&c), empty, "released with the component");
}
#[component]
fn SelfClosing() -> impl IntoElement {
let open = signal(true);
view().child(when(open, move || {
view().on_press(move |_: &()| {
Owner::current().dispose();
let _ = (signal(5), animated_value(1.0), element_ref());
spawn(async {});
})
}))
}
#[test]
fn a_listener_closing_its_own_view_leaves_nothing() {
let mut c = core();
let mut registry = Registry::new();
registry.register::<SelfClosing>();
c.set_components(registry);
let mount = |c: &mut Core<FakeEngine>, id| {
apply(
c,
vec![
Op::Create { id, tag: Tag::View },
Op::SetProp {
id,
prop: Prop::Component(std::any::type_name::<SelfClosing>().into()),
},
Op::Insert {
parent: ROOT_ID,
child: id,
before: None,
},
],
);
let batch = c.commit().unwrap();
batch
.items
.iter()
.find_map(|i| match i {
MountItem::Events { id, events } if events.contains(EventKind::Press) => {
Some(*id)
}
_ => None,
})
.unwrap()
};
let unmount = |c: &mut Core<FakeEngine>, id| {
apply(
c,
vec![
Op::Remove {
parent: ROOT_ID,
child: id,
},
Op::Release { id },
],
);
let _ = (c.commit(), c.commit());
};
let _ = mount(&mut c, 2);
unmount(&mut c, 2);
let empty = c.components.runtime.slots_in_use();
let button = mount(&mut c, 4);
assert!(c.component_event(button, EventKind::Press, &EventPayload::None));
c.run_component_tasks();
let _ = c.commit();
unmount(&mut c, 4);
assert_eq!(c.components.runtime.slots_in_use(), empty);
}
#[component]
fn Clearable() -> impl IntoElement {
let rows = signal((0..3).map(|i| format!("row {i}")).collect::<Vec<_>>());
view()
.flex_1()
.child(
view()
.h(px(20.0))
.on_press(move |_: &()| rows.set(Vec::new())),
)
.child(catch_error(
move || {
virtual_list(
move || rows.with(Vec::len),
move |at| {
view()
.h(px(40.0))
.child(text(move || rows.with(|r| r[at.index()].clone())))
},
)
.estimated_item_size(px(40.0))
.flex_1()
},
|e, _| text(format!("failed: {}", e.message)),
))
}
#[test]
fn virtual_lists_drop_rows_that_are_gone() {
let mut c = core();
c.mount_root(&crate::ui::Root::new::<Clearable>(|| ()));
let batch = c.commit().unwrap();
assert!(
texts(&batch).contains(&"row 2".to_owned()),
"{:?}",
texts(&batch)
);
let button = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Events { id, events } if events.contains(EventKind::Press) => Some(*id),
_ => None,
})
.expect("the button");
assert!(c.component_event(button, EventKind::Press, &EventPayload::None));
let mut shown = Vec::new();
for _ in 0..3 {
if let Some(batch) = c.commit() {
shown.extend(texts(&batch));
}
}
assert!(shown.iter().all(|t| !t.starts_with("failed")), "{shown:?}");
}
#[component]
fn Chat() -> impl IntoElement {
let tall = signal(false);
view()
.flex_1()
.child(
view()
.h(px(20.0))
.on_press(move |_: &()| tall.update(|t| *t = !*t)),
)
.child(
virtual_list(20, move |at| {
view().h(move || {
px(if tall.get() && at.is_last() {
120.0
} else {
40.0
})
})
})
.estimated_item_size(px(40.0))
.follow_end(px(10.0))
.flex_1(),
)
}
#[test]
fn virtual_lists_follow_their_end() {
let mut c = core();
c.mount_root(&crate::ui::Root::new::<Chat>(|| ()));
let batch = c.commit().unwrap();
let created = |kind: ViewKind| {
batch.items.iter().find_map(|i| match i {
MountItem::Create { id, kind: k } if *k == kind => Some(*id),
_ => None,
})
};
let list = created(ViewKind::Scroll).expect("a list");
let button = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Events { id, events } if events.contains(EventKind::Press) => Some(*id),
_ => None,
})
.expect("the button");
let scrolled = |batch: &MountBatch| -> f32 {
batch
.items
.iter()
.filter_map(|i| match i {
MountItem::ScrollBy { id, dy, .. } if *id == list => Some(dy.0),
_ => None,
})
.sum()
};
c.set_scroll_offset(list, point(px(0.0), px(800.0 - 460.0)), 0);
assert!(c.commit().is_none(), "every row is bound already");
assert!(c.component_event(button, EventKind::Press, &EventPayload::None));
assert_eq!(scrolled(&c.commit().unwrap()), 80.0);
c.set_scroll_offset(list, point(px(0.0), px(100.0)), 1);
assert!(c.component_event(button, EventKind::Press, &EventPayload::None));
assert_eq!(scrolled(&c.commit().unwrap()), 0.0);
assert!(c.component_event(button, EventKind::Press, &EventPayload::None));
assert_eq!(scrolled(&c.commit().unwrap()), 0.0);
}
thread_local! {
static SETUPS: Cell<u32> = const { Cell::new(0) };
}
#[derive(Clone, Copy, PartialEq, Props)]
struct DrawerProps {
#[prop(default)]
open: crate::ui::Prop<bool>,
#[prop(lazy)]
children: Children,
}
#[component]
fn Drawer(props: DrawerProps) -> impl IntoElement {
view().child(when(props.open, move || view().p_2().child(props.children)))
}
#[component]
fn Opened() -> impl IntoElement {
let n = SETUPS.with(|s| {
s.set(s.get() + 1);
s.get()
});
view().child(text(format!("opened {n}")))
}
#[component]
fn Shelf() -> impl IntoElement {
let open = signal(false);
view()
.child(
view()
.h(px(10.0))
.on_press(move |_: &()| open.update(|o| *o = !*o)),
)
.child(drawer().open(open).child(opened()))
}
#[test]
fn lazy_children_a_component_passes_are_made_while_placed() {
let mut c = core();
c.mount_root(&crate::ui::Root::new::<Shelf>(|| ()));
let batch = c.commit().unwrap();
let button = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Events { id, events } if events.contains(EventKind::Press) => Some(*id),
_ => None,
})
.expect("the button");
let instances = c.components.instances.len();
assert_eq!(SETUPS.with(Cell::get), 0, "closed: not made");
let events = js_events(&mut c);
assert!(events.is_empty(), "nothing for JS: {events:?}");
assert!(c.component_event(button, EventKind::Press, &EventPayload::None));
assert_eq!(texts(&c.commit().unwrap()), ["opened 1"]);
assert_eq!(c.components.instances.len(), instances + 1);
assert!(c.component_event(button, EventKind::Press, &EventPayload::None));
c.commit();
assert_eq!(c.components.instances.len(), instances);
assert!(c.component_event(button, EventKind::Press, &EventPayload::None));
assert_eq!(texts(&c.commit().unwrap()), ["opened 2"]);
let events = js_events(&mut c);
assert!(events.is_empty(), "nothing for JS: {events:?}");
}
#[component]
fn Paster() -> impl IntoElement {
let pasted = signal(String::new());
view()
.child(text(move || format!("pasted {}", pasted.get())))
.child(view().h(px(10.0)).on_press(move |_: &()| {
let (has, text) = (clipboard_has_string(), clipboard_string());
spawn(async move {
let value = if has.await { text.await } else { "-".into() };
pasted.set(value);
});
}))
}
#[test]
fn components_read_the_clipboard() {
let mut c = core();
c.mount_root(&crate::ui::Root::new::<Paster>(|| ()));
let batch = c.commit().unwrap();
let button = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Events { id, events } if events.contains(EventKind::Press) => Some(*id),
_ => None,
})
.expect("the button");
assert!(c.component_event(button, EventKind::Press, &EventPayload::None));
let reads: Vec<_> = c
.commit()
.unwrap()
.items
.into_iter()
.filter_map(|i| match i {
MountItem::Command {
id: ROOT_ID,
command: Command::ReadClipboard { id, has_only },
} => Some((id, has_only)),
_ => None,
})
.collect();
assert_eq!(reads, [(1, true), (2, false)]);
for (id, has_only) in reads {
let mut payload = vec![
("id", crate::PayloadValue::Number(f64::from(id))),
("has", crate::PayloadValue::Bool(true)),
];
if !has_only {
payload.push(("text", crate::PayloadValue::Str("hello".into())));
}
assert!(c.component_event(ROOT_ID, EventKind::Clipboard, &EventPayload::Map(payload)));
}
let shown = run_until(&mut c, |s| s.iter().any(|t| t.starts_with("pasted h")));
assert_eq!(shown.last().unwrap(), "pasted hello");
}
#[test]
fn a_clipboard_of_the_embedders_own_answers_in_place_of_the_platform() {
use crate::clipboard::{ClipboardBackend, MemoryClipboard};
let clipboard = std::sync::Arc::new(MemoryClipboard::new(Some("hello".into())));
let mut c = core();
c.set_clipboard(Some(clipboard.clone()));
c.mount_root(&crate::ui::Root::new::<Paster>(|| ()));
let batch = c.commit().unwrap();
let button = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Events { id, events } if events.contains(EventKind::Press) => Some(*id),
_ => None,
})
.expect("the button");
assert!(c.component_event(button, EventKind::Press, &EventPayload::None));
let items = c.commit().map(|b| b.items).unwrap_or_default();
assert!(
!items.iter().any(|i| matches!(
i,
MountItem::Command {
command: Command::ReadClipboard { .. },
..
}
)),
"{items:?}"
);
let shown = run_until(&mut c, |s| s.iter().any(|t| t.starts_with("pasted h")));
assert_eq!(shown.last().unwrap(), "pasted hello");
clipboard.set_text(None);
assert_eq!(clipboard.text(), None);
}
#[component]
fn ImagePaster() -> impl IntoElement {
let pasted = signal(String::new());
view()
.child(text(move || format!("pasted {}", pasted.get())))
.child(view().h(px(10.0)).on_press(move |_: &()| {
let (has, image, files) =
(clipboard_has_image(), clipboard_image(), clipboard_files());
spawn(async move {
let has = has.await;
let image = image
.await
.map(|i| format!("{}x{} {}", i.size.width.0, i.size.height.0, i.url));
let files = files.await.join(",");
pasted.set(format!("{has} {image:?} {files}"));
});
}))
}
#[test]
fn components_read_images_and_files_from_the_clipboard() {
let mut c = core();
c.mount_root(&crate::ui::Root::new::<ImagePaster>(|| ()));
let batch = c.commit().unwrap();
let button = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Events { id, events } if events.contains(EventKind::Press) => Some(*id),
_ => None,
})
.expect("the button");
assert!(c.component_event(button, EventKind::Press, &EventPayload::None));
let reads: Vec<_> = c
.commit()
.unwrap()
.items
.into_iter()
.filter_map(|i| match i {
MountItem::Command {
id: ROOT_ID,
command:
command @ (Command::ReadClipboardImage { .. }
| Command::ReadClipboardFiles { .. }),
} => Some(command),
_ => None,
})
.collect();
assert_eq!(
reads,
[
Command::ReadClipboardImage {
id: 1,
has_only: true
},
Command::ReadClipboardImage {
id: 2,
has_only: false
},
Command::ReadClipboardFiles { id: 3 },
]
);
use crate::PayloadValue::{Bool, Number, Str};
let answers = [
vec![("id", Number(1.0)), ("has", Bool(true))],
vec![
("id", Number(2.0)),
("has", Bool(true)),
("uri", Str("data:image/png;base64,AA==".into())),
("width", Number(4.0)),
("height", Number(3.0)),
],
vec![
("id", Number(3.0)),
("has", Bool(true)),
("files", Str("file:///tmp/a%20b.png\nfile:///tmp/c/".into())),
],
];
for payload in answers {
assert!(c.component_event(ROOT_ID, EventKind::Clipboard, &EventPayload::Map(payload)));
}
let shown = run_until(&mut c, |s| s.iter().any(|t| t.starts_with("pasted t")));
assert_eq!(
shown.last().unwrap(),
"pasted true Some(\"4x3 data:image/png;base64,AA==\") file:///tmp/a%20b.png,file:///tmp/c/"
);
}
#[test]
fn clipboard_images_need_the_capability() {
let mut c = core();
c.set_access(std::sync::Arc::new(crate::access::Access {
declared: crate::access::Capabilities::from_json(&serde_json::json!({})).unwrap(),
granted: None,
}));
c.mount_root(&crate::ui::Root::new::<ImagePaster>(|| ()));
let batch = c.commit().unwrap();
let button = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Events { id, events } if events.contains(EventKind::Press) => Some(*id),
_ => None,
})
.expect("the button");
assert!(c.component_event(button, EventKind::Press, &EventPayload::None));
let items = c.commit().map(|b| b.items).unwrap_or_default();
assert!(
!items.iter().any(|i| matches!(i, MountItem::Command { .. })),
"nothing reaches the platform"
);
let shown = run_until(&mut c, |s| s.iter().any(|t| t.starts_with("pasted f")));
assert_eq!(shown.last().unwrap(), "pasted false None ");
}
#[component]
fn Boundary(props: WithChildren) -> impl IntoElement {
let retry = stored(None::<crate::ui::Reset>);
view()
.on_press(move |_: &()| {
if let Some(reset) = retry.replace(None) {
reset.reset();
}
})
.child(catch_error(
move || props.children,
move |e, reset| {
retry.replace(Some(reset));
text(format!("oops: {} ({})", e.component, e.message))
},
))
}
#[derive(Clone, Copy, PartialEq, Props)]
struct FaultyProps {
#[prop(default)]
explode: crate::ui::Prop<bool>,
}
#[component]
fn Faulty(props: FaultyProps) -> impl IntoElement {
view()
.on_press(|_: &()| panic!("pressed"))
.child(text(move || {
assert!(!props.explode.get(), "exploded");
"fine"
}))
}
#[test]
fn errors_of_components_go_to_the_nearest_boundary() {
let mut c = core();
let mut registry = Registry::new();
registry.register::<Boundary>().register::<Faulty>();
c.set_components(registry);
let host = |id, name: &str| {
vec![
Op::Create { id, tag: Tag::View },
Op::SetProp {
id,
prop: Prop::Component(name.into()),
},
]
};
let mut ops = host(2, std::any::type_name::<Boundary>());
ops.push(Op::Create {
id: 4,
tag: Tag::View,
});
ops.extend(host(6, std::any::type_name::<Faulty>()));
for (parent, child) in [(4, 6), (2, 4), (ROOT_ID, 2)] {
ops.push(Op::Insert {
parent,
child,
before: None,
});
}
apply(&mut c, ops);
assert_eq!(texts(&c.commit().unwrap()), ["fine"]);
assert!(c.component_event(6, EventKind::Press, &EventPayload::None));
assert_eq!(
texts(&c.commit().unwrap()),
[format!(
"oops: {} (pressed)",
std::any::type_name::<Faulty>()
)]
);
assert!(c.component_event(2, EventKind::Press, &EventPayload::None));
c.commit();
apply(
&mut c,
vec![Op::SetProp {
id: 6,
prop: rust_prop::<FaultyProps>("explode", Some(serde_json::json!(true))),
}],
);
assert_eq!(
texts(&c.commit().unwrap()),
[format!(
"oops: {} (exploded)",
std::any::type_name::<Faulty>()
)]
);
}
#[component]
fn Sentence() -> impl IntoElement {
let taps = signal(0);
let long = signal(false);
view().child(
text(move || format!("Tapped {} ", taps.get()))
.text_size(px(16.0))
.child(text("the link").underline().on_press(move |_: &()| {
taps.update(|t| *t += 1);
long.set(true);
}))
.child(", ")
.child(text("bold").font_weight(FontWeight::BOLD))
.child(when(long, || " and more"))
.child(view().size(px(10.0)).bg(rgb(0xff0000))),
)
}
#[test]
fn rust_texts_nest_spans_links_and_inline_views() {
let mut c = core();
c.mount_root(&crate::ui::Root::new::<Sentence>(|| ()));
let batch = c.commit().unwrap();
let text = |batch: &MountBatch| {
batch.items.iter().find_map(|i| match i {
MountItem::Text { text, .. } => Some(text.clone()),
_ => None,
})
};
let t = text(&batch).expect("one paragraph");
assert_eq!(t.text, "Tapped 0 the link, bold\u{fffc}");
assert_eq!(t.attachments.len(), 1, "the view sits in the text");
assert!(t.spans.iter().any(|s| s.style.font_weight == 700));
let link = t.links[0];
assert_eq!((link.start, link.end), (9, 17));
let views = batch
.items
.iter()
.filter(|i| matches!(i, MountItem::Create { .. }))
.count();
assert_eq!(views, 3, "the root's host, the text and the view");
assert!(c.component_event(link.node, EventKind::Press, &EventPayload::None));
let t = text(&c.commit().unwrap()).expect("built again");
assert_eq!(t.text, "Tapped 1 the link, bold and more\u{fffc}");
}
}
mod touch {
use super::*;
use crate::{
TouchEvent, TouchPhase, TouchPoint,
ui::{ClaimRequest, ResponderEvent, TouchClaim, prelude::*},
};
#[derive(Clone, PartialEq, Props)]
struct PadProps {
#[prop(default)]
keep: bool,
}
#[component]
fn Pad(props: PadProps) -> impl IntoElement {
let log = signal(Vec::<String>::new());
let note = move |what: &'static str, f: fn(&ResponderEvent) -> String| {
move |e: &ResponderEvent| log.update(|l| l.push(format!("{what}{}", f(e))))
};
let none = |_: &ResponderEvent| String::new();
view()
.on_touch(move |t: &TouchEvent| log.update(|l| l.push(format!("touch {:?}", t.phase))))
.claim_touches(TouchClaim::on_move().capture().ask(|r: &ClaimRequest| {
if r.event.gesture.delta.x.abs() > px(50.0) {
r.claim();
}
}))
.on_responder_grant(note("outer grant", none))
.on_responder_reject(note("outer reject", none))
.child(
view()
.size(px(100.0))
.claim_touches(TouchClaim::on_start())
.keep_touches(props.keep)
.on_responder_grant(note("grant at ", |e| {
format!("{},{}", e.location.x.0, e.location.y.0)
}))
.on_responder_move(note("move ", |e| {
format!("{},{}", e.gesture.delta.x.0, e.gesture.delta.y.0)
}))
.on_responder_release(note("release ", |e| format!("{:.2}", e.gesture.vx)))
.on_responder_terminate(note("terminate", none)),
)
.child(text(move || log.with(|l| l.join("|"))))
}
fn touch(phase: TouchPhase, path: &[NodeId], x: f32, t: f64) -> EventPayload {
EventPayload::Touch(Box::new(TouchEvent {
phase,
timestamp: t,
activated: None,
points: vec![TouchPoint {
id: 7,
target: path[0],
path: if phase == TouchPhase::Start {
path.to_vec()
} else {
Vec::new()
},
page: point(px(x), px(100.0)),
location: point(px(x - 10.0), px(5.0)),
changed: true,
}],
}))
}
fn log(batch: &MountBatch) -> Option<String> {
batch.items.iter().rev().find_map(|i| match i {
MountItem::Text { text, .. } => Some(text.text.clone()),
_ => None,
})
}
fn commands(batch: &MountBatch) -> Vec<(NodeId, Command)> {
batch
.items
.iter()
.filter_map(|i| match i {
MountItem::Command { id, command } => Some((*id, command.clone())),
_ => None,
})
.collect()
}
fn mount_pad(keep: bool) -> (Core<FakeEngine>, NodeId, NodeId) {
let mut c = core();
c.mount_root(&crate::ui::Root::new::<Pad>(move || PadProps { keep }));
let batch = c.commit().unwrap();
let listening: Vec<NodeId> = batch
.items
.iter()
.filter_map(|i| match i {
MountItem::Events { id, events } if events.contains(EventKind::Touch) => Some(*id),
_ => None,
})
.collect();
let [outer, inner] = listening[..] else {
panic!("two listening views: {listening:?}");
};
(c, outer, inner)
}
#[test]
fn an_element_claims_the_touches_on_start_and_blocks_scrolling() {
let (mut c, outer, inner) = mount_pad(false);
let path = [inner, outer];
assert!(!c.component_event(
ROOT_ID,
EventKind::Touch,
&touch(TouchPhase::Start, &path, 20.0, 0.0)
));
let batch = c.commit().unwrap();
assert_eq!(log(&batch).unwrap(), "touch Start|grant at 10,5");
assert_eq!(
commands(&batch),
[(inner, Command::SetJsResponder { block: true })]
);
c.component_event(
ROOT_ID,
EventKind::Touch,
&touch(TouchPhase::Move, &path, 40.0, 16.0),
);
let batch = c.commit().unwrap();
assert_eq!(
log(&batch).unwrap(),
"touch Start|grant at 10,5|touch Move|move 20,0"
);
c.component_event(
ROOT_ID,
EventKind::Touch,
&touch(TouchPhase::End, &path, 40.0, 32.0),
);
let batch = c.commit().unwrap();
assert!(log(&batch).unwrap().ends_with("|touch End|release 0.88"));
assert_eq!(commands(&batch), [(ROOT_ID, Command::ClearJsResponder)]);
}
#[test]
fn a_capturing_claim_takes_the_touches_over_unless_they_are_kept() {
let (mut c, outer, inner) = mount_pad(false);
let path = [inner, outer];
let send = |c: &mut Core<FakeEngine>, phase, x, t| {
c.component_event(ROOT_ID, EventKind::Touch, &touch(phase, &path, x, t));
};
send(&mut c, TouchPhase::Start, 0.0, 0.0);
send(&mut c, TouchPhase::Move, 30.0, 16.0);
send(&mut c, TouchPhase::Move, 80.0, 32.0);
let batch = c.commit().unwrap();
let text = log(&batch).unwrap();
assert!(
text.ends_with("move 30,0|touch Move|terminate|outer grant"),
"{text}"
);
assert_eq!(
commands(&batch).last(),
Some(&(outer, Command::SetJsResponder { block: true }))
);
let (mut c, outer, inner) = mount_pad(true);
let path = [inner, outer];
let send = |c: &mut Core<FakeEngine>, phase, x, t| {
c.component_event(ROOT_ID, EventKind::Touch, &touch(phase, &path, x, t));
};
send(&mut c, TouchPhase::Start, 0.0, 0.0);
send(&mut c, TouchPhase::Move, 80.0, 16.0);
let text = log(&c.commit().unwrap()).unwrap();
assert!(text.ends_with("|outer reject|move 80,0"), "{text}");
send(&mut c, TouchPhase::Cancel, 80.0, 32.0);
let batch = c.commit().unwrap();
assert!(log(&batch).unwrap().ends_with("|terminate"));
assert_eq!(commands(&batch), [(ROOT_ID, Command::ClearJsResponder)]);
}
#[test]
fn js_and_component_responders_share_the_native_block() {
let (mut c, outer, inner) = mount_pad(false);
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
Op::Command {
id: 2,
command: Command::SetJsResponder { block: true },
},
],
);
assert_eq!(
commands(&c.commit().unwrap()),
[(2, Command::SetJsResponder { block: true })]
);
c.component_event(
ROOT_ID,
EventKind::Touch,
&touch(TouchPhase::Start, &[inner, outer], 0.0, 0.0),
);
assert_eq!(
commands(&c.commit().unwrap()),
[(inner, Command::SetJsResponder { block: true })]
);
c.component_event(
ROOT_ID,
EventKind::Touch,
&touch(TouchPhase::End, &[inner, outer], 0.0, 16.0),
);
assert_eq!(
commands(&c.commit().unwrap()),
[(2, Command::SetJsResponder { block: true })]
);
apply(
&mut c,
vec![Op::Command {
id: ROOT_ID,
command: Command::ClearJsResponder,
}],
);
assert_eq!(
commands(&c.commit().unwrap()),
[(ROOT_ID, Command::ClearJsResponder)]
);
}
#[component]
fn Scrub() -> impl IntoElement {
let log = signal(Vec::<String>::new());
view()
.child(
view()
.size(px(100.0))
.claim_touches(TouchClaim::on_start())
.claim_touches(TouchClaim::on_pan(crate::PanActivation::horizontal(
px(10.0),
px(10.0),
)))
.on_responder_grant(move |_: &ResponderEvent| {
log.update(|l| l.push("grant".into()));
}),
)
.child(text(move || log.with(|l| l.join("|"))))
}
#[test]
fn a_pan_claim_waits_for_the_platform_to_activate_it() {
let mut c = core();
c.mount_root(&crate::ui::Root::new::<Scrub>(|| ()));
let batch = c.commit().unwrap();
let (id, activation) = batch
.items
.iter()
.find_map(|i| match i {
MountItem::ViewProps { id, props } => props.pan_activation.map(|a| (*id, a)),
_ => None,
})
.expect("the activation is mounted");
assert_eq!(
activation,
crate::PanActivation::horizontal(px(10.0), px(10.0))
);
c.component_event(
ROOT_ID,
EventKind::Touch,
&touch(TouchPhase::Start, &[id], 0.0, 0.0),
);
c.component_event(
ROOT_ID,
EventKind::Touch,
&touch(TouchPhase::Move, &[id], 5.0, 16.0),
);
assert!(c.commit().is_none_or(|b| commands(&b).is_empty()));
let mut activated = touch(TouchPhase::Move, &[id], 14.0, 32.0);
if let EventPayload::Touch(t) = &mut activated {
t.activated = Some(id);
}
c.component_event(ROOT_ID, EventKind::Touch, &activated);
let batch = c.commit().unwrap();
assert_eq!(log(&batch), Some("grant".into()), "{batch:?}");
assert_eq!(
commands(&batch),
[(id, Command::SetJsResponder { block: true })]
);
}
#[test]
fn a_pan_activation_decides_by_axis() {
let pan = crate::PanActivation::horizontal(px(10.0), px(8.0));
assert_eq!(pan.decide(px(4.0), px(3.0)), None);
assert_eq!(pan.decide(px(-11.0), px(3.0)), Some(true));
assert_eq!(pan.decide(px(4.0), px(-9.0)), Some(false));
assert_eq!(pan.decide(px(20.0), px(9.0)), Some(false));
let any = crate::PanActivation::default();
assert_eq!(any.decide(px(6.0), px(6.0)), None);
assert_eq!(any.decide(px(8.0), px(8.0)), Some(true));
}
#[component]
fn Card() -> impl IntoElement {
let (scale, angle) = (animated_value(1.0), animated_value(0.0));
view()
.pinch_value(&scale)
.rotation_value(&angle)
.bind_scale(&scale)
.bind_rotate(&angle)
}
#[test]
fn pinches_and_rotations_drive_animated_values() {
let mut c = core();
c.mount_root(&crate::ui::Root::new::<Card>(|| ()));
let batch = c.commit().unwrap();
let values = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Gestures { values, .. } => Some(*values),
_ => None,
})
.expect("gesture values");
let created: Vec<_> = batch
.items
.iter()
.filter_map(|i| match i {
MountItem::Animation(AnimationOp::CreateValue { node, .. }) => Some(*node),
_ => None,
})
.collect();
assert_eq!(values.pinch, Some(created[0]));
assert_eq!(values.rotation, Some(created[1]));
}
}
mod painter {
use super::*;
use crate::{
canvas::{CanvasGradient, CanvasOp, FillRule, GradientShape, Num, Paint, svg_path},
geometry::{bounds, percentage, point, px, radians, size},
ui::prelude::*,
};
#[test]
fn painters_hit_test_what_they_drew() {
let slice = svg_path("M0 0 L40 0 L0 40 Z");
let p = Painter::new()
.save()
.translate(10.0, 10.0)
.fill_path(&slice, FillRule::NonZero)
.restore()
.line_width(4.0)
.stroke_svg("M0 2 L100 2")
.begin_path()
.rect(bounds(point(px(60.0), px(60.0)), size(px(20.0), px(20.0))));
let at = |x, y| point(px(x), px(y));
assert!(p.path_contains(&slice, at(5.0, 5.0), FillRule::NonZero));
assert!(!p.path_contains(&slice, at(45.0, 45.0), FillRule::NonZero));
let moved = Painter::new().translate(10.0, 10.0);
assert!(moved.path_contains(&slice, at(15.0, 15.0), FillRule::NonZero));
assert!(!moved.path_contains(&slice, at(5.0, 5.0), FillRule::NonZero));
assert!(p.is_point_in_path(at(70.0, 70.0), FillRule::EvenOdd));
assert!(!p.is_point_in_path(at(50.0, 70.0), FillRule::NonZero));
assert!(p.is_point_in_stroke(at(60.0, 70.0)));
assert!(!p.is_point_in_stroke(at(70.0, 70.0)));
let top = svg_path("M0 2 L100 2");
assert!(p.stroke_contains(&top, at(50.0, 3.5)));
assert!(!p.stroke_contains(&top, at(50.0, 5.0)));
}
#[component]
fn Label() -> impl IntoElement {
let offset = animated_value(3.0);
let width = crate::ui::prelude::measure_text("600 20px system-ui", "100%");
let mut p = Painter::new()
.stroke_style(
CanvasGradient::conic(radians(0.0), point(px(50.0), px(50.0)))
.add_color_stop(percentage(1.0), rgb(0x6366f1))
.add_color_stop(percentage(0.0), rgb(0x22d3ee)),
)
.stroke_path(&svg_path("M20 6L9 17l-5-5"))
.fill_style(CanvasGradient::radial(
point(px(0.0), px(0.0)),
px(4.0),
point(px(0.0), px(0.0)),
px(40.0),
))
.fill_path(&svg_path("M0 0h10v10z"), FillRule::EvenOdd)
.fill_text("100%", 50.0 - width.0, 20.0);
let x = p.num(&offset);
p = p.op(CanvasOp::LineDashOffset(x));
view().child(canvas(p))
}
#[test]
fn rust_drawings_measure_text_and_use_every_paint() {
let mut c = core();
c.mount_root(&crate::ui::Root::new::<Label>(|| ()));
let batch = c.commit().unwrap();
let drawing = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Canvas { drawing, .. } => Some(drawing.clone()),
_ => None,
})
.expect("a drawing");
let ops = &drawing.ops;
let CanvasOp::StrokeStyle(Paint::Gradient(ring)) = &ops[0] else {
panic!("{:?}", ops[0])
};
assert_eq!(
ring.shape,
GradientShape::Conic {
start_angle: radians(0.0),
center: point(px(50.0), px(50.0)),
}
);
assert_eq!(ring.stops[0], (percentage(0.0), Color::from(rgb(0x22d3ee))));
assert!(matches!(&ops[1], CanvasOp::Stroke(Some(path)) if path.len() == 3));
assert!(matches!(
&ops[2],
CanvasOp::FillStyle(Paint::Gradient(g)) if matches!(g.shape, GradientShape::Radial { .. })
));
assert!(matches!(
&ops[3],
CanvasOp::Fill(Some(_), FillRule::EvenOdd)
));
assert!(matches!(
&ops[4],
CanvasOp::FillText {
x: Num::Const(18.0),
..
}
));
assert_eq!(ops[5], CanvasOp::LineDashOffset(Num::Slot(0)));
assert_eq!(drawing.slots.len(), 1);
}
}
#[test]
fn overflowing_children_mount_their_area() {
let mut c = core();
let create = |id| Op::Create { id, tag: Tag::View };
let insert = |parent, child| Op::Insert {
parent,
child,
before: None,
};
apply(
&mut c,
vec![
create(5),
create(2),
create(4),
style_op(5, &[("padding", StyleValue::Number(10.0))]),
style_op(2, &[("height", StyleValue::Number(40.0))]),
style_op(
4,
&[
("position", StyleValue::Str("absolute")),
("top", StyleValue::Number(40.0)),
("left", StyleValue::Number(0.0)),
("width", StyleValue::Number(100.0)),
("height", StyleValue::Number(120.0)),
],
),
insert(2, 4),
insert(5, 2),
insert(ROOT_ID, 5),
],
);
let batch = c.commit().unwrap();
let overflow = |batch: &MountBatch, id: NodeId| {
batch.items.iter().rev().find_map(|i| match i {
MountItem::Overflow { id: i, extent } if *i == id => Some(*extent),
_ => None,
})
};
assert_eq!(
overflow(&batch, 2),
Some(Some(rect(0.0, 0.0, 300.0, 160.0)))
);
assert_eq!(
overflow(&batch, 5),
Some(Some(rect(0.0, 0.0, 320.0, 170.0)))
);
assert_eq!(overflow(&batch, 4), None);
c.apply(style_op(2, &[("height", StyleValue::Number(40.0))]))
.unwrap();
if let Some(batch) = c.commit() {
assert_eq!(overflow(&batch, 2), None);
}
c.apply(style_op(
5,
&[
("padding", StyleValue::Number(10.0)),
("overflow", StyleValue::Str("hidden")),
],
))
.unwrap();
c.apply(style_op(2, &[("height", StyleValue::Number(200.0))]))
.unwrap();
let batch = c.commit().unwrap();
assert_eq!(overflow(&batch, 5), Some(None));
assert_eq!(overflow(&batch, 2), Some(None));
}
#[test]
fn a_view_that_loses_its_absolute_child_lays_out() {
let mut c = core();
for (id, absolute) in [(2, false), (4, true), (6, false)] {
c.apply(Op::Create { id, tag: Tag::View }).unwrap();
let mut props = vec![("height", StyleValue::Number(20.0))];
if absolute {
props.push(("position", StyleValue::Str("absolute")));
}
c.apply(style_op(id, &props)).unwrap();
}
for (parent, child) in [(2, 4), (ROOT_ID, 2), (ROOT_ID, 6)] {
c.apply(Op::Insert {
parent,
child,
before: None,
})
.unwrap();
}
c.commit().unwrap();
c.apply(Op::Release { id: 4 }).unwrap();
c.apply(Op::Create {
id: 8,
tag: Tag::View,
})
.unwrap();
c.apply(Op::Insert {
parent: 6,
child: 8,
before: None,
})
.unwrap();
c.apply(style_op(2, &[("height", StyleValue::Number(30.0))]))
.unwrap();
let batch = c.commit().unwrap();
assert!(batch.items.iter().any(|i| matches!(
i,
MountItem::Frame { id: 6, frame, .. } if frame.origin.y.0 == 30.0
)));
}
#[test]
fn measures_in_root_coordinates() {
let mut c = core();
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::Scroll,
},
Op::Create {
id: 4,
tag: Tag::View,
},
Op::Create {
id: 6,
tag: Tag::View,
},
style_op(
2,
&[
("marginTop", StyleValue::Number(50.0)),
("height", StyleValue::Number(300.0)),
],
),
style_op(
4,
&[
("padding", StyleValue::Number(20.0)),
("height", StyleValue::Number(1000.0)),
],
),
style_op(
6,
&[
("marginTop", StyleValue::Number(500.0)),
("height", StyleValue::Number(30.0)),
],
),
Op::Insert {
parent: 4,
child: 6,
before: None,
},
Op::Insert {
parent: 2,
child: 4,
before: None,
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
c.commit().unwrap();
let m = c.measure(6).unwrap();
assert_eq!(m.frame, rect(20.0, 520.0, 280.0, 30.0));
assert_eq!(m.page, point(px(20.0), px(570.0)));
c.set_scroll_offset(2, point(px(0.0), px(400.0)), u32::MAX);
let m = c.measure(6).unwrap();
assert_eq!(m.page, point(px(20.0), px(170.0)));
c.apply(Op::Remove {
parent: 4,
child: 6,
})
.unwrap();
c.commit();
assert_eq!(c.measure(6), None);
assert_eq!(c.measure(1000), None);
}
#[test]
fn window_drag_is_a_view_prop() {
let mut c = core();
let drag = |value| Op::SetProp {
id: 2,
prop: Prop::WindowDrag(value),
};
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
drag(Some(true)),
Op::Insert {
parent: crate::ROOT_ID,
child: 2,
before: None,
},
],
);
let batch = c.commit().unwrap();
assert!(batch.items.iter().any(
|i| matches!(i, MountItem::ViewProps { id: 2, props } if props.window_drag == Some(true))
));
apply(&mut c, vec![drag(None)]);
let batch = c.commit().unwrap();
assert!(batch.items.iter().any(
|i| matches!(i, MountItem::ViewProps { id: 2, props } if props.window_drag.is_none())
));
}
#[test]
fn focusable_is_a_view_prop() {
let mut c = core();
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetProp {
id: 2,
prop: Prop::Focusable(Some(false)),
},
Op::Insert {
parent: crate::ROOT_ID,
child: 2,
before: None,
},
],
);
let batch = c.commit().unwrap();
assert!(batch.items.iter().any(
|i| matches!(i, MountItem::ViewProps { id: 2, props } if props.focusable == Some(false))
));
apply(
&mut c,
vec![Op::SetProp {
id: 2,
prop: Prop::Focusable(None),
}],
);
let batch = c.commit().unwrap();
assert!(
batch.items.iter().any(
|i| matches!(i, MountItem::ViewProps { id: 2, props } if props.focusable.is_none())
)
);
}
mod views {
use std::{cell::Cell, rc::Rc};
use serde_json::json;
use super::*;
use crate::{
Prop,
ui::{Component, Props, Registry, Root, Scope, prelude::*},
};
fn texts(batch: &MountBatch) -> Vec<String> {
batch
.items
.iter()
.filter_map(|i| match i {
MountItem::Text { text, .. } => Some(text.text.clone()),
_ => None,
})
.collect()
}
fn root_host(batch: &MountBatch) -> NodeId {
batch
.items
.iter()
.find_map(|i| match i {
MountItem::Insert {
parent: ROOT_ID,
child,
..
} => Some(*child),
_ => None,
})
.expect("the root's host is in the root view")
}
fn text_node(batch: &MountBatch, content: &str) -> NodeId {
batch
.items
.iter()
.find_map(|i| match i {
MountItem::Text { id, text, .. } if text.text == content => Some(*id),
_ => None,
})
.unwrap_or_else(|| panic!("no text {content:?}"))
}
fn native_parent<M: TextEngine>(c: &Core<M>, id: NodeId) -> Option<NodeId> {
let key = c.key(id).expect("a node");
let parent = c.nodes[key as usize].parent;
(parent != NO_KEY).then(|| c.nodes[parent as usize].id)
}
#[derive(Default)]
struct Counter;
#[derive(Clone, PartialEq, Props)]
struct CounterProps {
#[prop(default = "Taps".into())]
label: crate::ui::Prop<SharedString>,
}
#[derive(Event)]
enum CounterEvent {
Tap(u32),
}
impl Component for Counter {
type Props = CounterProps;
type Event = CounterEvent;
fn view(props: CounterProps, cx: Scope<CounterEvent>) -> impl IntoElement {
let count = signal(0);
view()
.p_2()
.on_press(move |_: &()| {
count.update(|n| *n += 1);
cx.emit(CounterEvent::Tap(count.get_untracked()));
})
.child(text(move || {
format!("{} {}", props.label.get(), count.get())
}))
.child(text("static"))
}
}
#[test]
fn a_view_runs_once_and_its_bindings_send_only_what_changed() {
let mut c = core();
c.mount_root(&Root::new::<Counter>(|| CounterProps {
label: "Hits".into(),
}));
let batch = c.commit().unwrap();
assert_eq!(texts(&batch), ["Hits 0", "static"]);
let host = root_host(&batch);
assert!(c.component_event(host, EventKind::Press, &EventPayload::None));
let batch = c.commit().unwrap();
assert_eq!(texts(&batch), ["Hits 1"], "one text changed");
assert!(
!batch
.items
.iter()
.any(|i| matches!(i, MountItem::Create { .. } | MountItem::Delete { .. })),
"no node made or freed: {:?}",
batch.items
);
}
#[component]
fn Twice() -> impl IntoElement {
let log = signal(String::new());
let note = move |c: char| log.update(|l| l.push(c));
view()
.on_press(move || note('a'))
.on_press(move || note('b'))
.child(
Counter::element(CounterProps::default())
.on_event(move |_: &CounterEvent| note('c'))
.on_event(move |_: &CounterEvent| note('d')),
)
.child(text(move || format!("log {}", log.get())))
}
#[test]
fn every_listener_of_an_event_runs_in_order() {
let mut c = core();
c.mount_root(&Root::new::<Twice>(|| ()));
let batch = c.commit().unwrap();
let host = root_host(&batch);
let counter = native_parent(&c, text_node(&batch, "Taps 0")).unwrap();
assert!(c.component_event(host, EventKind::Press, &EventPayload::None));
assert_eq!(texts(&c.commit().unwrap()), ["log ab"]);
assert!(c.component_event(counter, EventKind::Press, &EventPayload::None));
assert!(texts(&c.commit().unwrap()).contains(&"log abcd".to_owned()));
}
#[derive(Event)]
enum ChooserEvent {
Press,
Pick(u32),
Placed { at: u32 },
}
#[component]
fn Chooser(cx: Scope<ChooserEvent>) -> impl IntoElement {
view().child(text("choose")).on_press(move || {
cx.emit(ChooserEvent::Press);
cx.emit(ChooserEvent::Pick(7));
cx.emit(ChooserEvent::Placed { at: 9 });
})
}
#[component]
fn Chosen() -> impl IntoElement {
let log = signal(String::new());
let note = move |s: String| log.update(|l| l.push_str(&s));
view()
.child(
chooser()
.on_press(move || note("press ".into()))
.on_press(move |_: &()| note("again ".into()))
.on_pick(move |index: &u32| note(format!("pick {index} ")))
.on_placed(move |event: &ChooserEvent| {
if let ChooserEvent::Placed { at } = event {
note(format!("placed {at}"));
}
}),
)
.child(text(move || format!("log {}", log.get())))
}
#[test]
fn a_components_listener_methods_hear_their_event_and_its_data() {
let mut c = core();
c.mount_root(&Root::new::<Chosen>(|| ()));
let batch = c.commit().unwrap();
let chooser = native_parent(&c, text_node(&batch, "choose")).unwrap();
assert!(c.component_event(chooser, EventKind::Press, &EventPayload::None));
assert_eq!(
texts(&c.commit().unwrap()),
["log press again pick 7 placed 9"]
);
}
#[component]
fn Rerun() -> impl IntoElement {
let log = signal(String::new());
let run = signal(0);
view()
.child(dynamic(move || {
let at = run.get();
text("tap").on_press(move || log.update(|l| l.push_str(&at.to_string())))
}))
.child(text("again").on_press(move || run.update(|r| *r += 1)))
.child(text(move || format!("log {}", log.get())))
}
#[test]
fn a_node_made_again_hears_only_its_new_listeners() {
let mut c = core();
c.mount_root(&Root::new::<Rerun>(|| ()));
let batch = c.commit().unwrap();
let (tap, again) = (text_node(&batch, "tap"), text_node(&batch, "again"));
for _ in 0..3 {
assert!(c.component_event(again, EventKind::Press, &EventPayload::None));
let _ = c.commit();
}
assert!(c.component_event(tap, EventKind::Press, &EventPayload::None));
assert_eq!(texts(&c.commit().unwrap()), ["log 3"]);
}
#[test]
fn a_prop_from_js_reaches_only_what_reads_it() {
let mut c = core();
let mut registry = Registry::new();
registry.register::<Counter>();
c.set_components(registry);
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetProp {
id: 2,
prop: Prop::Component(std::any::type_name::<Counter>().into()),
},
Op::SetProp {
id: 2,
prop: rust_prop::<CounterProps>("label", Some(json!("Hits"))),
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
assert_eq!(texts(&c.commit().unwrap()), ["Hits 0", "static"]);
c.apply(Op::SetProp {
id: 2,
prop: rust_prop::<CounterProps>("label", Some(json!("Runs"))),
})
.unwrap();
assert_eq!(texts(&c.commit().unwrap()), ["Runs 0"]);
assert!(c.component_event(2, EventKind::Press, &EventPayload::None));
c.commit();
let events = js_events(&mut c);
assert!(events.iter().any(|(id, kind, payload)| *id == 2
&& *kind == EventKind::NativeEvent
&& matches!(payload, EventPayload::Native { name, data } if &**name == "tap" && *data == json!(1))));
}
#[derive(Default)]
struct Toggle;
#[derive(Clone, PartialEq, Props)]
struct ToggleProps {}
thread_local! {
static CLEANED: Cell<u32> = const { Cell::new(0) };
}
impl Component for Toggle {
type Props = ToggleProps;
type Event = NoEvent;
fn view(_: ToggleProps, _: Scope<NoEvent>) -> impl IntoElement {
let on = signal(false);
view()
.on_press(move |_: &()| on.update(|v| *v = !*v))
.child(text("before"))
.child(
when(on, || {
cleanup(|| CLEANED.with(|c| c.set(c.get() + 1)));
text("on")
})
.otherwise(|| text("off")),
)
.child(text("after"))
}
}
fn order(c: &Core<FakeEngine>, parent: NodeId) -> Vec<String> {
let key = c.key(parent).unwrap();
c.nodes[key as usize]
.children
.iter()
.filter_map(|&k| {
let node = &c.nodes[k as usize];
node.children
.first()
.map(|&raw| c.nodes[raw as usize].text.clone())
})
.collect()
}
#[test]
fn if_makes_the_branch_it_flips_to_in_place_and_drops_the_other() {
let mut c = core();
c.mount_root(&Root::new::<Toggle>(|| ToggleProps {}));
let batch = c.commit().unwrap();
let host = root_host(&batch);
assert_eq!(order(&c, host), ["before", "off", "after"]);
c.component_event(host, EventKind::Press, &EventPayload::None);
c.commit();
assert_eq!(order(&c, host), ["before", "on", "after"]);
c.component_event(host, EventKind::Press, &EventPayload::None);
c.commit();
assert_eq!(order(&c, host), ["before", "off", "after"]);
assert_eq!(CLEANED.with(Cell::get), 1, "the branch's cleanup ran");
}
#[derive(Default)]
struct Flicker;
#[derive(Clone, PartialEq, Props)]
struct FlickerProps {}
impl Component for Flicker {
type Props = FlickerProps;
type Event = NoEvent;
fn view(_: FlickerProps, _: Scope<NoEvent>) -> impl IntoElement {
let on = signal(false);
view()
.on_press(move |_: &()| {
on.set(true);
on.set(false);
})
.child(
when(on, || Counter::element(CounterProps { label: "On".into() })).otherwise(
|| {
Counter::element(CounterProps {
label: "Off".into(),
})
},
),
)
}
}
#[test]
fn a_region_run_replaced_before_it_applied_is_never_applied() {
let mut c = core();
c.mount_root(&Root::new::<Flicker>(|| FlickerProps {}));
let batch = c.commit().unwrap();
assert_eq!(texts(&batch), ["Off 0", "static"]);
let host = root_host(&batch);
c.component_event(host, EventKind::Press, &EventPayload::None);
let texts = c.commit().map(|batch| texts(&batch)).unwrap_or_default();
assert!(!texts.iter().any(|t| t.starts_with("On")), "{texts:?}");
}
#[derive(Default)]
struct Rows;
#[derive(Clone, PartialEq, Props)]
struct RowsProps {}
thread_local! {
static MADE: Cell<u32> = const { Cell::new(0) };
static ITEMS: Cell<Option<Signal<Vec<u32>>>> = const { Cell::new(None) };
}
impl Component for Rows {
type Props = RowsProps;
type Event = NoEvent;
fn view(_: RowsProps, _: Scope<NoEvent>) -> impl IntoElement {
let items = signal(vec![1, 2, 3]);
ITEMS.with(|i| i.set(Some(items)));
view().child(each(
items,
|n| *n,
|item, index| {
MADE.with(|m| m.set(m.get() + 1));
text(move || format!("{}@{}", item.get(), index.get()))
},
))
}
}
#[test]
fn each_keeps_the_rows_that_stay_and_makes_only_new_ones() {
let mut c = core();
c.mount_root(&Root::new::<Rows>(|| RowsProps {}));
let batch = c.commit().unwrap();
let list = root_host(&batch);
assert_eq!(order(&c, list), ["1@0", "2@1", "3@2"]);
let one = text_node(&batch, "1@0");
let items = ITEMS.with(Cell::get).unwrap();
{
let _entered = c.components.runtime.enter();
items.set(vec![3, 1, 4]);
}
let batch = c.commit().unwrap();
assert_eq!(order(&c, list), ["3@0", "1@1", "4@2"]);
assert_eq!(MADE.with(Cell::get), 4, "one new row");
assert!(
!batch
.items
.iter()
.any(|i| matches!(i, MountItem::Create { id, .. } if *id == one)),
"row 1 kept its node"
);
}
#[component]
fn Clock(_props: (), cx: Scope<NoEvent>) -> impl IntoElement {
let ticks = signal(0);
set_timeout(std::time::Duration::ZERO, move || ticks.set(1));
let _ = cx.insets();
view().child(text(move || {
format!(
"{} {} {}",
if dark() { "dark" } else { "light" },
ticks.get(),
tokens().number("radius").unwrap()
)
}))
}
#[test]
fn a_function_component_reads_the_environment_and_its_timers_run() {
let mut c = core();
c.mount_root(&Root::new::<Clock>(|| ()));
let batch = c.commit().unwrap();
let shown = texts(&batch);
assert!(shown[0].starts_with("light 0 "), "{shown:?}");
c.set_environment(crate::ui::Environment {
dark: true,
..Default::default()
});
let batch = c.commit().unwrap();
assert!(
texts(&batch)[0].starts_with("dark 0 "),
"{:?}",
texts(&batch)
);
}
thread_local! {
static INTROS: Cell<u32> = const { Cell::new(0) };
}
#[derive(Clone, PartialEq, Props)]
struct IntroProps {
label: SharedString,
}
#[component]
fn Intro(props: IntroProps) -> impl IntoElement {
INTROS.with(|n| n.set(n.get() + 1));
text(props.label)
}
#[component]
fn Tabs() -> impl IntoElement {
let first = signal(true);
view()
.on_press(move |_: &()| first.update(|f| *f = !*f))
.child(dynamic(move || {
intro().label(if first.get() { "one" } else { "two" })
}))
}
#[test]
fn a_component_a_region_makes_again_runs_again() {
INTROS.with(|n| n.set(0));
let mut c = core();
c.mount_root(&Root::new::<Tabs>(|| ()));
let batch = c.commit().unwrap();
assert_eq!(texts(&batch), ["one"]);
let host = root_host(&batch);
assert!(c.component_event(host, EventKind::Press, &EventPayload::None));
assert_eq!(texts(&c.commit().unwrap()), ["two"]);
assert_eq!(INTROS.with(Cell::get), 2);
}
#[component]
fn Later() -> impl IntoElement {
let shown = signal("soon");
set_timeout(std::time::Duration::from_millis(10), move || {
shown.set("late")
});
text(move || shown.get())
}
#[test]
fn the_core_keeps_its_tasks_timers() {
let mut c = core();
c.mount_root(&Root::new::<Later>(|| ()));
assert_eq!(texts(&c.commit().unwrap()), ["soon"]);
c.run_component_tasks();
let due = c.next_task_timer().expect("a timer waits");
assert!(due > std::time::Instant::now());
std::thread::sleep(due.saturating_duration_since(std::time::Instant::now()));
c.run_component_tasks();
assert_eq!(texts(&c.commit().unwrap()), ["late"]);
assert_eq!(c.next_task_timer(), None);
}
#[component]
fn Framed() -> impl IntoElement {
let on = signal(false);
view()
.size_10()
.on_press(move |_: &()| on.set(true))
.style_when(on, |s| s.border_1().border_color(rgb(0xff0000)))
}
#[test]
fn a_bound_style_layer_draws_its_border() {
let mut c = core();
c.mount_root(&Root::new::<Framed>(|| ()));
let host = root_host(&c.commit().unwrap());
assert!(c.component_event(host, EventKind::Press, &EventPayload::None));
let batch = c.commit().unwrap();
let style = batch.items.iter().find_map(|i| match i {
MountItem::ViewStyle { style, .. } if style.borders.widths.top > Points::ZERO => {
Some(style.clone())
}
_ => None,
});
assert!(style.is_some(), "{:#?}", batch.items);
}
#[component]
fn Track() -> impl IntoElement {
let on = animated_value(0.0);
let off_color = AdaptiveColor {
light: rgb(0xeeeeee).into(),
dark: rgb(0x222222).into(),
};
view().size_10().bind_background(&on.interpolate_colors(
&[0.0, 1.0],
&[off_color, AdaptiveColor::from(rgb(0x00ff00))],
))
}
#[test]
fn interpolated_theme_colors_follow_the_appearance() {
let colors = |batch: &MountBatch| {
batch.items.iter().find_map(|i| match i {
MountItem::Animation(
crate::animation::AnimationOp::CreateInterpolate { output, .. }
| crate::animation::AnimationOp::SetOutput { output, .. },
) => Some(output.clone()),
_ => None,
})
};
let mut c = core();
c.mount_root(&Root::new::<Track>(|| ()));
assert_eq!(
colors(&c.commit().unwrap()),
Some(crate::animation::Output::Colors(vec![
0xeeeeeeff, 0x00ff00ff
]))
);
c.set_environment(crate::ui::Environment {
dark: true,
..Default::default()
});
let batch = c.commit().unwrap();
assert_eq!(
colors(&batch),
Some(crate::animation::Output::Colors(vec![
0x222222ff, 0x00ff00ff
]))
);
assert!(!batch.items.iter().any(|i| matches!(
i,
MountItem::Animation(crate::animation::AnimationOp::CreateInterpolate { .. })
)));
}
#[component]
fn SharedLight() -> impl IntoElement {
let on = animated_value(0.0);
let green = AdaptiveColor::from(rgb(0x00ff00));
let track = |dark| {
let off = AdaptiveColor {
light: rgb(0x000000).into(),
dark,
};
view()
.size_10()
.bind_background(&on.interpolate_colors(&[0.0, 1.0], &[off, green]))
};
view()
.child(track(rgb(0xffffff).into()))
.child(track(rgb(0x808080).into()))
.child(track(rgb(0x000000).into()))
}
#[test]
fn color_interpolations_sharing_their_light_colors_are_apart() {
let outputs = |batch: &MountBatch| {
let mut outputs: Vec<_> = batch
.items
.iter()
.filter_map(|i| match i {
MountItem::Animation(
crate::animation::AnimationOp::CreateInterpolate { output, .. }
| crate::animation::AnimationOp::SetOutput { output, .. },
) => Some(output.clone()),
_ => None,
})
.collect();
outputs.sort_by_key(|o| format!("{o:?}"));
outputs
};
let colors = |off: u32| crate::animation::Output::Colors(vec![off, 0x00ff00ff]);
let mut c = core();
c.mount_root(&Root::new::<SharedLight>(|| ()));
assert_eq!(outputs(&c.commit().unwrap()), vec![colors(0x000000ff); 3]);
c.set_environment(crate::ui::Environment {
dark: true,
..Default::default()
});
assert_eq!(
outputs(&c.commit().unwrap()),
[colors(0x808080ff), colors(0xffffffff)]
);
}
#[derive(Default)]
struct Lamp;
#[derive(Clone, PartialEq, Props)]
struct LampProps {}
impl Component for Lamp {
type Props = LampProps;
type Event = NoEvent;
fn view(_: LampProps, _: Scope<NoEvent>) -> impl IntoElement {
let on = signal(false);
view()
.on_press(move |_: &()| on.update(|v| *v = !*v))
.w(px(10.0))
.bg(move || {
if on.get() {
rgb(0xff0000)
} else {
rgb(0x0000ff)
}
})
.style_when(on, |s| s.opacity(0.5))
}
}
#[test]
fn bound_styles_change_alone() {
let mut c = core();
c.mount_root(&Root::new::<Lamp>(|| LampProps {}));
let batch = c.commit().unwrap();
let host = root_host(&batch);
let style = view_style_of(&batch, host).unwrap();
assert_eq!(style.background_color, crate::style::Color(0x0000_ffff));
assert_eq!(style.opacity, 1.0);
c.component_event(host, EventKind::Press, &EventPayload::None);
let batch = c.commit().unwrap();
let style = view_style_of(&batch, host).unwrap();
assert_eq!(style.background_color, crate::style::Color(0xff00_00ff));
assert_eq!(style.opacity, 0.5);
let _ = Rc::new(());
}
#[derive(Default)]
struct Mailbox;
#[derive(Clone, PartialEq, Props)]
struct MailboxProps {
#[prop(default)]
rows: crate::ui::Prop<u32>,
}
impl Component for Mailbox {
type Props = MailboxProps;
type Event = NoEvent;
fn view(props: MailboxProps, _: Scope<NoEvent>) -> impl IntoElement {
let rows = signal(
(0..props.rows.get_untracked())
.map(|i| format!("mail {i}"))
.collect::<Vec<_>>(),
);
view()
.flex_1()
.child(view().h(px(20.0)).label("add").on_press(move |_: &()| {
rows.update(|r| {
let n = r.len();
r.push(format!("new {n}"));
});
}))
.child(
virtual_list(
move || rows.with(Vec::len),
move |at| {
view()
.h(px(40.0))
.on_press(move |_: &()| {
rows.update(|r| {
r.remove(at.index());
});
})
.child(text(move || {
let row = rows.with(|r| r[at.index()].clone());
let last = if at.is_last() { " (last)" } else { "" };
format!("{row}{last}")
}))
},
)
.estimated_item_size(px(40.0))
.keys(move || rows.get())
.flex_1(),
)
}
}
fn mailbox(rows: u32) -> (Core<FakeEngine>, MountBatch) {
let mut c = core();
c.mount_root(&Root::new::<Mailbox>(move || MailboxProps {
rows: rows.into(),
}));
let batch = c.commit().unwrap();
(c, batch)
}
fn pressable(batch: &MountBatch) -> Vec<NodeId> {
batch
.items
.iter()
.filter_map(|i| match i {
MountItem::Events { id, events } if events.contains(EventKind::Press) => Some(*id),
_ => None,
})
.collect()
}
fn creates(batch: &MountBatch) -> usize {
batch
.items
.iter()
.filter(|i| matches!(i, MountItem::Create { .. }))
.count()
}
#[test]
fn a_views_list_splices_and_its_cells_keep_their_rows() {
let (mut c, batch) = mailbox(100);
let shown = texts(&batch);
assert!(shown.contains(&"mail 0".to_owned()), "{shown:?}");
assert!(shown.len() < 100, "virtualized: {}", shown.len());
let row0 = pressable(&batch)[1];
assert!(c.component_event(row0, EventKind::Press, &EventPayload::None));
let batch = c.commit().unwrap();
assert_eq!(texts(&batch), [format!("mail {}", shown.len())]);
assert_eq!(creates(&batch), 0, "{:?}", batch.items);
}
#[test]
fn a_views_list_grows_in_place() {
let (mut c, batch) = mailbox(3);
let mut shown = texts(&batch);
shown.sort();
assert_eq!(shown, ["mail 0", "mail 1", "mail 2 (last)"]);
let add = pressable(&batch)[0];
assert!(c.component_event(add, EventKind::Press, &EventPayload::None));
let batch = c.commit().unwrap();
let mut shown = texts(&batch);
shown.sort();
assert_eq!(shown, ["mail 2", "new 3 (last)"]);
}
#[test]
fn a_views_list_cells_show_other_rows_through_their_bindings() {
let (mut c, batch) = mailbox(1000);
let list = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Create {
id,
kind: crate::ViewKind::Scroll,
} => Some(*id),
_ => None,
})
.expect("a list");
c.set_scroll_offset(list, point(px(0.0), px(10_000.0)), u32::MAX);
c.commit().unwrap();
c.set_scroll_offset(list, point(px(0.0), px(20_000.0)), u32::MAX);
let batch = c.commit().unwrap();
assert_eq!(creates(&batch), 0, "{:?}", batch.items);
let shown = texts(&batch);
assert!(shown.contains(&"mail 500".to_owned()), "{shown:?}");
}
#[test]
fn a_views_list_drops_the_rows_that_went_past_its_end() {
let (mut c, batch) = mailbox(3);
let text = text_node(&batch, "mail 2 (last)");
let last = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Insert { parent, child, .. } if *child == text => Some(*parent),
_ => None,
})
.expect("the last row");
assert!(c.component_event(last, EventKind::Press, &EventPayload::None));
let batch = c.commit().unwrap();
assert_eq!(texts(&batch), ["mail 1 (last)"]);
assert!(
batch
.items
.iter()
.any(|i| matches!(i, MountItem::Delete { .. })),
"the gone row's nodes: {:?}",
batch.items
);
}
#[derive(Default)]
struct Padded;
#[derive(Clone, PartialEq, Props)]
struct PaddedProps {}
impl Component for Padded {
type Props = PaddedProps;
type Event = NoEvent;
fn view(_: PaddedProps, _: Scope<NoEvent>) -> impl IntoElement {
let padded = signal(false);
view()
.flex_1()
.on_press(move |_: &()| padded.update(|p| *p = !*p))
.child(
virtual_list(3, |at| text(format!("row {}", at.index())).h(px(40.0)))
.content_padding_top(move || if padded.get() { 10.0_f32 } else { 0.0 })
.flex_1(),
)
}
}
#[test]
fn a_views_list_layout_is_a_binding() {
let mut c = core();
c.mount_root(&Root::new::<Padded>(|| PaddedProps {}));
let batch = c.commit().unwrap();
let host = root_host(&batch);
assert!(c.component_event(host, EventKind::Press, &EventPayload::None));
let batch = c.commit().unwrap();
assert_eq!(creates(&batch), 0, "{:?}", batch.items);
let moved: Vec<f32> = batch
.items
.iter()
.filter_map(|i| match i {
MountItem::Frame { frame, .. } => Some(frame.origin.y.0),
_ => None,
})
.collect();
assert!(moved.contains(&10.0), "{:?}", batch.items);
}
thread_local! {
static FRESH: Cell<u32> = const { Cell::new(0) };
}
#[derive(Default)]
struct Fresh;
#[derive(Clone, PartialEq, Props)]
struct FreshProps {}
impl Component for Fresh {
type Props = FreshProps;
type Event = NoEvent;
fn view(_: FreshProps, _: Scope<NoEvent>) -> impl IntoElement {
let n = FRESH.with(|f| {
f.set(f.get() + 1);
f.get()
});
view().child(text(format!("fresh {n}")))
}
}
#[derive(Default)]
struct Pocket;
#[derive(Clone, PartialEq, Props)]
struct PocketProps {
#[prop(default)]
open: crate::ui::Prop<bool>,
#[prop(lazy)]
children: Children,
}
impl Component for Pocket {
type Props = PocketProps;
type Event = NoEvent;
fn view(props: PocketProps, _: Scope<NoEvent>) -> impl IntoElement {
let children = props.children;
view().child(when(props.open, move || view().p_2().child(children)))
}
}
#[derive(Default)]
struct Holder;
#[derive(Clone, PartialEq, Props)]
struct HolderProps {
children: Children,
}
impl Component for Holder {
type Props = HolderProps;
type Event = NoEvent;
fn view(props: HolderProps, _: Scope<NoEvent>) -> impl IntoElement {
view().child(props.children)
}
}
#[component]
fn Label() -> impl IntoElement {
text("label")
}
#[component]
fn Shutter() -> impl IntoElement {
let open = signal(true);
view().on_press(move || open.set(false)).child(
Holder::element(HolderProps {
children: Children::default(),
})
.child(when(open, || {
vec![
label().into_element(),
text("plain").into_element(),
Holder::element(HolderProps {
children: Children::default(),
})
.child(text("inside"))
.into_element(),
]
})),
)
}
#[test]
fn content_leaving_a_nested_component_is_only_deleted() {
let mut c = core();
c.mount_root(&Root::new::<Shutter>(|| ()));
let batch = c.commit().unwrap();
let host = root_host(&batch);
assert!(c.component_event(host, EventKind::Press, &EventPayload::None));
let batch = c.commit().unwrap();
let count = |f: fn(&MountItem) -> bool| batch.items.iter().filter(|i| f(i)).count();
let removes = count(|i| matches!(i, MountItem::Remove { .. }));
let deletes = count(|i| matches!(i, MountItem::Delete { .. }));
assert_eq!((removes, deletes), (0, 4), "{:?}", batch.items);
let holder = c
.components
.instances
.values()
.find(|i| !i.js_children.is_empty());
assert!(holder.is_none(), "no holder keeps what left it");
}
#[derive(Default)]
struct Forward;
#[derive(Clone, PartialEq, Props)]
struct ForwardProps {
children: Children,
}
impl Component for Forward {
type Props = ForwardProps;
type Event = NoEvent;
fn view(props: ForwardProps, _: Scope<NoEvent>) -> impl IntoElement {
let shown = signal(true);
let children = props.children;
view()
.on_press(move || shown.update(|s| *s = !*s))
.child(when(shown, move || {
Holder::element(HolderProps {
children: Children::default(),
})
.child(text("first"))
.child(children)
}))
}
}
#[derive(Default)]
struct Relay;
#[derive(Clone, PartialEq, Props)]
struct RelayProps {}
impl Component for Relay {
type Props = RelayProps;
type Event = NoEvent;
fn view(_: RelayProps, _: Scope<NoEvent>) -> impl IntoElement {
let middle = signal(false);
view().on_press(move || middle.update(|m| *m = !*m)).child(
Forward::element(ForwardProps {
children: Children::default(),
})
.child(text("a"))
.child(when(middle, || text("b")))
.child(text("c")),
)
}
}
fn native_children<M: TextEngine>(c: &Core<M>, id: NodeId) -> Vec<NodeId> {
let key = c.key(id).expect("a node");
c.nodes[key as usize]
.children
.iter()
.map(|&k| c.nodes[k as usize].id)
.collect()
}
#[test]
fn children_passed_on_to_a_nested_component_are_placed_by_it() {
let mut c = core();
c.mount_root(&Root::new::<Relay>(|| RelayProps {}));
let batch = c.commit().unwrap();
let relay = root_host(&batch);
let [first, a, cc] = ["first", "a", "c"].map(|t| text_node(&batch, t));
let holder = native_parent(&c, a).expect("placed");
assert_eq!(native_children(&c, holder), [first, a, cc]);
let forward = native_parent(&c, holder).expect("in the forward");
assert_eq!(native_parent(&c, forward), Some(relay));
assert!(c.component_event(relay, EventKind::Press, &EventPayload::None));
let batch = c.commit().unwrap();
let b = text_node(&batch, "b");
assert_eq!(native_children(&c, holder), [first, a, b, cc]);
assert!(c.component_event(forward, EventKind::Press, &EventPayload::None));
let batch = c.commit().unwrap();
for id in [a, b, cc] {
assert_eq!(native_parent(&c, id), None);
assert!(
batch
.items
.iter()
.any(|i| matches!(i, MountItem::Remove { id: r } if *r == id)),
"{id} is removed: {:?}",
batch.items
);
assert!(
!batch
.items
.iter()
.any(|i| matches!(i, MountItem::Delete { id: d } if *d == id)),
"{id} stays"
);
}
assert!(c.component_event(forward, EventKind::Press, &EventPayload::None));
let batch = c.commit().unwrap();
let holder = native_parent(&c, a).expect("placed again");
let first = text_node(&batch, "first");
assert_eq!(native_children(&c, holder), [first, a, b, cc]);
assert!(c.component_event(relay, EventKind::Press, &EventPayload::None));
c.commit();
assert_eq!(native_children(&c, holder), [first, a, cc]);
}
#[test]
fn content_passed_on_two_deep_leaves_every_level() {
let mut c = core();
c.mount_root(&Root::new::<Relay>(|| RelayProps {}));
let batch = c.commit().unwrap();
let relay = root_host(&batch);
assert!(c.component_event(relay, EventKind::Press, &EventPayload::None));
let b = text_node(&c.commit().unwrap(), "b");
let holding = |c: &Core<FakeEngine>| {
c.components
.instances
.iter()
.filter(|(_, i)| i.js_children.contains(&b))
.count()
};
assert_eq!(holding(&c), 2, "the forward's and the holder's");
assert!(c.component_event(relay, EventKind::Press, &EventPayload::None));
c.commit().unwrap();
assert!(c.key(b).is_err(), "released");
assert_eq!(holding(&c), 0, "no component keeps it");
}
#[test]
fn js_children_through_a_forward() {
let mut c = core();
let mut registry = Registry::new();
registry.register::<Forward>();
c.set_components(registry);
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetProp {
id: 2,
prop: Prop::Component(std::any::type_name::<Forward>().into()),
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
Op::Create {
id: 6,
tag: Tag::View,
},
Op::Insert {
parent: 2,
child: 6,
before: None,
},
],
);
let batch = c.commit().unwrap();
let first = text_node(&batch, "first");
let holder = native_parent(&c, 6).expect("placed");
assert_eq!(native_children(&c, holder), [first, 6]);
assert!(c.component_event(2, EventKind::Press, &EventPayload::None));
let batch = c.commit().unwrap();
assert_eq!(native_parent(&c, 6), None, "{:?}", batch.items);
assert!(
batch
.items
.iter()
.any(|i| matches!(i, MountItem::Remove { id: 6 })),
"{:?}",
batch.items
);
assert!(
!batch
.items
.iter()
.any(|i| matches!(i, MountItem::Delete { id: 6 }))
);
apply(
&mut c,
vec![
Op::Create {
id: 8,
tag: Tag::View,
},
Op::Insert {
parent: 2,
child: 8,
before: None,
},
Op::Remove {
parent: 2,
child: 6,
},
],
);
let batch = c.commit();
assert_eq!(
native_parent(&c, 8),
None,
"hidden: {:?}",
batch.map(|b| b.items)
);
assert_eq!(native_parent(&c, 6), None);
assert!(c.component_event(2, EventKind::Press, &EventPayload::None));
let batch = c.commit().unwrap();
let first = text_node(&batch, "first");
let holder = native_parent(&c, 8).expect("placed again");
assert_eq!(native_children(&c, holder), [first, 8], "{:?}", batch.items);
apply(
&mut c,
vec![Op::Insert {
parent: 2,
child: 6,
before: Some(8),
}],
);
c.commit();
assert_eq!(native_children(&c, holder), [first, 6, 8]);
apply(
&mut c,
vec![Op::Insert {
parent: 2,
child: 8,
before: Some(6),
}],
);
c.commit();
assert_eq!(native_children(&c, holder), [first, 8, 6]);
apply(
&mut c,
vec![
Op::Remove {
parent: ROOT_ID,
child: 2,
},
Op::Release { id: 2 },
],
);
let batch = c.commit().unwrap();
for id in [6, 8] {
assert!(c.key(id).is_ok());
assert_eq!(native_parent(&c, id), None, "{id}: {:?}", batch.items);
assert!(
!batch
.items
.iter()
.any(|i| matches!(i, MountItem::Delete { id: d } if *d == id))
);
}
assert!(c.components.instances.is_empty());
apply(
&mut c,
vec![Op::Insert {
parent: ROOT_ID,
child: 6,
before: None,
}],
);
c.commit();
assert_eq!(native_parent(&c, 6), Some(ROOT_ID));
}
#[derive(Default)]
struct Shy;
#[derive(Clone, PartialEq, Props)]
struct ShyProps {
children: crate::ui::Prop<Children>,
}
impl Component for Shy {
type Props = ShyProps;
type Event = NoEvent;
fn view(props: ShyProps, _: Scope<NoEvent>) -> impl IntoElement {
let shown = signal(true);
let children = props.children;
view()
.on_press(move || shown.update(|s| *s = !*s))
.child(text(move || format!("empty {}", children.get().is_empty())))
.child(when(shown, move || view().child(children.get_untracked())))
}
}
#[component]
fn PassOn(props: HolderProps) -> impl IntoElement {
view().child(
Shy::element(ShyProps {
children: Default::default(),
})
.child(props.children),
)
}
#[component]
fn Giver() -> impl IntoElement {
let on = signal(true);
view()
.on_press(move || on.update(|o| *o = !*o))
.child(pass_on().child(when(on, || text("given"))))
}
#[test]
fn a_deep_component_knows_when_its_only_child_left() {
let mut c = core();
c.mount_root(&Root::new::<Giver>(|| ()));
let batch = c.commit().unwrap();
let giver = root_host(&batch);
let shy = native_parent(&c, text_node(&batch, "empty false")).unwrap();
assert!(c.component_event(giver, EventKind::Press, &EventPayload::None));
assert!(texts(&c.commit().unwrap()).contains(&"empty true".to_owned()));
for _ in 0..2 {
assert!(c.component_event(shy, EventKind::Press, &EventPayload::None));
let _ = c.commit();
}
let dead = c
.components
.instances
.values()
.flat_map(|i| &i.js_children)
.filter(|&&id| c.key(id).is_err())
.count();
assert_eq!(dead, 0, "no released child");
}
thread_local! {
static PLAQUE_TAPS: Cell<u32> = const { Cell::new(0) };
}
#[derive(Default)]
struct Plaque;
#[derive(Clone, PartialEq, Props)]
struct PlaqueProps {}
impl Component for Plaque {
type Props = PlaqueProps;
type Event = NoEvent;
fn view(_: PlaqueProps, _: Scope<NoEvent>) -> impl IntoElement {
Counter::element(CounterProps {
label: "Plaque".into(),
})
.opacity(0.5)
.on_event(|e: &CounterEvent| {
let CounterEvent::Tap(n) = e;
PLAQUE_TAPS.with(|t| t.set(*n));
})
}
}
#[derive(Default)]
struct Shelf;
#[derive(Clone, PartialEq, Props)]
struct ShelfProps {}
impl Component for Shelf {
type Props = ShelfProps;
type Event = NoEvent;
fn view(_: ShelfProps, _: Scope<NoEvent>) -> impl IntoElement {
Plaque::element(PlaqueProps {}).bg(rgb(0xff0000))
}
}
#[test]
fn a_component_rooted_in_another_shares_its_host() {
let mut c = core();
c.mount_root(&Root::new::<Counter>(|| CounterProps {
label: "Plaque".into(),
}));
let alone = creates(&c.commit().unwrap());
for deep in [false, true] {
let mut c = core();
if deep {
c.mount_root(&Root::new::<Shelf>(|| ShelfProps {}));
} else {
c.mount_root(&Root::new::<Plaque>(|| PlaqueProps {}));
}
let batch = c.commit().unwrap();
assert_eq!(creates(&batch), alone, "{:?}", batch.items);
let host = root_host(&batch);
let style = view_style_of(&batch, host).expect("styled");
assert_eq!(style.opacity, 0.5, "the styles the view gives it");
if deep {
assert_ne!(style.background_color, crate::Color::TRANSPARENT);
}
assert_eq!(texts(&batch), ["Plaque 0", "static"]);
let text = text_node(&batch, "Plaque 0");
assert_eq!(native_parent(&c, text), Some(host));
assert!(c.component_event(host, EventKind::Press, &EventPayload::None));
let batch = c.commit().unwrap();
assert_eq!(texts(&batch), ["Plaque 1"]);
assert_eq!(PLAQUE_TAPS.with(Cell::get), 1);
PLAQUE_TAPS.with(|t| t.set(0));
assert!(
batch.items.iter().all(|i| match i {
MountItem::Insert { parent, child, .. } =>
c.key(*parent).is_ok() && c.key(*child).is_ok(),
_ => true,
}),
"only nodes' own ids"
);
}
}
#[derive(Default)]
struct Key;
#[derive(Clone, PartialEq, Props)]
struct KeyProps {
#[prop(default)]
selected: crate::ui::Prop<bool>,
}
impl Component for Key {
type Props = KeyProps;
type Event = NoEvent;
fn view(props: KeyProps, _: Scope<NoEvent>) -> impl IntoElement {
let selected = props.selected;
view()
.size_10()
.accessibility_state(move || crate::AccessibilityState {
selected: selected.get(),
..crate::AccessibilityState::default()
})
.on_press(|| {})
}
}
#[derive(Default)]
struct Keys;
#[derive(Clone, PartialEq, Props)]
struct KeysProps {}
impl Component for Keys {
type Props = KeysProps;
type Event = NoEvent;
fn view(_: KeysProps, _: Scope<NoEvent>) -> impl IntoElement {
let key = |selected: bool| {
Key::element(KeyProps {
selected: selected.into(),
})
.bg(rgb(0x0000ff))
.selected(|s| s.bg(rgb(0x00ff00)))
.hover(|s| s.opacity(0.5))
.active(|s| s.bg(rgb(0xff0000)))
};
view().flex_row().child(key(false)).child(key(true))
}
}
#[test]
fn a_nested_components_state_styles_follow_its_roots_states() {
use crate::tailwind::state::{HOVERED, PRESSED};
let mut c = core();
c.mount_root(&Root::new::<Keys>(|| KeysProps {}));
let batch = c.commit().unwrap();
let tracked = |id: NodeId| {
batch.items.iter().any(|i| {
matches!(i, MountItem::TrackStates { id: v, states }
if *v == id && states & (PRESSED | HOVERED) == PRESSED | HOVERED)
})
};
let roots: Vec<NodeId> = batch
.items
.iter()
.filter_map(|i| match i {
MountItem::TrackStates { id, .. } if tracked(*id) => Some(*id),
_ => None,
})
.collect();
assert_eq!(roots.len(), 2, "{:?}", batch.items);
let is_selected = |id: NodeId| {
batch.items.iter().any(|i| {
matches!(i, MountItem::ViewProps { id: v, props }
if *v == id && props.accessibility.state.selected)
})
};
let (selected, plain) = match is_selected(roots[0]) {
true => (roots[0], roots[1]),
false => (roots[1], roots[0]),
};
let background =
|batch: &MountBatch, id| view_style_of(batch, id).map(|s| s.background_color);
assert_eq!(background(&batch, plain), Some(crate::Color(0x0000ffff)));
assert_eq!(
background(&batch, selected),
Some(crate::Color(0x00ff00ff)),
"selected:"
);
c.set_states(plain, PRESSED | HOVERED);
let batch = c.commit().unwrap();
let style = view_style_of(&batch, plain).expect("restyled");
assert_eq!(style.background_color, crate::Color(0xff0000ff), "active:");
assert_eq!(style.opacity, 0.5, "hover:");
c.set_states(plain, 0);
let batch = c.commit().unwrap();
assert_eq!(background(&batch, plain), Some(crate::Color(0x0000ffff)));
}
#[derive(Default)]
struct Page;
#[derive(Clone, PartialEq, Props)]
struct PageProps {}
impl Component for Page {
type Props = PageProps;
type Event = NoEvent;
#[allow(deprecated)]
fn view(_: PageProps, _: Scope<NoEvent>) -> impl IntoElement {
stack().flex_1().child(
screen()
.title("Page")
.header_style_with(|s| {
s.bg(AdaptiveColor::rgb(0xffffff, 0x000000))
.text_color(rgb(0x00ff00))
.tint_color(rgb(0x0000ff))
})
.child(view().flex_1()),
)
}
}
#[test]
fn typed_header_styles_reach_the_screens_config() {
let mut c = core();
c.mount_root(&Root::new::<Page>(|| PageProps {}));
let config = |batch: &MountBatch| {
batch.items.iter().find_map(|i| match i {
MountItem::ScreenConfig { config, .. } => Some((**config).clone()),
_ => None,
})
};
let first = config(&c.commit().unwrap()).expect("a screen");
assert_eq!(first.background_color, Some(crate::Color(0xffffffff)));
assert_eq!(first.title_color, Some(crate::Color(0x00ff00ff)));
assert_eq!(first.tint_color, Some(crate::Color(0x0000ffff)));
c.set_class_environment(crate::tailwind::ClassEnvironment {
dark: true,
..Default::default()
});
let dark = config(&c.commit().unwrap()).expect("restyled");
assert_eq!(dark.background_color, Some(crate::Color(0x000000ff)));
}
#[derive(Default)]
struct Tray;
#[derive(Clone, PartialEq, Props)]
struct TrayProps {
children: Children,
}
impl Component for Tray {
type Props = TrayProps;
type Event = NoEvent;
fn view(props: TrayProps, _: Scope<NoEvent>) -> impl IntoElement {
Holder::element(HolderProps {
children: Children::default(),
})
.opacity(0.5)
.child(props.children)
}
}
#[test]
fn a_js_host_of_a_rooted_component_takes_the_apps_styles_events_and_children() {
let mut c = core();
let mut registry = Registry::new();
registry.register::<Tray>();
registry.register::<Plaque>();
c.set_components(registry);
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetProp {
id: 2,
prop: Prop::Component(std::any::type_name::<Plaque>().into()),
},
Op::SetProp {
id: 2,
prop: Prop::Class("opacity-25".into()),
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
let batch = c.commit().unwrap();
assert_eq!(view_style_of(&batch, 2).map(|s| s.opacity), Some(0.25));
apply(
&mut c,
vec![
Op::Listen {
id: 2,
event: EventKind::Press,
enabled: true,
},
Op::Listen {
id: 2,
event: EventKind::Press,
enabled: false,
},
],
);
c.commit();
let key = c.key(2).unwrap();
assert!(c.nodes[key as usize].events.contains(EventKind::Press));
c.apply(Op::SetProp {
id: 2,
prop: Prop::Class("opacity-75".into()),
})
.unwrap();
let batch = c.commit().unwrap();
assert_eq!(view_style_of(&batch, 2).map(|s| s.opacity), Some(0.75));
apply(
&mut c,
vec![
Op::Create {
id: 4,
tag: Tag::View,
},
Op::SetProp {
id: 4,
prop: Prop::Component(std::any::type_name::<Tray>().into()),
},
Op::Insert {
parent: ROOT_ID,
child: 4,
before: None,
},
Op::Create {
id: 6,
tag: Tag::View,
},
Op::Insert {
parent: 4,
child: 6,
before: None,
},
],
);
let batch = c.commit().unwrap();
assert_eq!(native_parent(&c, 6), Some(4));
assert_eq!(creates(&batch), 2, "{:?}", batch.items);
apply(
&mut c,
vec![
Op::Remove {
parent: ROOT_ID,
child: 4,
},
Op::Release { id: 4 },
Op::Release { id: 2 },
],
);
let batch = c.commit().unwrap();
assert!(c.key(6).is_ok());
assert_eq!(native_parent(&c, 6), None);
assert!(c.components.instances.is_empty());
let deleted: Vec<NodeId> = batch
.items
.iter()
.filter_map(|i| match i {
MountItem::Delete { id } => Some(*id),
_ => None,
})
.collect();
assert!(deleted.contains(&2) && deleted.contains(&4), "{deleted:?}");
assert_eq!(c.nodes.len(), 2, "the root and JS's child are left");
}
#[derive(Default)]
struct Corner;
#[derive(Clone, PartialEq, Props)]
struct CornerProps {
#[prop(default)]
framed: bool,
corner: crate::ui::Prop<Option<Slot>>,
}
impl Component for Corner {
type Props = CornerProps;
type Event = NoEvent;
fn view(props: CornerProps, _: Scope<NoEvent>) -> impl IntoElement {
let framed = props.framed;
view().child(text("title")).child(
props
.corner
.place(move |c| if framed { c.p_2() } else { c }),
)
}
}
#[derive(Default)]
struct Corners;
#[derive(Clone, PartialEq, Props)]
struct CornersProps {}
impl Component for Corners {
type Props = CornersProps;
type Event = NoEvent;
fn view(_: CornersProps, _: Scope<NoEvent>) -> impl IntoElement {
let n = signal(0);
view()
.on_press(move || n.update(|n| *n += 1))
.child(Corner::element(CornerProps::default()).fill(
"corner",
Counter::element(CounterProps {
label: "Aside".into(),
}),
))
.child(dynamic(move || {
let fill = if n.get() % 2 == 0 {
text("even").into_element()
} else {
view().child(text("odd")).into_element()
};
Corner::element(CornerProps::default()).fill("corner", fill)
}))
.child(Corner::element(CornerProps::default()).fill(
"corner",
dynamic(move || (n.get() == 1).then(|| text("one"))),
))
.child(
Corner::element(CornerProps {
framed: true,
..Default::default()
})
.fill("corner", text("framed")),
)
}
}
#[test]
fn a_slot_a_view_fills_is_placed_as_it_is() {
let mut c = core();
c.mount_root(&Root::new::<Corners>(|| CornersProps {}));
let batch = c.commit().unwrap();
let root = root_host(&batch);
let corners = native_children(&c, root);
assert_eq!(corners.len(), 4);
assert_eq!(native_children(&c, corners[2]).len(), 1);
let counted = native_parent(&c, text_node(&batch, "Aside 0")).expect("placed");
assert_eq!(native_parent(&c, counted), Some(corners[0]));
assert_eq!(native_children(&c, corners[0]).len(), 2);
assert_eq!(
native_parent(&c, text_node(&batch, "even")),
Some(corners[1])
);
let framed = native_parent(&c, text_node(&batch, "framed")).expect("placed");
assert_ne!(framed, corners[3]);
assert_eq!(native_parent(&c, framed), Some(corners[3]));
assert!(c.component_event(counted, EventKind::Press, &EventPayload::None));
assert_eq!(texts(&c.commit().unwrap()), ["Aside 1"]);
assert!(c.component_event(root, EventKind::Press, &EventPayload::None));
let batch = c.commit().unwrap();
let odd = native_parent(&c, text_node(&batch, "odd")).expect("placed");
let corners = native_children(&c, root);
assert_eq!(native_parent(&c, odd), Some(corners[1]));
assert_eq!(native_children(&c, corners[1]).len(), 2);
let one = text_node(&batch, "one");
assert_eq!(native_parent(&c, one), Some(corners[2]));
assert!(c.component_event(root, EventKind::Press, &EventPayload::None));
let batch = c.commit().unwrap();
assert!(c.key(one).is_err(), "released");
assert!(
!batch
.items
.iter()
.any(|i| matches!(i, MountItem::Remove { .. })),
"{:?}",
batch.items
);
let corners = native_children(&c, root);
assert_eq!(native_children(&c, corners[2]).len(), 1, "the title alone");
let even = text_node(&batch, "even");
assert_eq!(native_parent(&c, even), Some(corners[1]));
assert_eq!(native_children(&c, corners[1]).len(), 2);
}
#[derive(Clone, PartialEq, Props)]
struct MidProps {
corner: crate::ui::Prop<Option<Slot>>,
}
#[component]
fn Mid(props: MidProps) -> impl IntoElement {
view().child(
Corner::element(CornerProps::default()).fill("corner", props.corner.place(|c| c)),
)
}
#[component]
fn Top() -> impl IntoElement {
let n = signal(0);
view()
.on_press(move || n.update(|n| *n += 1))
.child(mid().corner(text("deep")))
.child(mid().corner(dynamic(move || match n.get() % 3 {
0 => Some(text("switched").into_element()),
1 => None,
_ => Some(view().child(text("boxed")).into_element()),
})))
}
#[test]
fn a_filled_slot_passed_on_to_another_slot_is_placed_there() {
let mut c = core();
c.mount_root(&Root::new::<Top>(|| ()));
let batch = c.commit().unwrap();
let top = root_host(&batch);
let mids = native_children(&c, top);
let [first, second] = [0, 1].map(|i| native_children(&c, mids[i])[0]);
let deep = text_node(&batch, "deep");
assert_eq!(native_parent(&c, deep), Some(first), "{:?}", batch.items);
let switched = text_node(&batch, "switched");
assert_eq!(native_parent(&c, switched), Some(second));
assert!(c.component_event(top, EventKind::Press, &EventPayload::None));
c.commit().unwrap();
assert_eq!(native_children(&c, second).len(), 1, "the title alone");
assert!(c.component_event(top, EventKind::Press, &EventPayload::None));
let batch = c.commit().unwrap();
let boxed = native_parent(&c, text_node(&batch, "boxed")).expect("placed");
assert_eq!(native_parent(&c, boxed), Some(second), "{:?}", batch.items);
}
#[component]
fn RootedCorner() -> impl IntoElement {
let on = signal(true);
let flip = move || on.update(|o| *o = !*o);
Corner::element(CornerProps::default()).fill(
"corner",
dynamic(move || {
if on.get() {
text("x").on_press(flip).into_element()
} else {
view().on_press(flip).child(text("y")).into_element()
}
}),
)
}
#[test]
fn a_component_rooted_in_another_fills_its_slot() {
let mut c = core();
c.mount_root(&Root::new::<RootedCorner>(|| ()));
let batch = c.commit().unwrap();
let host = root_host(&batch);
assert_eq!(native_parent(&c, text_node(&batch, "x")), Some(host));
assert_eq!(native_children(&c, host).len(), 2);
for round in 0..4 {
let fill = native_children(&c, host)[1];
assert!(c.component_event(fill, EventKind::Press, &EventPayload::None));
let batch = c.commit().unwrap();
assert_eq!(
native_children(&c, host).len(),
2,
"{round}: {:?}",
batch.items
);
}
}
#[component]
fn FilledRows() -> impl IntoElement {
view().flex_1().child(
virtual_list(200, move |at| {
let i = at.index();
view()
.h(px(40.0))
.child(
Corner::element(CornerProps::default())
.fill("corner", text(format!("r{i}"))),
)
.child(
Holder::element(HolderProps {
children: Children::default(),
})
.child(text(format!("h{i}"))),
)
.child(Plaque::element(PlaqueProps {}))
})
.estimated_item_size(px(40.0))
.flex_1(),
)
}
#[test]
fn list_rows_with_filled_slots_follow_their_rows() {
let mut c = core();
c.mount_root(&Root::new::<FilledRows>(|| ()));
let batch = c.commit().unwrap();
let list = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Create {
id,
kind: ViewKind::Scroll,
} => Some(*id),
_ => None,
})
.expect("a list");
let mut shown: std::collections::HashMap<String, NodeId> = Default::default();
let record = |b: &MountBatch, shown: &mut std::collections::HashMap<String, NodeId>| {
for i in &b.items {
if let MountItem::Text { id, text, .. } = i {
shown.retain(|_, v| v != id);
shown.insert(text.text.clone(), *id);
}
}
};
record(&batch, &mut shown);
let mut sizes = Vec::new();
for step in 1..=12u32 {
let y = (step as f32 * 900.0) % 7000.0;
c.set_scroll_offset(list, point(px(0.0), px(y)), u32::MAX);
for _ in 0..3 {
if let Some(b) = c.commit() {
record(&b, &mut shown);
}
}
sizes.push((c.nodes.len(), c.components.instances.len()));
for (t, &id) in &shown {
let Some(n) = t.strip_prefix('r') else {
continue;
};
if c.key(id).is_err() {
continue;
}
let Some(corner) = native_parent(&c, id) else {
continue;
};
let row = native_parent(&c, corner).expect("a corner in a row");
if let Some(&h) = shown.get(&format!("h{n}"))
&& c.key(h).is_ok()
{
let holder = native_parent(&c, h).unwrap();
assert_eq!(native_parent(&c, holder), Some(row), "{step}: {t} and h{n}");
}
}
}
assert!(
sizes
.windows(2)
.skip(2)
.all(|w| w[0].0.abs_diff(w[1].0) < 40),
"{sizes:?}"
);
assert_eq!(sizes[3].1, sizes[sizes.len() - 1].1, "{sizes:?}");
}
#[derive(Default)]
struct Lit;
#[derive(Clone, PartialEq, Props)]
struct LitProps {}
impl Component for Lit {
type Props = LitProps;
type Event = NoEvent;
fn view(_: LitProps, _: Scope<NoEvent>) -> impl IntoElement {
text("lit").p_1()
}
}
#[derive(Default)]
struct Lighting;
#[derive(Clone, PartialEq, Props)]
struct LightingProps {}
impl Component for Lighting {
type Props = LightingProps;
type Event = NoEvent;
fn view(_: LightingProps, _: Scope<NoEvent>) -> impl IntoElement {
let v = animated_value(0.5);
let wide = signal(false);
let red = crate::ui::Style::default().with(
crate::StyleKey::BackgroundColor,
crate::PatchValue::Str("#ff0000".into()),
);
view()
.on_press(move || wide.update(|w| *w = !*w))
.child(
Lit::element(LitProps {})
.bind_opacity(&v)
.inline_style(red.clone())
.inset_padding(move || {
EdgeInsets::all(px(if wide.get() { 8.0 } else { 4.0 }))
}),
)
.child(Plaque::element(PlaqueProps {}).inline_style(red))
}
}
#[test]
fn what_a_view_gives_a_component_element_goes_on_its_root() {
let mut c = core();
c.mount_root(&Root::new::<Lighting>(|| LightingProps {}));
let batch = c.commit().unwrap();
let root = root_host(&batch);
let lit = text_node(&batch, "lit");
assert!(
batch
.items
.iter()
.any(|i| matches!(i, MountItem::AnimationBindings { id, .. } if *id == lit)),
"the opacity is bound on the text: {:?}",
batch.items
);
let red = crate::Color::rgba(0xff, 0, 0, 0xff);
assert_eq!(
view_style_of(&batch, lit).map(|s| s.background_color),
Some(red)
);
let insets = |c: &Core<FakeEngine>, id| {
let key = c.key(id).unwrap();
c.nodes[key as usize]
.insets
.as_deref()
.copied()
.unwrap_or_default()[0]
};
assert_eq!(insets(&c, lit), EdgeInsets::all(px(4.0)));
let plaque = native_children(&c, root)[1];
assert_eq!(
view_style_of(&batch, plaque).map(|s| s.background_color),
Some(red)
);
assert!(c.component_event(root, EventKind::Press, &EventPayload::None));
c.commit();
assert_eq!(insets(&c, lit), EdgeInsets::all(px(8.0)));
}
#[derive(Default)]
struct Dial;
#[derive(Clone, Copy)]
struct DialApi {
at: crate::ui::Signal<i32>,
}
#[crate::ui::methods]
impl DialApi {
pub(crate) fn at(&self) -> i32 {
self.at.get_untracked()
}
}
impl Component for Dial {
type Props = ();
type Event = NoEvent;
const METHODS: crate::ui::Methods = crate::ui::Methods::of::<DialApi>();
fn view(_: (), cx: Scope<NoEvent>) -> impl IntoElement {
let at = signal(7);
cx.methods(DialApi { at });
text(move || format!("at {}", at.get()))
}
}
thread_local! {
static DIAL: Cell<Option<crate::ui::ComponentRef<Dial>>> = const { Cell::new(None) };
}
#[derive(Clone, PartialEq, Props)]
struct DialPanelProps {
rooted: bool,
}
#[component]
fn DialPanel(props: DialPanelProps) -> impl IntoElement {
let handle = component_ref::<Dial>();
DIAL.with(|d| d.set(Some(handle)));
let el = Dial::element(()).component_ref(&handle);
if props.rooted {
el.into_element()
} else {
view().child(el).into_element()
}
}
#[test]
fn the_api_of_a_component_rooted_in_another_is_its_refs() {
for rooted in [false, true] {
let mut c = core();
c.mount_root(&Root::new::<DialPanel>(move || DialPanelProps { rooted }));
let _ = c.commit().unwrap();
let dial = DIAL.with(Cell::get).unwrap();
let _entered = c.components.runtime.enter();
let at = dial.api::<DialApi>().map(|api| api.at());
assert_eq!(at, Some(7), "rooted: {rooted}");
}
}
#[derive(Default)]
struct Wardrobe;
#[derive(Clone, PartialEq, Props)]
struct WardrobeProps {}
impl Component for Wardrobe {
type Props = WardrobeProps;
type Event = NoEvent;
fn view(_: WardrobeProps, _: Scope<NoEvent>) -> impl IntoElement {
let open = signal(false);
view()
.on_press(move |_: &()| open.update(|o| *o = !*o))
.child(
Pocket::element(PocketProps {
open: open.into(),
children: Children::default(),
})
.child(Fresh::element(FreshProps {})),
)
.child(
Holder::element(HolderProps {
children: Children::default(),
})
.child(text("before"))
.child(when(open, || text("open")))
.child(text("after")),
)
}
}
#[test]
fn a_view_passes_lazy_children_and_regions_to_nested_components() {
let mut c = core();
c.mount_root(&Root::new::<Wardrobe>(|| WardrobeProps {}));
let batch = c.commit().unwrap();
let mut shown = texts(&batch);
shown.sort();
assert_eq!(shown, ["after", "before"]);
assert_eq!(FRESH.with(Cell::get), 0, "closed: not made");
let host = root_host(&batch);
assert!(c.component_event(host, EventKind::Press, &EventPayload::None));
let batch = c.commit().unwrap();
let mut shown = texts(&batch);
shown.sort();
assert_eq!(shown, ["fresh 1", "open"]);
let holder = c
.components
.instances
.values()
.find(|i| i.js_children.len() == 3)
.expect("the holder has three children");
assert_eq!(holder.js_children[1], text_node(&batch, "open"));
assert!(c.component_event(host, EventKind::Press, &EventPayload::None));
c.commit();
assert!(c.component_event(host, EventKind::Press, &EventPayload::None));
let mut shown = texts(&c.commit().unwrap());
shown.sort();
assert_eq!(shown, ["fresh 2", "open"]);
let events = js_events(&mut c);
assert!(events.is_empty(), "nothing for JS: {events:?}");
}
#[derive(Default)]
struct Tally;
#[derive(Clone, PartialEq, Props)]
struct TallyProps {}
impl Component for Tally {
type Props = TallyProps;
type Event = NoEvent;
fn view(_: TallyProps, _: Scope<NoEvent>) -> impl IntoElement {
let n = signal(0);
text(move || format!("tally {}", n.get())).on_press(move |_: &()| n.update(|n| *n += 1))
}
}
#[derive(Default)]
struct Scroller;
#[derive(Clone, PartialEq, Props)]
struct ScrollerProps {}
impl Component for Scroller {
type Props = ScrollerProps;
type Event = NoEvent;
fn view(_: ScrollerProps, _: Scope<NoEvent>) -> impl IntoElement {
scroll().child(text("inside"))
}
}
#[test]
fn a_views_root_can_be_any_element() {
let mut c = core();
c.mount_root(&Root::new::<Tally>(|| TallyProps {}));
let batch = c.commit().unwrap();
let host = root_host(&batch);
assert!(
batch.items.iter().any(|i| matches!(
i,
MountItem::Create { id, kind: crate::ViewKind::Text } if *id == host
)),
"the host is the text: {:?}",
batch.items
);
assert_eq!(
batch
.items
.iter()
.filter(|i| matches!(i, MountItem::Create { id, .. } if *id == host))
.count(),
1,
"made once"
);
assert_eq!(texts(&batch), ["tally 0"]);
assert!(c.component_event(host, EventKind::Press, &EventPayload::None));
assert_eq!(texts(&c.commit().unwrap()), ["tally 1"]);
let mut c = core();
c.mount_root(&Root::new::<Scroller>(|| ScrollerProps {}));
let batch = c.commit().unwrap();
let host = root_host(&batch);
assert!(
batch.items.iter().any(|i| matches!(
i,
MountItem::Create { id, kind: crate::ViewKind::Scroll } if *id == host
)),
"the host is the scroll view: {:?}",
batch.items
);
assert_eq!(texts(&batch), ["inside"]);
}
#[derive(Default)]
struct Dimmer;
#[derive(Clone, PartialEq, Props)]
struct DimmerProps {}
impl Component for Dimmer {
type Props = DimmerProps;
type Event = NoEvent;
fn view(_: DimmerProps, _: Scope<NoEvent>) -> impl IntoElement {
let dim = signal(false);
view()
.on_press(move |_: &()| dim.update(|d| *d = !*d))
.child(
Holder::element(HolderProps {
children: Children::default(),
})
.w(px(10.0))
.opacity(move || if dim.get() { 0.5_f32 } else { 1.0 }),
)
}
}
#[test]
fn a_views_bound_styles_on_a_nested_component_are_a_binding() {
let mut c = core();
c.mount_root(&Root::new::<Dimmer>(|| DimmerProps {}));
let batch = c.commit().unwrap();
let host = root_host(&batch);
assert!(c.component_event(host, EventKind::Press, &EventPayload::None));
let batch = c.commit().unwrap();
let dimmed = batch.items.iter().find_map(|i| match i {
MountItem::ViewStyle { id, style } if style.opacity == 0.5 => Some(*id),
_ => None,
});
let nested = dimmed.expect("the nested host is dimmed");
assert_ne!(nested, host);
assert_eq!(creates(&batch), 0);
assert!(c.component_event(host, EventKind::Press, &EventPayload::None));
let batch = c.commit().unwrap();
assert_eq!(view_style_of(&batch, nested).map(|s| s.opacity), Some(1.0));
}
#[derive(Default)]
struct Catcher;
#[derive(Clone, PartialEq, Props)]
struct CatcherProps {}
impl Component for Catcher {
type Props = CatcherProps;
type Event = NoEvent;
fn view(_: CatcherProps, _: Scope<NoEvent>) -> impl IntoElement {
let caught = signal(0);
on_back_press(move || {
caught.update(|n| *n += 1);
true
});
view().child(text(move || format!("caught {}", caught.get())))
}
}
#[test]
fn a_views_back_press_listener_hears_the_back_button() {
let mut c = core();
c.mount_root(&Root::new::<Catcher>(|| CatcherProps {}));
assert_eq!(texts(&c.commit().unwrap()), ["caught 0"]);
assert!(c.component_event(ROOT_ID, EventKind::BackPress, &EventPayload::None));
assert_eq!(texts(&c.commit().unwrap()), ["caught 1"]);
}
#[derive(Default)]
struct Broken;
#[derive(Clone, PartialEq, Props)]
struct BrokenProps {}
impl Component for Broken {
type Props = BrokenProps;
type Event = NoEvent;
fn view(_: BrokenProps, _: Scope<NoEvent>) -> impl IntoElement {
panic!("no view");
#[allow(unreachable_code)]
view()
}
}
#[derive(Default)]
struct Guarded;
#[derive(Clone, PartialEq, Props)]
struct GuardedProps {}
impl Component for Guarded {
type Props = GuardedProps;
type Event = NoEvent;
fn view(_: GuardedProps, _: Scope<NoEvent>) -> impl IntoElement {
let n = signal(0);
view()
.on_press(move |_: &()| n.update(|n| *n += 1))
.child(catch_error(
move || {
text(move || {
assert!(n.get() < 2, "too many");
format!("n {}", n.get())
})
},
|e, _| text(format!("failed: {}", e.message)),
))
.child(catch_error(
|| Broken::element(BrokenProps {}),
|e, _| text(format!("{} failed", e.component)),
))
}
}
#[test]
fn error_boundaries_show_their_fallback() {
let mut c = core();
c.mount_root(&Root::new::<Guarded>(|| GuardedProps {}));
let batch = c.commit().unwrap();
let shown = texts(&batch);
assert!(shown.contains(&"n 0".to_owned()), "{shown:?}");
assert!(
shown.iter().any(|t| t.ends_with("Broken failed")),
"{shown:?}"
);
let host = root_host(&batch);
assert!(c.component_event(host, EventKind::Press, &EventPayload::None));
assert_eq!(texts(&c.commit().unwrap()), ["n 1"]);
assert!(c.component_event(host, EventKind::Press, &EventPayload::None));
assert_eq!(texts(&c.commit().unwrap()), ["failed: too many"]);
}
#[component]
fn Ticker() -> impl IntoElement {
let tick = signal(0_u32);
view()
.on_press(move |_: &()| tick.update(|t| *t += 1))
.child(text(move || format!("tick {}", tick.get())))
.children((0..300).map(|i| {
view()
.flex_row()
.p_2()
.gap_2()
.child(view().size(px(20.0)).bg(rgb(0x3366ff)))
.child(text(format!("row {i}")).text_sm())
}))
}
#[test]
#[ignore]
fn bench_view_tick() {
let mut c = core();
c.mount_root(&Root::new::<Ticker>(|| ()));
let host = root_host(&c.commit().unwrap());
let n = 2000;
let (mut event, mut commit) = (std::time::Duration::ZERO, std::time::Duration::ZERO);
let mut items = 0;
for _ in 0..n {
let start = std::time::Instant::now();
c.component_event(host, EventKind::Press, &EventPayload::None);
let mid = std::time::Instant::now();
items += c.commit().map_or(0, |b| b.items.len());
event += mid - start;
commit += mid.elapsed();
}
eprintln!(
"view tick: {:?} per event, {:?} per commit, {} mount items",
event / n,
commit / n,
items / n as usize
);
}
#[derive(Clone, Copy, PartialEq, Props)]
struct ShownProps {
value: crate::ui::Prop<SharedString>,
}
#[component]
fn Shown(props: ShownProps) -> impl IntoElement {
text(props.value)
}
#[component]
fn Passer() -> impl IntoElement {
let n = signal(0);
let fixed = signal(false);
view()
.on_press(move |_: &()| {
if n.get_untracked() == 1 {
fixed.set(true);
}
n.update(|n| *n += 1);
})
.child(dynamic(move || {
let value: crate::ui::Prop<SharedString> = if fixed.get() {
"fixed".into()
} else {
crate::ui::Prop::bind(move || format!("n {}", n.get()).into())
};
shown().value(value)
}))
}
#[test]
fn a_prop_follows_a_binding_and_a_region_made_again_makes_a_new_component() {
let mut c = core();
c.mount_root(&Root::new::<Passer>(|| ()));
let batch = c.commit().unwrap();
assert_eq!(texts(&batch), ["n 0"]);
let host = root_host(&batch);
let press = |c: &mut Core<FakeEngine>| {
assert!(c.component_event(host, EventKind::Press, &EventPayload::None));
c.commit().map(|b| texts(&b)).unwrap_or_default()
};
assert_eq!(press(&mut c), ["n 1"]);
assert_eq!(press(&mut c), ["fixed"]);
assert!(press(&mut c).is_empty());
}
#[component]
fn Shortcuts() -> impl IntoElement {
let count = signal(0);
let shown = signal(true);
view()
.on_press(move |_: &()| shown.set(false))
.child(text(move || format!("k {}", count.get())))
.child(when(shown, move || {
on_key("mod+k", move |_| count.update(|c| *c += 1));
view()
}))
}
#[test]
fn a_views_shortcuts_join_jss_and_run_in_the_core() {
use crate::keymap::{KeyBinding, Keymap, Keystroke, When};
let tables = |batch: &MountBatch| -> Vec<Vec<u32>> {
batch
.items
.iter()
.filter_map(|i| match i {
MountItem::Keymap(k) => Some(k.bindings().iter().map(|b| b.id).collect()),
_ => None,
})
.collect()
};
let mut c = core();
c.mount_root(&Root::new::<Shortcuts>(|| ()));
let batch = c.commit().unwrap();
let host = root_host(&batch);
let table = tables(&batch);
let [ours] = table.last().expect("a table").as_slice() else {
panic!("{table:?}");
};
let ours = *ours;
assert!(ours >= crate::ui::keys::FIRST_ID);
let js = KeyBinding::new(
1,
Keystroke::parse_with("mod+j", true).unwrap(),
When::Auto,
false,
);
apply(
&mut c,
vec![Op::SetProp {
id: ROOT_ID,
prop: Prop::Keymap(std::sync::Arc::new(Keymap::new(vec![js]))),
}],
);
assert_eq!(tables(&c.commit().unwrap()), [vec![1, ours]]);
let key = |id: u32| EventPayload::Value(crate::PayloadValue::Number(id as f64));
assert!(c.component_event(ROOT_ID, EventKind::KeyCommand, &key(ours)));
assert_eq!(texts(&c.commit().unwrap()), ["k 1"]);
assert!(!c.component_event(ROOT_ID, EventKind::KeyCommand, &key(1)));
assert!(c.component_event(host, EventKind::Press, &EventPayload::None));
assert_eq!(tables(&c.commit().unwrap()), [vec![1]]);
}
#[component]
fn Pointing() -> impl IntoElement {
view().size_10().cursor_pointer()
}
#[test]
fn a_typed_cursor_reaches_the_view() {
let mut c = core();
c.mount_root(&Root::new::<Pointing>(|| ()));
let batch = c.commit().unwrap();
assert!(
batch.items.iter().any(|i| matches!(
i,
MountItem::ViewStyle { style, .. } if style.cursor == crate::Cursor::Pointer
)),
"{:#?}",
batch.items
);
}
#[component]
fn Leafy() -> impl IntoElement {
view().child(dynamic(|| text("leaf")))
}
#[component]
fn Branch() -> impl IntoElement {
let shown = signal(true);
view()
.on_press(move |_: &()| shown.set(false))
.child(when(shown, leafy))
}
#[test]
fn a_released_view_releases_what_its_regions_made() {
let mut c = core();
c.mount_root(&Root::new::<Branch>(|| ()));
let batch = c.commit().unwrap();
let leaf = text_node(&batch, "leaf");
let host = root_host(&batch);
assert!(c.component_event(host, EventKind::Press, &EventPayload::None));
let batch = c.commit().unwrap();
assert!(
batch
.items
.iter()
.any(|i| matches!(i, MountItem::Delete { id } if *id == leaf)),
"{:?}",
batch.items
);
}
}
mod rust_desktop {
use std::cell::Cell;
use super::*;
use crate::{
desktop::{DesktopCommand, TitleBarStyle},
geometry::{Edges, Points},
ui::{
Root,
desktop::{close_window, set_title_bar, title_bar},
prelude::*,
},
};
thread_local! {
static DRAG: Cell<Option<Signal<bool>>> = const {Cell::new(None)};
static READS: Cell<u32> = const {Cell::new(0)};
}
#[component]
fn Chrome() -> impl IntoElement {
set_title_bar(TitleBarStyle::Hidden, Some(px(42.0)));
let drag = signal(true);
DRAG.set(Some(drag));
view()
.window_drag(move || drag.get())
.child(text(move || {
READS.set(READS.get() + 1);
let area = title_bar();
format!("{} {} {}", area.top.0, area.left.0, area.right.0)
}))
.child(
text("Close window")
.window_drag(false)
.on_press(|_: &()| close_window()),
)
}
#[test]
fn rust_title_bar_commands_and_reactive_drag_props_reach_the_platform() {
let mut c = core();
READS.set(0);
c.set_environment(crate::ui::Environment {
title_bar: Edges {
top: Points(42.0),
left: Points(78.0),
right: Points(0.0),
bottom: Points(0.0),
},
..Default::default()
});
c.mount_root(&Root::of::<Chrome>());
let batch = c.commit().unwrap();
assert!(batch.items.iter().any(|item| matches!(item, MountItem::Command {id: crate::ROOT_ID, command: Command::Desktop(command)} if **command == DesktopCommand::SetTitleBar {style:TitleBarStyle::Hidden, height:Some(px(42.0))})));
let header = batch
.items
.iter()
.find_map(|item| match item {
MountItem::ViewProps { id, props } if props.window_drag == Some(true) => Some(*id),
_ => None,
})
.unwrap();
let close = batch
.items
.iter()
.find_map(|item| match item {
MountItem::Text { id, text, .. } if text.text == "Close window" => Some(*id),
_ => None,
})
.unwrap();
assert!(batch.items.iter().any(|item| matches!(item, MountItem::ViewProps {id, props} if *id == close && props.window_drag == Some(false))));
assert!(
batch
.items
.iter()
.any(|item| matches!(item, MountItem::Text {text, ..} if text.text == "42 78 0"))
);
{
let _entered = c.components.runtime.enter();
DRAG.get().unwrap().set(false);
}
let batch = c.commit().unwrap();
assert!(batch.items.iter().any(|item| matches!(item, MountItem::ViewProps {id, props} if *id == header && props.window_drag == Some(false))));
assert!(
!batch
.items
.iter()
.any(|item| matches!(item, MountItem::Create { .. }))
);
let reads = READS.get();
c.set_environment(crate::ui::Environment {
dark: true,
title_bar: Edges {
top: Points(42.0),
left: Points(78.0),
right: Points(0.0),
bottom: Points(0.0),
},
..Default::default()
});
assert!(c.commit().is_none());
assert_eq!(
READS.get(),
reads,
"other environment changes do not rerun title_bar readers"
);
c.set_environment(crate::ui::Environment {
title_bar: Edges {
top: Points(42.0),
left: Points(0.0),
right: Points(138.0),
bottom: Points(0.0),
},
..Default::default()
});
let batch = c.commit().unwrap();
assert!(
batch
.items
.iter()
.any(|item| matches!(item, MountItem::Text {text, ..} if text.text == "42 0 138"))
);
c.set_environment(crate::ui::Environment::default());
let batch = c.commit().unwrap();
assert!(
batch
.items
.iter()
.any(|item| matches!(item, MountItem::Text {text, ..} if text.text == "0 0 0"))
);
assert!(c.component_event(close, EventKind::Press, &EventPayload::None));
let batch = c.commit().unwrap();
assert!(batch.items.iter().any(|item| matches!(item, MountItem::Command {id:crate::ROOT_ID, command:Command::Desktop(command)} if **command == DesktopCommand::Close)));
}
}
#[test]
fn file_drop_callbacks_keep_urls_and_disabled_targets_unmount_their_events() {
use crate::ui::prelude::*;
#[component]
fn DropView() -> impl IntoElement {
let received = signal(String::new());
let disabled = signal(false);
view()
.disabled(move || disabled.get())
.on_press(move |_: &()| disabled.set(true))
.on_file_drag_enter(|_: &()| {})
.on_file_drag_leave(|_: &()| {})
.on_file_drop(move |event: &crate::ui::FileDropEvent| {
received.set(event.uris.join("\n"))
})
.child(text(move || received.get()))
}
let mut c = core();
c.mount_root(&crate::ui::Root::of::<DropView>());
let batch = c.commit().unwrap();
let host = batch
.items
.iter()
.find_map(|item| match item {
MountItem::Events { id, events } if events.contains(EventKind::FileDrop) => Some(*id),
_ => None,
})
.unwrap();
let uris = vec![
"file:///tmp/a%20b.txt".into(),
"file:///tmp/%E4%B8%AD%E6%96%87/".into(),
];
assert!(c.component_event(
host,
EventKind::FileDrop,
&EventPayload::FileDrop(uris.clone())
));
let delivered = c.commit().unwrap();
assert!(delivered.items.iter().any(|item| matches!(item,
MountItem::Text { text, .. } if text.text == uris.join("\n"))));
assert!(c.component_event(host, EventKind::Press, &EventPayload::None));
let disabled = c.commit().unwrap();
let events = disabled
.items
.iter()
.find_map(|item| match item {
MountItem::Events { id, events } if *id == host => Some(*events),
_ => None,
})
.unwrap();
for event in [
EventKind::FileDragEnter,
EventKind::FileDragLeave,
EventKind::FileDrop,
] {
assert!(!events.contains(event));
}
}
#[test]
fn hover_moves_reach_rust_listeners_with_their_positions_and_modifiers() {
use crate::ui::prelude::*;
#[component]
fn Crosshair() -> impl IntoElement {
let at = signal(String::new());
view()
.on_hover_move(move |e: &HoverEvent| {
at.set(format!(
"{} {} {} {} {}",
e.position.x.0,
e.position.y.0,
e.page_position.x.0,
e.page_position.y.0,
e.modifiers.shift
))
})
.child(text(move || at.get()))
}
let mut c = core();
c.mount_root(&crate::ui::Root::of::<Crosshair>());
let batch = c.commit().unwrap();
let host = batch
.items
.iter()
.find_map(|item| match item {
MountItem::Events { id, events } if events.contains(EventKind::HoverMove) => Some(*id),
_ => None,
})
.expect("the listening view mounts HoverMove in its event mask");
let event = crate::HoverEvent {
position: point(px(4.0), px(5.0)),
page_position: point(px(14.0), px(25.0)),
modifiers: crate::keymap::Modifiers {
shift: true,
..Default::default()
},
};
assert!(c.component_event(host, EventKind::HoverMove, &EventPayload::Hover(event)));
let delivered = c.commit().unwrap();
assert!(delivered.items.iter().any(|item| matches!(item,
MountItem::Text { text, .. } if text.text == "4 5 14 25 true")));
assert!(c.component_event(host, EventKind::HoverMove, &EventPayload::None));
assert!(
c.commit()
.is_none_or(|b| !b.items.iter().any(|i| matches!(i, MountItem::Text { .. })))
);
}
#[test]
fn context_menus_reach_rust_listeners_with_their_positions() {
use crate::ui::prelude::*;
#[component]
fn Row() -> impl IntoElement {
let at = signal(String::new());
view()
.on_context_menu(move |e: &ContextMenuEvent| {
at.set(format!(
"{} {} {} {}",
e.position.x.0, e.position.y.0, e.page_position.x.0, e.page_position.y.0
))
})
.child(text(move || at.get()))
}
let mut c = core();
c.mount_root(&crate::ui::Root::of::<Row>());
let batch = c.commit().unwrap();
let host = batch
.items
.iter()
.find_map(|item| match item {
MountItem::Events { id, events } if events.contains(EventKind::ContextMenu) => {
Some(*id)
}
_ => None,
})
.expect("the listening view mounts ContextMenu in its event mask");
let event = crate::ContextMenuEvent {
position: point(px(4.0), px(5.0)),
page_position: point(px(14.0), px(25.0)),
..Default::default()
};
assert!(c.component_event(
host,
EventKind::ContextMenu,
&EventPayload::ContextMenu(event)
));
let delivered = c.commit().unwrap();
assert!(delivered.items.iter().any(|item| matches!(item,
MountItem::Text { text, .. } if text.text == "4 5 14 25")));
}
fn grid_frames(
container: &[(&str, StyleValue<'_>)],
children: &[&[(&str, StyleValue<'_>)]],
) -> Vec<Bounds<Points>> {
let mut c = core();
let mut ops = vec![
Op::Create {
id: 2,
tag: Tag::View,
},
style_op(2, container),
];
for (i, child) in children.iter().enumerate() {
let id = 4 + 2 * i as NodeId;
ops.push(Op::Create { id, tag: Tag::View });
ops.push(style_op(id, child));
ops.push(Op::Insert {
parent: 2,
child: id,
before: None,
});
}
ops.push(Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
});
apply(&mut c, ops);
let batch = c.commit().unwrap();
(0..children.len())
.map(|i| frame_of(&batch, 4 + 2 * i as NodeId).expect("a frame"))
.collect()
}
#[test]
fn grids_place_items_by_line_name() {
let s = StyleValue::Str;
let grid = [
("display", s("grid")),
("width", StyleValue::Number(300.0)),
("gridTemplateColumns", s("[a] 40px [b] 60px [c] 1fr [d]")),
];
let f = grid_frames(
&grid,
&[
&[("gridColumnStart", s("b"))],
&[("gridColumn", s("a / c"))],
&[("gridColumn", s("b / span d"))],
&[("gridColumnStart", s("c")), ("gridColumnEnd", s("d"))],
],
);
let xw = |r: Bounds<Points>| (r.origin.x.0, r.size.width.0);
assert_eq!(xw(f[0]), (40.0, 60.0));
assert_eq!(xw(f[1]), (0.0, 100.0));
assert_eq!(xw(f[2]), (40.0, 260.0));
assert_eq!(xw(f[3]), (100.0, 200.0));
let f = grid_frames(
&[
("display", s("grid")),
("width", StyleValue::Number(300.0)),
(
"gridTemplateColumns",
s("[full-start] 20px [main-start] repeat(2, [col] 1fr) [main-end] 20px [full-end]"),
),
],
&[
&[("gridColumn", s("main-start / main-end"))],
&[("gridColumn", s("2 col / full-end"))],
&[("gridColumn", s("full-start / full-end"))],
],
);
assert_eq!(xw(f[0]), (20.0, 260.0));
assert_eq!(xw(f[1]), (150.0, 150.0));
assert_eq!(xw(f[2]), (0.0, 300.0));
}
#[test]
fn grid_auto_tracks_stretch_unless_aligned() {
let s = StyleValue::Str;
let n = StyleValue::Number;
let grid = [
("display", s("grid")),
("width", n(300.0)),
("height", n(200.0)),
("gridTemplateColumns", s("1fr 1fr")),
];
let f = grid_frames(&grid, &[&[], &[], &[]]);
assert_eq!((f[0].origin.y.0, f[0].size.height.0), (0.0, 100.0));
assert_eq!((f[2].origin.y.0, f[2].size.height.0), (100.0, 100.0));
let mut normal = grid.to_vec();
normal.push(("alignContent", s("normal")));
assert_eq!(grid_frames(&normal, &[&[]])[0].size.height.0, 200.0);
let f = grid_frames(&grid[..3], &[&[]]);
assert_eq!(f[0].size.width.0, 300.0);
let mut start = grid.to_vec();
start.push(("alignContent", s("flex-start")));
assert_eq!(grid_frames(&start, &[&[]])[0].size.height.0, 0.0);
let mut start = grid[..3].to_vec();
start.push(("justifyContent", s("start")));
assert_eq!(grid_frames(&start, &[&[]])[0].size.width.0, 0.0);
let f = grid_frames(
&[
("flexDirection", s("row")),
("flexWrap", s("wrap")),
("width", n(300.0)),
("height", n(200.0)),
],
&[&[("width", n(200.0))], &[("width", n(200.0))]],
);
assert_eq!((f[0].size.height.0, f[1].origin.y.0), (0.0, 0.0));
}
fn class_frames(classes: &str, children: &[&str]) -> Vec<Bounds<Points>> {
let mut c = core();
let mut ops = vec![
Op::Create {
id: 2,
tag: Tag::View,
},
class_op(2, classes),
];
for (i, child) in children.iter().enumerate() {
let id = 4 + 2 * i as NodeId;
ops.push(Op::Create { id, tag: Tag::View });
ops.push(class_op(id, child));
ops.push(Op::Insert {
parent: 2,
child: id,
before: None,
});
}
ops.push(Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
});
apply(&mut c, ops);
let batch = c.commit().unwrap();
(0..children.len())
.map(|i| frame_of(&batch, 4 + 2 * i as NodeId).expect("a frame"))
.collect()
}
#[test]
fn alignment_classes_lay_out_as_in_a_browser() {
let yh = |r: Bounds<Points>| (r.origin.y.0, r.size.height.0);
let grid = "grid grid-cols-2 w-[300px] h-[200px]";
let f = class_frames(grid, &["", "", ""]);
assert_eq!((yh(f[0]), yh(f[2])), ((0.0, 100.0), (100.0, 100.0)));
let f = class_frames(&format!("{grid} content-normal"), &["", "", ""]);
assert_eq!(yh(f[2]), (100.0, 100.0));
let f = class_frames(&format!("{grid} content-start"), &["h-5", "", "h-8"]);
assert_eq!((yh(f[0]), yh(f[2])), ((0.0, 20.0), (20.0, 32.0)));
let free = "grid w-[300px] h-[200px]";
let f = class_frames(&format!("{free} justify-normal"), &[""]);
assert_eq!((f[0].size.width.0, f[0].size.height.0), (300.0, 200.0));
let f = class_frames(&format!("{free} place-content-start"), &[""]);
assert_eq!((f[0].size.width.0, f[0].size.height.0), (0.0, 0.0));
let f = class_frames(&format!("{free} place-content-end"), &["w-10 h-5"]);
assert_eq!((f[0].origin.x.0, f[0].origin.y.0), (260.0, 180.0));
let cells = "grid grid-cols-[100px] grid-rows-[40px] w-[300px]";
let f = class_frames(&format!("{cells} place-items-end"), &["w-10 h-5"]);
assert_eq!((f[0].origin.x.0, f[0].origin.y.0), (60.0, 20.0));
let f = class_frames(&format!("{cells} justify-items-start"), &["w-10"]);
assert_eq!((f[0].origin.x.0, f[0].size.height.0), (0.0, 40.0));
let flex = "flex flex-wrap w-[300px] h-[200px]";
let lines = ["w-[200px]", "w-[200px]"];
let f = class_frames(flex, &lines);
assert_eq!((yh(f[0]), yh(f[1])), ((0.0, 0.0), (0.0, 0.0)));
let f = class_frames(&format!("{flex} content-start"), &lines);
assert_eq!((yh(f[0]), yh(f[1])), ((0.0, 0.0), (0.0, 0.0)));
let f = class_frames(&format!("{flex} content-normal"), &lines);
assert_eq!((yh(f[0]), yh(f[1])), ((0.0, 100.0), (100.0, 100.0)));
let f = class_frames("flex w-[300px] justify-normal", &["w-10"]);
assert_eq!(f[0].origin.x.0, 0.0);
}
#[test]
fn start_and_end_follow_the_writing_direction() {
let s = StyleValue::Str;
let n = StyleValue::Number;
let item = [("width", n(50.0)), ("height", n(20.0))];
let grid = |justify: &'static str, direction: &'static str| {
[
("display", s("grid")),
("width", n(300.0)),
("height", n(100.0)),
("gridTemplateColumns", s("100px")),
("gridTemplateRows", s("40px")),
("justifyItems", s(justify)),
("alignItems", s("end")),
("direction", s(direction)),
]
};
let f = grid_frames(&grid("start", "ltr"), &[&item]);
assert_eq!((f[0].origin.x.0, f[0].origin.y.0), (0.0, 20.0));
let f = grid_frames(&grid("end", "ltr"), &[&item]);
assert_eq!(f[0].origin.x.0, 50.0);
let f = grid_frames(&grid("start", "rtl"), &[&item]);
assert_eq!(f[0].origin.x.0, 250.0);
let f = grid_frames(&grid("end", "rtl"), &[&item]);
assert_eq!(f[0].origin.x.0, 200.0);
let f = grid_frames(
&grid("start", "ltr"),
&[&[
("width", n(50.0)),
("justifySelf", s("self-end")),
("alignSelf", s("start")),
]],
);
assert_eq!((f[0].origin.x.0, f[0].origin.y.0), (50.0, 0.0));
let mut end = grid("start", "ltr").to_vec();
end.push(("justifyContent", s("end")));
assert_eq!(grid_frames(&end, &[&item])[0].origin.x.0, 200.0);
let mut end = grid("start", "rtl").to_vec();
end.push(("justifyContent", s("end")));
assert_eq!(grid_frames(&end, &[&item])[0].origin.x.0, 50.0);
let f = grid_frames(
&[
("width", n(300.0)),
("alignItems", s("end")),
("justifyContent", s("end")),
("height", n(100.0)),
],
&[&item],
);
assert_eq!((f[0].origin.x.0, f[0].origin.y.0), (250.0, 80.0));
}
mod builders {
use super::*;
use crate::ui::{Listener, Root, prelude::*};
fn run<C: Component>(
props: impl Fn() -> C::Props + Send + Sync + 'static,
taps: &[&str],
) -> Vec<String> {
let mut c = core();
c.mount_root(&Root::new::<C>(props));
let mut nodes = std::collections::HashMap::new();
let mut out = Vec::new();
let mut batch = c.commit().unwrap();
for tap in taps.iter().map(Some).chain([None]) {
for item in &batch.items {
if let MountItem::Text { id, text, .. } = item {
nodes.insert(text.text.clone(), *id);
}
}
out.push(format!("{:?}", batch.items));
let Some(tap) = tap else { break };
let node = *nodes
.get(*tap)
.unwrap_or_else(|| panic!("no text {tap:?} in {nodes:?}"));
assert!(c.component_event(node, EventKind::Press, &EventPayload::None));
batch = c.commit().unwrap_or_default();
}
out
}
fn tap<M>(label: &'static str, on_press: impl IntoListener<(), M>) -> Text {
text(label).on_press(on_press)
}
#[component]
fn ListenersOfEvents() -> impl IntoElement {
let n = signal(0);
view()
.child(text("one").on_press(move |_: &()| n.update(|n| *n += 1)))
.child(tap("ten", move |_: &()| n.update(|n| *n += 10)))
.child(tap(
"hundred",
Listener::from(move |_: &()| n.update(|n| *n += 100)),
))
.child(input().on_focus(move |_: &()| n.set(0)))
.child(text(move || format!("n {}", n.get())))
}
#[component]
fn ListenersWithout() -> impl IntoElement {
let n = signal(0);
view()
.child(text("one").on_press(move || n.update(|n| *n += 1)))
.child(tap("ten", move || n.update(|n| *n += 10)))
.child(tap(
"hundred",
Listener::from(move |_: &()| n.update(|n| *n += 100)),
))
.child(input().on_focus(move || n.set(0)))
.child(text(move || format!("n {}", n.get())))
}
#[test]
fn listeners_of_events_without_data_take_closures_without_arguments() {
let taps = ["one", "ten", "hundred", "one"];
let of_events = run::<ListenersOfEvents>(|| (), &taps);
assert_eq!(run::<ListenersWithout>(|| (), &taps), of_events);
assert!(of_events[4].contains("\"n 112\""), "{}", of_events[4]);
}
#[component]
fn ChildrenOneByOne() -> impl IntoElement {
view()
.child(text("a"))
.child(text("c"))
.child(text("d"))
.child(text("e"))
.child(text("f"))
.child(text("row 0"))
.child(text("row 1"))
.child(text("x").child("y").child("z"))
.child(dynamic(|| vec![text("left"), text("right")]))
}
#[component]
fn ChildrenTogether() -> impl IntoElement {
view()
.child(Some(text("a")))
.child(None::<Text>)
.child(vec![text("c"), text("d")])
.child(["e", "f"].map(text))
.child((0..2).map(|i| text(format!("row {i}"))))
.child(text("x").child(["y", "z"]))
.child(dynamic(|| [text("left"), text("right")]))
}
#[test]
fn options_vecs_arrays_and_iterators_are_children() {
let one_by_one = run::<ChildrenOneByOne>(|| (), &[]);
assert_eq!(run::<ChildrenTogether>(|| (), &[]), one_by_one);
assert!(one_by_one[0].contains("\"row 1\""));
assert!(one_by_one[0].contains("\"right\""));
}
#[component]
fn BoundOptions() -> impl IntoElement {
let flipped = signal(false);
let pick =
move |a: &'static str, b: &'static str| move || if flipped.get() { b } else { a };
view()
.child(text("flip").on_press(move || flipped.set(true)))
.child(
text("lines")
.number_of_lines(move || if flipped.get() { 2_u32 } else { 1 })
.selectable(flipped)
.max_font_size_multiplier(move || if flipped.get() { 3.0_f32 } else { 1.5 }),
)
.child(
image("a.png")
.resize_mode(move || {
if flipped.get() {
crate::ResizeMode::Contain
} else {
crate::ResizeMode::Cover
}
})
.placeholder(pick("before.png", "after.png"))
.fade_duration(move || if flipped.get() { 300.0_f32 } else { 100.0 }),
)
.child(web_view(pick(
"https://before.test/",
"https://after.test/",
)))
.child(video(pick(
"https://before.test/v.mp4",
"https://after.test/v.mp4",
)))
.child(native("map").prop("zoom", move || {
serde_json::json!(if flipped.get() { 9 } else { 4 })
}))
}
#[test]
fn host_options_take_signals_and_closures() {
let frames = run::<BoundOptions>(|| (), &["flip"]);
for before in [
"before.png",
"https://before.test/",
"https://before.test/v.mp4",
"Number(4)",
"Cover",
"fade_ms: Some(100.0)",
"max_lines: 1",
"selectable: Some(false)",
] {
assert!(frames[0].contains(before), "{before} in {}", frames[0]);
}
for after in [
"after.png",
"https://after.test/",
"https://after.test/v.mp4",
"Number(9)",
"resize_mode: Contain",
"fade_ms: Some(300.0)",
"max_lines: 2",
"selectable: Some(true)",
] {
assert!(frames[1].contains(after), "{after} in {}", frames[1]);
}
for gone in ["before.png", "before.test"] {
assert!(!frames[1].contains(gone), "{gone} in {}", frames[1]);
}
}
#[test]
fn a_status_bar_style_is_a_setting() {
let setting = crate::StatusBarStyle::Light.into_reactive();
assert_eq!(
setting.get(),
StatusBarSetting::default().foreground(crate::StatusBarStyle::Light)
);
}
}
mod renders {
use serde_json::json;
use super::*;
use crate::{
Prop,
ui::{AnyValue, Props, Registry, Rendered, Renderer, Root, dynamic, prelude::*},
};
#[derive(Clone, PartialEq, Props)]
struct ShelfProps {
#[prop(default)]
items: crate::ui::Prop<Vec<String>>,
#[prop(default)]
inline: bool,
render_item: crate::ui::Prop<Option<Renderer<String>>>,
}
fn split(item: &str) -> (String, String) {
let (key, label) = item.split_once(':').unwrap_or((item, item));
(key.to_owned(), label.to_owned())
}
#[component]
fn Shelf(props: ShelfProps) -> impl IntoElement {
let rows = each(
props.items,
|item: &String| split(item).0,
move |item, _| {
view().child(dynamic(move || -> Option<Rendered> {
let (key, label) = split(&item.get());
props.render_item.get().map(|r| r.render(key, label))
}))
},
);
if props.inline {
text("on the shelf: ").child(rows).into_element()
} else {
view().child(rows).into_element()
}
}
fn core_with_shelf(inline: bool) -> Core<FakeEngine> {
let mut c = core();
let mut registry = Registry::new();
registry.register::<Shelf>();
c.set_components(registry);
let mut ops = vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetProp {
id: 2,
prop: Prop::Component(std::any::type_name::<Shelf>().into()),
},
Op::SetProp {
id: 2,
prop: Prop::RustProp(
"renderItem",
Some(AnyValue::new(Some(Renderer::<String>::__js_of(
"renderItem",
2,
)))),
),
},
items(&["a:Apple", "b:Banana"]),
];
if inline {
ops.push(Op::SetProp {
id: 2,
prop: rust_prop::<ShelfProps>("inline", Some(json!(true))),
});
}
ops.push(Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
});
apply(&mut c, ops);
c
}
fn items(items: &[&str]) -> Op {
Op::SetProp {
id: 2,
prop: rust_prop::<ShelfProps>("items", Some(json!(items))),
}
}
fn asked(c: &mut Core<FakeEngine>) -> Vec<String> {
js_events(c)
.into_iter()
.filter_map(|(host, _, payload)| match payload {
EventPayload::Native { name, data } if name.starts_with("$") => {
assert_eq!(host, 2);
assert_eq!(data["prop"], "renderItem");
let key = data["key"].as_str().unwrap().to_owned();
Some(match data.get("value") {
Some(v) => format!("{name} {key} {}", v.as_str().unwrap()),
None => format!("{name} {key}"),
})
}
_ => None,
})
.collect()
}
fn holder(holder: NodeId, key: &str, label: &str) -> Vec<Op> {
vec![
Op::Create {
id: holder,
tag: Tag::View,
},
Op::SetProp {
id: holder,
prop: Prop::ComponentRender(format!("renderItem\u{1f}{key}").into()),
},
Op::Create {
id: holder + 2,
tag: Tag::Text,
},
Op::CreateText {
id: holder + 4,
text: label.into(),
},
Op::Insert {
parent: holder + 2,
child: holder + 4,
before: None,
},
Op::Insert {
parent: holder,
child: holder + 2,
before: None,
},
Op::Insert {
parent: 2,
child: holder,
before: None,
},
]
}
fn parent_of(c: &Core<FakeEngine>, id: NodeId) -> Option<NodeId> {
let key = c.key(id).ok()?;
let parent = c.nodes[key as usize].parent;
(parent != NO_KEY).then(|| c.nodes[parent as usize].id)
}
#[test]
fn js_renders_each_key_once_per_value_and_releases_it() {
let mut c = core_with_shelf(false);
c.commit().unwrap();
assert_eq!(asked(&mut c), ["$render a Apple", "$render b Banana"]);
apply(&mut c, holder(10, "a", "Apple"));
apply(&mut c, holder(20, "b", "Banana"));
c.commit().unwrap();
let (a, b) = (parent_of(&c, 10).unwrap(), parent_of(&c, 20).unwrap());
assert_eq!((a % 2, b % 2), (1, 1));
assert_ne!(a, b);
let row = |p| parent_of(&c, p).and_then(|r| parent_of(&c, r));
assert_eq!(row(a), row(b));
assert!(asked(&mut c).is_empty());
apply(&mut c, vec![items(&["a:Apple", "b:Banana"])]);
let _ = c.commit();
assert!(asked(&mut c).is_empty());
apply(&mut c, vec![items(&["a:Avocado", "b:Banana"])]);
let _ = c.commit();
assert_eq!(asked(&mut c), ["$render a Avocado"]);
apply(&mut c, holder(30, "a", "Avocado"));
apply(
&mut c,
vec![
Op::Remove {
parent: 2,
child: 10,
},
Op::Release { id: 10 },
],
);
c.commit().unwrap();
assert_eq!(parent_of(&c, 30), Some(a));
apply(&mut c, vec![items(&["b:Banana"])]);
let _ = c.commit();
assert_eq!(asked(&mut c), ["$unrender a"]);
assert_eq!(parent_of(&c, 30), None, "out of the tree");
assert!(c.key(a).is_err(), "the placeholder went");
apply(
&mut c,
vec![
Op::Remove {
parent: 2,
child: 30,
},
Op::Release { id: 30 },
],
);
let _ = c.commit();
assert!(asked(&mut c).is_empty());
apply(&mut c, vec![items(&["a:Apple", "b:Banana"])]);
let _ = c.commit();
assert_eq!(asked(&mut c), ["$render a Apple"]);
apply(
&mut c,
vec![
Op::Remove {
parent: ROOT_ID,
child: 2,
},
Op::Release { id: 2 },
],
);
let _ = c.commit();
assert!(c.components.renders.is_empty());
}
#[test]
fn a_holder_coming_after_its_key_left_stays_out_of_the_tree() {
let mut c = core_with_shelf(false);
c.commit().unwrap();
asked(&mut c);
apply(&mut c, vec![items(&["b:Banana"])]);
let _ = c.commit();
assert_eq!(asked(&mut c), ["$unrender a"]);
apply(&mut c, holder(10, "a", "Apple"));
let _ = c.commit();
assert_eq!(parent_of(&c, 10), None);
}
#[test]
fn renders_in_a_paragraph_are_inline_views() {
let mut c = core_with_shelf(true);
c.commit().unwrap();
assert_eq!(asked(&mut c), ["$render a Apple", "$render b Banana"]);
apply(&mut c, holder(10, "a", "Apple"));
let batch = c.commit().unwrap();
let place = parent_of(&c, 10).unwrap();
let paragraph = parent_of(&c, parent_of(&c, place).unwrap()).unwrap();
let key = c.key(paragraph).unwrap();
assert_eq!(c.nodes[key as usize].kind, Kind::Text);
assert!(frame_of(&batch, place).is_some(), "{:?}", batch.items);
}
#[derive(Clone, PartialEq, Props)]
struct ShelvesProps {}
#[component]
fn Shelves(_props: ShelvesProps) -> impl IntoElement {
shelf()
.items(vec!["a:Apple".to_owned(), "b:Banana".to_owned()])
.render_item(|label| text(format!("[{label}]")))
}
#[test]
fn a_rust_renderer_renders_in_place() {
let mut c = core();
c.mount_root(&Root::new::<Shelves>(|| ShelvesProps {}));
let batch = c.commit().unwrap();
let texts: Vec<_> = batch
.items
.iter()
.filter_map(|i| match i {
MountItem::Text { text, .. } => Some(text.text.clone()),
_ => None,
})
.collect();
assert_eq!(texts, ["[Apple]", "[Banana]"]);
assert!(js_events(&mut c).is_empty());
}
#[derive(Clone, PartialEq, Props)]
struct CardProps {
#[prop(default)]
heading_class: crate::ui::Prop<Option<SharedString>>,
}
#[component]
fn Card(props: CardProps) -> impl IntoElement {
view().child(
view()
.bg(crate::ui::palette::RED.shade(500))
.border_2()
.app_class(props.heading_class),
)
}
fn core_with_card(class: &str) -> Core<FakeEngine> {
let mut c = core();
let mut registry = Registry::new();
registry.register::<Card>();
c.set_components(registry);
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::View,
},
Op::SetProp {
id: 2,
prop: Prop::Component(std::any::type_name::<Card>().into()),
},
card_class(class),
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
c
}
fn card_class(class: &str) -> Op {
Op::SetProp {
id: 2,
prop: rust_prop::<CardProps>("headingClass", Some(json!(class))),
}
}
fn heading(c: &Core<FakeEngine>) -> NodeId {
let key = c.key(2).unwrap();
let child = c.nodes[key as usize].children[0];
c.nodes[child as usize].id
}
#[test]
fn the_apps_classes_go_after_an_elements_own_styles() {
let mut c = core_with_card("bg-white dark:bg-black");
let batch = c.commit().unwrap();
let id = heading(&c);
let style = view_style_of(&batch, id).unwrap();
assert_eq!(style.background_color, Color(0xffff_ffff));
assert_eq!(style.borders.widths.to_array().map(|w| w.0), [2.0; 4]);
c.set_class_environment(crate::tailwind::ClassEnvironment {
dark: true,
..Default::default()
});
let batch = c.commit().unwrap();
assert_eq!(
view_style_of(&batch, id).unwrap().background_color,
Color(0x0000_00ff)
);
apply(&mut c, vec![card_class("bg-red-500 dark:bg-white")]);
let batch = c.commit().unwrap();
assert_eq!(heading(&c), id);
assert_eq!(
view_style_of(&batch, id).unwrap().background_color,
Color(0xffff_ffff)
);
apply(
&mut c,
vec![Op::SetProp {
id: 2,
prop: rust_prop::<CardProps>("headingClass", None),
}],
);
let batch = c.commit().unwrap();
assert_eq!(
view_style_of(&batch, id).unwrap().background_color,
Color::from(crate::ui::palette::RED.shade(500))
);
}
}
mod rust_selection_area {
use std::cell::Cell;
use super::*;
use crate::ui::{Root, prelude::*};
thread_local! {
static AREA: Cell<Option<Signal<bool>>> = const { Cell::new(None) };
}
#[component]
fn Area() -> impl IntoElement {
let area = signal(true);
AREA.set(Some(area));
view()
.child(
view()
.selection_area(move || area.get())
.child(text("inside")),
)
.child(view().child(text("outside")))
}
#[test]
fn a_selection_area_is_a_view_prop() {
let mut c = core();
c.mount_root(&Root::of::<Area>());
let batch = c.commit().unwrap();
let areas: Vec<_> = batch
.items
.iter()
.filter_map(|item| match item {
MountItem::ViewProps { id, props } if props.selection_area => Some(*id),
_ => None,
})
.collect();
assert_eq!(areas.len(), 1, "{:?}", batch.items);
assert!(
!batch.items.iter().any(
|item| matches!(item, MountItem::ViewProps { props, .. } if !props.selection_area)
),
"views without the flag get no props: {:?}",
batch.items
);
{
let _entered = c.components.runtime.enter();
AREA.get().unwrap().set(false);
}
let batch = c.commit().unwrap();
assert!(batch.items.iter().any(
|item| matches!(item, MountItem::ViewProps { id, props } if *id == areas[0] && !props.selection_area)
));
}
}
fn add(
c: &mut Core<FakeEngine>,
id: NodeId,
tag: Tag,
parent: NodeId,
s: &[(&str, StyleValue<'_>)],
) {
apply(
c,
vec![
Op::Create { id, tag },
style_op(id, s),
Op::Insert {
parent,
child: id,
before: None,
},
],
);
}
fn inserts(batch: &MountBatch) -> Vec<(NodeId, NodeId, Option<NodeId>)> {
batch
.items
.iter()
.filter_map(|i| match i {
MountItem::Insert {
parent,
child,
before,
} => Some((*parent, *child, *before)),
_ => None,
})
.collect()
}
fn overlay_prop(id: NodeId, prop: crate::OverlayProp) -> Op {
Op::SetProp {
id,
prop: Prop::Overlay(prop),
}
}
#[test]
fn overlays_show_in_their_root_view_above_its_children() {
use StyleValue::Number;
let mut c = core();
add(
&mut c,
2,
Tag::View,
ROOT_ID,
&[
("height", Number(100.0)),
("overflow", StyleValue::Str("hidden")),
],
);
add(&mut c, 4, Tag::View, 2, &[("padding", Number(5.0))]);
add(
&mut c,
6,
Tag::Overlay,
4,
&[("left", Number(10.0)), ("top", Number(300.0))],
);
add(
&mut c,
8,
Tag::View,
6,
&[("width", Number(50.0)), ("height", Number(20.0))],
);
let batch = c.commit().unwrap();
let creates = batch.items.iter().find_map(|i| match i {
MountItem::Create { id: 6, kind } => Some(*kind),
_ => None,
});
assert_eq!(creates, Some(ViewKind::View));
let ins = inserts(&batch);
assert!(ins.contains(&(ROOT_ID, 6, None)), "{ins:?}");
assert!(!ins.iter().any(|&(p, c, _)| p == 4 && c == 6));
assert!(ins.contains(&(6, 8, None)));
assert_eq!(frame_of(&batch, 6), Some(rect(10.0, 300.0, 50.0, 20.0)));
assert_eq!(frame_of(&batch, 4), Some(rect(0.0, 0.0, 320.0, 10.0)));
let m = c.measure(8).unwrap();
assert_eq!(m.page, point(px(10.0), px(300.0)));
add(&mut c, 10, Tag::View, ROOT_ID, &[("height", Number(10.0))]);
let batch = c.commit().unwrap();
assert_eq!(inserts(&batch), vec![(ROOT_ID, 10, Some(6))]);
add(&mut c, 12, Tag::Overlay, ROOT_ID, &[]);
c.commit().unwrap();
apply(
&mut c,
vec![Op::Remove {
parent: ROOT_ID,
child: 2,
}],
);
let batch = c.commit().unwrap();
assert!(
batch
.items
.iter()
.any(|i| matches!(i, MountItem::Remove { id: 6 }))
);
apply(
&mut c,
vec![Op::Insert {
parent: ROOT_ID,
child: 2,
before: Some(10),
}],
);
let batch = c.commit().unwrap();
let ins = inserts(&batch);
assert!(ins.contains(&(ROOT_ID, 2, Some(10))), "{ins:?}");
assert!(ins.contains(&(ROOT_ID, 6, None)), "{ins:?}");
assert_eq!(c.overlays.last().map(|&k| c.nodes[k as usize].id), Some(6));
apply(
&mut c,
vec![
Op::Remove {
parent: ROOT_ID,
child: 2,
},
Op::Release { id: 6 },
],
);
let batch = c.commit().unwrap();
assert!(
batch
.items
.iter()
.any(|i| matches!(i, MountItem::Delete { id: 6 }))
);
assert_eq!(c.overlays.len(), 1);
add(&mut c, 14, Tag::View, ROOT_ID, &[]);
add(&mut c, 16, Tag::Overlay, 14, &[]);
c.commit().unwrap();
apply(
&mut c,
vec![style_op(14, &[("display", StyleValue::Str("none"))])],
);
let batch = c.commit().unwrap();
assert!(
batch
.items
.iter()
.any(|i| matches!(i, MountItem::Remove { id: 16 }))
);
}
#[test]
fn anchored_overlays_go_beside_their_anchor_and_follow_it() {
use StyleValue::Number;
let mut c = core();
add(
&mut c,
2,
Tag::Scroll,
ROOT_ID,
&[("height", Number(480.0))],
);
add(&mut c, 4, Tag::View, 2, &[("height", Number(100.0))]);
add(
&mut c,
6,
Tag::View,
2,
&[
("width", Number(80.0)),
("height", Number(40.0)),
("marginLeft", Number(20.0)),
],
);
add(&mut c, 8, Tag::View, 2, &[("height", Number(1000.0))]);
add(&mut c, 10, Tag::Overlay, 6, &[]);
add(
&mut c,
12,
Tag::View,
10,
&[("width", Number(120.0)), ("height", Number(60.0))],
);
apply(
&mut c,
vec![
overlay_prop(
10,
crate::OverlayProp::Anchor(crate::overlay::OverlayAnchor::Node(6)),
),
overlay_prop(10, crate::OverlayProp::Align(crate::overlay::Align::Start)),
overlay_prop(10, crate::OverlayProp::Offset(px(4.0))),
Op::Listen {
id: 10,
event: EventKind::Position,
enabled: true,
},
],
);
let batch = c.commit().unwrap();
assert_eq!(frame_of(&batch, 10), Some(rect(20.0, 144.0, 120.0, 60.0)));
let events = js_events(&mut c);
assert!(events.iter().any(|(id, kind, payload)| *id == 10
&& *kind == EventKind::Position
&& matches!(payload, EventPayload::Map(m) if m.contains(&("placement", PayloadValue::Str("bottom".into()))))));
c.set_scroll_offset(2, point(px(0.0), px(30.0)), 0);
let batch = c.commit().unwrap();
assert_eq!(frame_of(&batch, 10), Some(rect(20.0, 114.0, 120.0, 60.0)));
c.set_scroll_offset(2, point(px(0.0), px(-300.0)), 0);
let batch = c.commit().unwrap();
assert_eq!(frame_of(&batch, 10), Some(rect(20.0, 336.0, 120.0, 60.0)));
let events = js_events(&mut c);
assert!(events.iter().any(|(_, kind, payload)| *kind == EventKind::Position
&& matches!(payload, EventPayload::Map(m) if m.contains(&("placement", PayloadValue::Str("top".into()))))));
c.set_scroll_offset(2, point(px(0.0), px(0.0)), 0);
c.set_safe_area(EdgeInsets {
left: px(30.0),
..EdgeInsets::default()
});
apply(
&mut c,
vec![overlay_prop(10, crate::OverlayProp::Margin(px(8.0)))],
);
let batch = c.commit().unwrap();
assert_eq!(frame_of(&batch, 10).map(|f| f.origin.x), Some(px(38.0)));
apply(
&mut c,
vec![overlay_prop(
10,
crate::OverlayProp::Anchor(crate::overlay::OverlayAnchor::Point(point(
px(200.0),
px(50.0),
))),
)],
);
let batch = c.commit().unwrap();
assert_eq!(frame_of(&batch, 10), Some(rect(192.0, 54.0, 120.0, 60.0)));
}
fn touch_down(path: Vec<NodeId>) -> EventPayload {
EventPayload::Touch(Box::new(crate::TouchEvent {
phase: crate::TouchPhase::Start,
timestamp: 0.0,
points: vec![crate::TouchPoint {
id: 1,
target: path[0],
path,
page: point(px(5.0), px(6.0)),
location: point(px(5.0), px(6.0)),
changed: true,
}],
activated: None,
}))
}
#[test]
fn overlays_hear_presses_outside_and_cancel_from_the_top() {
use StyleValue::Number;
let mut c = core();
add(&mut c, 2, Tag::View, ROOT_ID, &[("height", Number(40.0))]);
add(&mut c, 4, Tag::Overlay, 2, &[]);
add(&mut c, 6, Tag::Overlay, 4, &[]);
apply(
&mut c,
vec![
overlay_prop(
4,
crate::OverlayProp::Anchor(crate::overlay::OverlayAnchor::Node(2)),
),
Op::Listen {
id: 4,
event: EventKind::PressOutside,
enabled: true,
},
Op::Listen {
id: 4,
event: EventKind::Cancel,
enabled: true,
},
Op::Listen {
id: 6,
event: EventKind::Cancel,
enabled: true,
},
],
);
let batch = c.commit().unwrap();
for id in [ROOT_ID, 2, 4, 6] {
let events = batch.items.iter().find_map(|i| match i {
MountItem::Events { id: i, events } if *i == id => Some(*events),
_ => None,
});
assert!(events.is_some_and(|e| e.contains(EventKind::Touch)), "{id}");
}
let keymap = batch.items.iter().find_map(|i| match i {
MountItem::Keymap(k) => Some(k.clone()),
_ => None,
});
assert!(keymap.is_some_and(|k| k.bindings().iter().any(|b| b.id == overlay::ESCAPE_BINDING)));
js_events(&mut c);
for (path, outside) in [
(vec![ROOT_ID], true),
(vec![2, ROOT_ID], false),
(vec![4, ROOT_ID], false),
(vec![6, ROOT_ID], false),
] {
c.component_event(ROOT_ID, EventKind::Touch, &touch_down(path.clone()));
let events = js_events(&mut c);
let pressed = events
.iter()
.any(|(id, kind, _)| *id == 4 && *kind == EventKind::PressOutside);
assert_eq!(pressed, outside, "{path:?}");
}
c.component_event(
ROOT_ID,
EventKind::KeyCommand,
&EventPayload::Value(PayloadValue::Number(overlay::ESCAPE_BINDING as f64)),
);
c.commit();
let events = js_events(&mut c);
assert!(
events.contains(&(6, EventKind::Cancel, EventPayload::None)),
"{events:?}"
);
assert!(!events.iter().any(|(id, ..)| *id == 4));
apply(
&mut c,
vec![Op::Listen {
id: 6,
event: EventKind::Cancel,
enabled: false,
}],
);
c.commit();
assert!(c.component_event(ROOT_ID, EventKind::BackPress, &EventPayload::None));
c.commit();
let events = js_events(&mut c);
assert!(
events.contains(&(4, EventKind::Cancel, EventPayload::None)),
"{events:?}"
);
apply(
&mut c,
vec![Op::Remove {
parent: 2,
child: 4,
}],
);
let batch = c.commit().unwrap();
let root_events = batch.items.iter().find_map(|i| match i {
MountItem::Events {
id: ROOT_ID,
events,
} => Some(*events),
_ => None,
});
assert!(root_events.is_some_and(|e| !e.contains(EventKind::Touch)));
}
#[test]
fn overlays_in_a_modal_show_in_it() {
use StyleValue::Number;
let mut c = core();
add(&mut c, 2, Tag::Modal, ROOT_ID, &[]);
add(&mut c, 4, Tag::View, 2, &[("height", Number(40.0))]);
add(
&mut c,
6,
Tag::Overlay,
4,
&[("right", Number(0.0)), ("bottom", Number(0.0))],
);
add(
&mut c,
8,
Tag::View,
6,
&[("width", Number(10.0)), ("height", Number(10.0))],
);
c.set_root_size(2, size_pt(200.0, 300.0), EdgeInsets::default());
let batch = c.commit().unwrap();
assert!(inserts(&batch).contains(&(2, 6, None)));
assert_eq!(frame_of(&batch, 6), Some(rect(190.0, 290.0, 10.0, 10.0)));
}
#[test]
fn overlays_anchored_to_content_go_by_what_the_wrapper_draws() {
use StyleValue::Number;
let mut c = core();
add(
&mut c,
2,
Tag::View,
ROOT_ID,
&[("paddingTop", Number(50.0))],
);
add(&mut c, 4, Tag::View, 2, &[]);
add(
&mut c,
6,
Tag::View,
4,
&[
("width", Number(80.0)),
("height", Number(30.0)),
("alignSelf", StyleValue::Str("flex-start")),
],
);
add(&mut c, 8, Tag::Overlay, 2, &[]);
add(
&mut c,
10,
Tag::View,
8,
&[("width", Number(40.0)), ("height", Number(20.0))],
);
apply(
&mut c,
vec![overlay_prop(
8,
crate::OverlayProp::Anchor(crate::overlay::OverlayAnchor::Content(4)),
)],
);
let batch = c.commit().unwrap();
assert_eq!(frame_of(&batch, 8), Some(rect(20.0, 80.0, 40.0, 20.0)));
apply(
&mut c,
vec![overlay_prop(
8,
crate::OverlayProp::Anchor(crate::overlay::OverlayAnchor::Node(4)),
)],
);
let batch = c.commit().unwrap();
assert_eq!(frame_of(&batch, 8), Some(rect(140.0, 80.0, 40.0, 20.0)));
}
fn texts_of(batch: &MountBatch) -> Vec<String> {
batch
.items
.iter()
.filter_map(|i| match i {
MountItem::Text { text, .. } => Some(text.text.clone()),
_ => None,
})
.collect()
}
fn mounts_listening(batch: &MountBatch, id: NodeId, kind: EventKind) -> Option<bool> {
batch.items.iter().rev().find_map(|i| match i {
MountItem::Events { id: i, events } if *i == id => Some(events.contains(kind)),
_ => None,
})
}
#[test]
fn tooltips_show_after_the_pointer_rests_and_hide_when_it_leaves_or_presses() {
use StyleValue::Number;
let clock = crate::time::Clock::manual(std::time::SystemTime::UNIX_EPOCH);
let _clock = clock.enter();
let mut c = core();
c.set_environment(crate::ui::Environment {
os: crate::platform::Os::Linux,
idiom: crate::platform::Idiom::Desktop,
..crate::ui::Environment::default()
});
add(
&mut c,
2,
Tag::View,
ROOT_ID,
&[
("marginTop", Number(100.0)),
("width", Number(80.0)),
("height", Number(40.0)),
],
);
apply(
&mut c,
vec![Op::SetProp {
id: 2,
prop: Prop::Tooltip(crate::overlay::TooltipContent::Text("Saves it".into())),
}],
);
let batch = c.commit().unwrap();
assert_eq!(mounts_listening(&batch, 2, EventKind::HoverIn), Some(true));
assert!(texts_of(&batch).is_empty(), "{:?}", texts_of(&batch));
c.component_event(2, EventKind::HoverIn, &EventPayload::None);
c.run_component_tasks();
assert!(c.commit().is_none());
clock.advance_to(crate::time::now() + std::time::Duration::from_millis(499));
c.run_component_tasks();
assert!(c.commit().is_none());
clock.advance_to(crate::time::now() + std::time::Duration::from_millis(1));
c.run_component_tasks();
let batch = c.commit().unwrap();
assert_eq!(texts_of(&batch), ["Saves it"]);
let overlay = inserts(&batch)
.into_iter()
.find(|&(p, ..)| p == ROOT_ID)
.map(|(_, child, _)| child)
.expect("the tooltip's overlay in the root view");
let frame = frame_of(&batch, overlay).unwrap();
assert_eq!(frame.bottom(), px(96.0));
let style = batch.items.iter().find_map(|i| match i {
MountItem::ViewStyle { id, style } if *id == overlay => Some(style.clone()),
_ => None,
});
assert!(
style.is_some_and(|s| s.background_color != Color::default() && s.border_radius.0 > 0.0)
);
c.component_event(2, EventKind::HoverOut, &EventPayload::None);
let batch = c.commit().unwrap();
assert!(
batch
.items
.iter()
.any(|i| matches!(i, MountItem::Remove { id } if *id == overlay))
);
c.component_event(2, EventKind::HoverIn, &EventPayload::None);
clock.advance_to(crate::time::now() + std::time::Duration::from_millis(500));
c.run_component_tasks();
assert!(c.commit().is_some());
c.component_event(ROOT_ID, EventKind::Touch, &touch_down(vec![2, ROOT_ID]));
let batch = c.commit().unwrap();
assert!(
batch
.items
.iter()
.any(|i| matches!(i, MountItem::Remove { id } if *id == overlay))
);
clock.advance_to(crate::time::now() + std::time::Duration::from_millis(1000));
c.run_component_tasks();
assert!(c.commit().is_none());
apply(
&mut c,
vec![Op::SetProp {
id: 2,
prop: Prop::Tooltip(crate::overlay::TooltipContent::None),
}],
);
let batch = c.commit().unwrap();
assert_eq!(mounts_listening(&batch, 2, EventKind::HoverIn), Some(false));
}
#[test]
fn tooltips_wait_for_nothing_on_touch_screens() {
use StyleValue::Number;
let mut c = core();
add(&mut c, 2, Tag::View, ROOT_ID, &[("height", Number(40.0))]);
apply(
&mut c,
vec![Op::SetProp {
id: 2,
prop: Prop::Tooltip(crate::overlay::TooltipContent::Text("Saves it".into())),
}],
);
let batch = c.commit().unwrap();
assert_eq!(mounts_listening(&batch, 2, EventKind::HoverIn), None);
}
#[test]
fn a_tooltip_element_is_shown_as_it_is() {
use StyleValue::Number;
let mut c = core();
add(&mut c, 2, Tag::View, ROOT_ID, &[("height", Number(40.0))]);
add(&mut c, 4, Tag::Text, ROOT_ID, &[]);
apply(
&mut c,
vec![
Op::Remove {
parent: ROOT_ID,
child: 4,
},
Op::SetProp {
id: 2,
prop: Prop::Tooltip(crate::overlay::TooltipContent::Node(4)),
},
Op::SetProp {
id: 2,
prop: Prop::Tooltip(crate::overlay::TooltipContent::Node(4)),
},
],
);
c.commit();
let key = c.key(4).unwrap();
let overlay = c.nodes[key as usize].parent;
assert_eq!(c.nodes[overlay as usize].kind, Kind::Overlay);
assert_eq!(c.nodes[c.nodes[overlay as usize].parent as usize].id, 2);
apply(
&mut c,
vec![Op::SetProp {
id: 2,
prop: Prop::Tooltip(crate::overlay::TooltipContent::Text("Hi".into())),
}],
);
c.commit();
assert_eq!(c.nodes[c.key(4).unwrap() as usize].parent, NO_KEY);
let made = c.overlay_state.tooltips[&2];
apply(
&mut c,
vec![
Op::Remove {
parent: ROOT_ID,
child: 2,
},
Op::Release { id: 2 },
],
);
c.commit();
assert!(c.overlay_state.tooltips.is_empty());
assert!(c.key(made.overlay).is_err());
}
#[test]
fn a_tooltip_element_that_is_a_hint_overlay_is_the_tooltip_itself() {
use StyleValue::Number;
let mut c = core();
add(&mut c, 2, Tag::View, ROOT_ID, &[("height", Number(40.0))]);
apply(
&mut c,
vec![
Op::Create {
id: 4,
tag: Tag::Overlay,
},
overlay_prop(
4,
crate::OverlayProp::Hint(Some(std::time::Duration::from_millis(300))),
),
Op::SetProp {
id: 2,
prop: Prop::Tooltip(crate::overlay::TooltipContent::Node(4)),
},
],
);
let wrapper = c.overlay_state.tooltips[&2].overlay;
c.commit();
let made = c.overlay_state.tooltips[&2];
assert_eq!(made.overlay, 4);
assert!(c.key(wrapper).is_err(), "the core's own overlay is gone");
let key = c.key(4).unwrap();
assert_eq!(c.nodes[c.nodes[key as usize].parent as usize].id, 2);
apply(
&mut c,
vec![Op::SetProp {
id: 2,
prop: Prop::Tooltip(crate::overlay::TooltipContent::Text("Hi".into())),
}],
);
c.commit();
assert_eq!(c.nodes[c.key(4).unwrap() as usize].parent, NO_KEY);
assert_ne!(c.overlay_state.tooltips[&2].overlay, 4);
}
fn image_ops(id: NodeId, source: ImageSource, placeholder: Option<ImageSource>) -> Vec<Op> {
let mut ops = vec![
Op::Create {
id,
tag: Tag::Image,
},
Op::SetProp {
id,
prop: Prop::Source(Some(source)),
},
];
if placeholder.is_some() {
ops.push(Op::SetProp {
id,
prop: Prop::Image(ImageProp::Placeholder(placeholder)),
});
}
ops.push(Op::Insert {
parent: ROOT_ID,
child: id,
before: None,
});
ops
}
fn asset(uri: &str, scales: &[f32]) -> ImageSource {
ImageSource {
uri: uri.into(),
size: Some(crate::geometry::size_pt(40.0, 30.0)),
scales: scales.iter().copied().collect(),
}
}
fn image_uris(batch: &MountBatch, id: NodeId) -> Option<(Option<String>, Option<String>)> {
batch.items.iter().rev().find_map(|i| match i {
MountItem::Image {
id: i,
source,
options,
..
} if *i == id => Some((
source.as_ref().map(|s| s.uri.to_string()),
options.placeholder.as_ref().map(|s| s.uri.to_string()),
)),
_ => None,
})
}
fn at_ratio(c: &mut Core<FakeEngine>, pixel_ratio: f32) {
c.set_environment(crate::ui::Environment {
pixel_ratio,
..Default::default()
});
}
#[test]
fn image_scales_pick_the_smallest_at_least_the_ratio_else_the_largest() {
let set: ImageScales = [1.0, 2.0, 3.0].into_iter().collect();
let picks: Vec<_> = [1.0, 1.5, 2.0, 2.625, 3.0, 3.5]
.into_iter()
.map(|r| set.pick(r))
.collect();
assert_eq!(
picks,
[
Some(1.0),
Some(2.0),
Some(2.0),
Some(3.0),
Some(3.0),
Some(3.0)
]
);
let set = |values: [f32; 2]| values.into_iter().collect::<ImageScales>();
assert_eq!(set([1.0, 1.5]).pick(1.2), Some(1.5));
assert_eq!(set([2.0, 4.0]).pick(1.0), Some(2.0));
assert_eq!(ImageScales::NONE.pick(2.0), None);
assert_eq!(
IMAGE_SCALES
.into_iter()
.collect::<ImageScales>()
.iter()
.collect::<Vec<_>>(),
IMAGE_SCALES
);
}
#[test]
fn image_scales_name_the_variants_and_keep_the_1x_file() {
let a = asset("asset:/img/a.png", &[1.0, 1.5, 2.0, 3.0, 4.0]);
let uris: Vec<_> = [1.0, 1.5, 2.0, 3.0, 4.0]
.into_iter()
.map(|r| a.uri_for(r).to_string())
.collect();
assert_eq!(
uris,
[
"asset:/img/a.png",
"asset:/img/a@1.5x.png",
"asset:/img/a@2x.png",
"asset:/img/a@3x.png",
"asset:/img/a@4x.png"
]
);
assert!(std::sync::Arc::ptr_eq(&a.uri_for(1.0), &a.uri));
assert_eq!(
&*asset("asset:/x.y/b", &[1.0, 2.0]).uri_for(2.0),
"asset:/x.y/b@2x"
);
assert_eq!(
&*asset("asset:/.hidden", &[1.0, 2.0]).uri_for(2.0),
"asset:/.hidden@2x"
);
assert_eq!(
&*asset("asset:/c.d.webp", &[3.0]).uri_for(1.0),
"asset:/c.d@3x.webp"
);
}
#[test]
fn image_scales_leave_remote_and_file_uris_alone() {
for uri in [
"https://x.org/a.png",
"http://x.org/a.png",
"/tmp/a.png",
"file:///a.png",
] {
let source = asset(uri, &[1.0, 2.0, 3.0]);
assert!(
std::sync::Arc::ptr_eq(&source.uri_for(3.0), &source.uri),
"{uri}"
);
}
}
#[test]
fn image_scales_leave_uris_with_a_query_or_a_fragment_alone() {
for uri in [
"asset:/a.png?v=2",
"asset:/a.png#frame",
"asset:/dir?x=1/a.png",
"asset:/a#b.png",
] {
let source = asset(uri, &[1.0, 2.0, 3.0]);
for ratio in [1.0, 2.0, 3.0] {
assert!(
std::sync::Arc::ptr_eq(&source.uri_for(ratio), &source.uri),
"{uri} at {ratio}"
);
}
let mut c = core();
at_ratio(&mut c, 3.0);
apply(&mut c, image_ops(2, source, None));
let batch = c.commit().unwrap();
assert_eq!(image_uris(&batch, 2), Some((Some(uri.into()), None)));
}
}
#[test]
fn image_scales_drop_invalid_values() {
let set: ImageScales = [f32::NAN, -2.0, 0.0, 2.5, 5.0, f32::INFINITY, 2.0]
.into_iter()
.collect();
assert_eq!(set.iter().collect::<Vec<_>>(), [2.0]);
assert!(
[f32::NAN, -1.0]
.into_iter()
.collect::<ImageScales>()
.is_empty()
);
}
#[test]
fn image_scales_decode_from_js() {
use crate::js::host::{HostCx, HostInput, HostList, HostValue};
struct J(serde_json::Value);
struct L(Vec<serde_json::Value>);
impl HostList for L {
type Item = J;
fn len(&self) -> usize {
self.0.len()
}
fn item(&self, i: usize) -> Option<J> {
self.0.get(i).cloned().map(J)
}
}
impl HostInput for J {
type List = L;
fn is_nullish(&self) -> bool {
self.0.is_null()
}
fn is_object(&self) -> bool {
self.0.is_object() || self.0.is_array()
}
fn as_bool(&self) -> Option<bool> {
self.0.as_bool()
}
fn as_number(&self) -> Option<f64> {
self.0.as_f64()
}
fn as_string(&self) -> Option<String> {
self.0.as_str().map(str::to_owned)
}
fn field(&self, key: &str) -> Option<J> {
self.0.get(key).cloned().map(J)
}
fn as_list(&self) -> Option<L> {
self.0.as_array().cloned().map(L)
}
fn entries(&self) -> Vec<(String, J)> {
Vec::new()
}
fn truthy(&self) -> bool {
!self.0.is_null()
}
}
let decode = |v: serde_json::Value| ImageSource::decode(&J(v), &mut HostCx::default());
let source = decode(serde_json::json!({
"url": "asset:/a.png", "size": { "width": 40, "height": 30 }, "scales": [1, 2, "3", -1, 2.5, 3]
}))
.unwrap();
assert_eq!(source.size, Some(crate::geometry::size_pt(40.0, 30.0)));
assert_eq!(source.scales.iter().collect::<Vec<_>>(), [1.0, 2.0, 3.0]);
let plain =
decode(serde_json::json!({ "url": "asset:/a.png", "size": { "width": 40, "height": 30 } }));
assert_eq!(plain.unwrap().scales, ImageScales::NONE);
assert_eq!(
decode(serde_json::json!("https://x.org/a.png"))
.unwrap()
.scales,
ImageScales::NONE
);
let odd = decode(serde_json::json!({ "url": "asset:/a.png", "scales": "2" })).unwrap();
assert!(odd.scales.is_empty());
let long = decode(serde_json::json!({
"url": "asset:/a.png", "scales": [1, 1, 1, 1, 1, 1, 1, 1, 2]
}));
assert_eq!(long.unwrap().scales.iter().collect::<Vec<_>>(), [1.0]);
}
#[test]
fn image_scales_mount_the_variant_for_the_pixel_ratio() {
for (ratio, file) in [
(1.0, "a.png"),
(2.0, "a@2x.png"),
(2.625, "a@3x.png"),
(3.0, "a@3x.png"),
] {
let mut c = core();
at_ratio(&mut c, ratio);
let mut ops = image_ops(
2,
asset("asset:/a.png", &[1.0, 2.0, 3.0]),
Some(asset("asset:/p.png", &[1.0, 2.0])),
);
ops.extend(image_ops(3, asset("asset:/a.png", &[1.0, 2.0, 3.0]), None));
ops.push(style_op(3, &[("alignSelf", StyleValue::Str("flex-start"))]));
apply(&mut c, ops);
let batch = c.commit().unwrap();
let placeholder = if ratio > 1.0 { "p@2x.png" } else { "p.png" };
assert_eq!(
image_uris(&batch, 2),
Some((
Some(format!("asset:/{file}")),
Some(format!("asset:/{placeholder}"))
)),
"at {ratio}"
);
assert_eq!(
frame_of(&batch, 3).map(|f| f.size),
Some(crate::geometry::size_pt(40.0, 30.0)),
"at {ratio}"
);
}
}
#[test]
fn image_scales_change_nothing_without_scales() {
let mut c = core();
at_ratio(&mut c, 3.0);
let plain = asset("asset:/a.png", &[]);
apply(
&mut c,
image_ops(2, plain.clone(), Some(asset("asset:/a@3x.png", &[]))),
);
let batch = c.commit().unwrap();
let source = batch.items.iter().find_map(|i| match i {
MountItem::Image { id: 2, source, .. } => source.clone(),
_ => None,
});
assert_eq!(source, Some(plain));
assert_eq!(
image_uris(&batch, 2),
Some((Some("asset:/a.png".into()), Some("asset:/a@3x.png".into())))
);
at_ratio(&mut c, 2.0);
let batch = c.commit();
assert!(
batch.is_none_or(|b| !b.items.iter().any(|i| matches!(i, MountItem::Image { .. }))),
"nothing for images without scales"
);
}
mod asset_variants {
use super::*;
use crate::ui::{ImageAsset, prelude::*};
const FLAG: ImageAsset = ImageAsset {
url: "asset:/@demo/flag.png",
size: crate::geometry::size_pt(17.0, 12.0),
scales: &[1.0, 2.0, 3.0],
};
const PLAIN: ImageAsset = ImageAsset {
url: "asset:/@demo/plain.png",
size: crate::geometry::size_pt(10.0, 10.0),
scales: &[],
};
#[component]
fn Flags() -> impl IntoElement {
view()
.child(image(FLAG))
.child(image(PLAIN).placeholder(FLAG))
}
fn shown(pixel_ratio: f32) -> Vec<(Option<String>, Option<String>)> {
let mut c = core();
at_ratio(&mut c, pixel_ratio);
c.mount_root(&crate::ui::Root::new::<Flags>(|| ()));
let batch = c.commit().unwrap();
batch
.items
.iter()
.filter_map(|i| match i {
MountItem::Image { id, .. } => image_uris(&batch, *id),
_ => None,
})
.collect()
}
fn some(uri: &str) -> Option<String> {
Some(uri.to_owned())
}
#[test]
fn asset_variants_pick_for_the_pixel_ratio() {
assert_eq!(
shown(3.0),
[
(some("asset:/@demo/flag@3x.png"), None),
(
some("asset:/@demo/plain.png"),
some("asset:/@demo/flag@3x.png")
)
]
);
assert_eq!(
shown(2.0),
[
(some("asset:/@demo/flag@2x.png"), None),
(
some("asset:/@demo/plain.png"),
some("asset:/@demo/flag@2x.png")
)
]
);
}
#[test]
fn asset_variants_none_keep_the_file() {
assert_eq!(
shown(1.0),
[
(some("asset:/@demo/flag.png"), None),
(
some("asset:/@demo/plain.png"),
some("asset:/@demo/flag.png")
)
]
);
let src: crate::ui::ImageSrc = PLAIN.into();
assert!(src.scales.is_empty());
let src: crate::ui::ImageSrc = FLAG.into();
assert_eq!(src.scales.iter().collect::<Vec<_>>(), [1.0, 2.0, 3.0]);
assert_eq!(src.size, Some((17.0, 12.0)));
}
}
mod rust_lazy_context {
use super::*;
use crate::ui::{Root, prelude::*};
#[derive(Clone, PartialEq, Props)]
struct ProviderProps {
#[prop(lazy)]
children: Children,
}
#[component]
fn Provider(props: ProviderProps) -> impl IntoElement {
provide_context("provided");
let open = signal(false);
view()
.on_press(move || open.set(true))
.child(when(open, move || props.children))
}
#[component]
fn Consumer() -> impl IntoElement {
text(use_context::<&'static str>().unwrap_or("missing"))
}
#[component]
fn App() -> impl IntoElement {
provider().child(consumer())
}
#[test]
fn rust_lazy_children_inherit_the_context_where_they_are_placed() {
let mut c = core();
c.mount_root(&Root::of::<App>());
let batch = c.commit().unwrap();
let trigger = batch
.items
.iter()
.find_map(|i| match i {
MountItem::Events { id, events } if events.contains(EventKind::Press) => Some(*id),
_ => None,
})
.unwrap();
c.component_event(trigger, EventKind::Press, &EventPayload::None);
let batch = c.commit().unwrap();
let texts: Vec<_> = batch
.items
.iter()
.filter_map(|i| match i {
MountItem::Text { text, .. } => Some(text.text.as_str()),
_ => None,
})
.collect();
assert_eq!(texts, ["provided"]);
}
}
#[test]
fn text_takes_the_tokens_sans_font() {
let mut c = core();
apply(
&mut c,
vec![
Op::Create {
id: 2,
tag: Tag::Text,
},
Op::CreateText {
id: 3,
text: "hello".into(),
},
Op::Insert {
parent: 2,
child: 3,
before: None,
},
Op::Insert {
parent: ROOT_ID,
child: 2,
before: None,
},
],
);
let text = text_item(&c.commit().unwrap()).unwrap();
assert_eq!(text.spans[0].style.font_family, None);
let mut tokens = crate::ui::tokens::sample();
tokens.set_font(crate::ui::FONT_SANS, "Inter");
c.set_tokens(tokens);
let text = text_item(&c.commit().unwrap()).unwrap();
assert_eq!(
text.spans[0].style.font_family,
Some(crate::style::FontFamily::new("Inter"))
);
}