From f587bcb95ac3f501ef09bdc4df961c4aba9deba5 Mon Sep 17 00:00:00 2001 From: Lucas Batista Vargas Date: Wed, 25 Nov 2026 12:38:16 -0300 Subject: [PATCH] fix(keytips): record ribbon menu_button commands and restore language test (#7317) (#7612) --- crates/ui-egui/src/keytips.rs | 58 +++++++++++++++++++++- crates/ui-egui/src/keytips/tests.rs | 74 +++++++++++++++++++++++++++++ crates/ui-egui/src/ribbon/common.rs | 3 ++ 3 files changed, 134 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/keytips.rs b/crates/ui-egui/src/keytips.rs index 5467e99939..61bb6357e4 100644 --- a/crates/ui-egui/src/keytips.rs +++ b/crates/ui-egui/src/keytips.rs @@ -3,6 +3,7 @@ //! machine, badges, claimed keys) is the framework's `Keytips`; this module gives it WordCraft's //! tabs, controls, legacy sequences and regions. +use std::collections::HashMap; use std::sync::{Arc, OnceLock}; use craft::behavior::overlay::MinWidth; @@ -34,6 +35,60 @@ pub(crate) fn backstage_region() -> egui::Id { egui::Id::unique("wc_keytips_backstage_region") } +/// Per-context map of ribbon `menu_button` widget ids to the command id each opens a menu for, +/// so [`Keytips`] places badges on menu buttons without binding them to fire on pointer click. +fn menu_buttons_id() -> egui::Id { + egui::Id::unique("wc_keytips_menu_buttons") +} + +/// Records (or clears) the command id associated with a ribbon `menu_button` widget id so +/// [`show`] can attach `CommandRef` to its inspected element for language-independent badge placement. +pub(crate) fn note_menu_button(ctx: &egui::Context, widget_id: egui::Id, command_id: Option<&str>) { + let existing = ctx.data(|d| d.get_temp::>>(menu_buttons_id())); + let unchanged = match (&existing, command_id) { + (Some(map), Some(id)) => map.get(&widget_id).is_some_and(|cmd| cmd.id == id && cmd.params.is_null()), + (Some(map), None) => !map.contains_key(&widget_id), + (None, None) => true, + (None, Some(_)) => false, + }; + if unchanged { + return; + } + ctx.data_mut(|d| { + let mut map = d.get_temp::>>(menu_buttons_id()).map(|a| (*a).clone()).unwrap_or_default(); + match command_id { + Some(id) => { + map.insert(widget_id, CommandRef::new(id)); + } + None => { + map.remove(&widget_id); + } + } + d.insert_temp(menu_buttons_id(), Arc::new(map)); + }); +} + +/// Attaches noted `menu_button` commands to uncommanded root elements of a ribbon `Recorded` +/// snapshot so `craft::menu::Keytips` places badges on menu buttons regardless of translation. +fn with_menu_commands(ctx: &egui::Context, recorded: Arc) -> Arc { + let Some(commands) = ctx.data(|d| d.get_temp::>>(menu_buttons_id())) else { + return recorded; + }; + if commands.is_empty() || !recorded.elements.iter().any(|e| e.part.is_none() && e.command.is_none() && commands.contains_key(&e.id)) { + return recorded; + } + let mut patched = (*recorded).clone(); + for element in &mut patched.elements { + if element.part.is_none() + && element.command.is_none() + && let Some(cmd) = commands.get(&element.id) + { + element.command = Some(cmd.clone()); + } + } + Arc::new(patched) +} + /// Word's tab letters, as Word for Windows shows them after Alt (File F, Design G, References S, /// Review R, View W, Help Y). File opens the Backstage. const TAB_LETTERS: [(&str, &str); 11] = [ @@ -487,7 +542,8 @@ pub(crate) fn show(ui: &mut Ui, app: &mut WordApp, ribbon: &RibbonOutput }; let mut places: Vec> = Vec::new(); - places.extend(ribbon.controls.clone()); + let patch_menu_places = enabled && (app.keytips_active || !ctx.input(|i| i.events.is_empty())); + places.extend(ribbon.controls.clone().map(|recorded| if patch_menu_places { with_menu_commands(&ctx, recorded) } else { recorded })); places.extend(inspect::recorded(&ctx, qat_region())); places.extend(inspect::recorded(&ctx, search_region())); if app.ui.backstage { diff --git a/crates/ui-egui/src/keytips/tests.rs b/crates/ui-egui/src/keytips/tests.rs index 5a15af6740..d899e84853 100644 --- a/crates/ui-egui/src/keytips/tests.rs +++ b/crates/ui-egui/src/keytips/tests.rs @@ -30,6 +30,25 @@ fn bold(h: &mut egui_kittest::Harness<'static, WordApp>) -> bool { h.state_mut().session.run("format.state", &serde_json::json!({})).unwrap()["bold"] == true } +fn painted_text_rects(shapes: &[egui::epaint::ClippedShape]) -> Vec<(String, egui::Rect)> { + fn walk(shape: &egui::Shape, out: &mut Vec<(String, egui::Rect)>) { + match shape { + egui::Shape::Text(t) => out.push((t.galley.text().to_string(), t.visual_bounding_rect())), + egui::Shape::Vec(children) => { + for child in children { + walk(child, out); + } + } + _ => {} + } + } + let mut out = Vec::new(); + for clipped in shapes { + walk(&clipped.shape, &mut out); + } + out +} + #[test] fn every_keytip_command_is_registered() { // The keytip tables name commands by id; a renamed or removed command must fail here, @@ -73,6 +92,61 @@ fn keytips_never_reuse_another_commands_shortcut_letter() { } } +/// Home's Paste, a menu button, shows its keytip whatever the interface language. The keytips +/// table holds the English tips, and `menu_button` previously matched it with its translated one, +/// which no other language's "Paste" equals or begins with. Pseudo-localization stands in for one. +#[test] +fn a_menu_buttons_keytip_does_not_depend_on_the_language() { + use egui::accesskit::Role; + use egui_kittest::kittest::Queryable as _; + + let a = app(); + let reserved = reserved_letters(&a); + let cache = KeytipCache::new(&a, "Home"); + let paste_idx = cache.tab_controls.iter().position(|c| c.command.id == "edit.paste").expect("Home tab has Paste"); + let labels: Vec<(&str, Option<&str>)> = cache.tab_controls.iter().map(|c| (c.label, c.letters)).collect(); + + let english_folder = craft::i18n::Folder::new("en"); + let english_badges = craft::keymap::mnemonic::assign_keytips(&labels, &reserved, &english_folder); + let paste_badge = &english_badges[paste_idx]; + assert!(!paste_badge.is_empty(), "Paste receives a keytip badge"); + + for lang in ["de", "fr", "es", "tr", "pseudo", "ja", "zh", "pt-BR"] { + let folder = craft::i18n::Folder::new(lang); + let badges = craft::keymap::mnemonic::assign_keytips(&labels, &reserved, &folder); + assert_eq!(&badges[paste_idx], paste_badge, "language={lang}: Paste badge must stay {paste_badge} regardless of language"); + } + + let mut h = harness(app()); + h.ctx.add_glyph_rasterizer(egui::GlyphRasterizer::new("wordcraft::keytips::pseudo_markers", |request| { + matches!(request.cluster, "⟦" | "⟧").then(|| egui::RasterizedGlyph { + bitmap: egui::GlyphBitmap { + image: egui::ColorImage::new([1, 1], vec![egui::Color32::WHITE]), + offset_px: egui::Vec2::ZERO, + is_color: false, + }, + advance_px: request.font_size_px.max(1.0), + }) + })); + h.state_mut().ui.language = "pseudo".into(); + crate::test_harness::frames(&mut h, 2); + + let paste_rect = h.get_by_role_and_label(Role::Button, "⟦Paste⟧").rect(); + h.input_mut().events.push(key_event(egui::Key::F10, true, egui::Modifiers::NONE)); + crate::test_harness::frame(&mut h); + h.input_mut().events.extend([key_event(egui::Key::H, true, egui::Modifiers::NONE), egui::Event::Text("h".into())]); + crate::test_harness::frames(&mut h, 3); + let painted = painted_text_rects(&h.output().shapes); + crate::i18n::apply_language(crate::i18n::Lang::EN); + + assert_eq!(h.state().ui.tab.as_str(), "Home"); + assert!(h.state().keytips_active, "F10, H activates Home command keytips"); + assert!( + painted.iter().any(|(text, rect)| text == paste_badge && paste_rect.expand(8.0).contains(rect.center())), + "Paste's {paste_badge:?} badge is drawn over Paste {paste_rect:?} under pseudo-localization: {painted:?}" + ); +} + /// #1697: legacy sequences never start on a tab keytip (table tabs included) or Search, and /// every one reaches a command or dialog that exists. #[test] diff --git a/crates/ui-egui/src/ribbon/common.rs b/crates/ui-egui/src/ribbon/common.rs index 89cfd930be..d6b479d563 100644 --- a/crates/ui-egui/src/ribbon/common.rs +++ b/crates/ui-egui/src/ribbon/common.rs @@ -36,6 +36,9 @@ pub(crate) fn menu_button( face = face.tooltip(TooltipSpec::new(hint.clone()).disabled_text(hint)); } let opener = face.trailing_icon(IconSpec::glyph(&MENU_ARROW)).enabled(enabled).show(ui); + if !ui.is_sizing_pass() { + crate::keytips::note_menu_button(ui.ctx(), opener.response.id, about.filter(|id| !id.is_empty())); + } Menu::new(name).opened_by(&opener.response, Trigger::Click).show(ui, body).output }