use crate::{
AppContext as _, Context, Entity, InteractiveElement as _, IntoElement, ParentElement as _,
Render, StyleRefinement, Styled as _, TestAppContext, Window, WindowHandle, div,
prelude::FluentBuilder as _, px,
};
use std::{cell::Cell, rc::Rc};
struct Row {
label: u32,
builds: Rc<Cell<usize>>,
}
impl Render for Row {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
self.builds.set(self.builds.get() + 1);
div()
.size_full()
.bg(crate::black())
.hover(|style| style.bg(crate::white()))
.child(format!("row {}", self.label))
}
}
struct Rows {
row: Entity<Row>,
covered: bool,
}
impl Render for Rows {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div()
.relative()
.size(px(300.))
.child(
self.row
.clone()
.cached(StyleRefinement::default().w(px(100.)).h(px(20.))),
)
.when(self.covered, |this| {
this.child(div().absolute().top_0().left_0().size(px(200.)).occlude())
})
}
}
fn window(cx: &mut TestAppContext) -> (WindowHandle<Rows>, Entity<Row>, Rc<Cell<usize>>) {
let builds = Rc::new(Cell::new(0));
let window = cx.add_window({
let builds = builds.clone();
move |_, cx| Rows {
row: cx.new(|_| Row { label: 0, builds }),
covered: false,
}
});
let row = window.update(cx, |rows, _, _| rows.row.clone()).unwrap();
(window, row, builds)
}
fn draw(cx: &mut TestAppContext, window: WindowHandle<Rows>) -> Vec<String> {
cx.update_window(window.into(), |_, window, cx| {
window.draw(cx).clear(cx);
window.describe_rendered_frame()
})
.unwrap()
}
fn notify_parent(cx: &mut TestAppContext, window: WindowHandle<Rows>) {
window.update(cx, |_, _, cx| cx.notify()).unwrap();
}
fn move_mouse(cx: &mut TestAppContext, window: WindowHandle<Rows>, x: f32, y: f32) {
cx.update_window(window.into(), |_, window, cx| {
window.simulate_mouse_move(crate::point(px(x), px(y)), cx);
})
.unwrap();
}
#[test]
fn a_cached_view_is_rendered_again_when_a_hover_it_was_painted_by_changes() {
let mut cx = TestAppContext::single();
let (window, _, builds) = window(&mut cx);
move_mouse(&mut cx, window, 10., 10.);
let hovered = draw(&mut cx, window);
notify_parent(&mut cx, window);
draw(&mut cx, window);
let builds_before = builds.get();
move_mouse(&mut cx, window, 250., 250.);
let left = draw(&mut cx, window);
assert_eq!(builds.get(), builds_before + 1);
assert_ne!(hovered, left);
}
#[test]
fn a_reused_cached_view_keeps_its_layout_nodes() {
let mut cx = TestAppContext::single();
let (window, row, builds) = window(&mut cx);
draw(&mut cx, window);
for _ in 0..3 {
notify_parent(&mut cx, window);
draw(&mut cx, window);
}
assert_eq!(
builds.get(),
1,
"the view is reused while its parent renders"
);
cx.update_window(window.into(), |_, window, _| window.reset_layout_stats())
.unwrap();
row.update(&mut cx, |row, cx| {
row.label = 7;
cx.notify();
});
draw(&mut cx, window);
assert_eq!(builds.get(), 2);
let stats = cx
.update_window(window.into(), |_, window, _| window.layout_stats())
.unwrap();
assert_eq!(
stats.nodes_created, 0,
"the view's nodes should have been kept while it was reused"
);
assert!(stats.nodes_reused > 0);
}
#[test]
fn a_cached_view_covered_while_hovered_is_rendered_on_the_next_frame() {
let mut cx = TestAppContext::single();
let (window, _, builds) = window(&mut cx);
move_mouse(&mut cx, window, 10., 10.);
let hovered = draw(&mut cx, window);
notify_parent(&mut cx, window);
draw(&mut cx, window);
let builds_before = builds.get();
window
.update(&mut cx, |rows, _, cx| {
rows.covered = true;
cx.notify();
})
.unwrap();
let frame_asked_for = |cx: &mut TestAppContext| {
cx.update_window(window.into(), |_, window, _| {
!window.next_frame_callbacks.borrow().is_empty()
})
.unwrap()
};
let mut asked_for_a_frame = frame_asked_for(&mut cx);
let mut look = None;
for _ in 0..3 {
if builds.get() > builds_before {
break;
}
asked_for_a_frame |= frame_asked_for(&mut cx);
look = Some(draw(&mut cx, window));
}
assert_eq!(builds.get(), builds_before + 1);
assert!(
asked_for_a_frame,
"a frame should be asked for to render it"
);
assert_ne!(Some(hovered), look);
}
struct Counted {
label: usize,
model: Option<Entity<Model>>,
builds: Rc<Cell<usize>>,
}
struct Model(usize);
impl Render for Counted {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
self.builds.set(self.builds.get() + 1);
let model = self.model.as_ref().map_or(0, |model| model.read(cx).0);
div()
.flex()
.flex_row()
.child(format!("{} {}", self.label, model))
}
}
struct Siblings {
first: Entity<Counted>,
second: Entity<Counted>,
spacer: f32,
}
impl Render for Siblings {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div()
.flex()
.flex_col()
.child(div().h(px(self.spacer)))
.child(self.first.clone())
.child(self.second.clone())
}
}
struct SiblingsWindow {
window: WindowHandle<Siblings>,
first: Entity<Counted>,
model: Entity<Model>,
first_builds: Rc<Cell<usize>>,
second_builds: Rc<Cell<usize>>,
}
fn siblings(cx: &mut TestAppContext) -> SiblingsWindow {
let first_builds = Rc::new(Cell::new(0));
let second_builds = Rc::new(Cell::new(0));
let model = cx.new(|_| Model(0));
let window = cx.add_window({
let (first_builds, second_builds, model) =
(first_builds.clone(), second_builds.clone(), model.clone());
move |_, cx| Siblings {
first: cx.new(|_| Counted {
label: 1,
model: None,
builds: first_builds,
}),
second: cx.new(|_| Counted {
label: 2,
model: Some(model),
builds: second_builds,
}),
spacer: 10.,
}
});
let first = window.update(cx, |view, _, _| view.first.clone()).unwrap();
SiblingsWindow {
window,
first,
model,
first_builds,
second_builds,
}
}
fn draw_siblings(cx: &mut TestAppContext, window: WindowHandle<Siblings>) -> Vec<String> {
cx.update_window(window.into(), |_, window, cx| {
window.draw(cx).clear(cx);
window.describe_rendered_frame()
})
.unwrap()
}
#[test]
fn a_view_is_rendered_again_only_when_something_it_read_changed() {
let mut cx = TestAppContext::single();
let s = siblings(&mut cx);
draw_siblings(&mut cx, s.window);
assert_eq!((s.first_builds.get(), s.second_builds.get()), (1, 1));
s.window.update(&mut cx, |_, _, cx| cx.notify()).unwrap();
draw_siblings(&mut cx, s.window);
assert_eq!(
(s.first_builds.get(), s.second_builds.get()),
(1, 1),
"notifying the parent leaves its children alone"
);
s.first.update(&mut cx, |first, cx| {
first.label = 3;
cx.notify();
});
draw_siblings(&mut cx, s.window);
assert_eq!((s.first_builds.get(), s.second_builds.get()), (2, 1));
s.model.update(&mut cx, |model, cx| {
model.0 = 5;
cx.notify();
});
draw_siblings(&mut cx, s.window);
assert_eq!(
(s.first_builds.get(), s.second_builds.get()),
(2, 2),
"a model the view read changing renders it again, unobserved"
);
cx.update_window(s.window.into(), |_, window, _| window.refresh())
.unwrap();
draw_siblings(&mut cx, s.window);
assert_eq!((s.first_builds.get(), s.second_builds.get()), (3, 3));
}
#[test]
fn a_moved_view_is_built_again_at_its_layout() {
let mut cx = TestAppContext::single();
let s = siblings(&mut cx);
draw_siblings(&mut cx, s.window);
s.window
.update(&mut cx, |view, _, cx| {
view.spacer = 30.;
cx.notify();
})
.unwrap();
cx.update_window(s.window.into(), |_, window, _| window.reset_layout_stats())
.unwrap();
let moved = draw_siblings(&mut cx, s.window);
assert_eq!((s.first_builds.get(), s.second_builds.get()), (2, 2));
let stats = cx
.update_window(s.window.into(), |_, window, _| window.layout_stats())
.unwrap();
assert_eq!(stats.nodes_created, 0, "the moved views keep their nodes");
cx.update_window(s.window.into(), |_, window, _| {
window.forget_retained_state()
})
.unwrap();
assert_eq!(moved, draw_siblings(&mut cx, s.window));
}
#[test]
fn views_are_rendered_every_frame_without_retention() {
let mut cx = TestAppContext::single();
let s = siblings(&mut cx);
cx.update_window(s.window.into(), |_, window, _| {
window.set_view_retention(false)
})
.unwrap();
draw_siblings(&mut cx, s.window);
let before = (s.first_builds.get(), s.second_builds.get());
s.window.update(&mut cx, |_, _, cx| cx.notify()).unwrap();
draw_siblings(&mut cx, s.window);
assert!(s.first_builds.get() > before.0 && s.second_builds.get() > before.1);
}
struct Sized {
row: Entity<Row>,
width: f32,
}
impl Render for Sized {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div().child(
self.row
.clone()
.cached(StyleRefinement::default().w(px(self.width)).h(px(20.))),
)
}
}
#[test]
fn a_cached_view_is_rendered_again_only_when_it_has_to_be() {
let mut cx = TestAppContext::single();
let builds = Rc::new(Cell::new(0));
let window = cx.add_window({
let builds = builds.clone();
move |_, cx| Sized {
row: cx.new(|_| Row { label: 0, builds }),
width: 100.,
}
});
let draw = |cx: &mut TestAppContext| {
cx.update_window(window.into(), |_, window, cx| {
window.draw(cx).clear(cx);
window.describe_rendered_frame()
})
.unwrap()
};
let change = |cx: &mut TestAppContext, f: fn(&mut Sized)| {
window
.update(cx, |view, _, cx| {
f(view);
cx.notify();
})
.unwrap()
};
let first = draw(&mut cx);
assert_eq!(builds.get(), 1);
change(&mut cx, |_| {});
assert_eq!(first, draw(&mut cx), "a reused view shows what it showed");
assert_eq!(builds.get(), 1);
change(&mut cx, |view| view.width = 150.);
draw(&mut cx);
assert_eq!(builds.get(), 2, "new bounds render it again");
cx.update_window(window.into(), |_, window, _| window.refresh())
.unwrap();
draw(&mut cx);
assert_eq!(builds.get(), 3, "a refreshed window renders every view");
cx.simulate_window_resize(window.into(), crate::size(px(800.), px(600.)));
draw(&mut cx);
change(&mut cx, |_| {});
draw(&mut cx);
assert_eq!(
builds.get(),
4,
"a resize refreshes once, then it is reused"
);
}
struct PopoverRow {
builds: Rc<Cell<usize>>,
}
impl Render for PopoverRow {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
self.builds.set(self.builds.get() + 1);
div().size_full().child(crate::deferred(
div()
.id("popover")
.w(px(50.))
.h(px(20.))
.bg(crate::black())
.hover(|style| style.bg(crate::white())),
))
}
}
struct WithPopover {
row: Entity<PopoverRow>,
}
impl Render for WithPopover {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div().child(
self.row
.clone()
.cached(StyleRefinement::default().w(px(100.)).h(px(20.))),
)
}
}
#[test]
fn a_cached_view_is_rendered_again_when_the_pointer_moves_over_what_it_deferred() {
let mut cx = TestAppContext::single();
let builds = Rc::new(Cell::new(0));
let window = cx.add_window({
let builds = builds.clone();
move |_, cx| WithPopover {
row: cx.new(|_| PopoverRow { builds }),
}
});
let draw = |cx: &mut TestAppContext| {
cx.update_window(window.into(), |_, window, cx| {
window.draw(cx).clear(cx);
window.describe_rendered_frame()
})
.unwrap()
};
let move_to = |cx: &mut TestAppContext, x: f32, y: f32| {
cx.update_window(window.into(), |_, window, cx| {
window.simulate_mouse_move(crate::point(px(x), px(y)), cx);
})
.unwrap();
};
draw(&mut cx);
assert_eq!(builds.get(), 1);
move_to(&mut cx, 500., 500.);
let away = draw(&mut cx);
assert_eq!(
builds.get(),
2,
"the pointer leaving the popover changes its look"
);
window.update(&mut cx, |_, _, cx| cx.notify()).unwrap();
draw(&mut cx);
assert_eq!(builds.get(), 2, "and nothing else does");
move_to(&mut cx, 10., 10.);
let over = draw(&mut cx);
assert_eq!(builds.get(), 3, "the pointer coming back changes it again");
assert_ne!(away, over);
move_to(&mut cx, 500., 500.);
let away_again = draw(&mut cx);
assert_eq!(builds.get(), 4);
assert_eq!(away, away_again);
}
struct NamedInput(&'static str);
impl crate::InputHandler for NamedInput {
fn selected_text_range(
&mut self,
_: bool,
_: &mut Window,
_: &mut crate::App,
) -> Option<crate::UTF16Selection> {
None
}
fn marked_text_range(
&mut self,
_: &mut Window,
_: &mut crate::App,
) -> Option<std::ops::Range<usize>> {
None
}
fn text_for_range(
&mut self,
_: std::ops::Range<usize>,
_: &mut Option<std::ops::Range<usize>>,
_: &mut Window,
_: &mut crate::App,
) -> Option<String> {
Some(self.0.to_string())
}
fn replace_text_in_range(
&mut self,
_: Option<std::ops::Range<usize>>,
_: &str,
_: &mut Window,
_: &mut crate::App,
) {
}
fn replace_and_mark_text_in_range(
&mut self,
_: Option<std::ops::Range<usize>>,
_: &str,
_: Option<std::ops::Range<usize>>,
_: &mut Window,
_: &mut crate::App,
) {
}
fn unmark_text(&mut self, _: &mut Window, _: &mut crate::App) {}
fn bounds_for_range(
&mut self,
_: std::ops::Range<usize>,
_: &mut Window,
_: &mut crate::App,
) -> Option<crate::Bounds<crate::Pixels>> {
None
}
fn character_index_for_point(
&mut self,
_: crate::Point<crate::Pixels>,
_: &mut Window,
_: &mut crate::App,
) -> Option<usize> {
None
}
}
pub(super) fn text_input(focus: crate::FocusHandle, name: &'static str) -> impl IntoElement {
crate::canvas(
|_, _, _| {},
move |_, _, window, cx| window.handle_input(&focus, NamedInput(name), cx),
)
.size_full()
}
struct Field {
focus: crate::FocusHandle,
builds: Rc<Cell<usize>>,
}
impl Render for Field {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
self.builds.set(self.builds.get() + 1);
text_input(self.focus.clone(), "inside")
}
}
struct Inputs {
field: Entity<Field>,
outside: crate::FocusHandle,
cached_first: bool,
}
impl Render for Inputs {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
let cached = self
.field
.clone()
.cached(StyleRefinement::default().w(px(100.)).h(px(20.)))
.into_any_element();
let outside = div()
.w(px(100.))
.h(px(20.))
.child(text_input(self.outside.clone(), "outside"))
.into_any_element();
let children = if self.cached_first {
[cached, outside]
} else {
[outside, cached]
};
div().children(children)
}
}
#[test]
fn a_reused_cached_view_hands_the_platform_its_input_handler() {
for cached_first in [true, false] {
let mut cx = TestAppContext::single();
let builds = Rc::new(Cell::new(0));
let window = cx.add_window({
let builds = builds.clone();
move |_, cx| Inputs {
field: cx.new(|cx| Field {
focus: cx.focus_handle(),
builds,
}),
outside: cx.focus_handle(),
cached_first,
}
});
let (inside, outside) = window
.update(&mut cx, |view, _, cx| {
(view.field.read(cx).focus.clone(), view.outside.clone())
})
.unwrap();
let draw = |cx: &mut TestAppContext| {
cx.update_window(window.into(), |_, window, cx| window.draw(cx).clear(cx))
.unwrap()
};
let handed = |cx: &mut TestAppContext| {
let mut handler = cx
.update_window(window.into(), |_, window, _| {
window.platform_window.take_input_handler()
})
.unwrap()?;
let name = handler.text_for_range(0..1, &mut None);
cx.update_window(window.into(), |_, window, _| {
window.platform_window.set_input_handler(handler)
})
.unwrap();
name
};
let focus = |cx: &mut TestAppContext, handle: &crate::FocusHandle| {
cx.update_window(window.into(), |_, window, cx| window.focus(handle, cx))
.unwrap()
};
focus(&mut cx, &inside);
draw(&mut cx);
assert_eq!(handed(&mut cx).as_deref(), Some("inside"));
let builds_then = builds.get();
for _ in 0..3 {
window.update(&mut cx, |_, _, cx| cx.notify()).unwrap();
draw(&mut cx);
assert_eq!(handed(&mut cx).as_deref(), Some("inside"));
}
assert_eq!(builds.get(), builds_then, "the cached view was reused");
focus(&mut cx, &outside);
draw(&mut cx);
assert_eq!(handed(&mut cx).as_deref(), Some("outside"));
window.update(&mut cx, |_, _, cx| cx.notify()).unwrap();
draw(&mut cx);
assert_eq!(handed(&mut cx).as_deref(), Some("outside"));
focus(&mut cx, &inside);
draw(&mut cx);
window.update(&mut cx, |_, _, cx| cx.notify()).unwrap();
draw(&mut cx);
assert_eq!(handed(&mut cx).as_deref(), Some("inside"));
}
}
struct Card;
impl Render for Card {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div()
.flex()
.flex_col()
.items_center()
.child(div().h(px(10.)).bg(crate::black()).child("card"))
}
}
struct Stretched {
card: Entity<Card>,
spacer: f32,
}
impl Render for Stretched {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div()
.flex()
.flex_col()
.w(px(300.))
.child(div().h(px(self.spacer)))
.child(self.card.clone())
}
}
#[test]
fn a_moved_view_keeps_the_size_its_parent_gave_it() {
let mut cx = TestAppContext::single();
let window = cx.add_window(|_, cx| Stretched {
card: cx.new(|_| Card),
spacer: 10.,
});
let draw = |cx: &mut TestAppContext| {
cx.update_window(window.into(), |_, window, cx| {
window.draw(cx).clear(cx);
window.describe_rendered_frame()
})
.unwrap()
};
draw(&mut cx);
window
.update(&mut cx, |view, _, cx| {
view.spacer = 30.;
cx.notify();
})
.unwrap();
let moved = draw(&mut cx);
cx.update_window(window.into(), |_, window, _| window.forget_retained_state())
.unwrap();
assert_eq!(moved, draw(&mut cx));
}
#[test]
fn a_view_is_rendered_again_when_a_model_it_read_was_updated_without_a_notify() {
let mut cx = TestAppContext::single();
let s = siblings(&mut cx);
draw_siblings(&mut cx, s.window);
assert_eq!((s.first_builds.get(), s.second_builds.get()), (1, 1));
s.model.update(&mut cx, |model, _| model.0 = 7);
s.window.update(&mut cx, |_, _, cx| cx.notify()).unwrap();
let updated = draw_siblings(&mut cx, s.window);
assert_eq!(
(s.first_builds.get(), s.second_builds.get()),
(1, 2),
"only the view that read the model is built again"
);
s.window.update(&mut cx, |_, _, cx| cx.notify()).unwrap();
draw_siblings(&mut cx, s.window);
assert_eq!(
(s.first_builds.get(), s.second_builds.get()),
(1, 2),
"an update is seen once"
);
cx.update_window(s.window.into(), |_, window, _| {
window.forget_retained_state()
})
.unwrap();
assert_eq!(updated, draw_siblings(&mut cx, s.window));
}
#[test]
fn text_that_did_not_change_keeps_its_measurement() {
let mut cx = TestAppContext::single();
let s = siblings(&mut cx);
draw_siblings(&mut cx, s.window);
let stats = |cx: &mut TestAppContext| {
cx.update_window(s.window.into(), |_, window, _| window.layout_stats())
.unwrap()
};
let reset = |cx: &mut TestAppContext| {
cx.update_window(s.window.into(), |_, window, _| window.reset_layout_stats())
.unwrap()
};
reset(&mut cx);
s.window
.update(&mut cx, |view, _, cx| {
view.spacer = 30.;
cx.notify();
})
.unwrap();
draw_siblings(&mut cx, s.window);
let after_move = stats(&mut cx);
assert_eq!(
after_move.measure_rebinds, 0,
"moved text is not measured again"
);
assert_eq!(after_move.measurements_kept, 2);
reset(&mut cx);
s.first.update(&mut cx, |first, cx| {
first.label = 9;
cx.notify();
});
draw_siblings(&mut cx, s.window);
let after_change = stats(&mut cx);
assert_eq!(
(
after_change.measure_rebinds,
after_change.measurements_replayed
),
(0, 1),
"changed text of the same size leaves its node clean"
);
reset(&mut cx);
s.first.update(&mut cx, |first, cx| {
first.label = 12345;
cx.notify();
});
let changed = draw_siblings(&mut cx, s.window);
let after_change = stats(&mut cx);
assert_eq!(
(
after_change.measure_rebinds,
after_change.measurements_replayed
),
(1, 0),
"changed text of another size is measured again"
);
cx.update_window(s.window.into(), |_, window, _| {
window.forget_retained_state()
})
.unwrap();
assert_eq!(changed, draw_siblings(&mut cx, s.window));
}
struct Reader {
child: Entity<Counted>,
builds: Rc<Cell<usize>>,
}
impl Render for Reader {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
self.builds.set(self.builds.get() + 1);
let label = self.child.read(cx).label;
div()
.flex()
.flex_col()
.child(format!("showing {label}"))
.child(self.child.clone())
}
}
#[test]
fn a_view_that_read_a_notified_view_is_built_again_only_if_it_was_updated() {
let mut cx = TestAppContext::single();
let reader_builds = Rc::new(Cell::new(0));
let child_builds = Rc::new(Cell::new(0));
let window = cx.add_window({
let (reader_builds, child_builds) = (reader_builds.clone(), child_builds.clone());
move |_, cx| Reader {
child: cx.new(|_| Counted {
label: 1,
model: None,
builds: child_builds,
}),
builds: reader_builds,
}
});
let child = window.read_with(&cx, |view, _| view.child.clone()).unwrap();
let draw = |cx: &mut TestAppContext| {
cx.update_window(window.into(), |_, window, cx| {
window.draw(cx).clear(cx);
window.describe_rendered_frame()
})
.unwrap()
};
draw(&mut cx);
let first = (reader_builds.get(), child_builds.get());
cx.update(|cx| cx.notify(child.entity_id()));
draw(&mut cx);
assert_eq!(
(reader_builds.get(), child_builds.get()),
(first.0, first.1 + 1),
"a notification alone changes nothing the reader read"
);
child.update(&mut cx, |child, cx| {
child.label = 2;
cx.notify();
});
let updated = draw(&mut cx);
assert_eq!(
(reader_builds.get(), child_builds.get()),
(first.0 + 1, first.1 + 2)
);
cx.update_window(window.into(), |_, window, _| window.forget_retained_state())
.unwrap();
assert_eq!(updated, draw(&mut cx));
}
struct Painter {
color: crate::Hsla,
}
impl Render for Painter {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div().size(px(40.)).bg(self.color)
}
}
#[derive(IntoElement)]
struct Paint {
painter: Entity<Painter>,
color: crate::Hsla,
}
impl crate::RenderOnce for Paint {
fn render(self, _: &mut Window, cx: &mut crate::App) -> impl IntoElement {
let color = self.color;
self.painter.update(cx, |painter, _| painter.color = color);
self.painter
}
}
struct PaintHost {
painter: Entity<Painter>,
red: bool,
}
impl Render for PaintHost {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div().size(px(300.)).child(Paint {
painter: self.painter.clone(),
color: if self.red {
crate::red()
} else {
crate::blue()
},
})
}
}
fn backgrounds(cx: &mut TestAppContext, window: crate::AnyWindowHandle) -> Vec<String> {
cx.update_window(window, |_, window, cx| {
window.draw(cx).clear(cx);
window
.painted_quads()
.iter()
.map(|quad| format!("{:?} {:?}", quad.bounds, quad.background))
.collect()
})
.unwrap()
}
#[test]
fn a_view_written_while_the_window_draws_is_rendered_again() {
let mut cx = TestAppContext::single();
let window = cx.add_window(|_, cx| PaintHost {
painter: cx.new(|_| Painter {
color: crate::blue(),
}),
red: false,
});
let blue = backgrounds(&mut cx, window.into());
assert_eq!(blue, backgrounds(&mut cx, window.into()));
window
.update(&mut cx, |host, _, cx| {
host.red = true;
cx.notify();
})
.unwrap();
let red = backgrounds(&mut cx, window.into());
assert_ne!(blue, red, "the painter was drawn with the color written");
let again = backgrounds(&mut cx, window.into());
assert_eq!(red, again, "and nothing wrote it since");
let reused = cx
.update_window(window.into(), |_, window, cx| {
window.draw(cx).clear(cx);
window.draw(cx).clear(cx);
window.next_frame.retained.reused_any() || window.rendered_frame.retained.reused_any()
})
.unwrap();
assert!(reused, "a frame where nothing changed reuses the views");
}
struct HoverProbe;
impl Render for HoverProbe {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div().size(px(300.)).child(
crate::canvas(
|bounds, window, _| window.insert_hitbox(bounds, crate::HitboxBehavior::Normal),
|bounds, hitbox, window, _| {
let color = if hitbox.is_hovered(window) {
crate::red()
} else {
crate::blue()
};
window.paint_quad(crate::fill(bounds, color));
},
)
.size(px(100.)),
)
}
}
#[test]
fn a_view_is_rendered_again_when_a_hover_an_element_asked_about_changes() {
let mut cx = TestAppContext::single();
let window = cx.add_window(|_, _| HoverProbe);
let move_to = |cx: &mut TestAppContext, x: f32, y: f32| {
cx.update_window(window.into(), |_, window, cx| {
window.simulate_mouse_move(crate::point(px(x), px(y)), cx);
})
.unwrap();
};
move_to(&mut cx, 250., 250.);
backgrounds(&mut cx, window.into());
let away = backgrounds(&mut cx, window.into());
move_to(&mut cx, 10., 10.);
let over = backgrounds(&mut cx, window.into());
assert_ne!(
away, over,
"the pointer over the element changes how it looks"
);
move_to(&mut cx, 250., 250.);
assert_eq!(away, backgrounds(&mut cx, window.into()));
}
struct PointerProbe;
impl Render for PointerProbe {
fn render(&mut self, window: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
let x = window.mouse_position().x;
let wide = window.modifiers().shift;
div().size(px(300.)).child(
div()
.w(if wide { px(80.) } else { x })
.h(px(10.))
.bg(crate::red()),
)
}
}
#[test]
fn a_view_that_read_the_pointer_or_modifiers_is_rendered_again_when_they_change() {
let mut cx = TestAppContext::single();
let window = cx.add_window(|_, _| PointerProbe);
let at = |cx: &mut TestAppContext, x: f32, modifiers: crate::Modifiers| {
cx.update_window(window.into(), |_, window, cx| {
window.dispatch_event(
crate::PlatformInput::MouseMove(crate::MouseMoveEvent {
position: crate::point(px(x), px(5.)),
pressed_button: None,
modifiers,
}),
cx,
);
})
.unwrap();
backgrounds(cx, window.into())
};
let first = at(&mut cx, 20., crate::Modifiers::default());
let moved = at(&mut cx, 60., crate::Modifiers::default());
assert_ne!(first, moved, "the view reads where the pointer is");
let shifted = at(
&mut cx,
60.,
crate::Modifiers {
shift: true,
..Default::default()
},
);
assert_ne!(moved, shifted, "and which modifiers are held");
}
struct TextField {
focus: crate::FocusHandle,
notify_when_asked: Cell<bool>,
builds: Rc<Cell<usize>>,
}
impl Render for TextField {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
self.builds.set(self.builds.get() + 1);
let (focus, field) = (self.focus.clone(), cx.entity());
crate::canvas(
|_, _, _| {},
move |bounds, _, window, cx| {
window.handle_input(&focus, crate::ElementInputHandler::new(bounds, field), cx)
},
)
.w(px(100.))
.h(px(20.))
}
}
impl crate::EntityInputHandler for TextField {
fn text_for_range(
&mut self,
_: std::ops::Range<usize>,
_: &mut Option<std::ops::Range<usize>>,
_: &mut Window,
_: &mut Context<Self>,
) -> Option<String> {
None
}
fn selected_text_range(
&mut self,
_: bool,
_: &mut Window,
_: &mut Context<Self>,
) -> Option<crate::UTF16Selection> {
None
}
fn marked_text_range(
&self,
_: &mut Window,
_: &mut Context<Self>,
) -> Option<std::ops::Range<usize>> {
None
}
fn unmark_text(&mut self, _: &mut Window, _: &mut Context<Self>) {}
fn replace_text_in_range(
&mut self,
_: Option<std::ops::Range<usize>>,
_: &str,
_: &mut Window,
_: &mut Context<Self>,
) {
}
fn replace_and_mark_text_in_range(
&mut self,
_: Option<std::ops::Range<usize>>,
_: &str,
_: Option<std::ops::Range<usize>>,
_: &mut Window,
_: &mut Context<Self>,
) {
}
fn bounds_for_range(
&mut self,
_: std::ops::Range<usize>,
_: crate::Bounds<crate::Pixels>,
_: &mut Window,
_: &mut Context<Self>,
) -> Option<crate::Bounds<crate::Pixels>> {
None
}
fn character_index_for_point(
&mut self,
_: crate::Point<crate::Pixels>,
_: &mut Window,
_: &mut Context<Self>,
) -> Option<usize> {
None
}
fn accepts_text_input(&self, _: &mut Window, cx: &mut Context<Self>) -> bool {
if self.notify_when_asked.take() {
cx.notify();
}
true
}
}
struct Form {
field: Entity<TextField>,
builds: Rc<Cell<usize>>,
}
impl Render for Form {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
self.builds.set(self.builds.get() + 1);
let asks = self.field.read(cx).notify_when_asked.get();
div()
.flex()
.flex_col()
.child(format!("form {asks}"))
.child(self.field.clone())
}
}
struct Shell {
child: crate::AnyView,
builds: Rc<Cell<usize>>,
}
impl Render for Shell {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
self.builds.set(self.builds.get() + 1);
div()
.flex()
.flex_col()
.child("shell")
.child(self.child.clone())
}
}
#[test]
fn asking_the_focused_text_field_is_not_a_change_unless_it_notifies() {
let mut cx = TestAppContext::single();
let (form_builds, field_builds) = (Rc::new(Cell::new(0)), Rc::new(Cell::new(0)));
let window = cx.add_window({
let (form_builds, field_builds) = (form_builds.clone(), field_builds.clone());
move |_, cx| {
let field = cx.new(|cx| TextField {
focus: cx.focus_handle(),
notify_when_asked: Cell::new(false),
builds: field_builds,
});
Shell {
child: cx
.new(|_| Form {
field,
builds: form_builds,
})
.into(),
builds: Rc::new(Cell::new(0)),
}
}
});
let field = cx.update(|cx| {
window
.read(cx)
.unwrap()
.child
.clone()
.downcast::<Form>()
.unwrap()
.read(cx)
.field
.clone()
});
let draw = |cx: &mut TestAppContext| {
cx.update_window(window.into(), |_, window, cx| {
window.draw(cx).clear(cx);
window.describe_rendered_frame()
})
.unwrap()
};
let focus = field.read_with(&cx, |field, _| field.focus.clone());
cx.update_window(window.into(), |_, window, cx| window.focus(&focus, cx))
.unwrap();
draw(&mut cx);
draw(&mut cx);
let before = (form_builds.get(), field_builds.get());
for _ in 0..3 {
window.update(&mut cx, |_, _, cx| cx.notify()).unwrap();
draw(&mut cx);
}
assert_eq!(
(form_builds.get(), field_builds.get()),
before,
"being asked every frame changes nothing the form read"
);
field.read_with(&cx, |field, _| field.notify_when_asked.set(true));
draw(&mut cx);
let asked = (form_builds.get(), field_builds.get());
let notified = draw(&mut cx);
assert_eq!(
(form_builds.get(), field_builds.get()),
(asked.0 + 1, asked.1 + 1),
"a field that notified while asked is built again, with the form that read it"
);
cx.update_window(window.into(), |_, window, _| window.forget_retained_state())
.unwrap();
assert_eq!(notified, draw(&mut cx));
}
struct Ping;
impl crate::EventEmitter<Ping> for Model {}
struct Subscriber {
model: Entity<Model>,
builds: Rc<Cell<usize>>,
_subscription: crate::Subscription,
}
impl Render for Subscriber {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
self.builds.set(self.builds.get() + 1);
div().child(format!("model {}", self.model.read(cx).0))
}
}
struct Subscribers {
first: Entity<Subscriber>,
second: Entity<Subscriber>,
bystander: Entity<Counted>,
}
impl Render for Subscribers {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div()
.flex()
.flex_col()
.child(self.first.clone())
.child(self.second.clone())
.child(self.bystander.clone())
}
}
#[test]
fn a_view_updated_without_a_notify_is_built_again_only_inside_a_notified_view() {
let mut cx = TestAppContext::single();
let builds = Rc::new(Cell::new(0));
let model = cx.new(|_| Model(0));
let window = cx.add_window({
let (builds, model) = (builds.clone(), model.clone());
move |_, cx| {
let mut subscriber = || {
cx.new(|cx| Subscriber {
_subscription: cx.subscribe(&model, |_, _, _: &Ping, _| {}),
model: model.clone(),
builds: builds.clone(),
})
};
Subscribers {
first: subscriber(),
second: subscriber(),
bystander: cx.new(|_| Counted {
label: 0,
model: None,
builds: Rc::new(Cell::new(0)),
}),
}
}
});
let bystander = window
.read_with(&cx, |view, _| view.bystander.clone())
.unwrap();
let draw = |cx: &mut TestAppContext| {
cx.update_window(window.into(), |_, window, cx| {
window.draw(cx).clear(cx);
window.describe_rendered_frame()
})
.unwrap()
};
let emit = |cx: &mut TestAppContext| model.update(cx, |_, cx| cx.emit(Ping));
draw(&mut cx);
assert_eq!(builds.get(), 2);
emit(&mut cx);
draw(&mut cx);
assert_eq!(builds.get(), 2, "an ignored event changes nothing drawn");
emit(&mut cx);
bystander.update(&mut cx, |bystander, cx| {
bystander.label += 1;
cx.notify();
});
draw(&mut cx);
assert_eq!(
builds.get(),
2,
"nor does it when a sibling is notified and the parent is drawn around it"
);
emit(&mut cx);
window.update(&mut cx, |_, _, cx| cx.notify()).unwrap();
draw(&mut cx);
assert_eq!(
builds.get(),
4,
"inside a notified view, what was updated since is built again"
);
model.update(&mut cx, |model, cx| {
model.0 = 3;
cx.emit(Ping);
cx.notify();
});
let changed = draw(&mut cx);
assert_eq!(builds.get(), 6, "a model updated and notified has changed");
cx.update_window(window.into(), |_, window, _| window.forget_retained_state())
.unwrap();
assert_eq!(changed, draw(&mut cx));
}
struct Theme(usize);
impl crate::Global for Theme {}
struct AsksForTheme(Rc<Cell<usize>>);
impl Render for AsksForTheme {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
self.0.set(self.0.get() + 1);
div().child(format!("themed {}", cx.has_global::<Theme>()))
}
}
struct ReadsTheme(Rc<Cell<usize>>);
impl Render for ReadsTheme {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
self.0.set(self.0.get() + 1);
let theme = cx.try_global::<Theme>().map_or(0, |theme| theme.0);
div().child(format!("theme {theme}"))
}
}
struct Themed {
asks: Entity<AsksForTheme>,
reads: Entity<ReadsTheme>,
}
impl Render for Themed {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div()
.flex()
.flex_col()
.child(self.asks.clone())
.child(self.reads.clone())
}
}
#[test]
fn a_view_that_asked_whether_a_global_is_set_is_built_again_only_when_that_changes() {
let mut cx = TestAppContext::single();
let (asks, reads) = (Rc::new(Cell::new(0)), Rc::new(Cell::new(0)));
cx.update(|cx| cx.set_global(Theme(0)));
let window = cx.add_window({
let (asks, reads) = (asks.clone(), reads.clone());
move |_, cx| Themed {
asks: cx.new(|_| AsksForTheme(asks)),
reads: cx.new(|_| ReadsTheme(reads)),
}
});
let draw = |cx: &mut TestAppContext| {
cx.update_window(window.into(), |_, window, cx| {
window.draw(cx).clear(cx);
window.describe_rendered_frame()
})
.unwrap()
};
let builds = || (asks.get(), reads.get());
draw(&mut cx);
assert_eq!(builds(), (1, 1));
cx.update(|cx| cx.global_mut::<Theme>().0 = 1);
draw(&mut cx);
assert_eq!(builds(), (1, 2), "global_mut writes the theme");
cx.update_global::<Theme, _>(|theme, _| theme.0 = 2);
draw(&mut cx);
assert_eq!(builds(), (1, 3), "update_global writes the theme");
cx.update(|cx| cx.set_global(Theme(3)));
draw(&mut cx);
assert_eq!(builds(), (1, 4), "setting a theme that was set writes it");
cx.update(|cx| cx.remove_global::<Theme>());
draw(&mut cx);
assert_eq!(builds(), (2, 5), "removing it changes whether it is set");
cx.update(|cx| cx.set_global(Theme(4)));
let set = draw(&mut cx);
assert_eq!(builds(), (3, 6), "setting it again changes that back");
cx.update_window(window.into(), |_, window, _| window.forget_retained_state())
.unwrap();
assert_eq!(set, draw(&mut cx));
}
fn shell(
cx: &mut TestAppContext,
child: impl FnOnce(&mut crate::App) -> crate::AnyView + 'static,
) -> (WindowHandle<Shell>, Rc<Cell<usize>>) {
let builds = Rc::new(Cell::new(0));
let window = cx.add_window({
let builds = builds.clone();
move |_, cx| Shell {
child: child(cx),
builds,
}
});
(window, builds)
}
fn draw_shell(cx: &mut TestAppContext, window: WindowHandle<Shell>) -> Vec<String> {
cx.update_window(window.into(), |_, window, cx| {
window.draw(cx).clear(cx);
window.describe_rendered_frame()
})
.unwrap()
}
struct Tinted {
tint: usize,
model: Option<Entity<Model>>,
builds: Rc<Cell<usize>>,
}
impl Render for Tinted {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
self.builds.set(self.builds.get() + 1);
let model = self.model.as_ref().map_or(0, |model| model.read(cx).0);
let color = if (self.tint + model).is_multiple_of(2) {
crate::black()
} else {
crate::white()
};
div().w(px(50.)).h(px(20.)).bg(color)
}
}
#[test]
fn a_view_around_one_whose_model_changed_is_drawn_around_it() {
let mut cx = TestAppContext::single();
let inner_builds = Rc::new(Cell::new(0));
let model = cx.new(|_| Model(0));
let (window, outer_builds) = shell(&mut cx, {
let (inner_builds, model) = (inner_builds.clone(), model.clone());
move |cx| {
cx.new(|_| Tinted {
tint: 0,
model: Some(model),
builds: inner_builds,
})
.into()
}
});
let before = draw_shell(&mut cx, window);
assert_eq!((outer_builds.get(), inner_builds.get()), (1, 1));
model.update(&mut cx, |model, cx| {
model.0 = 1;
cx.notify();
});
assert_eq!((outer_builds.get(), inner_builds.get()), (1, 2));
let changed = describe_shell(&mut cx, window);
assert_ne!(before, changed);
cx.update_window(window.into(), |_, window, _| window.forget_retained_state())
.unwrap();
assert_eq!(changed, draw_shell(&mut cx, window));
}
#[test]
fn a_view_drawn_around_a_nested_view_keeps_its_dispatch_tree() {
let mut cx = TestAppContext::single();
let inner_builds = Rc::new(Cell::new(0));
let model = cx.new(|_| Model(0));
let (window, outer_builds) = shell(&mut cx, {
let (inner_builds, model) = (inner_builds.clone(), model.clone());
move |cx| {
cx.new(|_| Tinted {
tint: 0,
model: Some(model),
builds: inner_builds,
})
.into()
}
});
let describe = |cx: &mut TestAppContext| {
cx.update_window(window.into(), |_, window, _| {
let tree = &window.rendered_frame.dispatch_tree;
tree.nodes
.iter()
.map(|node| (node.parent.map(|parent| parent.0), node.view_id))
.collect::<Vec<_>>()
})
.unwrap()
};
draw_shell(&mut cx, window);
let built = describe(&mut cx);
for value in 1..4 {
model.update(&mut cx, |model, cx| {
model.0 = value;
cx.notify();
});
assert_eq!(
describe(&mut cx),
built,
"after {value} frames drawn around"
);
}
assert_eq!((outer_builds.get(), inner_builds.get()), (1, 4));
}
fn describe_shell(cx: &mut TestAppContext, window: WindowHandle<Shell>) -> Vec<String> {
cx.update_window(window.into(), |_, window, _| {
window.describe_rendered_frame()
})
.unwrap()
}
struct Pair {
left: crate::AnyView,
right: crate::AnyView,
}
impl Render for Pair {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div()
.flex()
.flex_col()
.child(self.left.clone())
.child(self.right.clone())
}
}
#[test]
fn a_view_drawn_around_a_nested_view_is_reused_on_a_later_frame() {
let mut cx = TestAppContext::single();
let (outer_builds, inner_builds) = (Rc::new(Cell::new(0)), Rc::new(Cell::new(0)));
let model = cx.new(|_| Model(0));
let tinted = |cx: &mut crate::App, model: Option<Entity<Model>>, builds| {
cx.new(|_| Tinted {
tint: 0,
model,
builds,
})
};
let window = cx.add_window({
let (outer_builds, inner_builds, model) =
(outer_builds.clone(), inner_builds.clone(), model.clone());
move |_, cx| {
let inner = tinted(cx, Some(model), inner_builds);
Pair {
left: cx
.new(|_| Shell {
child: inner.into(),
builds: outer_builds,
})
.into(),
right: tinted(cx, None, Rc::new(Cell::new(0))).into(),
}
}
});
let right = window
.read_with(&cx, |pair, _| {
pair.right.clone().downcast::<Tinted>().unwrap()
})
.unwrap();
let draw = |cx: &mut TestAppContext| {
cx.update_window(window.into(), |_, window, cx| {
window.draw(cx).clear(cx);
window.describe_rendered_frame()
})
.unwrap()
};
draw(&mut cx);
model.update(&mut cx, |model, cx| {
model.0 = 1;
cx.notify();
});
let builds_then = (outer_builds.get(), inner_builds.get());
assert_eq!(builds_then, (1, 2), "the shell is drawn around the view");
right.update(&mut cx, |right, cx| {
right.tint = 1;
cx.notify();
});
let later = cx
.update_window(window.into(), |_, window, _| {
window.describe_rendered_frame()
})
.unwrap();
assert_eq!(
(outer_builds.get(), inner_builds.get()),
builds_then,
"nothing the shell read changed since the frame it was drawn around the view"
);
cx.update_window(window.into(), |_, window, _| window.forget_retained_state())
.unwrap();
assert_eq!(later, draw(&mut cx));
}
struct Hoverable(Rc<Cell<usize>>);
impl Render for Hoverable {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
self.0.set(self.0.get() + 1);
div().child(
div()
.id("hoverable")
.w(px(50.))
.h(px(20.))
.bg(crate::black())
.hover(|style| style.bg(crate::white())),
)
}
}
#[test]
fn a_hover_inside_a_nested_view_builds_only_that_view_again() {
let mut cx = TestAppContext::single();
let inner_builds = Rc::new(Cell::new(0));
let (window, outer_builds) = shell(&mut cx, {
let inner_builds = inner_builds.clone();
move |cx| cx.new(|_| Hoverable(inner_builds)).into()
});
let move_to = |cx: &mut TestAppContext, x: f32, y: f32| {
cx.update_window(window.into(), |_, window, cx| {
window.simulate_mouse_move(crate::point(px(x), px(y)), cx);
})
.unwrap();
};
move_to(&mut cx, 500., 500.);
let away = draw_shell(&mut cx, window);
let before = (outer_builds.get(), inner_builds.get());
let hoverable = cx
.update_window(window.into(), |_, window, _| {
window
.rendered_frame
.hitboxes
.last()
.map(|hitbox| hitbox.bounds.center())
})
.unwrap()
.expect("the hoverable element has a hitbox");
move_to(&mut cx, hoverable.x.into(), hoverable.y.into());
let over = draw_shell(&mut cx, window);
assert_eq!(
(outer_builds.get(), inner_builds.get()),
(before.0, before.1 + 1)
);
assert_ne!(away, over);
cx.update_window(window.into(), |_, window, _| window.forget_retained_state())
.unwrap();
assert_eq!(over, draw_shell(&mut cx, window));
move_to(&mut cx, 500., 500.);
let away_again = draw_shell(&mut cx, window);
assert_eq!(
outer_builds.get(),
before.0 + 1,
"only the forgetting built it"
);
assert_eq!(away, away_again);
}
struct AroundNested {
child: Entity<Tinted>,
cached: bool,
builds: Rc<Cell<usize>>,
}
impl Render for AroundNested {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
self.builds.set(self.builds.get() + 1);
let child = if self.cached {
self.child
.clone()
.cached(StyleRefinement::default().w(px(50.)).h(px(20.)))
.into_any_element()
} else {
self.child.clone().into_any_element()
};
div()
.flex()
.flex_col()
.child("around")
.child(child)
.child("after")
}
}
#[test]
fn a_view_around_a_notified_nested_view_is_drawn_around_it() {
for cached in [false, true] {
let mut cx = TestAppContext::single();
let (outer_builds, child_builds) = (Rc::new(Cell::new(0)), Rc::new(Cell::new(0)));
let window = cx.add_window({
let (outer_builds, child_builds) = (outer_builds.clone(), child_builds.clone());
move |_, cx| AroundNested {
child: cx.new(|_| Tinted {
tint: 0,
model: None,
builds: child_builds,
}),
cached,
builds: outer_builds,
}
});
let child = window.read_with(&cx, |view, _| view.child.clone()).unwrap();
let draw = |cx: &mut TestAppContext| {
cx.update_window(window.into(), |_, window, cx| {
window.draw(cx).clear(cx);
window.describe_rendered_frame()
})
.unwrap()
};
let mut last = draw(&mut cx);
assert_eq!((outer_builds.get(), child_builds.get()), (1, 1));
for tint in 1..4 {
child.update(&mut cx, |child, cx| {
child.tint = tint;
cx.notify();
});
let changed = cx
.update_window(window.into(), |_, window, _| {
window.describe_rendered_frame()
})
.unwrap();
assert_eq!(
(outer_builds.get(), child_builds.get()),
(1, tint + 1),
"cached: {cached}, notified {tint} times"
);
assert_ne!(last, changed);
last = changed;
}
cx.update_window(window.into(), |_, window, _| window.forget_retained_state())
.unwrap();
assert_eq!(last, draw(&mut cx), "cached: {cached}");
}
}