ui-widgets: DocumentTab::name_len, the label's name part, which a modified tab's mark follows: "Untitled-1* @ 93.5 % (RGB/Preview)"

This commit is contained in:
bflatastic
2026-11-24 12:58:00 +07:00
parent bb06692b13
commit 45a8457c7b
4 changed files with 95 additions and 12 deletions
+14
View File
@@ -28,6 +28,9 @@ pub struct DocumentTab<D> {
pub label: Label,
/// Unsaved changes: the tab shows the modified mark.
pub modified: bool,
/// The length in bytes of the label's name part (the document's name before its zoom and colour mode): the
/// modified mark follows the name, not the whole label. `None`: the label is all name.
pub name_len: Option<usize>,
/// The tab shows a close button.
pub closable: bool,
/// How far the document has opened, between 0 and 1; `None` for a document that is open.
@@ -49,6 +52,7 @@ impl<D> DocumentTab<D> {
id,
label: label.into(),
modified: false,
name_len: None,
closable: true,
progress: None,
detail: None,
@@ -65,6 +69,13 @@ impl<D> DocumentTab<D> {
self
}
/// The label's name part: its first `len` bytes, which the modified mark follows.
#[must_use]
pub fn name_len(mut self, len: usize) -> Self {
self.name_len = Some(len);
self
}
/// Whether the tab can be closed.
#[must_use]
pub fn closable(mut self, on: bool) -> Self {
@@ -312,6 +323,9 @@ impl<'a, D: Clone + Eq + Hash + Send + Sync + std::fmt::Debug + 'static> Documen
if document.modified {
tab = tab.modified();
}
if let Some(len) = document.name_len {
tab = tab.name_len(len);
}
if let Some(fraction) = document.progress {
tab = tab.progress(fraction);
}
@@ -63,3 +63,17 @@ fn large_document_tabs(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
let out = DocumentTabs::new("Documents", &tabs).selected(shown).reorderable(true).takes_files(true).large(true).show(ui);
out.response.into()
}
/// Modified tabs whose label carries the view after the document's name: with a name part (`name_len`) the modified
/// mark follows the name, "Untitled-1* @ 93.5 % (RGB/Preview)"; without one it follows the whole label.
#[story(DocumentTabs<'static, usize>, covers = [], drive = Drive::Never)]
fn document_tabs_with_name_part(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
const NAME: &str = "Untitled-1";
let label = format!("{NAME} @ 93.5 % (RGB/Preview)");
let tabs = [
DocumentTab::new(0, label.clone()).modified(true).name_len(NAME.len()),
DocumentTab::new(1, label.replace(NAME, "Untitled-2")).modified(true),
];
let shown = *data.value("shown", 0_usize);
DocumentTabs::new("Documents", &tabs).selected(shown).show(ui).response.into()
}
+37 -12
View File
@@ -7,6 +7,7 @@
use super::rules::{CloseSide, TabKind, close_side};
use super::words::TabWords;
use std::borrow::Cow;
/// The width, in digits of the label role, a document tab shows of its name at most.
const MAX_NAME_DIGITS: usize = 22;
@@ -189,6 +190,10 @@ pub struct Tab<'a> {
/// [`DocumentTabLook::Shrinking`](super::DocumentTabLook::Shrinking).
#[prop(flag)]
modified: bool,
/// The length in bytes of the label's name part (a document's name before its zoom and colour mode): a modified
/// tab under the `*` look shows the mark right after the name, inside the label.
#[prop]
name_len: Option<usize>,
/// Whether the tab can be closed; by default whether the kind shows a close button.
#[prop]
closable: Option<bool>,
@@ -218,6 +223,7 @@ impl<'a> Tab<'a> {
detail: None,
icon: None,
modified: false,
name_len: None,
closable: None,
progress: None,
close_label: None,
@@ -252,9 +258,24 @@ impl<'a> Tab<'a> {
self.marked || (self.modified && style.shrinking)
}
/// Whether the modified mark `*` shows: a modified tab whose look shows no dot for it.
fn starred(&self, style: &TabStyle) -> bool {
self.modified && !style.shrinking
/// Where in `label` the modified mark `*` goes: after the name part, for a modified tab whose look shows no dot for
/// it.
fn mark_at(&self, style: &TabStyle, label: &str) -> Option<usize> {
self.name_len.filter(|at| self.modified && !style.shrinking && label.is_char_boundary(*at))
}
/// Whether the modified mark `*` shows after the text: a modified tab whose look shows no dot for it, and whose
/// label holds no name part for the mark to follow.
fn starred(&self, style: &TabStyle, label: &str) -> bool {
self.modified && !style.shrinking && self.mark_at(style, label).is_none()
}
/// The text the tab shows for `label`: the label, with the modified mark after its name part where it has one.
fn shown_label<'t>(&self, style: &TabStyle, label: &'t str) -> Cow<'t, str> {
match self.mark_at(style, label) {
Some(at) => Cow::Owned(format!("{}{MARK}{}", &label[..at], &label[at..])),
None => Cow::Borrowed(label),
}
}
/// The modified mark, in the label's type.
@@ -285,7 +306,7 @@ impl<'a> Tab<'a> {
let label_natural = natural(&self.label_run(style, label));
let detail_natural = self.detail_text().map_or(0.0, |text| natural(&TextRun::new(text).role(style.detail.role)));
// The marks keep their room; the label is cut first, then the detail.
let mark_width = if self.starred(style) { natural(&self.mark_run(style)) } else { 0.0 };
let mark_width = if self.starred(style, label) { natural(&self.mark_run(style)) } else { 0.0 };
let marker_width = if self.dotted(style) { sizes.gap + style.marker } else { 0.0 };
let marks = mark_width + marker_width;
let detail_gap = if detail_natural > 0.0 { sizes.gap } else { 0.0 };
@@ -424,16 +445,18 @@ impl Component for Tab<'_> {
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
let sizes = &cx.style.sizes;
let shown = self.shown_label(cx.style, cx.resolved.label);
let label: &str = &shown;
// The first letters of the title: the least a shrunk tab shows.
let letters = || {
let first: String = cx.resolved.label.chars().take(MIN_LETTERS).collect();
let first: String = label.chars().take(MIN_LETTERS).collect();
cx.layout_at(&self.label_run(cx.style, &first), f32::INFINITY).size().x
};
if cx.proposal == Proposal::Min && !cx.style.shrinking {
return Vec2::new(letters(), sizes.label_line);
}
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 label_width = width(&self.label_run(cx.style, label));
// 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 cx.style.shrinking { label_width.min(width(&self.label_run(cx.style, &"0".repeat(MAX_NAME_DIGITS)))) } else { label_width };
@@ -444,7 +467,7 @@ impl Component for Tab<'_> {
if let Some(detail) = self.detail_text() {
natural += sizes.gap + width(&TextRun::new(detail).role(cx.style.detail.role));
}
if self.starred(cx.style) {
if self.starred(cx.style, label) {
natural += width(&self.mark_run(cx.style));
}
if self.dotted(cx.style) {
@@ -454,8 +477,8 @@ impl Component for Tab<'_> {
natural += sizes.gap + sizes.close_box;
}
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 end = label.char_indices().nth(12).map_or(label.len(), |(index, _)| index);
let prefix = label.get(..end).unwrap_or(label);
let readable = natural - capped + width(&self.label_run(cx.style, prefix));
readable.max(letters()).min(natural)
} else {
@@ -469,7 +492,8 @@ impl Component for Tab<'_> {
}
fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) {
let placed = self.place(cx.style, cx.content_rect, cx.flow.is_rtl(), cx.resolved.label, |run| cx.layout_at(run, f32::INFINITY).size().x);
let shown = self.shown_label(cx.style, cx.resolved.label);
let placed = self.place(cx.style, cx.content_rect, cx.flow.is_rtl(), &shown, |run| cx.layout_at(run, f32::INFINITY).size().x);
if placed.cut {
let full = match self.detail_text() {
Some(detail) => format!("{}, {detail}", cx.resolved.label),
@@ -530,7 +554,8 @@ impl Component for Tab<'_> {
let sizes = style.sizes;
let selected = cx.resolved.selected == Some(true) || cx.state().flags.contains(Flags::SELECTED);
let draw = ui.draw();
let placed = self.place(style, cx.content_rect, cx.flow.is_rtl(), cx.resolved.label, |run| draw.layout(run, f32::INFINITY).size().x);
let shown = self.shown_label(style, cx.resolved.label);
let placed = self.place(style, cx.content_rect, cx.flow.is_rtl(), &shown, |run| draw.layout(run, f32::INFINITY).size().x);
if style.divider {
draw.edge_rule(RuleKind::Divider, rect, RectSide::end(cx.flow.is_rtl()));
}
@@ -545,7 +570,7 @@ impl Component for Tab<'_> {
draw.stroke(&line, StrokeWidth::Thick, style.underline);
}
let role = if selected { style.label.role } else { style.idle_role };
let label = TextRun::new(cx.resolved.label).role(role).color(cx.resolve(&style.label.color));
let label = TextRun::new(&*shown).role(role).color(cx.resolve(&style.label.color));
draw.text(placed.label, &if self.kind.clips_label() { label.wrap(Wrap::Clip) } else { label.elide() });
if let (Some(at), Some(detail)) = (placed.detail, self.detail_text()) {
draw.text(at, &TextRun::new(detail).role(style.detail.role).color(cx.resolve(&style.detail.color)).elide());
+30
View File
@@ -317,6 +317,36 @@ fn a_document_tab_shows_its_own_tooltip() {
}
}
/// A modified document tab with a name part shows the `*` right after the name, inside its text, and takes the mark's
/// room once, as the same tab whose `*` follows the whole label does.
#[trial]
fn a_modified_tab_shows_its_mark_after_the_name_part() {
use craft_ui::tabs::{DocumentTab, DocumentTabs};
const NAME: &str = "Untitled-1";
const LABEL: &str = "Untitled-1 @ 93.5 % (RGB/Preview)";
let shown = |name_len: Option<usize>| {
let mut fixture = Fixture::new(move |ui| {
let mut tab = DocumentTab::new(0_usize, LABEL).modified(true);
if let Some(len) = name_len {
tab = tab.name_len(len);
}
let _ = DocumentTabs::new("Documents", &[tab]).show(ui);
});
fixture.room(egui::Vec2::new(640.0, 120.0)).settle();
let texts: Vec<(String, Rect)> = fixture.texts().iter().map(|run| (run.text.clone(), run.rect)).collect();
(texts, tab_rects(fixture.tree(), &format!("{LABEL}, modified")).first().copied())
};
let (named, named_tab) = shown(Some(NAME.len()));
let (whole, whole_tab) = shown(None);
assert!(named.iter().any(|(text, _)| text == "Untitled-1* @ 93.5 % (RGB/Preview)"), "the mark follows the name: {named:?}");
assert!(!named.iter().any(|(text, _)| text == "*"), "and is not drawn again after the label: {named:?}");
let star = whole.iter().find(|(text, _)| text == "*").map(|(_, rect)| *rect).expect("without a name part the mark follows the label");
assert!(whole.iter().any(|(text, _)| text == LABEL), "and the label is drawn whole: {whole:?}");
let (named_tab, whole_tab) = (named_tab.expect("the named tab's node"), whole_tab.expect("the whole tab's node"));
let apart = (named_tab.width() - whole_tab.width()).abs();
assert!(apart < star.width() / 2.0, "the mark takes its room once: {named_tab:?} against {whole_tab:?}, the mark {star:?}");
}
/// A middle click on a document tab asks to close it, unless the tab cannot close; it does not show the tab.
#[trial]
fn a_middle_click_closes_a_closable_document_tab() {