Three things the measurements asked for, all about how much a box that came from an answer costs. **A part in the box's own coordinates.** Saying "less eleven pixels at the end" in frame lengths from the box's start means reading how long the box is, and a container whose box is its own answer then depends on its own answer: `Pad` drew sixty-four times in one resize frame at seed 13, chasing its own width. `Part::Of` says the same thing as a part of the box, which composes without a length -- pixels are pixels wherever the box lands -- and what a child under it holds for maps back through that part onto the container's own box rather than onto the frame. **One axis of the box at a time.** `extent_len` pinned both axes, so a span dividing one of them held for one length of the other as well, and a resize broke every span whose cross-axis answer moved. **No lazy placement.** Leaving a child's answer to be placed at the end of the parent's draw, rather than as the child answers, was meant to save a recomposition. It costs one instead: the drawing is put in the part first and in the answer's box after, and where it does not hold for both that is two drawings rather than one. Seed 1 at depth 8 went from 391 widget draws on a resize to 29 with it gone. The test that pinned three draws for a numeric leaf in a span goes with it. Seed 1 at depth 8, widget draws / distinct widgets / update, against #18's head and against the commit this branch started from: | phase |e44dea3|34cafb6| here | | --- | --- | --- | --- | | cold | 369/261/10.6 | 463/274/13.3 | 516/288/12.0 | | repaint | 1 | 1 | 1 | | many | 157/95/0.33 | 263/108/0.59 | 187/119/0.52 | | size | 16/12/0.018 | 3/3 | 3/3/0.010 | | scroll | 2/0.002 | 1 | 1/0.004 | | resize | 13/13/0.019 | 22/15/0.032 | 24/76/0.090 | Seed 13 at depth 8 is where the protocol still costs: `many` 1091 draws against #18's 524, and `resize` 2215 against a frame #18 does not draw at all. Both are the same shape -- an answer measured in one box and drawn in another -- and the handoff says where that comes from. Checked: fmt, clippy with -D warnings, 108 suite tests, 20 core tests, the 11 generated cases, and the shrinker at 400 trees of depth 5, which fails seeds 2 (repaint) and 108 (reorder).
104 lines
3.9 KiB
Rust
104 lines
3.9 KiB
Rust
//! A measurement that decides control flow.
|
|
//!
|
|
//! Comparing boxes catches a widget that moved. It does not catch a widget
|
|
//! that measured a child, believed a different answer from the one a cold
|
|
//! start would give, and took the other branch -- which is the same defect
|
|
//! arriving somewhere it cannot be ignored. A widget here branches on what it
|
|
//! measured, so a disagreement shows up as a different tree.
|
|
|
|
use iris::harness::Harness;
|
|
use iris::prelude::*;
|
|
|
|
/// Measures `probe` across `axis` and draws one of two children on the
|
|
/// answer. Its own configuration never changes, so which child is drawn is a
|
|
/// property of the layout alone.
|
|
struct BranchesOnMeasurement {
|
|
probe: StrongWidget,
|
|
wide: StrongWidget,
|
|
narrow: StrongWidget,
|
|
threshold: f32,
|
|
}
|
|
|
|
impl Widget for BranchesOnMeasurement {
|
|
fn draw(&mut self, painter: &mut Painter) -> Size {
|
|
let cut = Len::from_parts(Rel::ZERO, Px::from_int(40));
|
|
let top = Place::Within(Part::From(UiSpan::new(Len::ZERO, cut)));
|
|
let measured = painter
|
|
.widget_at(&self.probe, UiRegion::FULL, [Place::Within(Part::All), top])
|
|
.len(Axis::X);
|
|
let px = measured.apply_leftover().to_px(painter.px_len(Axis::X));
|
|
|
|
let below = Place::Within(Part::From(UiSpan::new(cut, painter.extent_len(Axis::Y))));
|
|
let place = [Place::Within(Part::All), below];
|
|
match px > Px::from_f32(self.threshold) {
|
|
true => painter.widget_at(&self.wide, UiRegion::FULL, place),
|
|
false => painter.widget_at(&self.narrow, UiRegion::FULL, place),
|
|
};
|
|
Size::LEFTOVER
|
|
}
|
|
}
|
|
|
|
fn plant(h: &mut Harness, threshold: f32) -> (WidgetId, WidgetId) {
|
|
let words = "the quick brown fox jumps over the lazy dog and keeps running";
|
|
let probe = wtext(words).size(16).wrap(true).add(&mut h.rsc);
|
|
let wide = rect(Color::RED).add(&mut h.rsc);
|
|
let narrow = rect(Color::BLUE).add(&mut h.rsc);
|
|
let branch = BranchesOnMeasurement {
|
|
probe: probe.add_strong(&mut h.rsc),
|
|
wide: wide.add_strong(&mut h.rsc),
|
|
narrow: narrow.add_strong(&mut h.rsc),
|
|
threshold,
|
|
}
|
|
.add(&mut h.rsc);
|
|
let side = rect(Color::GREEN).width(120).add(&mut h.rsc);
|
|
h.set_root((side, branch).span(Dir::RIGHT));
|
|
(wide.id(), narrow.id())
|
|
}
|
|
|
|
/// Which of the two branches drew, as a pair a test can compare.
|
|
fn taken(h: &Harness, wide: WidgetId, narrow: WidgetId) -> (bool, bool) {
|
|
(h.region(&wide).is_some(), h.region(&narrow).is_some())
|
|
}
|
|
|
|
#[test]
|
|
fn a_branch_taken_on_a_measurement_holds_across_repaints() {
|
|
for threshold in [0.0, 200.0, 400.0, 600.0, 779.0, 780.0, 781.0, 2000.0] {
|
|
let mut h = Harness::new((900, 600));
|
|
let (wide, narrow) = plant(&mut h, threshold);
|
|
let first = taken(&h, wide, narrow);
|
|
assert_ne!(first, (false, false), "threshold {threshold}: neither drew");
|
|
|
|
for frame in 0..4 {
|
|
h.rsc.widgets_mut().get_dyn_mut(wide);
|
|
h.rsc.widgets_mut().get_dyn_mut(narrow);
|
|
h.frame();
|
|
assert_eq!(
|
|
taken(&h, wide, narrow),
|
|
first,
|
|
"threshold {threshold}, repaint {frame}: the branch moved when nothing did"
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_branch_taken_on_a_measurement_is_the_one_a_cold_start_takes() {
|
|
for threshold in [0.0, 200.0, 400.0, 600.0, 779.0, 780.0, 781.0, 2000.0] {
|
|
let mut warm = Harness::new((900, 600));
|
|
let (wide, narrow) = plant(&mut warm, threshold);
|
|
warm.resize((640, 480));
|
|
warm.frame();
|
|
warm.rsc.widgets_mut().get_dyn_mut(wide);
|
|
warm.frame();
|
|
|
|
let mut cold = Harness::new((640, 480));
|
|
let (cwide, cnarrow) = plant(&mut cold, threshold);
|
|
|
|
assert_eq!(
|
|
taken(&warm, wide, narrow),
|
|
taken(&cold, cwide, cnarrow),
|
|
"threshold {threshold}: warm and cold took different branches"
|
|
);
|
|
}
|
|
}
|