//! The native menu bar on macOS. //! //! Lutra's own menu lives in the window (the frontend's app menu) on every //! platform. macOS still needs a native menu bar: WKWebView gets Cut, Copy, //! Paste, Select All, Undo and Redo through the Edit menu's key equivalents //! (⌘X, ⌘C, ⌘V, ⌘A, ⌘Z, ⇧⌘Z) and does not handle those keys without it, and //! users expect the app menu (About, Hide, Quit). //! //! Tauri's default macOS menu is not used because its key equivalents take //! keys from Lutra's keymap before the page sees them: "Close Window" (⌘W) //! would close the whole window, and so quit, where Lutra closes the editor //! tab (`buffer.close`), and its "Quit" (⌘Q) would end the process without //! Lutra's `app.quit` (closing terminals, shutting the language server down). //! This menu leaves ⌘W to the page and routes Quit through `app.quit`. //! //! The menu is built with Tauri's cross-platform API, so it compiles and is //! type-checked everywhere; [`install`] adds it on macOS only. use std::sync::Arc; use tauri::menu::{Menu, MenuEvent, MenuItem, PredefinedMenuItem, Submenu}; use tauri::{AppHandle, Manager, Runtime}; use crate::host::Host; /// Menu item id of Quit; equal to the engine command it runs. const QUIT_ID: &str = "app.quit"; /// Adds the native menu and its event handler to `builder` on macOS; /// returns `builder` unchanged elsewhere, where the window has no native /// menu bar. pub fn install(builder: tauri::Builder) -> tauri::Builder { if !cfg!(target_os = "macos") { return builder; } builder.menu(build).on_menu_event(handle) } /// Builds the menu bar: the app menu, Edit and Window. /// /// # Errors /// Tauri's error if a menu or item cannot be created. pub fn build(app: &AppHandle) -> tauri::Result> { let quit = MenuItem::with_id(app, QUIT_ID, "Quit Lutra", true, Some("CmdOrCtrl+Q"))?; let app_menu = Submenu::with_items( app, "Lutra", true, &[ &PredefinedMenuItem::about(app, Some("About Lutra"), None)?, &PredefinedMenuItem::separator(app)?, &PredefinedMenuItem::services(app, None)?, &PredefinedMenuItem::separator(app)?, &PredefinedMenuItem::hide(app, None)?, &PredefinedMenuItem::hide_others(app, None)?, &PredefinedMenuItem::show_all(app, None)?, &PredefinedMenuItem::separator(app)?, &quit, ], )?; let edit = Submenu::with_items( app, "Edit", true, &[ &PredefinedMenuItem::undo(app, None)?, &PredefinedMenuItem::redo(app, None)?, &PredefinedMenuItem::separator(app)?, &PredefinedMenuItem::cut(app, None)?, &PredefinedMenuItem::copy(app, None)?, &PredefinedMenuItem::paste(app, None)?, &PredefinedMenuItem::select_all(app, None)?, ], )?; let window = Submenu::with_items( app, "Window", true, &[ &PredefinedMenuItem::minimize(app, None)?, &PredefinedMenuItem::maximize(app, None)?, &PredefinedMenuItem::separator(app)?, &PredefinedMenuItem::fullscreen(app, None)?, ], )?; Menu::with_items(app, &[&app_menu, &edit, &window]) } /// Runs the engine command behind a custom menu item. Errors are ignored on /// purpose: `app.quit` cannot fail, and without a host (before setup) the /// app has nothing to shut down, so it exits directly. fn handle(app: &AppHandle, event: MenuEvent) { if event.id() != QUIT_ID { return; } match app.try_state::>() { Some(host) => { let _ = host.run(QUIT_ID, serde_json::json!({})); } None => app.exit(0), } }