From b5b1300783d61a58be37f28e1b723ccfb5327d4d Mon Sep 17 00:00:00 2001 From: David Mann Date: Thu, 19 Nov 2026 00:55:45 +0100 Subject: [PATCH] Lazy::show_or: a view that leaves its room takes none next has been red since #2273 on two counts, both in the dock's settling host view (3bcc5bec4): - design-lint: contract/lazy.rs named `egui::LayerId` for the run's layer. The run now holds the runtime's `Layer`, the type `Place` and `replay::close` already use. - hovering_a_tab_runs_only_its_group: the dock ran its host view in a `scope_builder`, which takes room and item spacing in the Lazy's child even when the host leaves the group to the dock. A replayed group then sat 4 points lower in flow than a group that ran, so the child's extent changed whenever a hovered group ran, layout memory asked for a discard pass, and that pass ran every group (5 runs where 1 is expected). `show_or` now runs `add` in a child `Ui` that takes room only when it returns `Some`, so `otherwise` takes the same place whether it runs or replays. --- crates/ui-contract/src/contract/lazy.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/crates/ui-contract/src/contract/lazy.rs b/crates/ui-contract/src/contract/lazy.rs index bc6ffb266..c65a6cf75 100644 --- a/crates/ui-contract/src/contract/lazy.rs +++ b/crates/ui-contract/src/contract/lazy.rs @@ -3,6 +3,7 @@ use crate::layout::{flow_region, take_in_flow}; use crate::runtime::egui::alloc; +use crate::runtime::egui::areas::Layer; use crate::runtime::env::Env; use crate::runtime::focus; use crate::runtime::layout_memory; @@ -100,12 +101,23 @@ impl Lazy { /// again in the next pass rather than replay it. A dock group's host view is one: where the host leaves the group /// to the dock, the dock's group shows in its place and settles by its own rules. `None` when the child did not /// run: it replayed a run of `add`, or was out of view. + /// + /// `add` runs in a `Ui` that takes room in the child only when it returns `Some`: an `add` that leaves its room + /// leaves the child as it found it, so `otherwise` takes the same place whether it runs or replays, and the child's + /// extent does not change between them (a change would cost the frame a discard pass). pub fn show_or(self, ui: &mut Ui, add: impl FnOnce(&mut Ui) -> Option, otherwise: impl FnOnce(&mut Ui) -> U) -> Option> { - self.run(ui, |ui, run| match add(ui) { - Some(shown) => Ok(shown), - None => { - run.unkept(ui.ctx()); - Err(otherwise(ui)) + self.run(ui, |ui, run| { + let mut first = alloc::child(ui, UiBuilder::new().max_rect(flow_region(ui)).layout(*ui.layout())); + match add(&mut first) { + Some(shown) => { + alloc::advance_past(ui, first.min_rect()); + Ok(shown) + } + None => { + drop(first); + run.unkept(ui.ctx()); + Err(otherwise(ui)) + } } }) } @@ -206,7 +218,7 @@ impl Lazy { struct Run { recording: bool, kept: bool, - layer: egui::LayerId, + layer: Layer, } impl Run {