DocumentTabs: the trailing control follows the last tab when every tab shows (strip takes the room less the control's last width; trial)

This commit is contained in:
Dylan M. Taylor
2026-11-22 19:48:49 -05:00
parent 402dc57690
commit fa918b1090
2 changed files with 47 additions and 2 deletions
+24 -2
View File
@@ -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)
};
+23
View File
@@ -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;