Document tabs: a long name is cut to a compact tab, and a tab drag shows no badge (CollectionProps::drag_badge; trials)

This commit is contained in:
Dylan M. Taylor
2026-11-22 21:10:47 -05:00
parent baaa53c801
commit c7e1e41266
6 changed files with 68 additions and 3 deletions
@@ -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<Counted>,
/// 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 {
@@ -455,8 +455,9 @@ impl<C: Collection> 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()))),
};
+1
View File
@@ -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."),
+2
View File
@@ -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<D>, _, insert| {
// `insert` counts every tab; `keeps_fixed` takes the place among the tabs left without the dragged ones.
+12 -2
View File
@@ -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
+41
View File
@@ -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");
}