Lutra/apps/lutra-desktop/src/menu.rs
danny 1bf69ded55 A1: macOS readiness: setup guide, cargo xtask mac-report, unix/macOS fixes
- docs/macos.md: step-by-step setup for a fresh Apple Silicon Mac (Xcode
  CLT, Homebrew, git, PHP, Composer, Node 22, pnpm, rustup, clone from
  Forgejo), the machine report, Gatekeeper notes, macOS differences and
  what Windows cannot check.
- cargo xtask mac-report: versions, the gate steps, the bundled-UI desktop
  build and a self-check over the control channel (project, file,
  Phpactor, hover, git.status, terminal, screenshot, quit); every step
  times out and runs in a lutra_proc group; one report.md to send back.
- Fixes: macOS native menu (system menus only; Cmd+W stays buffer.close,
  Cmd+Q runs app.quit), xcap screenshot region in points on macOS
  (Retina), login shell (-l, /bin/zsh fallback) and LANG for the
  terminal on macOS, PHP lookup in Homebrew/MacPorts dirs for Finder
  starts, case-insensitive LSP path mapping on macOS, rendezvous file
  mode re-set to 0600, CLI-started desktop in its own process group.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 14:47:45 +01:00

102 lines
3.8 KiB
Rust

//! 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<R: Runtime>(builder: tauri::Builder<R>) -> tauri::Builder<R> {
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<R: Runtime>(app: &AppHandle<R>) -> tauri::Result<Menu<R>> {
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<R: Runtime>(app: &AppHandle<R>, event: MenuEvent) {
if event.id() != QUIT_ID {
return;
}
match app.try_state::<Arc<Host>>() {
Some(host) => {
let _ = host.run(QUIT_ID, serde_json::json!({}));
}
None => app.exit(0),
}
}