From d376ca2aaad10087f199faa11c4fae965f4533aa Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 10:33:18 -0800 Subject: [PATCH 1/2] craft-menu: the minimal macOS app menu takes the app's translations mac::install_app_menu wrote About, Hide and Quit in English with format!() and left Services, Hide Others and Show All to muda's English defaults, so a localized app that installs it to make Cmd+Q ask first got an English app menu in every language. mac::install_app_menu_with takes a LayoutOptions and names the six items through LayoutOptions::label with the keys mac_layout already uses for the same items (About {app}, Services, Hide {app}, Hide Others, Show All, Quit {app}). install_app_menu calls it with English labels, so its menu is unchanged. --- crates/menu/README.md | 9 +++++++++ crates/menu/src/layout.rs | 27 ++++++++++++++++++++++++++ crates/menu/src/lib.rs | 3 ++- crates/menu/src/mac.rs | 29 ++++++++++++++++++++-------- crates/menu/src/tests.rs | 40 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 99 insertions(+), 9 deletions(-) diff --git a/crates/menu/README.md b/crates/menu/README.md index 063c2d0e40..7d64e35910 100644 --- a/crates/menu/README.md +++ b/crates/menu/README.md @@ -175,6 +175,15 @@ Hide Others, Show All and Quit, where Quit asks the app first. craft_menu::mac::install_app_menu(&cc.egui_ctx, "WordCraft")?; ``` +Its labels are English. `mac::install_app_menu_with` takes a `LayoutOptions` instead, and its +`label` translates `About {app}`, `Services`, `Hide {app}`, `Hide Others`, `Show All` and +`Quit {app}`, the keys `mac_layout` uses for the same items. + +```rust +#[cfg(target_os = "macos")] +craft_menu::mac::install_app_menu_with(&cc.egui_ctx, &LayoutOptions { label: &translate, ..LayoutOptions::new("WordCraft") })?; +``` + craft-menu owns muda's menu event handler. muda keeps the first one set per process, so the crate sets it once and both `mac::install` and `mac::install_app_menu` use it; a later `mac::install` replaces the minimal app menu, and a second `mac::install` returns `None`. diff --git a/crates/menu/src/layout.rs b/crates/menu/src/layout.rs index 51f45959e9..41a5a5b909 100644 --- a/crates/menu/src/layout.rs +++ b/crates/menu/src/layout.rs @@ -93,6 +93,33 @@ fn english(s: &'static str) -> String { s.into() } +/// The labels of the minimal macOS app menu (`mac::install_app_menu_with`), translated through +/// the keys [`mac_layout`] uses for its own app menu. The menu's title is the app's name. +#[derive(Clone, Debug, PartialEq, Eq)] +#[cfg_attr(not(any(test, target_os = "macos")), allow(dead_code))] +pub(crate) struct AppMenuLabels { + pub(crate) title: String, + pub(crate) about: String, + pub(crate) services: String, + pub(crate) hide: String, + pub(crate) hide_others: String, + pub(crate) show_all: String, + pub(crate) quit: String, +} + +#[cfg_attr(not(any(test, target_os = "macos")), allow(dead_code))] +pub(crate) fn app_menu_labels(options: &LayoutOptions) -> AppMenuLabels { + AppMenuLabels { + title: options.app_name.to_string(), + about: options.text("About {app}"), + services: options.text(Standard::Services.english()), + hide: options.text("Hide {app}"), + hide_others: options.text("Hide Others"), + show_all: options.text(Standard::ShowAll.english()), + quit: options.text("Quit {app}"), + } +} + /// A system or browser shortcut the app also uses for one of its commands. #[derive(Clone, Debug, PartialEq)] pub struct Clash { diff --git a/crates/menu/src/lib.rs b/crates/menu/src/lib.rs index 6e46250c16..897d5da5ad 100644 --- a/crates/menu/src/lib.rs +++ b/crates/menu/src/lib.rs @@ -45,7 +45,8 @@ //! ([`LayoutOptions::extra_shortcuts`]). //! //! The labels the layout writes itself are English unless the app passes its translations in -//! [`LayoutOptions::label`]. +//! [`LayoutOptions::label`]. `mac::install_app_menu_with` takes the same translations for the +//! minimal app menu. //! //! **Keys stay with egui.** AppKit runs a menu's key equivalents before the window sees the key, //! which would skip the app's own key rules (a focused text field keeps ⌘C, ⌘V and ⌘Z, dialogs diff --git a/crates/menu/src/mac.rs b/crates/menu/src/mac.rs index e5bdd306d9..3dfc06a921 100644 --- a/crates/menu/src/mac.rs +++ b/crates/menu/src/mac.rs @@ -45,7 +45,9 @@ use objc2_app_kit::{NSApplication, NSEventModifierFlags, NSEventType}; use craft_keymap::{Chord, CommandRef, Key, Mods}; +use crate::LayoutOptions; use crate::driver::{Backend, Dispatch, Event, NativeMenu, native_ok}; +use crate::layout::app_menu_labels; use crate::model::{Builtin, Item, ItemKind, Menu, MenuBar, MenuRole, Node, Standard, native_id, structure_key, window_key}; /// Set by [`install`]: there is one full menu bar per process. @@ -117,7 +119,8 @@ pub fn install_with(ctx: &egui::Context, bar: &MenuBar, options: MacOptions) -> /// Replace winit's default macOS app menu with a minimal one whose Quit (⌘Q) asks the app first, /// for an app without a [`MenuBar`] of its own: About, Services, Hide, Hide Others, Show All and -/// Quit, labelled in English with `app_name` ("Quit WordCraft"). +/// Quit, labeled in English with `app_name` ("Quit WordCraft"). [`install_app_menu_with`] takes +/// the app's translations. /// /// winit's Quit ends the process without asking, so unsaved work is lost. This Quit has the id /// [`craft_quit::QUIT_MENU_ID`] and runs [`craft_quit::on_menu_event`]: it asks the root window to @@ -128,22 +131,32 @@ pub fn install_with(ctx: &egui::Context, bar: &MenuBar, options: MacOptions) -> /// Does nothing when this menu or a full menu bar ([`install`]) is already installed. On error /// winit's menu stays: the app still runs, but ⌘Q quits without asking. pub fn install_app_menu(ctx: &egui::Context, app_name: &str) -> Result<(), Box> { + install_app_menu_with(ctx, &LayoutOptions::new(app_name)) +} + +/// [`install_app_menu`] with the app's translations. [`LayoutOptions::label`] names the items +/// through the keys [`crate::mac_layout`] uses for its app menu: `About {app}`, `Services`, +/// `Hide {app}`, `Hide Others`, `Show All` and `Quit {app}`, with `{app}` replaced by +/// [`LayoutOptions::app_name`], which also titles the menu. [`LayoutOptions::extra_shortcuts`] +/// is not used. +pub fn install_app_menu_with(ctx: &egui::Context, options: &LayoutOptions) -> Result<(), Box> { if MainThreadMarker::new().is_none() { return Err("craft_menu::mac::install_app_menu runs on the main thread".into()); } if INSTALLED.load(Ordering::SeqCst) || APP_MENU.with_borrow(Option::is_some) { return Ok(()); } - let quit = MenuItem::with_id(craft_quit::QUIT_MENU_ID, format!("Quit {app_name}"), true, Some(Accelerator::new(Modifiers::META, Code::KeyQ))); - let app_menu = Submenu::new(app_name, true); + let labels = app_menu_labels(options); + let quit = MenuItem::with_id(craft_quit::QUIT_MENU_ID, labels.quit, true, Some(Accelerator::new(Modifiers::META, Code::KeyQ))); + let app_menu = Submenu::new(labels.title, true); app_menu.append_items(&[ - &PredefinedMenuItem::about(Some(&format!("About {app_name}")), None), + &PredefinedMenuItem::about(Some(&labels.about), None), &PredefinedMenuItem::separator(), - &PredefinedMenuItem::services(None), + &PredefinedMenuItem::services(Some(&labels.services)), &PredefinedMenuItem::separator(), - &PredefinedMenuItem::hide(Some(&format!("Hide {app_name}"))), - &PredefinedMenuItem::hide_others(None), - &PredefinedMenuItem::show_all(None), + &PredefinedMenuItem::hide(Some(&labels.hide)), + &PredefinedMenuItem::hide_others(Some(&labels.hide_others)), + &PredefinedMenuItem::show_all(Some(&labels.show_all)), &PredefinedMenuItem::separator(), &quit, ])?; diff --git a/crates/menu/src/tests.rs b/crates/menu/src/tests.rs index d0854f7391..2c78e8f604 100644 --- a/crates/menu/src/tests.rs +++ b/crates/menu/src/tests.rs @@ -162,6 +162,46 @@ fn an_untagged_quit_is_craft_quits_and_labels_are_translated() { assert_eq!(ids(&l.bar.menu(MenuRole::Window).unwrap().children), ["", "", "---", "", "---", "doc.0"]); } +/// The minimal app menu is English by default and takes the app's translations through the keys +/// the Mac layout uses for the same items. +#[test] +fn the_minimal_app_menu_labels_are_translated() { + use crate::layout::{AppMenuLabels, app_menu_labels}; + + let english = app_menu_labels(&LayoutOptions::new("Sample")); + let expected = AppMenuLabels { + title: "Sample".into(), + about: "About Sample".into(), + services: "Services".into(), + hide: "Hide Sample".into(), + hide_others: "Hide Others".into(), + show_all: "Show All".into(), + quit: "Quit Sample".into(), + }; + assert_eq!(english, expected); + + let german = |s: &'static str| match s { + "About {app}" => "Über {app}".to_string(), + "Services" => "Dienste".into(), + "Hide {app}" => "{app} ausblenden".into(), + "Hide Others" => "Andere ausblenden".into(), + "Show All" => "Alle einblenden".into(), + "Quit {app}" => "{app} beenden".into(), + other => panic!("no app menu label {other:?}"), + }; + let labels = app_menu_labels(&LayoutOptions { label: &german, ..LayoutOptions::new("Sample") }); + let expected = AppMenuLabels { + title: "Sample".into(), + about: "Über Sample".into(), + services: "Dienste".into(), + hide: "Sample ausblenden".into(), + hide_others: "Andere ausblenden".into(), + show_all: "Alle einblenden".into(), + quit: "Sample beenden".into(), + }; + assert_eq!(labels, expected); +} + /// App commands keep ⌘H, ⌘M, ⌘W and ⌘,: the system items give way, and each clash is reported. #[test] fn clashes_are_resolved_in_the_apps_favour() { From f6e7f8d651a9b8611c743dd2d89eaa4e79134e3f Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 10:54:38 -0800 Subject: [PATCH 2/2] craft-menu: ampersands in macOS menu labels stay visible muda on macOS drops a single `&` from an item's text as a mnemonic marker. The minimal app menu passed the app name and the translated labels to muda unescaped, so "Fish & Chips" showed as "Fish Chips". The labels are now escaped before the native items are built, and the Mac layout's standard items (Services, Show All, Minimize, Zoom, Bring All to Front, Close Window) are escaped as its other items already were. --- crates/menu/src/layout.rs | 18 ++++++++++++++++++ crates/menu/src/mac.rs | 3 ++- crates/menu/src/tests.rs | 22 ++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/crates/menu/src/layout.rs b/crates/menu/src/layout.rs index 41a5a5b909..77dda52336 100644 --- a/crates/menu/src/layout.rs +++ b/crates/menu/src/layout.rs @@ -120,6 +120,24 @@ pub(crate) fn app_menu_labels(options: &LayoutOptions) -> AppMenuLabels { } } +impl AppMenuLabels { + /// The labels as muda reads them: muda drops a single `&` as a mnemonic marker and shows `&&` + /// as `&`, so each `&` is doubled. + #[cfg_attr(not(any(test, target_os = "macos")), allow(dead_code))] + pub(crate) fn escaped(&self) -> AppMenuLabels { + let e = |s: &str| s.replace('&', "&&"); + AppMenuLabels { + title: e(&self.title), + about: e(&self.about), + services: e(&self.services), + hide: e(&self.hide), + hide_others: e(&self.hide_others), + show_all: e(&self.show_all), + quit: e(&self.quit), + } + } +} + /// A system or browser shortcut the app also uses for one of its commands. #[derive(Clone, Debug, PartialEq)] pub struct Clash { diff --git a/crates/menu/src/mac.rs b/crates/menu/src/mac.rs index 3dfc06a921..7a3f735169 100644 --- a/crates/menu/src/mac.rs +++ b/crates/menu/src/mac.rs @@ -146,7 +146,7 @@ pub fn install_app_menu_with(ctx: &egui::Context, options: &LayoutOptions) -> Re if INSTALLED.load(Ordering::SeqCst) || APP_MENU.with_borrow(Option::is_some) { return Ok(()); } - let labels = app_menu_labels(options); + let labels = app_menu_labels(options).escaped(); let quit = MenuItem::with_id(craft_quit::QUIT_MENU_ID, labels.quit, true, Some(Accelerator::new(Modifiers::META, Code::KeyQ))); let app_menu = Submenu::new(labels.title, true); app_menu.append_items(&[ @@ -464,6 +464,7 @@ fn append(entries: &mut Entries, sub: &Submenu, nodes: &[Node]) -> muda::Result< sub.append(&s)?; } Node::Standard { kind, label } => { + let label = escape(label); let text = Some(label.as_str()); let p = match kind { Standard::Services => PredefinedMenuItem::services(text), diff --git a/crates/menu/src/tests.rs b/crates/menu/src/tests.rs index 2c78e8f604..85b43bba92 100644 --- a/crates/menu/src/tests.rs +++ b/crates/menu/src/tests.rs @@ -202,6 +202,28 @@ fn the_minimal_app_menu_labels_are_translated() { assert_eq!(labels, expected); } +/// An `&` in the app's name or in a translation reaches muda doubled, so the menu shows it. +#[test] +fn the_minimal_app_menu_keeps_ampersands() { + use crate::layout::{AppMenuLabels, app_menu_labels}; + + let translated = |s: &'static str| match s { + "Services" => "Dienste & Co".to_string(), + other => other.into(), + }; + let labels = app_menu_labels(&LayoutOptions { label: &translated, ..LayoutOptions::new("Fish & Chips") }).escaped(); + let expected = AppMenuLabels { + title: "Fish && Chips".into(), + about: "About Fish && Chips".into(), + services: "Dienste && Co".into(), + hide: "Hide Fish && Chips".into(), + hide_others: "Hide Others".into(), + show_all: "Show All".into(), + quit: "Quit Fish && Chips".into(), + }; + assert_eq!(labels, expected); +} + /// App commands keep ⌘H, ⌘M, ⌘W and ⌘,: the system items give way, and each clash is reported. #[test] fn clashes_are_resolved_in_the_apps_favour() {