fix(ui): Recent shortens long names and paths to the row (#5212) (#5321)

This commit is contained in:
Marc authored and GitHub committed 2026-11-17 05:53:08 +01:00
1 parent 2dd0f9a67b
commit 0cb315edaf
2 files changed
+350 -5

No files matched your search

+67 -5
View File
@@ -635,17 +635,27 @@ fn open_list(app: &mut WordApp, ui: &mut Ui) {
let shown = app.ui.recent.iter().take(app.ui.recent_shown()).cloned().collect::<Vec<_>>();
let file_name = |p: &str| std::path::Path::new(p).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or(p.to_string());
let names = shown.iter().map(|p| file_name(p)).collect::<Vec<_>>();
let (mut focused, mut hovered) = (None, None);
use egui::epaint::text::TextDirection;
for (p, name) in shown.iter().zip(&names) {
// #2716: each row carries its own Remove, as Word's context menu does. The × is a button
// of its own at the trailing end, so it never opens the document; clicking the rest of the
// row opens it as before.
let (r, resp) = ui.allocate_exact_size(vec2(ui.available_width().min(700.0) - REMOVE_W, 40.0), Sense::click());
// The row shows a name and a path that may not fit: the path in full is its tooltip.
let resp = resp.on_hover_text(crate::i18n::isolate_path(p));
// Two documents with the same file name are told apart by their path, as on screen.
let spoken = if names.iter().filter(|n| *n == name).count() > 1 { format!("{name}, {p}") } else { name.clone() };
let twin = names.iter().filter(|n| *n == name).count() > 1;
let spoken = if twin { format!("{name}, {p}") } else { name.clone() };
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, ui.is_enabled(), &spoken));
// Only one row shows its path as a tooltip, so every row gives a screen reader the path
// itself, unless its label already says it.
if !twin {
ui.ctx().accesskit_node_builder(resp.id, |node| node.set_description(crate::i18n::isolate_path(p)));
}
if resp.has_focus() {
focused = Some((resp.clone(), p));
}
if resp.hovered() {
hovered = Some((resp.clone(), p));
ui.painter().rect_filled(r, 4.0, t.hover);
}
// Names and paths are set apart (a right-to-left line would scramble their marks and
@@ -659,8 +669,25 @@ fn open_list(app: &mut WordApp, ui: &mut Ui) {
t.accent,
);
let side = crate::rtl::lead_center(rtl);
ui.painter().text(pos2(crate::rtl::lead_x(rtl, r, 40.0), r.min.y + 13.0), side, crate::i18n::isolate(name), medium(13.0), t.text);
ui.painter().text(pos2(crate::rtl::lead_x(rtl, r, 40.0), r.min.y + 29.0), side, crate::i18n::isolate_path(p), regular(11.0), t.text_dim);
// A wide row on Home can push the row past the visible area at small window sizes, so the
// text stops at the visible edge and its ellipsis stays in view. Only its far end does: it
// starts at the row's start, in view or not.
let clip = ui.clip_rect();
let room = if rtl { r.max.x - r.min.x.max(clip.min.x) } else { r.max.x.min(clip.max.x) - r.min.x };
let text_w = (room - 48.0).max(0.0);
let fit = |text: String, direction: TextDirection, font: egui::FontId, color: egui::Color32, y: f32| {
let mut job = egui::text::LayoutJob::simple_singleline(text, font, color);
job.direction = direction;
job.wrap = egui::text::TextWrapping::truncate_at_width(text_w);
let g = ui.painter().layout_job(job);
let at = side.anchor_size(pos2(crate::rtl::lead_x(rtl, r, 40.0), y), g.size()).min;
ui.painter().galley(at, g, color);
};
// An isolate hides a right-to-left name's direction from its line, which would put the
// ellipsis beside the name's first letters.
let direction = if wordcraft_doc::bidi::first_strong_is_rtl(name) { TextDirection::RightToLeft } else { TextDirection::Auto };
fit(crate::i18n::isolate(name), direction, medium(13.0), t.text, r.min.y + 13.0);
fit(crate::i18n::isolate_path(p), TextDirection::Auto, regular(11.0), t.text_dim, r.min.y + 29.0);
let xr = Rect::from_min_max(pos2(r.max.x + 2.0, r.min.y + 4.0), pos2(r.max.x + REMOVE_W, r.max.y - 4.0));
// Right-to-left: the × sits on the left, past the row's trailing edge.
let xr = if rtl { xr.translate(vec2(-(r.width() + REMOVE_W + 2.0), 0.0)) } else { xr };
@@ -691,6 +718,41 @@ fn open_list(app: &mut WordApp, ui: &mut Ui) {
{
let _ = app.run("ui.recentClear", json!({}));
}
// A row shows a name and a path that may not fit: the path in full is its tooltip, for the
// hovered row or, after a key press, the focused one. egui shows one tooltip per layer, so
// only one of them is drawn, and the focus tooltip lets the pointer through to the rows below.
let keyboard = recent_keyboard_last(ui);
match (focused, hovered) {
(Some((resp, p)), hovered) if keyboard || hovered.is_none() => {
egui::Tooltip::for_widget(&resp).show(|ui| ui.label(crate::i18n::isolate_path(p)));
}
(_, Some((resp, p))) => {
resp.on_hover_text(crate::i18n::isolate_path(p));
}
_ => {}
}
}
/// Was a key pressed more recently than the pointer moved? Kept across passes, so a focused
/// Recent row keeps its tooltip while the pointer rests on another row.
fn recent_keyboard_last(ui: &Ui) -> bool {
let id = egui::Id::unique("backstage_recent_keyboard_last");
let now = ui.input(|i| {
if i.pointer.delta() != egui::Vec2::ZERO {
Some(false)
} else if i.events.iter().any(|e| matches!(e, egui::Event::Key { pressed: true, .. })) {
Some(true)
} else {
None
}
});
match now {
Some(keyboard) => {
ui.ctx().data_mut(|d| d.insert_temp(id, keyboard));
keyboard
}
None => ui.ctx().data(|d| d.get_temp(id)).unwrap_or(false),
}
}
/// Stable storage id for the Recent list's open error.
+283
View File
@@ -75,6 +75,289 @@ fn opening_a_missing_recent_file_keeps_file_open_with_the_error() {
assert!(app.ui.backstage, "File stays open");
}
/// Opens File > Home in a window of `size` with one long Recent entry; returns its name and path.
fn long_recent_harness(size: egui::Vec2) -> (egui_kittest::Harness<'static, WordApp>, String, String) {
let name = format!("{}.docx", "AVeryLongDocumentName".repeat(8));
let path = format!("C:/Users/Someone/{}/{name}", "LongFolderName".repeat(20));
(recent_harness(std::slice::from_ref(&path), size), name, path)
}
/// Opens File > Home in a window of `size` with `recent` as the Recent list.
fn recent_harness(recent: &[String], size: egui::Vec2) -> egui_kittest::Harness<'static, WordApp> {
let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), Services::default());
a.ui.recent = recent.to_vec();
a.run("ui.backstage", json!({"value": true, "page": "home"})).unwrap();
let mut h = egui_kittest::Harness::builder().with_size(size).build_ui_state(
|ui, app: &mut WordApp| {
let ctx = ui.ctx().clone();
app.logic(&ctx);
app.ui(ui);
},
a,
);
h.run_steps(4);
h
}
/// Collects every text shape, including those nested in shape lists.
fn collect_text<'a>(s: &'a egui::Shape, out: &mut Vec<&'a egui::epaint::TextShape>) {
match s {
egui::Shape::Text(ts) => out.push(ts),
egui::Shape::Vec(v) => {
for child in v {
collect_text(child, out);
}
}
_ => {}
}
}
/// Returns the painted bounds and clip rect of the Recent entry's name and path texts, and
/// whether each ends with an ellipsis.
fn recent_text_bounds(h: &egui_kittest::Harness<'_, WordApp>) -> Vec<(String, egui::Rect, egui::Rect, bool)> {
let mut found = Vec::new();
for cs in &h.output().shapes {
let mut texts = Vec::new();
collect_text(&cs.shape, &mut texts);
for ts in texts {
let text = ts.galley.text();
if text.contains("C:/Users/Someone") || text.contains("AVeryLongDocumentName") {
let ellipsis = ts.galley.rows.first().and_then(|row| row.glyphs.last()).is_some_and(|g| g.chr == '\u{2026}');
found.push((text.to_owned(), ts.visual_bounding_rect(), cs.clip_rect, ellipsis));
}
}
}
found
}
/// #5212: long names and paths in File > Home > Recent shorten to fit their row and show on hover.
#[test]
fn recent_shortens_a_long_name_and_path_to_its_row() {
let (mut h, name, path) = long_recent_harness(egui::vec2(1440.0, 900.0));
let row = h.get_by_label(&name).rect();
let texts = recent_text_bounds(&h);
assert_eq!(texts.len(), 2, "both file name and path shapes must be found");
for (text, bounds, _, ellipsis) in &texts {
assert!(*ellipsis, "text '{text}' must end with an ellipsis");
assert!(bounds.max.x <= row.max.x + 0.5, "text '{text}' bounds.max.x ({}) > row.max.x + 0.5 ({})", bounds.max.x, row.max.x + 0.5);
}
h.event(egui::Event::PointerMoved(row.center()));
h.run_steps(4);
h.get_by_label(&path);
}
/// #5212: at the smallest window width the shortened texts end inside the visible area, so the
/// ellipsis is not cut off.
#[test]
fn recent_texts_end_inside_the_visible_area_at_the_smallest_window() {
let (h, _, _) = long_recent_harness(egui::vec2(760.0, 480.0));
let texts = recent_text_bounds(&h);
assert_eq!(texts.len(), 2, "both file name and path shapes must be found");
for (text, bounds, clip, ellipsis) in &texts {
assert!(*ellipsis, "text '{text}' must end with an ellipsis");
assert!(bounds.max.x <= clip.max.x + 0.5, "text '{text}' ends at {} past the visible edge {}", bounds.max.x, clip.max.x);
}
}
/// #5212: a Recent row focused with the keyboard shows its full path, as hovering does.
#[test]
fn recent_shows_the_full_path_when_a_row_has_keyboard_focus() {
let (mut h, name, path) = long_recent_harness(egui::vec2(1440.0, 900.0));
assert!(h.query_by_label(&path).is_none(), "the full path must not show before focus");
h.get_by_label(&name).focus();
h.run_steps(4);
h.get_by_label(&path);
}
/// #5212: the focus tooltip lets the pointer through, so the Recent row under it still gets
/// hovered, shows its own path and opens on click.
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn recent_focus_tooltip_does_not_block_the_row_below() {
let dir = std::env::temp_dir().join(format!("wordcraft-5212-{}", std::process::id()));
let paths = ["one.docx", "two.docx"].map(|n| dir.join(n).to_string_lossy().to_string());
let mut h = recent_harness(&paths, egui::vec2(1440.0, 900.0));
h.get_by_label("one.docx").focus();
h.run_steps(4);
let tip = h.get_by_label(&paths[0]).rect();
let below = tip.intersect(h.get_by_label("two.docx").rect());
assert!(below.is_positive(), "the focus tooltip must cover part of the next row: tip={tip:?}");
h.event(egui::Event::PointerMoved(below.center()));
h.run_steps(4);
h.get_by_label(&paths[1]);
for pressed in [true, false] {
h.event(egui::Event::PointerButton { pos: below.center(), button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE });
h.run_steps(1);
}
h.run_steps(3);
let error = h.ctx.data(|d| d.get_temp::<String>(super::open_error_id())).unwrap_or_default();
assert!(error.contains("two.docx"), "the click must reach two.docx and try to open it, got error '{error}'");
}
/// #5212: every Recent row gives a screen reader its full path, not only the hovered or
/// focused row that shows the path as a tooltip.
#[test]
fn recent_rows_describe_their_full_path_without_hover_or_focus() {
use egui_kittest::kittest::NodeT;
let dir = std::env::temp_dir().join("wordcraft-5212-describe");
let paths = ["one.docx", "two.docx"].map(|n| dir.join(n).to_string_lossy().to_string());
let mut h = recent_harness(&paths, egui::vec2(1440.0, 900.0));
let one = h.get_by_label("one.docx").rect();
h.event(egui::Event::PointerMoved(one.center()));
h.run_steps(4);
for (name, path) in ["one.docx", "two.docx"].iter().zip(&paths) {
let described = h.get_by_label(name).accesskit_node().description();
assert_eq!(described.as_deref(), Some(crate::i18n::isolate_path(path).as_str()), "{name} must describe its full path");
}
}
/// A right-to-left language for the Recent tests. `rtl` goes by its code, so it needs no Arabic font.
fn recent_right_to_left() -> crate::i18n::Lang {
crate::i18n::fixture_lang("ar", "\tHome\tHome\n")
}
/// The Recent list alone with one `path`, in `lang`, in a window 1000 wide. `start` points on the
/// side its rows start from and `end` points on the other side are out of view, as on a page
/// scrolled sideways or wider than the window.
fn recent_list_cut(path: &str, lang: crate::i18n::Lang, start: f32, end: f32) -> egui_kittest::Harness<'static, WordApp> {
let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), Services::default());
a.ui.recent = vec![path.to_string()];
let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1000.0, 500.0)).build_ui_state(
move |ui, app: &mut WordApp| {
let ctx = ui.ctx().clone();
app.logic(&ctx);
crate::i18n::set_current(lang);
crate::rtl::sync(&ctx);
let mut clip = ui.clip_rect();
let (left, right) = if crate::rtl::on() { (end, start) } else { (start, end) };
clip.min.x += left;
clip.max.x -= right;
ui.set_clip_rect(clip);
super::open_list(app, ui);
},
a,
);
h.run_steps(4);
h
}
/// #5212: text runs from its row's start, so a row whose start is out of view has as much room
/// for its text as before. Only the far end stops at the visible edge.
#[test]
fn recent_text_keeps_its_room_when_its_row_starts_out_of_view() {
let name = format!("{}.docx", "AVeryLongDocumentName".repeat(8));
let path = format!("C:/Users/Someone/{name}");
for lang in [crate::i18n::Lang::EN, recent_right_to_left()] {
let widths = |cut| recent_text_bounds(&recent_list_cut(&path, lang, cut, 0.0)).iter().map(|t| t.1.width().round()).collect::<Vec<_>>();
let whole = widths(0.0);
assert_eq!(whole.len(), 2, "both lines must be painted: {whole:?}");
assert_eq!(widths(500.0), whole, "the cut must not take room from the text");
}
crate::i18n::set_current(crate::i18n::Lang::EN);
}
/// #5212: a row running past the visible area stops its text there, in either direction, with
/// its ellipsis in view.
#[test]
fn recent_text_stops_at_the_visible_edge_in_either_direction() {
let name = format!("{}.docx", "AVeryLongDocumentName".repeat(8));
let path = format!("C:/Users/Someone/{name}");
for lang in [crate::i18n::Lang::EN, recent_right_to_left()] {
let texts = recent_text_bounds(&recent_list_cut(&path, lang, 0.0, 400.0));
assert_eq!(texts.len(), 2, "both lines must be painted");
for (text, bounds, clip, ellipsis) in &texts {
assert!(
bounds.left() >= clip.left() - 0.5 && bounds.right() <= clip.right() + 0.5,
"{text} must end inside the visible area: {bounds:?} in {clip:?}"
);
assert!(ellipsis, "{text} must end in an ellipsis");
}
}
crate::i18n::set_current(crate::i18n::Lang::EN);
}
/// #5212: multibyte text is cut between its characters and stays in its row, starting at the
/// side the row starts from, in either direction.
#[test]
fn recent_shortens_multibyte_text_in_either_direction() {
let name = format!("AVeryLongDocumentName{}.docx", "\u{fc}\u{20ac}".repeat(120));
let path = format!("C:/Users/Someone/{name}");
for lang in [crate::i18n::Lang::EN, recent_right_to_left()] {
let h = recent_list_cut(&path, lang, 0.0, 0.0);
let row = h.get_by_label(&name).rect();
let texts = recent_text_bounds(&h);
assert_eq!(texts.len(), 2, "both lines must be painted");
for (text, bounds, _, ellipsis) in &texts {
let lead = if lang.rtl() { row.right() - bounds.right() } else { bounds.left() - row.left() };
assert!((lead - 40.0).abs() < 1.0, "{text} must start at its row's start: {bounds:?} in {row:?}");
assert!(bounds.left() >= row.left() - 0.5 && bounds.right() <= row.right() + 0.5, "{text} must stay in its row: {bounds:?} in {row:?}");
assert!(ellipsis, "{text} must end in an ellipsis");
}
}
crate::i18n::set_current(crate::i18n::Lang::EN);
}
/// #5212: a right-to-left name runs from the right, so it is cut on its left, and its ellipsis
/// belongs there, not beside the name's first letters.
#[test]
fn recent_puts_the_ellipsis_at_the_end_of_a_right_to_left_name() {
let name = format!("{}.docx", "\u{5d0}\u{5d1}\u{5d2} ".repeat(120));
let h = recent_list_cut(&format!("/docs/{name}"), recent_right_to_left(), 0.0, 0.0);
let output = h.output();
let mut texts = Vec::new();
for clipped in &output.shapes {
collect_text(&clipped.shape, &mut texts);
}
let shown = texts.iter().find(|t| t.galley.text().contains('\u{5d0}')).expect("the name");
let glyphs = &shown.galley.rows[0].glyphs;
let ellipsis = glyphs.iter().find(|g| g.chr == '\u{2026}').expect("an ellipsis");
let left = glyphs.iter().filter(|g| g.size().x > 0.0).map(|g| g.pos.x).fold(f32::MAX, f32::min);
assert!(ellipsis.pos.x <= left + 0.5, "the ellipsis must be the leftmost glyph: {} against {left}", ellipsis.pos.x);
crate::i18n::set_current(crate::i18n::Lang::EN);
}
/// #5212: two files with one name speak their path in the label, so it isn't read twice.
#[test]
fn recent_rows_with_one_name_speak_their_path_once() {
use egui_kittest::kittest::NodeT;
let dir = std::env::temp_dir().join("wordcraft-5212-twins");
let paths = ["one", "two"].map(|d| dir.join(d).join("same.docx").to_string_lossy().to_string());
let h = recent_harness(&paths, egui::vec2(1440.0, 900.0));
for path in &paths {
let label = format!("same.docx, {path}");
assert_eq!(h.get_by_label(&label).accesskit_node().description(), None, "{label} must not repeat its path");
}
}
/// #5212: with the pointer resting on one Recent row, Tab to the next row shows the focused
/// row's path, and moving the pointer again brings back the hovered row's path.
#[test]
fn recent_tab_shows_the_focused_path_while_the_pointer_rests_on_another_row() {
let dir = std::env::temp_dir().join("wordcraft-5212-tab");
let paths = ["one.docx", "two.docx"].map(|n| dir.join(n).to_string_lossy().to_string());
let mut h = recent_harness(&paths, egui::vec2(1440.0, 900.0));
let one = h.get_by_label("one.docx").rect();
h.event(egui::Event::PointerMoved(one.center()));
h.run_steps(4);
h.get_by_label(&paths[0]);
h.get_by_label("one.docx").focus();
h.run_steps(1);
// The first Tab goes to the row's own Remove ×, the second to the next row.
for _ in 0..2 {
h.key_press(egui::Key::Tab);
h.run_steps(4);
}
assert!(h.get_by_label("two.docx").is_focused(), "Tab must move focus to the next Recent row");
assert!(h.query_by_label(&paths[1]).is_some(), "the focused row's path must show while the pointer rests on another row");
h.event(egui::Event::PointerMoved(one.center() + egui::vec2(4.0, 0.0)));
h.run_steps(4);
assert!(h.query_by_label(&paths[0]).is_some(), "moving the pointer must bring back the hovered row's path");
}
/// #1873: the macOS back chevron sits beside the traffic lights windowed, at the plain
/// control inset in full screen.
#[test]