From f8176040a0b16dbb55a0e17034fe5f338f3d47bb Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sun, 22 Nov 2026 04:31:12 +0700 Subject: [PATCH] menu: summary::text_for writes the shortcut summary without a Ui, in the host's language --- crates/menu/src/shortcuts/summary.rs | 9 +++++++++ crates/menu/src/shortcuts/words.rs | 8 ++++++++ crates/menu/tests/it/shortcuts/rows.rs | 22 ++++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/crates/menu/src/shortcuts/summary.rs b/crates/menu/src/shortcuts/summary.rs index 387099b22..13eef64bb 100644 --- a/crates/menu/src/shortcuts/summary.rs +++ b/crates/menu/src/shortcuts/summary.rs @@ -3,6 +3,9 @@ //! cheat sheet outside the dialog. use std::fmt::Write as _; +use std::sync::Arc; + +use craft_host::Host; use craft_keymap::{Binding, Keymap, Scope}; use egui::Ui; @@ -109,6 +112,12 @@ pub fn text(km: &Keymap, ui: &Ui, title: &str) -> String { text_in(km, &Say::of(ui), title) } +/// [`text`] without a `Ui`, in the interface language of `host` (a command or a file export, which run outside a +/// frame). +pub fn text_for(km: &Keymap, host: &Arc, title: &str) -> String { + text_in(km, &Say::of_host(host), title) +} + pub(crate) fn text_in(km: &Keymap, text: &Say, title: &str) -> String { let mut s = one_line(title); s.push('\n'); diff --git a/crates/menu/src/shortcuts/words.rs b/crates/menu/src/shortcuts/words.rs index cf5c21901..5b11c7b29 100644 --- a/crates/menu/src/shortcuts/words.rs +++ b/crates/menu/src/shortcuts/words.rs @@ -262,6 +262,14 @@ impl Say { Say { words: view.words(), keys: view.words(), host: view.host().cloned(), rtl: view.direction().is_rtl(), folder: env.folder() } } + /// The words of `host`'s interface language, for code without a `Ui`: what [`Say::of`] reads in a scope under + /// `host`, with the direction of the language. + pub(crate) fn of_host(host: &Arc) -> Say { + let words = host.words(); + let rtl = host.i18n().is_some_and(|i18n| i18n.rtl(host.lang())); + Say { words: words.set(), keys: words.set(), host: Some(Arc::clone(host)), rtl, folder: Folder::new(host.language_code()) } + } + /// The folder that folds search text in the dialog's language. pub(crate) fn folder(&self) -> Folder { self.folder diff --git a/crates/menu/tests/it/shortcuts/rows.rs b/crates/menu/tests/it/shortcuts/rows.rs index d44ce2363..eb9a2c6c6 100644 --- a/crates/menu/tests/it/shortcuts/rows.rs +++ b/crates/menu/tests/it/shortcuts/rows.rs @@ -88,3 +88,25 @@ fn the_summary_lists_hold_bindings() { let (text, html) = summarize(&mut rig); assert!(!text.contains("Hold K") && !html.contains("Hold K"), "with holds off the summary has no hold: {text}"); } + +/// The summary written without a `Ui` (a command's file export) is the one Summarize… writes under the same host. +#[trial] +fn the_summary_without_a_ui_is_the_one_summarize_writes() { + let host = Arc::new(Host::builder(AppInfo { id: "trial", name: "Trial", version: "0" }).build()); + let km = fixture::keymap(Platform::Mac); + let mut rig = Rig::new(km.clone(), fixture::options(), None); + rig.fixture.host(host.clone()); + rig.pass(); + rig.click("Summarize…"); + let text = rig + .outcomes() + .into_iter() + .rev() + .find_map(|o| match o { + Outcome::Summary { text, .. } => Some(text), + _ => None, + }) + .expect("a summary"); + let (title, _) = text.split_once('\n').expect("a title line"); + assert_eq!(craft_menu::shortcuts::summary::text_for(&km, &host, title), text); +}