Dock chrome settling: a group keeps its ids when its first tab changes

The group's scope takes the parent's next id, as the group did before it settled, and its Lazy is keyed by that scope's unique id; a column bar's Lazy is keyed by its scope's id under the column. Both stay unique inside a scroll area. Trials: a tab moved to the front keeps the shown body's ids, and Close in a settled group's panel menu closes the panel.
This commit is contained in:
aiongg
2026-11-18 00:23:14 +00:00
parent 12b9ac772f
commit 5f10a501e4
3 changed files with 67 additions and 16 deletions
@@ -116,16 +116,16 @@ pub(in crate::docking) fn show_bar<P: Clone + Send + Sync + 'static>(
/// 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<u64>, 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.
+10 -8
View File
@@ -781,6 +781,7 @@ fn chrome_key<P: Hash>(layout: &Layout<P>, 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<P: Clone + Eq + Hash + Debug + Send + Sync + 'static> 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.
+50 -1
View File
@@ -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<RefCell<Vec<(usize, egui::Id)>>>,
}
impl DockPanels<usize> 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<RefCell<Vec<(usize, egui::Id)>>> = 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<Vec<(usize, egui::Id)>>| 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");
}