diff --git a/crates/ui-panels/src/docking/config.rs b/crates/ui-panels/src/docking/config.rs index 581a867c9..5f5809314 100644 --- a/crates/ui-panels/src/docking/config.rs +++ b/crates/ui-panels/src/docking/config.rs @@ -77,6 +77,10 @@ pub struct DockConfig { /// closes them, for tabs that act as documents. Off: panels close from their menu. Accordion headers and iconic /// columns never show it. pub tab_close_buttons: bool, + /// A group's header shows the shown panel's title, its count and a close button in place of a tab strip, as a + /// single titled panel does; the panels are shown from a rail or a menu instead. Closing follows + /// `tab_close_buttons`. + pub titled_headers: bool, /// The wheel over a tab strip shows the next or previous panel, one per wheel notch. pub wheel_selects_tabs: bool, /// A floating window opens in an OS window of its own where the window host opens OS windows and reports where @@ -103,6 +107,7 @@ impl Default for DockConfig { close_menu_items: true, close_button: true, tab_close_buttons: false, + titled_headers: false, wheel_selects_tabs: false, native_windows: true, group_order_items: false, @@ -196,7 +201,14 @@ impl DockConfig { self } - /// Wheel selection of tabs on or off. + /// Titled group headers on or off. + #[must_use] + pub fn titled_headers(mut self, on: bool) -> Self { + self.titled_headers = on; + self + } + + /// See [`DockConfig::wheel_selects_tabs`]. #[must_use] pub fn wheel_selects_tabs(mut self, on: bool) -> Self { self.wheel_selects_tabs = on; diff --git a/crates/ui-panels/src/docking/group.rs b/crates/ui-panels/src/docking/group.rs index 76aeaa5d5..0606fd437 100644 --- a/crates/ui-panels/src/docking/group.rs +++ b/crates/ui-panels/src/docking/group.rs @@ -240,6 +240,10 @@ pub struct DockGroup<'a, P: Clone + Hash + Send + Sync + 'static> { /// The wheel over the tabs shows the next or previous panel, one per wheel notch. #[prop(flag)] wheel_selects_tabs: bool, + /// The header shows the shown panel's title (with its count) and a close button in place of the tab strip, as a + /// single titled panel does: the panels are shown from a rail, not by tabs. + #[prop(flag)] + titled: bool, /// The dock whose drop registry the group's spots go to. #[prop] dock: Option, @@ -287,6 +291,7 @@ impl<'a, P: Clone + Hash + Send + Sync + 'static> DockGroup<'a, P> { movable: true, closable: false, wheel_selects_tabs: false, + titled: false, dock: None, zones: ZoneRule::Fraction, in_window: false, @@ -477,7 +482,9 @@ impl Container for DockGroup<'_, P> { let words = cx.words::(); let from = PanelKey::of(&self.anchor); let started = drop::started(&cx.env); - let (panels, active, closable, wheel_selects) = (self.panels, self.active, self.closable, self.wheel_selects_tabs); + let (panels, active, closable, wheel_selects, titled) = + (self.panels, self.active, self.closable, self.wheel_selects_tabs, self.titled); + let close_name = words.close_panel.clone(); let menu_rows = self.menu.clone(); let panel_menu = words.panel_menu.clone(); let mut extra = self.extra.take(); @@ -494,9 +501,30 @@ impl Container for DockGroup<'_, P> { let layout = if rtl { egui::Layout::right_to_left(egui::Align::Min) } else { egui::Layout::left_to_right(egui::Align::Min) }; ui.with_layout(layout, |ui| { ui.spacing_mut().item_spacing.x = gap; // design: allow[style-mutation](the header lays its title cells with an egui layout and sets that layout's gap) + let shown = panels.iter().find(|panel| panel.key == active); + if titled { + if let Some(panel) = shown { + Text::new(panel.title.clone()).role(TypeRole::Title).show(ui); + if let Some(count) = panel.count { + let count = env.format().integer(u64::try_from(count).unwrap_or(u64::MAX)); + Text::new(count).role(TypeRole::Caption).shade(Shade::Faint).show(ui); + } + } + } if let Some(hook) = extra.as_mut() { hook.run(ui, cx); } + if titled { + let can_close = closable && shown.is_some_and(|panel| panel.closable); + if can_close { + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + if IconButton::new(IconId::new("close")).size(ControlSize::Sm).label(close_name.clone()).show(ui).clicked() { + reported.closed.push(active); + } + }); + } + return; + } let source = keyed(panels, |panel: &PanelTitle| panel.key).with_text(|panel| Cow::Borrowed(panel.title.as_str())); let closes = |panel: &PanelTitle| closable && panel.closable; let kind = if panels.iter().any(closes) { TabKind::Document } else { TabKind::Panel }; diff --git a/crates/ui-panels/src/docking/words.rs b/crates/ui-panels/src/docking/words.rs index 71ba1ffd1..a3652d730 100644 --- a/crates/ui-panels/src/docking/words.rs +++ b/crates/ui-panels/src/docking/words.rs @@ -22,6 +22,8 @@ craft_ui::words! { close_window: "floatingGroup.close" = "Close panel window"; /// The tooltip and accessible name of the panel-menu button. panel_menu: "dockGroup.panelMenu" = "Panel Menu"; + /// The name of the close button in a titled group's header. + close_panel: "dockGroup.closePanel" = "Close panel"; /// The name of the workspace switcher's select. choose_workspace: "workspaceSwitcher.choose" = "Choose a workspace"; diff --git a/crates/ui-panels/tests/it/dock_group.rs b/crates/ui-panels/tests/it/dock_group.rs index db7193b45..6c632fded 100644 --- a/crates/ui-panels/tests/it/dock_group.rs +++ b/crates/ui-panels/tests/it/dock_group.rs @@ -64,3 +64,33 @@ fn the_shown_panels_caption_sits_under_the_tabs_and_its_count_names_its_tab() { assert_eq!(shown.get(), 1, "the click shows Pages"); assert!(fixture.by_label("2 replies").is_none(), "Comments' caption goes with it"); } + +// A titled group shows the shown panel's name as a heading with its count and a close button, not a tab strip: the +// panel's name is not cut to a tab's width, and the close button closes the shown panel. +#[trial] +fn a_titled_group_shows_the_panel_title_and_a_close_button_instead_of_tabs() { + let closed = Rc::new(Cell::new(false)); + let seen = Rc::clone(&closed); + let panels = vec![ + PanelTitle::new(PanelKey::new(1), "Accessibility Checker").closable(true), + PanelTitle::new(PanelKey::new(2), "Pages"), + ]; + let mut fixture = Fixture::new(move |ui| { + let out = DockGroup::new(1usize, &panels, panels[0].key).titled().closable().show_at( + ui, + Rect::from_min_size(Pos2::new(10.0, 10.0), Vec2::new(300.0, 240.0)), + |ui, _| { + Button::new("Body button").show(ui); + }, + ); + if out.output.closed.contains(&panels[0].key) { + seen.set(true); + } + }); + fixture.settle(); + assert!(fixture.by_label("Accessibility Checker").is_some(), "the title shows whole"); + assert!(fixture.by_label("Pages").is_none(), "no tab for the other panel"); + let Some(close) = fixture.by_label("Close panel") else { panic!("a close button shows") }; + fixture.click(&close).settle(); + assert!(closed.get(), "it closes the shown panel"); +} diff --git a/crates/ui-patterns/src/patterns/dock/area.rs b/crates/ui-patterns/src/patterns/dock/area.rs index 0ea187702..f866c1c1f 100644 --- a/crates/ui-patterns/src/patterns/dock/area.rs +++ b/crates/ui-patterns/src/patterns/dock/area.rs @@ -910,6 +910,9 @@ impl Pass<'_, P> { if scene.config.tab_close_buttons { group = group.closable(); } + if scene.config.titled_headers { + group = group.titled(); + } if scene.config.wheel_selects_tabs { group = group.wheel_selects_tabs(); }