From fa918b1090e3882e15fef3f698d1e29b844b07b3 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 22 Nov 2026 19:48:49 -0500 Subject: [PATCH] DocumentTabs: the trailing control follows the last tab when every tab shows (strip takes the room less the control's last width; trial) --- crates/ui-widgets/src/tabs/document.rs | 26 ++++++++++++++++++++++++-- crates/ui-widgets/tests/it/tabs.rs | 23 +++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/crates/ui-widgets/src/tabs/document.rs b/crates/ui-widgets/src/tabs/document.rs index 0951fe9fd..957685ff6 100644 --- a/crates/ui-widgets/src/tabs/document.rs +++ b/crates/ui-widgets/src/tabs/document.rs @@ -336,10 +336,32 @@ impl<'a, D: Clone + Eq + Hash + Send + Sync + std::fmt::Debug + 'static> Documen if let Some(area) = leading.as_mut() { area(ui); } - shown = Some(show_strip(ui, make_strip())); + let strip = show_strip(ui, make_strip()); if let Some(area) = trailing.as_mut() { - area(ui); + // The strip fills the row less the room the control took last pass; the control follows the last tab + // when every tab shows, and is the row's last item when they overflow. + let memory = ui.id().with("document-tabs-trailing-width"); + let held: f32 = ui.ctx().data(|data| data.get_temp(memory)).unwrap_or(0.0); + let slot = ui.allocate_exact_size(egui::vec2(held, strip.response.response.rect.height()), egui::Sense::hover()).0; + let all_show = strip.tab_rects.len() == tabs.len(); + let after = match strip.tab_rects.last() { + Some((_, rect)) if all_show => Some(rect.right() + ui.spacing().item_spacing.x), + None => Some(strip.response.response.rect.left()), + _ => None, + }; + let left = match after { + Some(at) if !ui.layout().prefer_right_to_left() => at.min(slot.left()), + _ => slot.left(), + }; + let room = egui::Rect::from_min_max(egui::pos2(left, slot.top()), egui::pos2(slot.right().max(left + 400.0), slot.bottom())); + let builder = egui::UiBuilder::new().max_rect(room).layout(egui::Layout::left_to_right(egui::Align::Center)); + let drawn = ui.scope_builder(builder, |ui| area(ui)).response.rect.width(); + if (drawn - held).abs() > 0.5 { + ui.ctx().data_mut(|data| data.insert_temp(memory, drawn)); + ui.ctx().request_repaint(); + } } + shown = Some(strip); }); shown.ok_or(row.output) }; diff --git a/crates/ui-widgets/tests/it/tabs.rs b/crates/ui-widgets/tests/it/tabs.rs index 8eca33652..3353b3fba 100644 --- a/crates/ui-widgets/tests/it/tabs.rs +++ b/crates/ui-widgets/tests/it/tabs.rs @@ -115,6 +115,29 @@ fn document_tabs_reserve_space_for_the_trailing_control() { } } +/// Production's Open button follows the last tab; it does not wait at the far end of the row. With tabs that overflow +/// it is still the strip's last item (the reserve trials above). +#[trial] +fn the_trailing_control_follows_the_last_tab_when_they_fit() { + 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: Vec<_> = (0..2).map(|i| DocumentTab::new(i, format!("Doc {i}.pdf")).closable(true)).collect(); + DocumentTabs::new("Documents", &tabs) + .selected(1) + .trailing(|ui| { + Button::new("Open").show(ui); + }) + .show(ui); + }); + fixture.room(egui::vec2(1000.0, 100.0)).settle(); + let open = fixture.by_label("Open").expect("trailing button").rect; + let last = fixture.by_label("Doc 1.pdf").expect("last tab").rect; + assert!(open.left() >= last.right() - 0.5, "the button is not over the tab: {open:?}, {last:?}"); + assert!(open.left() - last.right() < 80.0, "the button sits by the last tab, not at the row's end: {open:?}, {last:?}"); +} + #[trial] fn nested_document_tabs_reserve_space_for_the_trailing_control() { use craft_ui::buttons::Button;