Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Menus: Chinese, Japanese and Korean labels get access keys, "文件(F)" (#5863)

Higgs32584b153c4e74e23
diff --git a/crates/ui-egui/src/menu_keys.rs b/crates/ui-egui/src/menu_keys.rs
index 5762b2a94..e49849b40 100644
--- a/crates/ui-egui/src/menu_keys.rs
+++ b/crates/ui-egui/src/menu_keys.rs
@@ -8,7 +8,8 @@
//! They are assigned per menu level from the labels as shown (so translated), unique within
//! the level: Photoshop's letters for the top menus where the label has them (F E I L Y S T V
//! W H), else the first unused letter, word starts first. Only ASCII letters and digits are keys
-//! the keyboard reports as such, so a label without one (Japanese, Chinese…) has no mnemonic.
+//! the keyboard reports as such, so a label without one (Japanese, Chinese…) shows its English
+//! label's letter after it, as Windows menus do in those languages: "文件(F)" ([`localized`]).
//!
//! The state is view state in egui memory, like [`crate::menu_nav::Nav`]. On macOS the native
//! menu bar replaces the in-window one and this never runs.
@@ -94,6 +95,37 @@ pub fn assign(labels: &[&str], preferred: &[Option<char>]) -> Vec<Option<char>>
out
}
+/// The labels to show and their mnemonics for translated `labels` whose English originals are
+/// `english` (same order). A label with an ASCII letter or digit keeps its text and is assigned
+/// as [`assign`] does. One without (Chinese, Japanese, Korean…) can't be typed on its own, so it
+/// shows its English label's letter after it in parentheses and takes that letter, as Windows menus
+/// do in those languages ("文件(F)", #5863). Mnemonics stay unique within the group.
+pub fn localized(labels: &[String], english: &[&str], preferred: &[Option<char>]) -> (Vec<String>, Vec<Option<char>>) {
+ let typed = |l: &str| l.chars().any(|c| c.is_ascii_alphanumeric());
+ // The key goes before a trailing ellipsis, as Windows writes it: "新建(N)…".
+ let with_key = |l: &str, k: char| match l.strip_suffix('…').map(|b| (b, "…")).or_else(|| l.strip_suffix("...").map(|b| (b, "..."))) {
+ Some((base, tail)) => format!("{base}({k}){tail}"),
+ None => format!("{l}({k})"),
+ };
+ let english_keys = assign(english, preferred);
+ let mut shown = Vec::with_capacity(labels.len());
+ let mut pref = Vec::with_capacity(labels.len());
+ for (i, label) in labels.iter().enumerate() {
+ match english_keys.get(i).copied().flatten().filter(|_| !typed(label)) {
+ Some(k) => {
+ shown.push(with_key(label, k));
+ pref.push(Some(k));
+ }
+ None => {
+ shown.push(label.clone());
+ pref.push(preferred.get(i).copied().flatten());
+ }
+ }
+ }
+ let keys = assign(&shown.iter().map(String::as_str).collect::<Vec<_>>(), &pref);
+ (shown, keys)
+}
+
/// `prefix + label` as menu text with the mnemonic's first occurrence in `label` underlined
/// (none when `mnemonic` is None). `color` is the text colour; None takes the widget's.
pub fn mnemonic_text(ui: &Ui, prefix: &str, label: &str, mnemonic: Option<char>, color: Option<Color32>) -> egui::WidgetText {
diff --git a/crates/ui-egui/src/menu_keys_tests.rs b/crates/ui-egui/src/menu_keys_tests.rs
index 1f226054e..0e0ce65e3 100644
--- a/crates/ui-egui/src/menu_keys_tests.rs
+++ b/crates/ui-egui/src/menu_keys_tests.rs
@@ -210,3 +210,27 @@ fn the_shortcuts_wait_while_the_keyboard_is_on_the_bar() {
h.run_steps(3);
assert_ne!(h.state().ui.tool, tool, "off the bar, E picks the Eraser again");
}
+
+/// #5863: a label with no Latin letter (Chinese, Japanese…) shows its English label's letter in
+/// parentheses, before a trailing ellipsis, and takes it as its mnemonic; labels with letters and
+/// the top menus' Photoshop letters are as before, and the letters stay unique.
+#[test]
+fn labels_without_latin_letters_show_their_english_key() {
+ let labels: Vec<String> = ["文件", "编辑", "Image", "图层"].map(String::from).to_vec();
+ let english = ["File", "Edit", "Image", "Layer"];
+ let preferred = [Some('F'), Some('E'), Some('I'), Some('L')];
+ let (shown, keys) = crate::menu_keys::localized(&labels, &english, &preferred);
+ assert_eq!(shown, ["文件(F)", "编辑(E)", "Image", "图层(L)"]);
+ assert_eq!(keys, [Some('F'), Some('E'), Some('I'), Some('L')]);
+ // Rows: the key goes before the ellipsis; a clash takes the next letter of the shown label.
+ let labels: Vec<String> = ["新建…", "打开...", "关闭"].map(String::from).to_vec();
+ let (shown, keys) = crate::menu_keys::localized(&labels, &["New…", "Open...", "Close"], &[]);
+ assert_eq!(shown, ["新建(N)…", "打开(O)...", "关闭(C)"]);
+ assert_eq!(keys, [Some('N'), Some('O'), Some('C')]);
+ let (shown, keys) = crate::menu_keys::localized(&["关闭".to_string(), "复制".to_string()], &["Close", "Copy"], &[]);
+ assert_eq!(shown, ["关闭(C)", "复制(O)"]);
+ assert_eq!(keys, [Some('C'), Some('O')]);
+ // An English UI is unchanged.
+ let labels: Vec<String> = ["File", "Edit"].map(String::from).to_vec();
+ assert_eq!(crate::menu_keys::localized(&labels, &["File", "Edit"], &[Some('F'), Some('E')]).0, labels);
+}
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs
index 7f47e6ea6..f031b8650 100644
--- a/crates/ui-egui/src/menus.rs
+++ b/crates/ui-egui/src/menus.rs
@@ -1208,9 +1208,10 @@ pub fn menu_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) -> f32 {
let mut keys = crate::menu_keys::Keys::load(ui.ctx());
keys.take_first_row(&mut nav);
nav.underline = keys.show_underlines(ui.ctx());
- let top_labels: Vec<String> = TOP_MENUS.iter().map(|top| crate::i18n::tr(lang, top).to_string()).collect();
+ let translated: Vec<String> = TOP_MENUS.iter().map(|top| crate::i18n::tr(lang, top).to_string()).collect();
let preferred: Vec<Option<char>> = TOP_MENUS.iter().map(|top| crate::menu_keys::TOP_PREFERRED.iter().find(|(t, _)| t == top).map(|(_, c)| *c)).collect();
- let top_mnemonics = crate::menu_keys::assign(&top_labels.iter().map(String::as_str).collect::<Vec<_>>(), &preferred);
+ // A title with no Latin letter shows its English key: "文件(F)" (#5863).
+ let (top_labels, top_mnemonics) = crate::menu_keys::localized(&translated, &TOP_MENUS, &preferred);
ui.scope(|ui| {
ui.spacing_mut().item_spacing.x = 0.0;
let v = &mut ui.style_mut().visuals;
@@ -1496,24 +1497,25 @@ fn render_level_rows(ui: &mut egui::Ui, items: &[&MenuItem], depth: usize, click
// of its first child.
// Each row's mnemonic (#4988), unique within this level, in display order: the leaves' labels
// and the submenus' names as shown.
- let shown_labels: Vec<String> = {
+ // A label with no Latin letter shows its English label's key after it: "新建(N)…" (#5863).
+ let (translated, english): (Vec<String>, Vec<&str>) = {
let mut subs: Vec<&str> = Vec::new();
items
.iter()
.filter_map(|it| {
if it.path.len() == depth {
- (it.label != "---").then(|| translated_menu_label(lang, it))
+ (it.label != "---").then(|| (translated_menu_label(lang, it), it.label.as_str()))
} else {
let name = it.path.get(depth).map(String::as_str)?;
(!subs.contains(&name)).then(|| {
subs.push(name);
- crate::i18n::tr(lang, name).to_string()
+ (crate::i18n::tr(lang, name).to_string(), name)
})
}
})
- .collect()
+ .unzip()
};
- let mnemonics = crate::menu_keys::assign(&shown_labels.iter().map(String::as_str).collect::<Vec<_>>(), &[]);
+ let (shown_labels, mnemonics) = crate::menu_keys::localized(&translated, &english, &[]);
let mut next_row = 0usize;
let mut shown_subs: Vec<&str> = Vec::new();
let mut last_was_sep = true;
@@ -1526,7 +1528,7 @@ fn render_level_rows(ui: &mut egui::Ui, items: &[&MenuItem], depth: usize, click
}
continue;
}
- let label = translated_menu_label(lang, it);
+ let label = shown_labels.get(next_row).cloned().unwrap_or_else(|| translated_menu_label(lang, it));
let prefix = match it.checked {
Some(true) => "✔ ",
Some(false) => " ",
@@ -1567,12 +1569,9 @@ fn render_level_rows(ui: &mut egui::Ui, items: &[&MenuItem], depth: usize, click
let child: Vec<&MenuItem> = items.iter().copied().filter(|c| c.path.len() > depth && c.path[depth] == name).collect();
let enabled = submenu_enabled(&child);
let mnemonic = mnemonics.get(next_row).copied().flatten();
+ let shown = shown_labels.get(next_row).cloned().unwrap_or_else(|| crate::i18n::tr(lang, name).to_string());
next_row += 1;
- let title: egui::WidgetText = if nav.underline {
- crate::menu_keys::mnemonic_text(ui, "", crate::i18n::tr(lang, name), mnemonic, None)
- } else {
- crate::i18n::tr(lang, name).into()
- };
+ let title: egui::WidgetText = if nav.underline { crate::menu_keys::mnemonic_text(ui, "", &shown, mnemonic, None) } else { shown.into() };
ui.add_enabled_ui(enabled, |ui| {
nav.row(ui, depth - 1, enabled, None, mnemonic, |ui, nav| {
let r = ui.menu_button(title, |ui| render_level(ui, &child, depth + 1, clicked, nav));