Skip to content

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

Menus: a shown CJK "(K)" suffix always keeps its key

Higgs32584d0fd764ee310
diff --git a/crates/ui-egui/src/menu_keys.rs b/crates/ui-egui/src/menu_keys.rs
index b724275b8..6ea9f0df5 100644
--- a/crates/ui-egui/src/menu_keys.rs
+++ b/crates/ui-egui/src/menu_keys.rs
@@ -99,7 +99,8 @@ pub fn assign(labels: &[&str], preferred: &[Option<char>]) -> Vec<Option<char>>
/// `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.
+/// do in those languages ("文件(F)", #5863). Mnemonics stay unique within the group, and a shown
+/// "(F)" is always the row's key.
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)…".
@@ -122,7 +123,18 @@ pub fn localized(labels: &[String], english: &[&str], preferred: &[Option<char>]
}
}
}
- let keys = assign(&shown.iter().map(String::as_str).collect::<Vec<_>>(), &pref);
+ // The suffixed labels take their keys first: a label with letters of its own must not take
+ // the key a suffix shows ("Camera Raw 滤镜…" before "转换为智能滤镜(C)" leaves C to the latter).
+ let suffixed = |i: &usize| shown.get(*i) != labels.get(*i);
+ let order: Vec<usize> = (0..shown.len()).filter(suffixed).chain((0..shown.len()).filter(|i| !suffixed(i))).collect();
+ let ordered: Vec<&str> = order.iter().map(|&i| shown.get(i).map_or("", String::as_str)).collect();
+ let ordered_pref: Vec<Option<char>> = order.iter().map(|&i| pref.get(i).copied().flatten()).collect();
+ let mut keys = vec![None; shown.len()];
+ for (&i, key) in order.iter().zip(assign(&ordered, &ordered_pref)) {
+ if let Some(slot) = keys.get_mut(i) {
+ *slot = key;
+ }
+ }
(shown, keys)
}
diff --git a/crates/ui-egui/src/menu_keys_tests.rs b/crates/ui-egui/src/menu_keys_tests.rs
index 894450bb2..f4a8e4460 100644
--- a/crates/ui-egui/src/menu_keys_tests.rs
+++ b/crates/ui-egui/src/menu_keys_tests.rs
@@ -261,6 +261,10 @@ fn labels_without_latin_letters_show_their_english_key() {
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')]);
+ // A shown "(C)" keeps its key even when a label with letters of its own comes first.
+ let (shown, keys) = crate::menu_keys::localized(&["Copy".to_string(), "关闭".to_string()], &["Open", "Close"], &[]);
+ assert_eq!(shown, ["Copy", "关闭(C)"]);
+ assert_eq!(keys, [Some('O'), Some('C')]);
// 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);