diff --git a/crates/menu/README.md b/crates/menu/README.md index 69947e7bae..c834b7b72b 100644 --- a/crates/menu/README.md +++ b/crates/menu/README.md @@ -208,6 +208,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::new("WordCraft").label(&translate))?; +``` + craft-menu owns the process's menu event handler (muda keeps the first one set, in craft-window), 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 1bc7f4112f..2719bae213 100644 --- a/crates/menu/src/layout.rs +++ b/crates/menu/src/layout.rs @@ -99,7 +99,7 @@ impl<'a> LayoutOptions<'a> { self } - fn text(&self, english: &'static str) -> String { + pub(crate) fn text(&self, english: &'static str) -> String { (self.label)(english).replace("{app}", self.app_name) } diff --git a/crates/menu/src/lib.rs b/crates/menu/src/lib.rs index 4e66e3310d..5c12bdf08c 100644 --- a/crates/menu/src/lib.rs +++ b/crates/menu/src/lib.rs @@ -64,7 +64,8 @@ //! [`LanguageMenu`] does for languages. //! //! 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 ae30fb801d..321a49139e 100644 --- a/crates/menu/src/mac.rs +++ b/crates/menu/src/mac.rs @@ -39,6 +39,7 @@ use craft_ui::menu::Alternate; use craft_ui::runtime::{frame, motion}; use craft_window::native_menu as native; +use crate::LayoutOptions; use crate::driver::{Attempt, Backend, Dispatch, Event, NativeMenu, RebuildRetry, native_ok}; use crate::model::{Builtin, Item, ItemKind, Menu, MenuBar, MenuRole, Node, Standard, label_or_system, native_id, structure_key, window_key}; @@ -95,7 +96,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 @@ -106,13 +108,36 @@ pub fn install_with(ctx: &egui::Context, bar: &MenuBar, options: MacOptions) -> /// platforms without an application menu bar. 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: the titles [`app_menu`] gives. +pub fn install_app_menu_with(ctx: &egui::Context, options: &LayoutOptions) -> Result<(), Box> { let ctx = ctx.clone(); let handler = Arc::new(move |id: &str, _: native::Chosen| { craft_quit::on_menu_event(&ctx, id); // Choosing an item is not window input, so nothing else wakes egui. ctx.request_repaint(); }); - native::install_app_menu(app_name, craft_quit::QUIT_MENU_ID, handler) + native::install_app_menu_with(&app_menu(options), craft_quit::QUIT_MENU_ID, handler) +} + +/// The titles of the minimal app menu ([`install_app_menu_with`]). [`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. Services, Hide Others and Show All take +/// the system's localized titles when the app supplies no translation, as in [`crate::mac_layout`]. +/// [`LayoutOptions::keys`] is not used. +pub fn app_menu(options: &LayoutOptions) -> native::AppMenu { + let mut menu = native::AppMenu::english(options.app_name); + let system = |english: &'static str| label_or_system(&options.text(english), english, native::system_title); + menu.about = options.text("About {app}"); + menu.services = system(Standard::Services.english()); + menu.hide = options.text("Hide {app}"); + menu.hide_others = system("Hide Others"); + menu.show_all = system(Standard::ShowAll.english()); + menu.quit = options.text("Quit {app}"); + menu } /// What each native item runs, by native id, for the handler. diff --git a/crates/menu/tests/it/layout.rs b/crates/menu/tests/it/layout.rs index 7d34578b53..1462fdb23c 100644 --- a/crates/menu/tests/it/layout.rs +++ b/crates/menu/tests/it/layout.rs @@ -1,6 +1,6 @@ //! The Mac layout, its shortcut clashes, and the keys that decide rebuild against update in place. -use craft_menu::{Builtin, Document, Item, ItemKind, LayoutOptions, MenuBar, MenuRole, Node, mac_layout, structure_key, window_key}; +use craft_menu::{Builtin, Document, Item, ItemKind, LayoutOptions, MenuBar, MenuRole, Node, mac, mac_layout, structure_key, window_key}; use craft_ui::testing::trial; use crate::sample::{chord, cmd, ids, sample, titled}; @@ -41,6 +41,30 @@ fn the_app_menu_is_laid_out_the_mac_way() { ); } +/// The minimal app menu names the app by default and takes the app's translations through the keys +/// the Mac layout uses for the same items. +#[trial] +fn the_minimal_app_menu_takes_the_apps_translations() { + let english = mac::app_menu(&LayoutOptions::new("Sample")); + assert_eq!(english.title, "Sample"); + for label in [&english.about, &english.hide, &english.quit] { + assert!(label.contains("Sample"), "{label:?} names the app"); + } + + 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 m = mac::app_menu(&LayoutOptions::new("Sample").label(&german)); + let got: [&str; 7] = [&m.title, &m.about, &m.services, &m.hide, &m.hide_others, &m.show_all, &m.quit]; + assert_eq!(got, ["Sample", "Über Sample", "Dienste", "Sample ausblenden", "Andere ausblenden", "Alle einblenden", "Sample beenden"]); +} + /// App commands keep ⌘H, ⌘M, ⌘W and ⌘,: the system items give way, and each clash is reported. #[trial] fn clashes_are_resolved_in_the_apps_favour() { diff --git a/crates/window/README.md b/crates/window/README.md index 1434712d22..fc43ec6224 100644 --- a/crates/window/README.md +++ b/crates/window/README.md @@ -146,7 +146,8 @@ application's menu bar, `NativeBar::apply` keeps it in step (a changed `structur `windows` key rebuilds only the Windows menu, anything else updates in place; `NativeBar::failed` reports a rebuild that failed and left the menus as they were), and the `Handler` set with `NativeBar::set_handler` receives each chosen item's `id`, the modifiers held and whether a key press chose it. -`install_app_menu` installs a minimal app menu whose Quit reports to a handler. Off macOS (and without the +`install_app_menu` installs a minimal app menu whose Quit reports to a handler, with English titles; +`install_app_menu_with` takes the titles as an `AppMenu`. Off macOS (and without the feature) every call does nothing, so callers need no platform check. craft-menu lowers its menu model to this description. diff --git a/crates/window/src/native_menu.rs b/crates/window/src/native_menu.rs index 0c8fffbcbe..6d607548cc 100644 --- a/crates/window/src/native_menu.rs +++ b/crates/window/src/native_menu.rs @@ -33,7 +33,7 @@ mod other; #[cfg(not(all(target_os = "macos", feature = "appkit")))] use other as imp; -pub use imp::{hide_app, hide_other_apps, install_app_menu, system_title}; +pub use imp::{hide_app, hide_other_apps, install_app_menu, install_app_menu_with, system_title}; /// Called when an item is chosen: the item's `id` and how it was chosen ([`Chosen`]: key press or /// click, and the modifiers held either way). The platform calls it between frames from the event @@ -166,7 +166,7 @@ pub enum Node { Platform { /// Which one. kind: Platform, - /// The title. + /// The title. `&` is literal. title: String, }, /// An item the app handles. @@ -254,6 +254,42 @@ impl Options { } } +/// The titles of the minimal application menu ([`install_app_menu_with`]). `&` is literal. +#[derive(Clone, Debug, PartialEq, Eq)] +#[non_exhaustive] +pub struct AppMenu { + /// The menu's title. + pub title: String, + /// About. + pub about: String, + /// The Services submenu. + pub services: String, + /// Hide. + pub hide: String, + /// Hide Others. + pub hide_others: String, + /// Show All. + pub show_all: String, + /// Quit. + pub quit: String, +} + +impl AppMenu { + /// English titles with `app_name`: the menu is titled `app_name`, and About, Hide and Quit + /// name it ("Quit WordCraft"). + pub fn english(app_name: &str) -> AppMenu { + AppMenu { + title: app_name.to_owned(), + about: format!("About {app_name}"), + services: "Services".to_owned(), + hide: format!("Hide {app_name}"), + hide_others: "Hide Others".to_owned(), + show_all: "Show All".to_owned(), + quit: format!("Quit {app_name}"), + } + } +} + /// Whether a menu is open. Safe from any thread; false where there is no application menu bar. pub fn menu_tracking() -> bool { imp::menu_tracking() diff --git a/crates/window/src/native_menu/mac.rs b/crates/window/src/native_menu/mac.rs index 5d874a02c0..98fe2bdd18 100644 --- a/crates/window/src/native_menu/mac.rs +++ b/crates/window/src/native_menu/mac.rs @@ -30,7 +30,7 @@ use objc2::{MainThreadMarker, sel}; use objc2_app_kit::{NSApplication, NSEventModifierFlags, NSEventType}; use objc2_foundation::{NSBundle, NSLocale, NSObjectProtocol, NSString}; -use super::{Bar, Chosen, Handler, Held, Item, Menu, MenuKind, Node, Options, Platform, Shortcut, Wake}; +use super::{AppMenu, Bar, Chosen, Handler, Held, Item, Menu, MenuKind, Node, Options, Platform, Shortcut, Wake}; /// Set by [`Installed::install`]: there is one full menu bar per process. static INSTALLED: AtomicBool = AtomicBool::new(false); @@ -118,22 +118,27 @@ fn lock(m: &Mutex) -> MutexGuard<'_, T> { /// /// Does nothing when this menu or a full menu bar ([`Installed::install`]) is already installed. pub fn install_app_menu(app_name: &str, quit_id: &str, handler: Handler) -> Result<(), Box> { + install_app_menu_with(&AppMenu::english(app_name), quit_id, handler) +} + +/// [`install_app_menu`] with the titles `menu`, for an app that translates them. +pub fn install_app_menu_with(menu: &AppMenu, quit_id: &str, handler: Handler) -> Result<(), Box> { if MainThreadMarker::new().is_none() { return Err("the application menu is installed on the main thread".into()); } if INSTALLED.load(Ordering::SeqCst) || APP_MENU.with_borrow(Option::is_some) { return Ok(()); } - let quit = MenuItem::with_id(quit_id, format!("Quit {app_name}"), true, Some(Accelerator::new(Modifiers::META, Code::KeyQ))); - let app_menu = Submenu::new(app_name, true); + let quit = MenuItem::with_id(quit_id, escape(&menu.quit), true, Some(Accelerator::new(Modifiers::META, Code::KeyQ))); + let app_menu = Submenu::new(escape(&menu.title), true); app_menu.append_items(&[ - &PredefinedMenuItem::about(Some(&format!("About {app_name}")), None), + &PredefinedMenuItem::about(Some(&escape(&menu.about)), None), &PredefinedMenuItem::separator(), - &PredefinedMenuItem::services(None), + &PredefinedMenuItem::services(Some(&escape(&menu.services))), &PredefinedMenuItem::separator(), - &PredefinedMenuItem::hide(Some(&format!("Hide {app_name}"))), - &PredefinedMenuItem::hide_others(None), - &PredefinedMenuItem::show_all(None), + &PredefinedMenuItem::hide(Some(&escape(&menu.hide))), + &PredefinedMenuItem::hide_others(Some(&escape(&menu.hide_others))), + &PredefinedMenuItem::show_all(Some(&escape(&menu.show_all))), &PredefinedMenuItem::separator(), &quit, ])?; @@ -407,6 +412,7 @@ fn append(entries: &mut Entries, sub: &Submenu, nodes: &[Node]) -> muda::Result< sub.append(&s)?; } Node::Platform { kind, title } => { + let title = escape(title); let text = Some(title.as_str()); let p = match kind { Platform::Services => PredefinedMenuItem::services(text), diff --git a/crates/window/src/native_menu/other.rs b/crates/window/src/native_menu/other.rs index 24251b9b86..d2cd8320bb 100644 --- a/crates/window/src/native_menu/other.rs +++ b/crates/window/src/native_menu/other.rs @@ -2,7 +2,7 @@ use std::convert::Infallible; -use super::{Bar, Handler, Options, Wake}; +use super::{AppMenu, Bar, Handler, Options, Wake}; /// No menu bar here, so no menu is ever open. pub(super) fn menu_tracking() -> bool { @@ -52,6 +52,11 @@ pub fn install_app_menu(_app_name: &str, _quit_id: &str, _handler: Handler) -> R Ok(()) } +/// [`install_app_menu`] with the titles `menu`. Does nothing here. +pub fn install_app_menu_with(_menu: &AppMenu, _quit_id: &str, _handler: Handler) -> Result<(), Box> { + Ok(()) +} + /// The platform's own title for the English menu title `english`; `english` here. pub fn system_title(english: &str) -> String { english.to_owned()