diff --git a/core/src/ui/painter.rs b/core/src/ui/painter.rs index 8071cce..410cfb9 100644 --- a/core/src/ui/painter.rs +++ b/core/src/ui/painter.rs @@ -32,6 +32,10 @@ pub struct Painter<'a> { /// drawing one that holds for that length alone -- the way reading a /// length in pixels makes it hold for that number of pixels. pub(super) extent_len: [Option; 2], + /// Symbolic box lengths read only to compute the answer. A container can + /// replace the provisional drawings used for that answer with drawings + /// in decided boxes, so this contract is independent of the final one. + pub(super) answer_extent_len: [Option; 2], /// The frame in pixels, which its children's frames are a length of: /// threaded down rather than composed back up the chain, so every length /// in layout is one multiply from its parent's and [`Holds::through`] @@ -58,8 +62,12 @@ pub struct Painter<'a> { pub(super) frame_own: [Holds; 2], /// The same for its own box. pub(super) extent_own: [Holds; 2], - /// Dependencies of every child drawing, including unmeasured overlays. - pub(super) under: LayoutHolds, + /// Pixel-box dependencies used only to compute the answer. These do not + /// constrain a retained drawing placed inside that answer. + pub(super) answer_extent_own: [Holds; 2], + /// The final drawing kept for each child. Asking one child again replaces + /// its provisional drawing and therefore replaces this contract too. + pub(super) under: Vec<(WidgetId, LayoutHolds)>, /// The movable region this widget's primitives are positioned through: /// its own when opted in, otherwise the nearest ancestor's. pub(super) move_idx: MoveIdx, @@ -228,7 +236,11 @@ impl<'a> Painter<'a> { self.rsc, ); let compose = |holds| in_parent(holds, local, extent, place, declared); - self.under = self.under.and(compose(holds)); + let holds = compose(holds); + match self.under.iter_mut().find(|(child, _)| *child == id.id()) { + Some((_, kept)) => *kept = holds, + None => self.under.push((id.id(), holds)), + } DrawResult { child: id, painter: self, @@ -242,6 +254,7 @@ impl<'a> Painter<'a> { /// this frame; what it answered is still something this widget asked. pub fn undraw(&mut self, id: &StrongWidget) { self.children.retain(|child| *child != id.id()); + self.under.retain(|(child, _)| *child != id.id()); self.state.undraw_rec(id.id(), self.rsc); } @@ -356,9 +369,31 @@ impl<'a> Painter<'a> { pub fn extent_len(&mut self, axis: Axis) -> Len { let len = self.extent.axis(axis).len(); self.extent_len[axis as usize] = Some(len); + self.answer_extent_len[axis as usize] = Some(len); len } + /// The symbolic length used to compute this widget's answer, where the + /// final drawing itself is rebuilt without depending on that length. + pub fn answer_extent_len(&mut self, axis: Axis) -> Len { + let len = self.extent.axis(axis).len(); + self.answer_extent_len[axis as usize] = Some(len); + len + } + + /// Says that the final drawing uses a symbolic length already read for + /// the answer. + pub fn drawing_uses_extent_len(&mut self, axis: Axis, len: Len) { + debug_assert_eq!(self.extent.axis(axis).len(), len); + self.extent_len[axis as usize] = Some(len); + } + + /// A part of this widget's box, expressed in its frame coordinates so it + /// can be used as a child frame decided here. + pub fn extent_part(&self, axis: Axis, part: Part) -> UiSpan { + part.of(*self.extent.axis(axis)) + } + /// Where this widget sits in a box longer than the length it takes. A /// widget that positions its own content reads it to place that content /// the way the box around it would have placed the widget. @@ -390,6 +425,14 @@ impl<'a> Painter<'a> { PxVec2::new(self.px_len(Axis::X), self.px_len(Axis::Y)) } + /// This widget's own box in pixels, used only to compute its answer. + pub fn answer_px_size(&mut self) -> PxVec2 { + PxVec2::new( + self.answer_px_len(Axis::X), + self.answer_px_len(Axis::Y), + ) + } + /// One axis of this widget's own box in pixels. Prefer this to /// [`Self::px_size`] when the other axis cannot affect the drawing. pub fn px_len(&mut self, axis: Axis) -> Px { @@ -402,6 +445,17 @@ impl<'a> Painter<'a> { len } + /// One pixel length used only to compute this widget's answer. The final + /// drawing may be retained when that answer is placed in another box. + pub fn answer_px_len(&mut self, axis: Axis) -> Px { + let len = self.extent.axis(axis).len().to_px(self.px.axis(axis)); + let own = &mut self.answer_extent_own[axis as usize]; + if *own == Holds::ANY { + *own = Holds::at(len); + } + len + } + /// The lengths of this widget's own box on `axis` that what it is drawing /// holds for -- the same primitives, in the same fractions and offsets /// of the box, and the same reported size. A widget that read its length @@ -555,7 +609,7 @@ pub(crate) fn in_parent( // own box, which is what lets that box move without a redraw. A // length it pinned is this widget's length wherever the part is // the whole of it, and pins the same way. - (Part::All, None) => { + (Part::All, None) if *frame.axis(axis) == UiSpan::FULL => { result.extent[n] = holds.extent[n]; result.extent_len[n] = holds.extent_len[n]; } diff --git a/core/src/ui/render_state.rs b/core/src/ui/render_state.rs index 531f113..183bb1d 100644 --- a/core/src/ui/render_state.rs +++ b/core/src/ui/render_state.rs @@ -251,7 +251,7 @@ impl UiRenderState { true => None, false => self .retained_answer(id, part, info) - .or_else(|| self.try_reuse(id, frame, part, info, rsc)), + .and_then(|answer| self.try_reuse(id, frame, part, info, rsc).map(|_| answer)), }; let answer = retained.unwrap_or_else(|| { if old.is_none() { @@ -265,49 +265,25 @@ impl UiRenderState { // axis the parent left open. The frame itself does not change, so // nothing under it resolves a fraction a second time. // - // From the answer it gave when its parent measured it, and not from - // what a placing evaluation reported: placing a drawing by what it - // said in the box its own answer chose would move the box out from - // under it. - let measured = match info.offer() { - true => answer.0, - false => self.active[&id].measured().unwrap_or(answer.0), - }; let extent = placed_extent( part, - measured, + answer.0, declared_lens(rsc.widgets(), id), info.fill(), align, ); self.place(id, extent, info, rsc); - // On axes the parent filled, measurement and drawing share an extent. - // Otherwise the answer fixes the final extent as a function of the - // frame, so pull that drawing's validity back through it. let drawing_holds = self.active[&id].holds; - let mut settled = answer; - for axis in AXES { - let n = axis as usize; - settled.1.frame[n] = settled.1.frame[n].and(drawing_holds.frame[n]); - if info.fill()[n] { - settled.1.extent[n] = settled.1.extent[n].and(drawing_holds.extent[n]); - } else { - settled.1.frame[n] = settled.1.frame[n] - .and(drawing_holds.extent[n].through(extent.axis(axis).len())); - } - } let active = self.active.get_mut(&id).unwrap(); // Whoever asked owns how the boxes were reached: the frame it stated, // and what of its own box it gave the drawing. A local redraw asks // the same question again from these. active.frame_abs = frame; active.frame = info.frame; - if info.offer() { - active.answer = Some(answer); - active.offer_place = info.offer_place; - active.offer_part = part; - } + active.answer = Some(answer); + active.offer_place = info.offer_place; + active.offer_part = part; active.place = info.place; active.own_align = align; // A subtree can be reused whole under a different parent -- same box, @@ -322,7 +298,7 @@ impl UiRenderState { { old_parent.children.retain(|child| *child != id); } - (answer.0, answer.1, settled.1) + (answer.0, answer.1, drawing_holds) } /// Recompose retained geometry when the evaluation still holds at this extent. @@ -333,6 +309,15 @@ impl UiRenderState { { return; } + assert_eq!( + extent, + info.part, + "'{}' ({id:?}) does not hold for the box its answer chose: part {:?}, wanted {extent:?}, retained {:?} with {:?}", + rsc.widgets().label(id), + info.part, + self.active.get(&id).map(|active| active.extent), + self.active.get(&id).map(|active| active.holds), + ); #[cfg(feature = "layout-diagnostics")] diag::bump(Counter::PlaceRedraws); let old = self.remove(id, false, rsc); @@ -376,6 +361,7 @@ impl UiRenderState { frame: local, extent, extent_len: [None; 2], + answer_extent_len: [None; 2], px, mask: info.mask, layer: info.layer, @@ -389,8 +375,9 @@ impl UiRenderState { at_offer, size_deps: Vec::new(), frame_own: [Holds::ANY; 2], - under: LayoutHolds::ANY, + under: Vec::new(), extent_own: [Holds::ANY; 2], + answer_extent_own: [Holds::ANY; 2], answer_under: LayoutHolds::ANY, depth: info.depth, move_idx, @@ -419,7 +406,9 @@ impl UiRenderState { primitives, mask_region, extent_own, + answer_extent_own, extent_len, + answer_extent_len, answer_under, children, offered: _, @@ -465,13 +454,23 @@ impl UiRenderState { if let Some(idx) = retired_move { self.moves.remove(idx); } - let own_holds = LayoutHolds { + let drawing_own = LayoutHolds { frame: frame_own, extent: extent_own, extent_len, }; - let answer_holds = own_holds.and(answer_under); - let holds = answer_holds.and(under); + let answer_own = LayoutHolds { + extent: [ + drawing_own.extent[0].and(answer_extent_own[0]), + drawing_own.extent[1].and(answer_extent_own[1]), + ], + extent_len: answer_extent_len, + ..drawing_own + }; + let answer_holds = answer_own.and(answer_under); + let holds = under + .into_iter() + .fold(drawing_own, |holds, (_, child)| holds.and(child)); debug_assert!( holds.contains(px, extent), "'{}' ({id:?}) drew in {px:?}, outside the ranges it reported: {holds:?}", @@ -1179,10 +1178,10 @@ impl UiRenderState { }; let px = self.asked_px(id); let (was_answer, was_holds) = (active.answer, active.holds); - // The boxes its parent gave it, then and now: its frame is the same - // on every ask, so the question its parent asked is the one this - // asks again -- there is no box here that could be its parent's to - // choose instead. + let needs_replacement = active.place != active.offer_place; + // Re-ask the question its answer came from, not a later placement of + // that answer. A parent may move the retained drawing into a box the + // answer chose without making that box a new question. let parent_extent = self.active[&parent].extent; let info = DrawInfo { layer: active.layer, @@ -1193,49 +1192,16 @@ impl UiRenderState { mask: active.parent_mask, frame: active.frame, frame_abs: active.frame_abs, - part: Self::re_ask(active, parent_extent, active.place).1, - place: active.place, + part: Self::re_ask(active, parent_extent, active.offer_place).1, + place: active.offer_place, offer_place: active.offer_place, px, }; - // The ask that measured it, asked again: the box it was measured in - // as its parent left it, rather than where that ask's place resolves - // to now -- a parent drawn again in the box its own answer chose - // gives its children boxes it never measured anything in. - let offered = DrawInfo { - place: info.offer_place, - part: active.offer_part, - ..info - }; #[cfg(feature = "layout-diagnostics")] diag::bump(Counter::LocalRedraws); - // Asked again in the box its parent gave it, which is the question - // its parent asked only while that box is as long as the one it was - // measured in. Any other box is a different question, so the parent - // asks it, with the mark left on. Lengths and not whole boxes: what - // a drawing depends on is its lengths, so the same lengths elsewhere - // is one question. - // - // The frame is the same on every ask now, so this is about the box - // the drawing goes in alone. Removing it -- asking the measuring - // question here and placing the answer afterwards -- is what the - // transparent-frames plan asks for next, and it does not hold yet: - // seeds 104 (`align`) and 210 (`reorder`) at depth 5 settle - // differently warm and cold without it. - if info.part.size() != offered.part.size() { - self.mark(id, rsc.widgets_mut()); - self.mark(parent, rsc.widgets_mut()); - return false; - } let old = self.remove(id, false, rsc); - // The original measurement is refreshed before the assigned slot is - // restored: its lengths may differ even though the frame is - // unchanged. - let answer = self.draw_inner(id, offered, old, rsc); - if info.place != info.offer_place || info.part != offered.part { - self.draw_inner(id, info, None, rsc); - } + let answer = self.draw_inner(id, info, old, rsc); let active = self.active.get_mut(&id).unwrap(); // A wider contract does not invalidate the guarantee the parent kept. // Retain that guarantee so widening and narrowing back do not churn it. @@ -1248,7 +1214,7 @@ impl UiRenderState { if active.holds.covers(was_holds) && was_holds.contains(px, active.extent) { active.holds = was_holds; } - if active.answer != was_answer || active.holds != was_holds { + if needs_replacement || active.answer != was_answer || active.holds != was_holds { // The parent retains both the answer and the drawing's validity; // even an unchanged size can narrow the range safe for a resize. #[cfg(feature = "layout-diagnostics")] diff --git a/src/random.rs b/src/random.rs index b3dc2c7..d27ee70 100644 --- a/src/random.rs +++ b/src/random.rs @@ -124,7 +124,7 @@ impl Widget for Branch { .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 below = Place::Within(Part::Of(UiSpan::new(cut, Len::FULL))); let place = [Place::Within(Part::All), below]; match px > Px::from_f32(self.threshold) { true => painter.widget_at(&self.wide, UiRegion::FULL, place), diff --git a/src/widget/position/span.rs b/src/widget/position/span.rs index 320fda0..865f83f 100644 --- a/src/widget/position/span.rs +++ b/src/widget/position/span.rs @@ -13,8 +13,8 @@ impl Widget for Span { // The row: this span's own box, as a length of the frame its children // are laid out against. Its start is nothing's business -- a slot is // a length from it -- so what this reads is the length alone. - let far = painter.extent_len(axis); - let along = |from: Len, to: Len| match self.dir.sign { + let far = painter.answer_extent_len(axis); + let measure_along = |from: Len, to: Len| match self.dir.sign { Sign::Pos => UiSpan::new(from, to), Sign::Neg => UiSpan::new(far - to, far - from), }; @@ -30,7 +30,7 @@ impl Widget for Span { let mut cursor = Len::rel_min(); let mut lens = Vec::with_capacity(self.children.len()); for child in &self.children { - let room = Place::Within(Part::From(along(cursor, far))); + let room = Place::Fill(Part::From(measure_along(cursor, far))); let len = painter .widget_at(child, UiRegion::FULL, axis.pair(room, across)) .len(axis); @@ -50,9 +50,10 @@ impl Widget for Span { |sum, len| sum + *len, ); + let fixed_total = Len::from_parts(total.rel, total.px); // What is left for the shares to divide: the row less everything // fixed, as a length of the frame rather than a number of pixels. - let room = far - Len::from_parts(total.rel, total.px); + let room = far - fixed_total; // Whether anything is left over is a question in pixels: `rel(0.5)` // beside 300 px is full at 600 and overfull at 400. Asked of `room` // itself, and answered back through the same expression, so the @@ -72,6 +73,18 @@ impl Widget for Span { painter.frame_holds(axis, holds.through(room)); } + if shares { + painter.drawing_uses_extent_len(axis, far); + } + let drawing_far = match shares { + true => far, + false => fixed_total, + }; + let along = |from: Len, to: Len| match self.dir.sign { + Sign::Pos => UiSpan::new(from, to), + Sign::Neg => UiSpan::new(drawing_far - to, drawing_far - from), + }; + // Across itself a span is as long as its longest child -- unless a // rule beside it gives that length outright, and then reading them // answers nothing and makes its size depend on theirs for it. A rule @@ -108,8 +121,19 @@ impl Widget for Span { // Along the row the span says where the child goes, and that slot // is the drawing's box outright rather than something to place an // answer inside again. - let slot = Place::Fill(Part::From(along(from, start))); - let placed = painter.widget_at(child, UiRegion::FULL, axis.pair(slot, across)); + let span = along(from, start); + let (frame, slot) = match len.leftover > Weight::ZERO && shares { + true => ( + UiRegion::from_axis( + axis, + painter.extent_part(axis, Part::From(span)), + UiSpan::FULL, + ), + Place::Fill(Part::All), + ), + false => (UiRegion::FULL, Place::Fill(Part::From(span))), + }; + let placed = painter.widget_at(child, frame, axis.pair(slot, across)); if shrinks { let used = placed.len(!axis); // Choosing between a fixed and a relative length from the diff --git a/src/widget/position/stack.rs b/src/widget/position/stack.rs index f83ba09..32d131b 100644 --- a/src/widget/position/stack.rs +++ b/src/widget/position/stack.rs @@ -33,9 +33,15 @@ impl Widget for Stack { continue; } painter.child_layer_at(i); - // Every other child is drawn in the stack's own box, and where it - // sits in one bigger than itself is its own business. - painter.widget(child); + let place = [Axis::X, Axis::Y].map(|axis| { + let len = size.axis(axis); + let part = match len.leftover > Weight::ZERO { + true => Part::All, + false => Part::From(UiSpan::new(Len::ZERO, Len::from_parts(len.rel, len.px))), + }; + Place::Within(part) + }); + painter.widget_at(child, UiRegion::FULL, place); } size } diff --git a/tests/cases/retained.rs b/tests/cases/retained.rs index 5d97630..061a9db 100644 --- a/tests/cases/retained.rs +++ b/tests/cases/retained.rs @@ -12,6 +12,7 @@ struct Counted { draws: Rc>, size: Size, reads_box: bool, + reads_answer_box: bool, } impl Widget for Counted { @@ -19,6 +20,8 @@ impl Widget for Counted { self.draws.set(self.draws.get() + 1); if self.reads_box { painter.px_size(); + } else if self.reads_answer_box { + painter.answer_px_size(); } self.size } @@ -38,11 +41,18 @@ fn counted(h: &mut Harness, size: Size, reads_box: bool) -> (WeakWidget draws: draws.clone(), size, reads_box, + reads_answer_box: false, } .add(&mut h.rsc); (id, Counts(draws)) } +fn answer_counted(h: &mut Harness, size: Size) -> (WeakWidget, Counts) { + let (id, draws) = counted(h, size, false); + h.rsc[id].reads_answer_box = true; + (id, draws) +} + struct Layered { children: [StrongWidget; 2], _revision: usize, @@ -156,8 +166,8 @@ fn a_span_child_that_declares_its_length_is_drawn_once() { h.set_root((hinted, asked).span(Dir::RIGHT)); assert_eq!(told_draws.get(), 1); - // Only the available length changes: positioning the final slot does - // not invalidate a numeric size read. + // Its final slot is a parent decision, so it is evaluated there after + // the provisional ask established its length. assert_eq!(asked_draws.get(), 2); } @@ -240,7 +250,7 @@ fn a_parent_that_only_read_a_hint_relays_out_when_the_hint_changes() { assert_corners!(h, inner, (0, 0), (400, 120)); } -/// Reads its box's size, which nothing but its own draw can put right. +/// Reads its box's size to compute its answer. struct ReadsBox { draws: Rc>, } @@ -248,26 +258,36 @@ struct ReadsBox { impl Widget for ReadsBox { fn draw(&mut self, painter: &mut Painter) -> Size { self.draws.set(self.draws.get() + 1); - Size::from_px(painter.px_size().div_int(4)) + Size::from_px(painter.answer_px_size().div_int(4)) } } /// Reads its box across one axis only, so its drawing holds for a taller /// box on its own and only a wider one is worth a draw. /// -/// Both of these report a quarter of what they read, without saying that the -/// drawing holds there too, so each length they are asked at costs two draws: -/// one to answer, and one in the quarter-sized box that answer places them -/// in. The counts below are in those pairs. +/// Both report a quarter of what they read. Their empty drawings are +/// independent of that read, so a changed question costs one draw. struct ReadsWidth { draws: Rc>, } +struct ReadsDrawingWidth { + draws: Rc>, +} + +impl Widget for ReadsDrawingWidth { + fn draw(&mut self, painter: &mut Painter) -> Size { + self.draws.set(self.draws.get() + 1); + painter.px_len(Axis::X); + Size::LEFTOVER + } +} + impl Widget for ReadsWidth { fn draw(&mut self, painter: &mut Painter) -> Size { self.draws.set(self.draws.get() + 1); Size::from_px(PxVec2::new( - painter.px_len(Axis::X).div_int(4), + painter.answer_px_len(Axis::X).div_int(4), Px::from_int(20), )) } @@ -323,6 +343,7 @@ fn a_row_moves_what_follows_a_child_that_grew_rather_than_drawing_it() { draws: ruled.clone(), size: Size::LEFTOVER, reads_box: false, + reads_answer_box: false, }; let second = match declared { true => second.width(rel(0.25)).add(&mut h.rsc), @@ -384,7 +405,7 @@ fn a_resize_redraws_what_read_its_box() { h.resize((800, 100)); h.frame(); - assert_eq!(draws.get(), settled + 2); + assert_eq!(draws.get(), settled + 1); } #[test] @@ -404,7 +425,7 @@ fn a_resize_only_redraws_read_axes() { h.resize((800, 300)); h.frame(); - assert_eq!(draws.get(), settled + 2, "width changes its answer"); + assert_eq!(draws.get(), settled + 1, "width changes its answer"); } /// A window is measured onto the grid like everything else, so a resize too @@ -431,7 +452,7 @@ fn a_resize_within_one_step_is_not_a_resize() { h.resize((400.0 + step, 200.0)); h.frame(); - assert_eq!(draws.get(), settled + 2); + assert_eq!(draws.get(), settled + 1); } /// The same for a box that changes because a sibling did: what is compared @@ -500,7 +521,7 @@ fn a_change_two_levels_under_its_reader_still_reaches_it() { // Every wrapper up to the outer pad read the size below it, so the outer // pad is what draws again -- and the span it hands the box to is the same // size as before, which is what lets a draw reuse its way past the leaf. - let (leaf, _) = counted(&mut h, Size::px((100, 100).into()), true); + let (leaf, _) = counted(&mut h, Size::px((100, 100).into()), false); let padded = leaf.pad(10).add(&mut h.rsc); let below = rect(Color::RED).add(&mut h.rsc); h.set_root((padded, below).span(Dir::DOWN).pad(12)); @@ -1066,7 +1087,7 @@ fn a_declared_size_change_stops_at_an_independent_parent() { fn an_unmeasured_child_still_invalidates_its_parents_drawing_on_resize() { let mut h = Harness::new((400, 200)); let draws = Rc::new(Cell::new(0)); - let leaf = ReadsWidth { + let leaf = ReadsDrawingWidth { draws: draws.clone(), } .add(&mut h.rsc); @@ -1077,7 +1098,7 @@ fn an_unmeasured_child_still_invalidates_its_parents_drawing_on_resize() { h.frame(); assert!(draws.get() > settled); - assert_corners!(h, leaf, (300, 90), (500, 110)); + assert_corners!(h, leaf, (0, 0), (800, 200)); } #[test] @@ -1212,7 +1233,7 @@ fn moving_an_extent_child_preserves_the_slot_chosen_from_its_measurement() { struct Measured; impl Widget for Measured { fn draw(&mut self, painter: &mut Painter) -> Size { - let width = painter.px_len(Axis::X); + let width = painter.answer_px_len(Axis::X); painter.primitive(RectPrimitive::color(Color::BLUE)); Size::from((80, if width > Px::from_int(100) { 40 } else { 60 })) } @@ -1312,7 +1333,10 @@ fn extent_frames_keep_fractional_reports_and_numeric_dependencies_valid() { } else { Size::from((80, 27)) }; - let (leaf, _) = counted(h, size, !fractional); + let (leaf, _) = match fractional { + true => counted(h, size, false), + false => answer_counted(h, size), + }; let child = Container { child: leaf.add_strong(&mut h.rsc), region,