From c7e1e412665cba2312605e8629b1bca9797e7b56 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 22 Nov 2026 21:10:47 -0500 Subject: [PATCH] Document tabs: a long name is cut to a compact tab, and a tab drag shows no badge (CollectionProps::drag_badge; trials) --- .../src/contract/collection/props.rs | 10 +++++ crates/ui-contract/src/contract/items/drag.rs | 3 +- crates/ui-macros/src/props.rs | 1 + crates/ui-widgets/src/tabs/document.rs | 2 + crates/ui-widgets/src/tabs/tab.rs | 14 ++++++- crates/ui-widgets/tests/it/tabs.rs | 41 +++++++++++++++++++ 6 files changed, 68 insertions(+), 3 deletions(-) diff --git a/crates/ui-contract/src/contract/collection/props.rs b/crates/ui-contract/src/contract/collection/props.rs index ce25fbbb15..74e0ffa93c 100644 --- a/crates/ui-contract/src/contract/collection/props.rs +++ b/crates/ui-contract/src/contract/collection/props.rs @@ -235,6 +235,8 @@ pub struct CollectionProps { /// The count word the drag badge shows for the dragged items, one item included ("1 page", "3 pages"); `None` /// shows the number of items for several and the item's text for one. pub drag_count: Option, + /// Whether a row drag shows its badge by the pointer. Off, the drag carries no preview. + pub drag_badge: bool, /// The collection takes the full height of its rows and owns no scroll, so it can sit inside a scrolling page: /// every row is built, the wheel and the keys that move past an end act on the page's scroll owner, and moving /// the lead scrolls the page to keep the row in view. For short collections only; a long one keeps its own @@ -350,6 +352,7 @@ impl CollectionProps { delete_target: DeleteTarget::Focus, drag_payload: None, drag_count: None, + drag_badge: true, natural_height: false, min_rows: 0, max_rows: None, @@ -396,6 +399,13 @@ impl CollectionProps { self } + /// Whether a row drag shows its badge by the pointer (on by default). + #[must_use] + pub const fn drag_badge(mut self, on: bool) -> Self { + self.drag_badge = on; + self + } + /// The drag badge reads `count` for the number of dragged items. #[must_use] pub const fn drag_count(mut self, count: Counted) -> Self { diff --git a/crates/ui-contract/src/contract/items/drag.rs b/crates/ui-contract/src/contract/items/drag.rs index 5abff2965c..df4bbcc866 100644 --- a/crates/ui-contract/src/contract/items/drag.rs +++ b/crates/ui-contract/src/contract/items/drag.rs @@ -455,8 +455,9 @@ impl ItemsHooks<'_, '_, C> { let key = self.source.key(at); let keys = self.keys_at(&self.acted_on(at)); let count = keys.len() as u64; - let preview = match props.drag_count { + let preview = match props.drag_count.filter(|_| props.drag_badge) { Some(word) => DragPreview::Badge(Label::from(cx.env.counted(word, count))), + None if !props.drag_badge => DragPreview::None, None if count > 1 => DragPreview::Badge(Label::from(count.to_string())), None => self.source.text(at).map_or(DragPreview::None, |text| DragPreview::Badge(Label::from(text.into_owned()))), }; diff --git a/crates/ui-macros/src/props.rs b/crates/ui-macros/src/props.rs index 2c4cf65951..e10b9f9673 100644 --- a/crates/ui-macros/src/props.rs +++ b/crates/ui-macros/src/props.rs @@ -195,6 +195,7 @@ const COLLECTION: &[Forwarded] = &[ ("deletable", "bool", "Delete and Backspace at the lead report the items to delete."), ("delete_target", "$ui::contract::DeleteTarget", "Where a deletable collection takes Delete and Backspace."), ("drag_count", "$ui::runtime::env::Counted", "The drag badge reads this count word for the dragged items, one included."), + ("drag_badge", "bool", "A row drag shows its badge by the pointer (default on); off, the dragged row shows alone."), ("natural_height", "bool", "The collection takes its full content height and owns no scroll: for a short collection inside a scrolling page."), ("min_rows", "u16", "The collection is at least this many rows tall; rows it does not have count the row height."), ("max_rows", "u16", "The collection shows at most this many rows and scrolls the rest, inside a scrolling body too."), diff --git a/crates/ui-widgets/src/tabs/document.rs b/crates/ui-widgets/src/tabs/document.rs index c55622dad0..bbc9ae538d 100644 --- a/crates/ui-widgets/src/tabs/document.rs +++ b/crates/ui-widgets/src/tabs/document.rs @@ -288,6 +288,8 @@ impl<'a, D: Clone + Eq + Hash + Send + Sync + std::fmt::Debug + 'static> Documen if reorderable { strip = strip .reorder(ReorderRule::new()) + // The held tab is the preview; production shows no filename badge beside the pointer. + .drag_badge(false) .can_drag(move |key: &D| tabs.iter().find(|tab| &tab.id == key).is_none_or(|tab| tab.movable)) .accepts(move |drag: &ListDrag, _, insert| { // `insert` counts every tab; `keeps_fixed` takes the place among the tabs left without the dragged ones. diff --git a/crates/ui-widgets/src/tabs/tab.rs b/crates/ui-widgets/src/tabs/tab.rs index b9e97d47e8..33d85c83f7 100644 --- a/crates/ui-widgets/src/tabs/tab.rs +++ b/crates/ui-widgets/src/tabs/tab.rs @@ -7,6 +7,9 @@ use super::rules::{CloseSide, TabKind, close_side}; use super::words::TabWords; + +/// The width, in digits of the label role, a document tab shows of its name at most. +const MAX_NAME_DIGITS: usize = 22; use crate::buttons::IconButton; use crate::buttons::icon_button::{MarkerStyle, paint_marker}; use crate::contract::{ @@ -293,7 +296,14 @@ impl Component for Tab<'_> { } let width = |run: &TextRun<'_>| cx.layout_at(run, f32::INFINITY).size().x; let label_width = width(&self.label_run(cx.style, cx.resolved.label)); - let mut natural = sizes.tail + label_width; + // A document tab never takes more than a name of `MAX_NAME_DIGITS` digits' width, as production's tabs cut a long + // name short so the other tabs and the Open control keep their room; the label is cut with an ellipsis. + let capped = if self.kind == TabKind::Document { + label_width.min(width(&self.label_run(cx.style, &"0".repeat(MAX_NAME_DIGITS)))) + } else { + label_width + }; + let mut natural = sizes.tail + capped; if self.icon.is_some() { natural += sizes.icon + sizes.gap; } @@ -309,7 +319,7 @@ impl Component for Tab<'_> { let measured = if cx.proposal == Proposal::Min { let end = cx.resolved.label.char_indices().nth(12).map_or(cx.resolved.label.len(), |(index, _)| index); let prefix = cx.resolved.label.get(..end).unwrap_or(cx.resolved.label); - let readable = natural - label_width + width(&self.label_run(cx.style, prefix)); + let readable = natural - capped + width(&self.label_run(cx.style, prefix)); readable.max(sizes.min_content).min(natural) } else { natural diff --git a/crates/ui-widgets/tests/it/tabs.rs b/crates/ui-widgets/tests/it/tabs.rs index d2b2f3b67f..38e4fb94c7 100644 --- a/crates/ui-widgets/tests/it/tabs.rs +++ b/crates/ui-widgets/tests/it/tabs.rs @@ -634,3 +634,44 @@ fn a_modified_document_tab_shows_a_dot_and_no_asterisk() { assert_eq!(modified.0, 0, "no asterisk is painted"); assert!(modified.1 > plain.1, "the dot takes room beside the name: {} vs {}", modified.1, plain.1); } + +/// Production shows the held tab in the strip and no filename badge by the pointer: while a document tab is dragged, its +/// name is painted once. +#[trial] +fn a_dragged_document_tab_shows_no_badge() { + use craft_ui::tabs::{DocumentTab, DocumentTabs}; + let tabs = [DocumentTab::new(0_usize, "Quarterly"), DocumentTab::new(1, "Other")]; + let mut frames = Frames::new(); + let mut show = |ui: &mut Ui| { + let _ = DocumentTabs::new("Documents", &tabs).selected(0).reorderable(true).show(ui); + }; + frames.idle(2, &mut show); + let tree = frames.tree(); + let one = tab_rect(&tree, "Quarterly"); + let to = Pos2::new(one.center().x, one.center().y + 60.0); + frames.frame(vec![moved(one.center())], &mut show); + frames.frame(vec![press(one.center())], &mut show); + for step in 1..=4 { + frames.frame(vec![moved(one.center().lerp(to, step as f32 / 4.0))], &mut show); + } + frames.idle(2, &mut show); + let shown = frames.texts().iter().filter(|text| text.text == "Quarterly").count(); + assert_eq!(shown, 1, "the name is painted once, by the held tab, with no badge beside the pointer"); +} + +/// A very long document name does not stretch its tab across the strip: the tab stays near 22 characters wide and the +/// next tab keeps its room. +#[trial] +fn a_very_long_document_name_is_cut_to_a_compact_tab() { + use craft_ui::tabs::{DocumentTab, DocumentTabs}; + let long = "a-very-long-synthetic-document-file-name-for-truncation-test-1234567890.pdf"; + let tabs = [DocumentTab::new(0_usize, long), DocumentTab::new(1, "Short")]; + let mut fixture = Fixture::new(move |ui| { + let _ = DocumentTabs::new("Documents", &tabs).selected(0).show(ui); + }); + fixture.idle(6); + let tab = fixture.by_label(long).expect("the long tab").rect; + let short = fixture.by_label("Short").expect("the next tab").rect; + assert!(tab.width() < 320.0, "the long name is cut short, got {}", tab.width()); + assert!(short.left() >= tab.right(), "the next tab follows it"); +}