Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 76 additions & 8 deletions crates/nsh-core/src/desktop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,13 +815,38 @@ pub struct MenuLabels {
pub delete: Option<String>,
}

/// NSH's translations of the menus it builds (`desktop/menu.json`).
fn menu_texts() -> &'static crate::i18n::Translations {
static TEXTS: std::sync::OnceLock<crate::i18n::Translations> = std::sync::OnceLock::new();
TEXTS.get_or_init(|| {
crate::i18n::Translations::from_json(include_str!("desktop/menu.json"))
.expect("the built-in menu translations are valid")
})
}

/// Whether `label` titles an Edit menu in one of the languages NSH
/// translates its menus to (`"Edit"`, `"编辑"`, `"&Edit"`), ignoring case
/// and Windows mnemonics. AppKit finds the Edit menu it adds Dictation and
/// Emoji & Symbols to by its title, too.
fn is_edit_title(label: &str) -> bool {
let title = label.replace('&', "");
let title = title.trim();
if title.is_empty() {
return false;
}
let texts = menu_texts();
texts.locales().iter().any(|locale| {
["Edit", "Windows Edit"].iter().any(|key| {
texts.lookup(locale, key).is_some_and(|edit| {
edit.replace('&', "").trim().to_lowercase() == title.to_lowercase()
})
})
})
}

impl MenuLabels {
pub(crate) fn for_locale(locale: crate::platform::Locale, mac: bool) -> Self {
static TEXTS: std::sync::OnceLock<crate::i18n::Translations> = std::sync::OnceLock::new();
let texts = TEXTS.get_or_init(|| {
crate::i18n::Translations::from_json(include_str!("desktop/menu.json"))
.expect("the built-in menu translations are valid")
});
let texts = menu_texts();
// Region-only Chinese tags select the traditional script there.
let language = match locale.as_str() {
"zh-TW" | "zh-HK" | "zh-MO" => "zh-Hant",
Expand Down Expand Up @@ -1184,8 +1209,13 @@ impl MenuItem {
/// menu has it somewhere.
/// - Everywhere: an Edit menu (Undo, Redo, Cut, Copy, Paste, Select
/// All) follows the app's first menu (its File menu) unless the menu
/// holds an editing role, so text fields answer the standard keys
/// and the menu bar lists them.
/// holds an editing role or a top-level menu titled Edit (in any
/// language NSH translates its menus to), so text fields answer the
/// standard keys and the menu bar lists them. An app's own Edit menu
/// without roles (an editor with its own undo and clipboard) replaces
/// the standard one, as an app's menu does in AppKit and Electron: its
/// items take their shortcuts; give them the editing roles to leave the
/// keys to a focused text field.
///
/// An empty menu stays empty. The added items are roles (id 0): the
/// platform labels them and carries them out. The added Edit and Window
Expand Down Expand Up @@ -1307,7 +1337,10 @@ pub fn menu_bar(items: &[MenuItem], mac: bool) -> Vec<MenuItem> {
}
items.push(role(R::Quit));
}
if !has(&out, EDIT) {
let titled_edit = out
.iter()
.any(|item| matches!(item, MenuItem::Submenu { label, inline: false, .. } if is_edit_title(label)));
if !has(&out, EDIT) && !titled_edit {
let at = if out.len() > first {
first + 1
} else {
Expand Down Expand Up @@ -1662,6 +1695,41 @@ mod tests {
assert!(super::menu_bar(&[], false).is_empty());
}

#[test]
fn an_app_edit_menu_without_roles_replaces_the_standard_one() {
use super::{MenuItem, MenuLabels};
// An editor's own Edit menu: its own undo and copy, no roles.
let with_edit = |title: &str| {
let mut menu = file_menu();
let mut edit = menu[0].clone();
if let MenuItem::Submenu { label, items, .. } = &mut edit {
*label = title.into();
items.truncate(1);
}
menu.push(edit);
menu
};
for mac in [true, false] {
for title in ["Edit", "&Edit", "编辑", "Bearbeiten"] {
let out = outline(&titled(
super::menu_bar(&with_edit(title), mac),
&MenuLabels::default(),
));
let edits = out
.iter()
.filter(|(t, _)| t == "Edit" || t == title)
.count();
assert_eq!(edits, 1, "{title} on mac: {mac}");
}
// Another title is not an Edit menu.
let out = outline(&titled(
super::menu_bar(&with_edit("View"), mac),
&MenuLabels::default(),
));
assert!(out.iter().any(|(t, _)| t == "Edit"), "mac: {mac}");
}
}

#[test]
fn the_application_block_reads_the_quit_mode() {
let read = |json: &str| {
Expand Down
Loading