ui-widgets: DocumentTab::tooltip gives a document tab a tooltip of its own, shown also when the strip cuts its name

This commit is contained in:
bflatastic
2026-11-22 08:46:46 +07:00
parent 78b03dd901
commit 359ebf3e5d
2 changed files with 38 additions and 2 deletions
+16 -2
View File
@@ -7,7 +7,8 @@ pub(crate) mod stories;
use super::{Tab, TabStrip};
use crate::contract::{
CollectionEvent, ContextMenuRequest, DropAt, DropKind, DropPlace, ListDrag, OsFiles, ReorderRule, ShowCollection as _, ShowContainer as _,
CollectionEvent, ContextMenuRequest, DropAt, DropKind, DropPlace, ListDrag, OsFiles, Props as _, ReorderRule, ShowCollection as _,
ShowContainer as _,
};
use crate::layout_components::Row;
use crate::runtime::drag::OsFile;
@@ -35,12 +36,15 @@ pub struct DocumentTab<D> {
pub detail: Option<Label>,
/// The accessible name of the close button, for a tab whose close cancels the opening.
pub close_label: Option<Label>,
/// The tab's tooltip, such as the whole title of a tab that shows the name alone; a shortened tab shows it in
/// place of its label.
pub tooltip: Option<Label>,
}
impl<D> DocumentTab<D> {
/// A closable, unmodified tab for document `id` named `label`.
pub fn new(id: D, label: impl Into<Label>) -> Self {
DocumentTab { id, label: label.into(), modified: false, closable: true, progress: None, detail: None, close_label: None }
DocumentTab { id, label: label.into(), modified: false, closable: true, progress: None, detail: None, close_label: None, tooltip: None }
}
/// Unsaved changes.
@@ -77,6 +81,13 @@ impl<D> DocumentTab<D> {
self.close_label = Some(label.into());
self
}
/// The tab's tooltip.
#[must_use]
pub fn tooltip(mut self, text: impl Into<Label>) -> Self {
self.tooltip = Some(text.into());
self
}
}
/// What the tabs of a group did this pass, in the order they happened.
@@ -261,6 +272,9 @@ impl<'a, D: Clone + Eq + Hash + Send + Sync + std::fmt::Debug + 'static> Documen
if let Some(close) = &document.close_label {
tab = tab.close_label(close.clone());
}
if let Some(tooltip) = &document.tooltip {
tab = tab.tooltip(tooltip.clone());
}
tab
})
};
+22
View File
@@ -136,6 +136,28 @@ fn a_double_click_on_the_empty_strip_asks_for_a_new_document() {
assert!(!seen.contains(&DocumentTabsEvent::EmptyDoubleClick), "{seen:?}");
}
/// A tab that shows the name alone gives its whole title as its tooltip, also when the strip cuts its name.
#[trial]
fn a_document_tab_shows_its_own_tooltip() {
use craft_ui::contract::tooltip::DELAY;
use craft_ui::tabs::{DocumentTab, DocumentTabs};
const WHOLE: &str = "Poster.vc @ 50% (CMYK/Preview)";
for width in [640.0, 60.0] {
let mut fixture = Fixture::new(|ui| {
let tabs = [DocumentTab::new(0_usize, "Poster.vc").tooltip(WHOLE)];
let _ = DocumentTabs::new("Documents", &tabs).show(ui);
});
fixture.room(egui::Vec2::new(width, 120.0)).settle();
let tab = tab_rect(fixture.tree(), "Poster.vc");
let cut = fixture.texts().iter().any(|run| run.elided);
assert_eq!(cut, width < 100.0, "the strip cuts the name at {width} pt only");
fixture.frame(vec![moved(tab.center())]).advance(DELAY).settle();
let texts: Vec<String> = fixture.texts().iter().map(|run| run.text.clone()).collect();
assert!(texts.iter().any(|text| text == WHOLE), "the whole title shows over the tab at {width} pt: {texts:?}");
assert!(!fixture.texts().iter().any(|run| run.text == "Poster.vc" && !run.elided && cut), "and not the name alone: {texts:?}");
}
}
/// The bounds of every tab node named `name`, from the reading start.
fn tab_rects(tree: &AccessTree, name: &str) -> Vec<Rect> {
let mut rects: Vec<Rect> =