Return the size from draw, and fold placing back into drawing
Review response. `Painter::set_size` is gone: `Widget::draw` returns the `Size` instead, so a widget that does not say what it used cannot compile rather than panicking at the widget that forgot. That also settles setting it twice -- a branch that learns something late just returns a different value. `Painter::place` and `UiRenderState::place` are gone too. `draw_inner` already tried to reuse an active widget's drawing before redrawing it, so `place` was `widget_within` with its own bookkeeping bolted on; drawing a child a second time now *is* how a parent puts it where it belongs, and a child is deduplicated in `children` because listing one twice would move it twice. The unification also drops `place`'s use of `ActiveData::layer`, which is the layer a widget's own `child_layer()` left the painter on rather than the layer it was drawn into. What `place` did unconditionally and `widget_within` did not is record the size dependency, so `Painter::size_hint` now records one: reading a child's length to lay out around it is reading its size, whether it came from a draw or from a hint. `tests/retained.rs` has a parent that only ever reads the hint, which is the case no existing widget exercises. `()` sizes itself `Size::default()` -- rest -- rather than zero, so it is a gap that takes an even share of a span; `WidgetPtr` with nothing in it does the same, since it is the same situation. `Widget::on_resize`'s default body said `Translate` while the enum's `#[default]` said `Redraw`; it now defers to the enum. `was` is `old` throughout, the `OnResize` variant comments are gone, and so are two empty `impl` blocks. Checked: fmt, clippy and 27 tests across the workspace; `tabs` on each of its five tabs, and `tabs` with a replay that adds two images, all byte-identical to `upstream/main`; `view` and `minimal` likewise; and the `text` example rendered at 1920x1200 and 900x1200 to see the paragraph reflow and its container follow.
This commit is contained in:
1 parent
b108645240
commit
9520996623
20 files changed
+143
-151
No files matched your search
@@ -32,3 +32,30 @@ fn resizing_relays_out_against_the_new_output() {
|
||||
assert_corners!(h, left, (0, 0), (100, 100));
|
||||
assert_corners!(h, right, (100, 0), (800, 100));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_empty_widget_takes_a_share_of_a_span() {
|
||||
let mut h = Harness::new((400, 200));
|
||||
let gap = ().add(&mut h.rsc);
|
||||
let right = rect(Color::BLUE).width(100).add(&mut h.rsc);
|
||||
h.set_root((gap, right).span(Dir::RIGHT));
|
||||
|
||||
assert_corners!(h, gap, (0, 0), (300, 200));
|
||||
assert_corners!(h, right, (300, 0), (400, 200));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_child_drawn_twice_moves_once() {
|
||||
let mut h = Harness::new((400, 200));
|
||||
// `Aligned` draws its child twice; listing it twice would move it twice.
|
||||
let inner = rect(Color::BLUE).add(&mut h.rsc);
|
||||
let centered = inner.center().width(200).add(&mut h.rsc);
|
||||
let left = rect(Color::RED).width(100).add(&mut h.rsc);
|
||||
h.set_root((left, centered).span(Dir::RIGHT));
|
||||
assert_corners!(h, inner, (100, 0), (300, 200));
|
||||
|
||||
h.rsc[left].x = Some(Len::abs(150));
|
||||
h.frame();
|
||||
|
||||
assert_corners!(h, inner, (150, 0), (350, 200));
|
||||
}
|
||||
+34
-2
@@ -14,9 +14,9 @@ struct Counted {
|
||||
}
|
||||
|
||||
impl Widget for Counted {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
fn draw(&mut self, _: &mut Painter) -> Size {
|
||||
self.draws.set(self.draws.get() + 1);
|
||||
painter.set_size(self.size);
|
||||
self.size
|
||||
}
|
||||
|
||||
fn on_resize(&self, _: Axis) -> OnResize {
|
||||
@@ -131,3 +131,35 @@ fn a_placed_child_survives_the_next_frame() {
|
||||
assert_corners!(h, top, (0, 0), (400, 80));
|
||||
assert_corners!(h, bottom, (0, 80), (400, 200));
|
||||
}
|
||||
|
||||
/// Lays its child out from the hint alone, never reading what it drew.
|
||||
struct FromHint {
|
||||
inner: StrongWidget,
|
||||
}
|
||||
|
||||
impl Widget for FromHint {
|
||||
fn draw(&mut self, painter: &mut Painter) -> Size {
|
||||
let len = painter.size_hint(&self.inner, Axis::Y).unwrap();
|
||||
let mut region = UiRegion::FULL;
|
||||
region.y.end = region.y.start.offset(len.abs);
|
||||
painter.widget_within(&self.inner, region);
|
||||
Size::REST
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_parent_that_only_read_a_hint_relays_out_when_the_hint_changes() {
|
||||
let mut h = Harness::new((400, 200));
|
||||
let inner = rect(Color::RED).height(80).add(&mut h.rsc);
|
||||
let parent = FromHint {
|
||||
inner: inner.add_strong(&mut h.rsc),
|
||||
}
|
||||
.add(&mut h.rsc);
|
||||
h.set_root(parent);
|
||||
assert_corners!(h, inner, (0, 0), (400, 80));
|
||||
|
||||
h.rsc[inner].y = Some(Len::abs(120));
|
||||
h.frame();
|
||||
|
||||
assert_corners!(h, inner, (0, 0), (400, 120));
|
||||
}
|
||||
Reference in new issue
Block a user