diff --git a/crates/ui-contract/src/contract/lazy.rs b/crates/ui-contract/src/contract/lazy.rs index 2d859408f..bc6ffb266 100644 --- a/crates/ui-contract/src/contract/lazy.rs +++ b/crates/ui-contract/src/contract/lazy.rs @@ -92,6 +92,25 @@ impl Lazy { /// Runs `add` in a child `Ui` placed in flow in `ui`, or takes the remembered extent there; `None` when it did not /// run. pub fn show(self, ui: &mut Ui, add: impl FnOnce(&mut Ui) -> R) -> Option { + self.run(ui, |ui, _| add(ui)) + } + + /// [`show`](Self::show) for a child that may leave its room to other content: `add` runs first and settles; when + /// it returns `None`, `otherwise` runs in the same child `Ui` and the run is not kept, so a settling child runs + /// 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. + 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)) + } + }) + } + + fn run(self, ui: &mut Ui, add: impl FnOnce(&mut Ui, &mut Run) -> R) -> Option { let ctx = ui.ctx().clone(); let view = Env::of(ui).view().lazy_view(); let id = match view { @@ -149,17 +168,20 @@ impl Lazy { return None; } let mark = focus::mark(&ctx); - let records = self.settles && real; - if records { + let mut run = Run { recording: self.settles && real, kept: true, layer: place.layer }; + if run.recording { replay::open(&ctx); } let mut child = alloc::child(ui, UiBuilder::new().max_rect(available).layout(layout).scope_id(id)); // Its extent in the body around it comes from its content's measure, so a change inside reaches that body. - let out = layout_memory::boundary(&ctx, false, || add(&mut child)); + let out = layout_memory::boundary(&ctx, false, || add(&mut child, &mut run)); let rect = child.min_rect(); // The child `Ui` writes its widget rect when it drops: the record holds it as written. drop(child); - let record = records.then(|| replay::close(&ctx, place.layer)); + let record = run.recording.then(|| replay::close(&ctx, place.layer)); + if !run.kept { + lazy::forget(&ctx, id); + } if real { layout_memory::backstop(&ctx, id, rect.size()); } @@ -179,3 +201,22 @@ impl Lazy { Some(out) } } + +/// The run of a child: whether it records what it adds for a replay, and whether it is kept. +struct Run { + recording: bool, + kept: bool, + layer: egui::LayerId, +} + +impl Run { + /// Ends the run's record here and keeps it from settling: what the child adds from here on is not what it settles + /// on. + fn unkept(&mut self, ctx: &egui::Context) { + if self.recording { + replay::close(ctx, self.layer); + self.recording = false; + } + self.kept = false; + } +} diff --git a/crates/ui-contract/src/runtime/lazy.rs b/crates/ui-contract/src/runtime/lazy.rs index 9df7a798c..8cefd9fb5 100644 --- a/crates/ui-contract/src/runtime/lazy.rs +++ b/crates/ui-contract/src/runtime/lazy.rs @@ -389,6 +389,15 @@ pub(crate) fn ran(ctx: &Context, id: Id, record: Record, place: Place, rect: Rec }); } +/// Drops the kept run of the settling child `id`: its last run is not one it settles on, so it runs again. +pub(crate) fn forget(ctx: &Context, id: Id) { + with(ctx, |store, _, _| { + if let Some(entry) = store.entries.get_mut(&id) { + entry.settled = None; + } + }); +} + /// Whether the measure-only child `id` takes the size its last measuring run at `place` found, in place of measuring /// again, and that size. It does when that run was in this pass, or in an earlier frame and since then none of the /// settled rules a measure reads has broken (`settled`): nothing in it moves or asked to be shown again, no value it diff --git a/crates/ui-panels/src/docking/panels.rs b/crates/ui-panels/src/docking/panels.rs index bb1d9b206..a988978d6 100644 --- a/crates/ui-panels/src/docking/panels.rs +++ b/crates/ui-panels/src/docking/panels.rs @@ -39,7 +39,9 @@ pub trait DockPanels

{ /// The version of the app data `panel`'s body shows that the framework does not track (a document's revision, a /// job's progress). With one, the body settles (`Lazy::settles` with `Lazy::content`): it runs only when its /// version, its room or something the framework tracks in it changed, the pointer is over it or focus is in it, and - /// otherwise repaints its last run. `None` (the default) runs the body every pass. + /// otherwise repaints its last run. A [`group_view`](Self::group_view) settles the same way under the version of + /// the group's shown panel, its panels and its shown tab: the version covers what the view shows, and whether the + /// host takes the group depends only on its panels. `None` (the default) runs the body, and the view, every pass. fn content(&self, _panel: &P) -> Option { None } @@ -59,7 +61,8 @@ pub trait DockPanels

{ /// `GroupAt::Show(rect)` it shows its view in `ui`, which the dock holds to `rect` and clips to it; at /// `GroupAt::Measure(width)` it measures that view at `width` the way it shows it. It returns the height the view /// takes (at `Show`, the rect's). `None` (the default) leaves the group to the dock, at both. A canvas group uses - /// the panels to tell which document it holds and draws it in the room `ui` has. + /// the panels to tell which document it holds and draws it in the room `ui` has. At `Show` the view settles when + /// the shown panel has a [`content`](Self::content) version: a settled view repaints its last run instead of running. fn group_view(&mut self, _panels: &[P], _active: usize, _ui: &mut Ui, _at: GroupAt, _cx: &mut BodyCx<'_>) -> Option { None } diff --git a/crates/ui-patterns/src/patterns/dock/area.rs b/crates/ui-patterns/src/patterns/dock/area.rs index b7759451b..902dda70e 100644 --- a/crates/ui-patterns/src/patterns/dock/area.rs +++ b/crates/ui-patterns/src/patterns/dock/area.rs @@ -891,25 +891,24 @@ impl Pass<'_, P> { let scene = self.scene; let anchor = members.first()?; let active = active.min(members.len() - 1); - let viewed = match at { - // The view draws in a child `Ui` held to the group's rect and clipped to it: it lays out in the group's - // room, never in the room the area has left. - GroupAt::Show(rect) => { - ui.scope_builder(UiBuilder::new().max_rect(rect), |ui| { - ui.shrink_clip_rect(rect); - self.panels.group_view(members, active, ui, at, body) - }) - .inner - } - GroupAt::Measure(_) => self.panels.group_view(members, active, ui, at, body), - }; - if let Some(height) = viewed { - return matches!(at, GroupAt::Measure(_)).then_some(height); + if let GroupAt::Measure(_) = at + && let Some(height) = self.panels.group_view(members, active, ui, at, body) + { + return Some(height); } let shown = &members[active]; + let content = self.panels.content(shown); + // A host view settles as a panel body does, under the shown panel's content version (`DockPanels::content`): + // it runs again when that version, the group's panels or the shown tab change. + let view_version = content.map(|content| { + use std::hash::Hasher; + let mut hasher = std::collections::hash_map::DefaultHasher::new(); + (content, members, active).hash(&mut hasher); + hasher.finish() + }); // The group settles when the host versions its specs and the shown body: it runs again when the pass's chrome // key, the body's version or how the group shows changes. - let version = scene.chrome.zip(self.panels.content(shown)).map(|(chrome, content)| { + let version = scene.chrome.zip(content).map(|(chrome, content)| { use std::hash::Hasher; let mut hasher = std::collections::hash_map::DefaultHasher::new(); (chrome, content, members, active, look.minimized, look.section, look.in_window).hash(&mut hasher); @@ -967,25 +966,49 @@ impl Pass<'_, P> { GroupAt::Show(rect) => rect, }; // One child `Ui` held to the group's rect, whether the group settles or not. It takes the parent's next id, as - // the group did before it settled, so a change of the group's first tab keeps every id inside; the Lazy is - // keyed by that id, unique among the groups of a dock in a scroll area too. + // the group did before it settled, so a change of the group's first tab keeps every id inside; each Lazy is + // keyed by its `Ui`'s id, unique among the groups of a dock in a scroll area too. The host view runs first and + // settles; where the host leaves the group to the dock, the group shows in its place and settles under its own + // version. let output = ui .scope_builder(UiBuilder::new().max_rect(rect), |ui| { - let lazy = Lazy::new(ui.unique_id(), 0.0).settles(version.is_some()); - let lazy = match version { - Some(version) => lazy.content(version), - None => lazy, + let view = Lazy::new(ui.unique_id(), 0.0).settles(view_version.is_some()); + let view = match view_version { + Some(version) => view.content(version), + None => view, }; - lazy.show(ui, |ui| { - let output = make(active, look.minimized).show_at(ui, rect, |ui, body| panel_body(&panels, shown, ui, body)).output; - // `show_at` takes no room in flow; the run covers the group, so the pointer over it runs it again. - ui.expand_to_include_rect(rect); - output - }) + view.show_or( + ui, + |ui| { + // The view draws held to the group's rect and clipped to it: it lays out in the group's room, + // never in the room the area has left. + let taken = ui + .scope_builder(UiBuilder::new().max_rect(rect), |ui| { + ui.shrink_clip_rect(rect); + panels.borrow_mut().group_view(members, active, ui, at, body) + }) + .inner; + // The run covers the group, so the pointer over it runs the view again. + taken.map(|_| ui.expand_to_include_rect(rect)) + }, + |ui| { + let lazy = Lazy::new(ui.unique_id(), 0.0).settles(version.is_some()); + let lazy = match version { + Some(version) => lazy.content(version), + None => lazy, + }; + lazy.show(ui, |ui| { + let output = make(active, look.minimized).show_at(ui, rect, |ui, body| panel_body(&panels, shown, ui, body)).output; + // `show_at` takes no room in flow; the run covers the group, so the pointer over it runs it again. + ui.expand_to_include_rect(rect); + output + }) + }, + ) }) .inner; - // A settled group replayed its last run: nothing reached it. - let output = output?; + // A host view took the group, or a settled view or group replayed its last run: nothing reached it. + let output = output.and_then(Result::err).flatten()?; // The size `panel` floats at out of this group: the group's width, and the height the group measures at with // `panel` shown in a floating window's room, so content that fills the body takes that room. let fitted = |ui: &Ui, panel: &P| { diff --git a/crates/ui-patterns/tests/it/docking/runs.rs b/crates/ui-patterns/tests/it/docking/runs.rs index 376ef899c..158de715b 100644 --- a/crates/ui-patterns/tests/it/docking/runs.rs +++ b/crates/ui-patterns/tests/it/docking/runs.rs @@ -6,7 +6,7 @@ use craft_behavior::Axis; use craft_behavior::dock::{Action, Floating, Layout, Node, Permissions, SplitSize, Workspace}; use craft_ui::contract::{Bind, BodyCx, Role}; -use craft_ui::docking::{DockConfig, DockPanels, PanelDrag, PanelSpec}; +use craft_ui::docking::{DockConfig, DockPanels, GroupAt, PanelDrag, PanelSpec}; use craft_ui::menu::PanelRow; use craft_ui::patterns::dock::DockArea; use craft_ui::runtime::drag; @@ -121,6 +121,69 @@ fn a_panel_body_with_a_content_version_runs_only_when_its_version_changes() { assert_eq!(frame()[0], 0, "and it settles at it"); } +/// Panel 0's group is the host's view and panel 1's the dock's; both panels give the content version `version`. The +/// view counts its runs where it shows, the bodies theirs. +struct Hosted { + views: Rc>, + bodies: Rc>, + version: Rc>, +} + +impl DockPanels for Hosted { + fn spec(&self, panel: &usize) -> PanelSpec { + PanelSpec::new(format!("Panel {panel}")) + } + + fn body(&mut self, _: &usize, _: &mut Ui, _: &mut BodyCx<'_>) { + self.bodies.set(self.bodies.get() + 1); + } + + fn content(&self, _: &usize) -> Option { + Some(self.version.get()) + } + + fn group_view(&mut self, panels: &[usize], _: usize, _: &mut Ui, at: GroupAt, _: &mut BodyCx<'_>) -> Option { + if !panels.contains(&0) { + return None; + } + match at { + GroupAt::Measure(_) => Some(0.0), + GroupAt::Show(rect) => { + self.views.set(self.views.get() + 1); + Some(rect.height()) + } + } + } +} + +/// A host view settles as a panel body does: it runs in the frame its panel's version changes or the pointer comes +/// over it, and not in a settled frame. The group the host leaves to the dock keeps showing beside it. +#[trial] +fn a_host_view_with_a_content_version_runs_only_when_its_version_changes() { + let (views, bodies, version) = (Rc::new(Cell::new(0)), Rc::new(Cell::new(0)), Rc::new(Cell::new(1))); + let root = Node::split(Axis::X, SplitSize::Ratio(0.5), Node::tabs(vec![0], 0), Node::tabs(vec![1], 0)); + let mut workspace = Workspace::new(Layout::new(Some(root), Vec::new())); + let mut panels = Hosted { views: Rc::clone(&views), bodies: Rc::clone(&bodies), version: Rc::clone(&version) }; + let mut fixture = Fixture::new(move |ui| { + DockArea::new("Dock", Bind::bound(&mut workspace), &mut panels, DockConfig::default()).show(ui); + }); + fixture.room(Vec2::new(1200.0, 600.0)).settle(); + let mut frame = |events: Vec| { + views.set(0); + bodies.set(0); + fixture.frame(events); + (views.get(), bodies.get()) + }; + assert_eq!(frame(Vec::new()), (0, 0), "a settled frame runs neither the view nor the versioned body"); + assert_eq!(frame(Vec::new()), (0, 0), "nor the next one"); + version.set(2); + let (viewed, bodied) = frame(Vec::new()); + assert!(viewed > 0 && bodied > 0, "a new version runs the view and the body: {viewed}, {bodied}"); + assert_eq!(frame(Vec::new()), (0, 0), "both settle at it"); + assert!(frame(vec![moved(Pos2::new(300.0, 300.0))]).0 > 0, "the pointer over the view runs it"); + assert!(fixture.by_label("Panel 1").is_some(), "the dock's group shows beside the view"); +} + /// Panels whose specs and bodies all have versions, so the dock's chrome settles; `plain` is a panel whose body has /// none. struct Settling {