From cedf9e01b6e380ac2f1739906c49228bbc4a4fed Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 21 Nov 2026 17:46:00 -0500 Subject: [PATCH] Size document tabs to available title bar room --- .../ui-contract/src/contract/items/hooks.rs | 2 +- crates/ui-widgets/src/tabs/document.rs | 8 +++- crates/ui-widgets/tests/it/tabs.rs | 42 +++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/crates/ui-contract/src/contract/items/hooks.rs b/crates/ui-contract/src/contract/items/hooks.rs index 176974164..a980a34cf 100644 --- a/crates/ui-contract/src/contract/items/hooks.rs +++ b/crates/ui-contract/src/contract/items/hooks.rs @@ -227,7 +227,7 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { let mut layout = comp.item_layout_from(previous, &*self.source, width, item, scx); // An auto-width layout sets its items side by side: where its width comes from its rows, it takes their run at // their ideal widths, not the widest one (a tab strip in a bar's unbounded row). - let run = (layout.auto_widths() && (asked.is_some() || !bounded) && !self.source.is_empty()) + let run = (layout.auto_widths() && (asked.is_some() || !bounded || self.content_sized(comp)) && !self.source.is_empty()) .then(|| self.run_in(ui, id, room).min(room.x)) .filter(|run| *run > width); let (width, asked) = match run { diff --git a/crates/ui-widgets/src/tabs/document.rs b/crates/ui-widgets/src/tabs/document.rs index 93e9fc4cc..fc6df751b 100644 --- a/crates/ui-widgets/src/tabs/document.rs +++ b/crates/ui-widgets/src/tabs/document.rs @@ -8,7 +8,7 @@ mod stories; use super::{Tab, TabStrip}; use crate::contract::{ CollectionEvent, ContextMenuRequest, DropAt, DropKind, DropPlace, ListDrag, OsFiles, Props as _, ReorderRule, ShowCollection as _, - ShowContainer as _, + ShowContainer as _, Sizing, }; use crate::layout_components::Row; use crate::runtime::drag::OsFile; @@ -269,10 +269,14 @@ impl<'a, D: Clone + Eq + Hash + Send + Sync + std::fmt::Debug + 'static> Documen pub fn show(self, ui: &mut Ui) -> DocumentTabsOutput { let DocumentTabs { label, tabs, selected, reorderable, tear_off, leading, trailing, large, takes_files } = self; let source = keyed(tabs, |tab: &DocumentTab| tab.id.clone()).with_text(|tab| Cow::Borrowed(tab.label.as_str())); + let has_trailing = trailing.is_some(); let make_strip = || { let mut strip = TabStrip::new(label.clone()) .takes_drop(DropKind::new(DropPlace::Between, |_: &ListDrag, _| true)) .can_close(move |key: &D| tabs.iter().any(|tab| &tab.id == key && tab.closable)); + if has_trailing { + strip = strip.sizing(Sizing::Content); + } if takes_files { strip = strip.takes_drop(DropKind::new(DropPlace::Between, |_: &OsFiles, _| true)); } @@ -332,7 +336,7 @@ impl<'a, D: Clone + Eq + Hash + Send + Sync + std::fmt::Debug + 'static> Documen let mut shown = None; // The row measures more than once to reserve non-filling children before sharing the // remaining width. Recreate the strip and show both areas on every sweep. - let row = Row::new().show(ui, |ui, _| { + let row = Row::new().sizing(Sizing::FillWidth).show(ui, |ui, _| { if let Some(area) = leading.as_mut() { area(ui); } diff --git a/crates/ui-widgets/tests/it/tabs.rs b/crates/ui-widgets/tests/it/tabs.rs index 44b79bb67..057d7d78a 100644 --- a/crates/ui-widgets/tests/it/tabs.rs +++ b/crates/ui-widgets/tests/it/tabs.rs @@ -68,6 +68,48 @@ fn document_tabs_shrink_before_overflow() { assert!(fit.shown.iter().all(|(_, width)| *width < 100.0)); } +#[trial] +fn document_tabs_with_trailing_control_fill_their_parent_row() { + use craft_ui::buttons::Button; + use craft_ui::contract::{Props as _, Show as _, ShowContainer as _, Sizing}; + use craft_ui::layout_components::Row; + use craft_ui::tabs::{DocumentTab, DocumentTabs}; + let mut fixture = Fixture::new(|ui| { + let tabs: Vec<_> = (0..5).map(|i| DocumentTab::new(i, format!("Document number {i}, scanned.pdf"))).collect(); + Row::new().sizing(Sizing::FillWidth).show(ui, |ui, _| { + Button::new("Home").show(ui); + let _ = DocumentTabs::new("Documents", &tabs) + .trailing(|ui| { + Button::new("Open").show(ui); + }) + .show(ui); + Button::new("Settings").show(ui); + }); + }); + fixture.room(egui::vec2(1000.0, 100.0)).settle(); + let visible = (0..5).filter(|i| fixture.by_label(&format!("Document number {i}, scanned.pdf")).is_some()).count(); + assert!(visible > 1, "the strip uses the available width: {visible} tab visible"); +} + +#[trial] +fn trailing_control_follows_short_document_tabs() { + use craft_ui::buttons::Button; + use craft_ui::contract::Show as _; + use craft_ui::tabs::{DocumentTab, DocumentTabs}; + let mut fixture = Fixture::new(|ui| { + let tabs = [DocumentTab::new(1, "One document")]; + let _ = DocumentTabs::new("Documents", &tabs) + .trailing(|ui| { + Button::new("Open").show(ui); + }) + .show(ui); + }); + fixture.room(egui::vec2(1000.0, 100.0)).settle(); + let tab = fixture.by_label("One document").expect("document tab").rect; + let open = fixture.by_label("Open").expect("trailing control").rect; + assert!(open.left() - tab.right() < tab.width(), "trailing control follows the tab: {tab:?}, {open:?}"); +} + #[trial] fn document_tabs_reserve_space_for_the_trailing_control() { use craft_ui::buttons::Button;