diff --git a/crates/ui-panels/src/docking/dock_column/bar.rs b/crates/ui-panels/src/docking/dock_column/bar.rs index 1b34cb9f3..08384181d 100644 --- a/crates/ui-panels/src/docking/dock_column/bar.rs +++ b/crates/ui-panels/src/docking/dock_column/bar.rs @@ -116,16 +116,16 @@ pub(in crate::docking) fn show_bar( /// settles: it runs when that version, its band or something the framework tracks in it changed, or the pointer or /// focus is on it, and otherwise repaints its last run, which reports nothing. pub(in crate::docking) fn settled_bar(ui: &mut Ui, band: Rect, content: Option, show: impl FnOnce(&mut Ui) -> BarRecord) -> BarRecord { - let key = "column bar"; - let lazy = Lazy::new(key, 0.0).settles(content.is_some()); - let lazy = match content { - Some(version) => lazy.content(version), - None => lazy, - }; if !band.is_positive() { return BarRecord::default(); } - ui.scope_builder(UiBuilder::new().max_rect(band).id_salt(key), |ui| { + ui.scope_builder(UiBuilder::new().max_rect(band).id_salt("column bar"), |ui| { + // Keyed by the scope's id, under its column's: unique among the bars of a scroll area's columns. + let lazy = Lazy::new(ui.id(), 0.0).settles(content.is_some()); + let lazy = match content { + Some(version) => lazy.content(version), + None => lazy, + }; lazy.show(ui, |ui| { let record = show(ui); // The grip takes no room in flow; the run covers the band, so the pointer over it runs it again. diff --git a/crates/ui-patterns/src/patterns/dock/area.rs b/crates/ui-patterns/src/patterns/dock/area.rs index 052ff5054..b7759451b 100644 --- a/crates/ui-patterns/src/patterns/dock/area.rs +++ b/crates/ui-patterns/src/patterns/dock/area.rs @@ -781,6 +781,7 @@ fn chrome_key(layout: &Layout

, version: u64, config: &DockConfig, lo side.hash(hasher); columns.iter().for_each(|one| column(one, hasher)); } + // `Node` is non_exhaustive: a variant added later hashes only its discriminant until it is listed here. _ => {} } } @@ -965,15 +966,16 @@ impl Pass<'_, P> { } GroupAt::Show(rect) => rect, }; - // One child `Ui` held to the group's rect, whether the group settles or not, so its contents keep their ids. - let key = ("dock group", PanelKey::of(anchor).0); - let lazy = Lazy::new(key, 0.0).settles(version.is_some()); - let lazy = match version { - Some(version) => lazy.content(version), - None => lazy, - }; + // 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. let output = ui - .scope_builder(UiBuilder::new().max_rect(rect).id_salt(key), |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, + }; 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. diff --git a/crates/ui-patterns/tests/it/docking/runs.rs b/crates/ui-patterns/tests/it/docking/runs.rs index bfe5b5c9f..0a5f79fad 100644 --- a/crates/ui-patterns/tests/it/docking/runs.rs +++ b/crates/ui-patterns/tests/it/docking/runs.rs @@ -5,7 +5,7 @@ use craft_behavior::Axis; use craft_behavior::dock::{Action, Floating, Layout, Node, Permissions, SplitSize, Workspace}; -use craft_ui::contract::{Bind, BodyCx}; +use craft_ui::contract::{Bind, BodyCx, Role}; use craft_ui::docking::{DockConfig, DockPanels, PanelDrag, PanelSpec}; use craft_ui::menu::PanelRow; use craft_ui::patterns::dock::DockArea; @@ -247,3 +247,52 @@ fn a_tab_dropped_on_a_settled_group_lands_there() { let group = layout.place(&4).and_then(|place| layout.node_of(&place)); assert!(matches!(group, Some(Node::Tabs { panels, .. }) if panels.contains(&0)), "the tab joined the third group: {group:?}"); } + +/// Close in a settled group's panel menu closes the panel in the frame after the pick, with the pointer gone from the +/// group: the command the menu queued for the group reaches it. +#[trial] +fn close_in_a_settled_groups_panel_menu_closes_the_panel() { + let (mut fixture, _, workspace) = settling(side_by_side(2), None); + let buttons: Vec<_> = + fixture.by_role(Role::BUTTON).into_iter().filter(|button| fixture.tree().name_of(button.node) == Some("Panel Menu")).collect(); + assert_eq!(buttons.len(), 2, "each group shows its panel menu button"); + fixture.click(&buttons[1]).idle(2); + let close = fixture.by_label("Close").expect("the menu's Close row"); + fixture.click(&close); + fixture.frame(vec![egui::Event::PointerGone]); + assert!(workspace.borrow().layout.place(&2).is_none(), "Close closed the second group's panel"); +} + +/// Panels without versions whose bodies note the id of the `Ui` they run in. +struct Noted { + ids: Rc>>, +} + +impl DockPanels for Noted { + fn spec(&self, panel: &usize) -> PanelSpec { + PanelSpec::new(format!("Panel {panel}")) + } + + fn body(&mut self, panel: &usize, ui: &mut Ui, _: &mut BodyCx<'_>) { + self.ids.borrow_mut().push((*panel, ui.id())); + } +} + +/// Moving another tab to the front of a group keeps the shown body's ids, and with them its scroll, collapsing and +/// text state. +#[trial] +fn a_tab_moved_to_the_front_keeps_the_shown_bodys_ids() { + let ids: Rc>> = Rc::default(); + let workspace = Rc::new(RefCell::new(Workspace::new(Layout::new(Some(Node::tabs(vec![0, 1], 0)), Vec::new())))); + let (shown, mut panels) = (Rc::clone(&workspace), Noted { ids: Rc::clone(&ids) }); + let mut fixture = Fixture::new(move |ui| { + DockArea::new("Dock", Bind::bound(&mut *shown.borrow_mut()), &mut panels, DockConfig::default()).show(ui); + }); + fixture.room(Vec2::new(800.0, 600.0)).settle(); + let body_id = |ids: &RefCell>| ids.borrow().iter().rev().find(|(panel, _)| *panel == 0).map(|(_, id)| *id); + let before = body_id(&ids).expect("panel 0's body ran"); + workspace.borrow_mut().layout.root = Some(Node::tabs(vec![1, 0], 1)); + ids.borrow_mut().clear(); + fixture.frame(Vec::new()); + assert_eq!(body_id(&ids), Some(before), "the shown body keeps its id when another tab moves to the front"); +}