From 3ed0218033a157fab6dc832e318b14a45e05e184 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 18 Nov 2026 19:49:24 -0800 Subject: [PATCH] Tabs: a shortened title shows at fractional tab widths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit egui rounds a wrap width to the nearest point, so a shortened title laid out for 66.5 pt could come out 67 pt wide, and the tab then hid it as too wide for its room. The width is now rounded down before the title is laid out. A new test lays the title out at every quarter point from the width of "…" to 200 pt and checks that each one fits. --- crates/ui-egui/src/chrome.rs | 2 +- crates/ui-egui/src/chrome/tabs.rs | 24 +++++++++++++++++++----- crates/ui-egui/src/chrome/tests.rs | 23 +++++++++++++++++++++++ 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/crates/ui-egui/src/chrome.rs b/crates/ui-egui/src/chrome.rs index 254a5a0c8b..e0338b5253 100644 --- a/crates/ui-egui/src/chrome.rs +++ b/crates/ui-egui/src/chrome.rs @@ -32,7 +32,7 @@ pub use status_bar::{StatusBarShow, deserialize_status_bar_show, set_status_bar_ #[cfg(test)] use status_bar::{StatusReadoutWidths, color_profile_name, date_time_label, status_readouts}; #[cfg(test)] -use tabs::{CLOSE_BEFORE_TITLE, first_tab_shown, leading_tab, tab_drop, tab_groups, tab_room, tab_title}; +use tabs::{CLOSE_BEFORE_TITLE, first_tab_shown, leading_tab, shortened_title, tab_drop, tab_groups, tab_room, tab_title}; pub use tabs::{doc_tabs, isolation_bar, step_document}; pub(crate) use tabs::{document_window_title, show_document, window_tab}; diff --git a/crates/ui-egui/src/chrome/tabs.rs b/crates/ui-egui/src/chrome/tabs.rs index d23fb775ad..4f7666e171 100644 --- a/crates/ui-egui/src/chrome/tabs.rs +++ b/crates/ui-egui/src/chrome/tabs.rs @@ -146,6 +146,24 @@ pub(super) fn tab_room(lead: Option, leading: bool, w: f32, left: f32) -> f } } +/// `title` in one line at most `max_width` wide, ending in "…" when it is cut short, and whether +/// it fits in `max_width`. egui rounds a wrap width to the nearest point, so the width is rounded +/// down first. +pub(super) fn shortened_title( + ctx: &egui::Context, + title: &str, + font: egui::FontId, + color: egui::Color32, + max_width: f32, +) -> (std::sync::Arc, bool) { + let max_width = max_width.max(0.0); + let mut job = egui::text::LayoutJob::simple_singleline(title.to_owned(), font, color); + job.wrap = egui::text::TextWrapping { max_width: max_width.floor(), max_rows: 1, break_anywhere: true, overflow_character: Some('…') }; + let galley = ctx.fonts_mut(|f| f.layout_job(job)); + let fits = galley.size().x <= max_width; + (galley, fits) +} + /// The × of a document tab `w` wide, drawn in `xr` and clicked within 3 pt of it, or `None` for a /// tab shortened narrower than the × and its click area. fn tab_close_box(ui: &Ui, t: &Tokens, xr: egui::Rect, w: f32, id: egui::Id) -> Option { @@ -227,11 +245,7 @@ pub fn doc_tabs(app: &mut VectorcraftApp, ui: &mut Ui) { break; } let color = if Some(*win) == shown { t.text_strong } else { t.text_dim }; - let mut job = egui::text::LayoutJob::simple_singleline(title.clone(), theme::semibold(title_size), color); - let max_width = (left - TAB_PADDING).max(0.0); - job.wrap = egui::text::TextWrapping { max_width, max_rows: 1, break_anywhere: true, overflow_character: Some('…') }; - let galley = ui.painter().layout_job(job); - let fits = galley.size().x <= max_width; + let (galley, fits) = shortened_title(ui.ctx(), title, theme::semibold(title_size), color, left - TAB_PADDING); (galley, left.max(0.0), true, fits) } else { (galley.clone(), *w, false, true) diff --git a/crates/ui-egui/src/chrome/tests.rs b/crates/ui-egui/src/chrome/tests.rs index 1fd8245365..cfe22456a4 100644 --- a/crates/ui-egui/src/chrome/tests.rs +++ b/crates/ui-egui/src/chrome/tests.rs @@ -29,6 +29,29 @@ fn the_tabs_after_the_leading_one_are_drawn_whole() { assert_eq!(super::tab_room(None, false, 120.0, 119.99), 119.99, "no leading tab"); } +/// A shortened title shows at every width that holds "…", fractional ones included (#8584). +#[test] +fn a_shortened_title_fits_at_fractional_widths() { + let ctx = egui::Context::default(); + crate::theme::install_fonts(&ctx); + let font = crate::theme::semibold(13.0); + let title = "Untitled-12 @ 66.67 % (RGB/Preview)"; + let mut too_wide = vec![]; + let mut output = ctx.run_ui(egui::RawInput::default(), |ui| { + let (ellipsis, _) = super::shortened_title(ui.ctx(), "…", font.clone(), egui::Color32::WHITE, 1000.0); + let mut width = ellipsis.size().x.ceil() + 1.0; + while width < 200.0 { + let (galley, fits) = super::shortened_title(ui.ctx(), title, font.clone(), egui::Color32::WHITE, width); + if !fits { + too_wide.push((width, galley.size().x)); + } + width += 0.25; + } + }); + output.textures_delta.clear(); + assert!(too_wide.is_empty(), "shortened titles wider than their room (room, width): {too_wide:?}"); +} + /// Tabs 100 wide at 0, 100, 200…, for documents `docs` (a document listed twice has two views). fn slots(docs: &[usize]) -> Vec<(usize, egui::Rect)> { docs.iter().enumerate().map(|(k, &d)| (d, egui::Rect::from_min_size(egui::pos2(k as f32 * 100.0, 0.0), egui::vec2(100.0, 30.0)))).collect()