Skip to content

DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.

fix(menu): dispatch native commands to a single shell window (#876)

云游君6a9ac4f60d1b
diff --git a/src-tauri/src/desktop/builder.rs b/src-tauri/src/desktop/builder.rs
index 96116788..9e512b64 100644
--- a/src-tauri/src/desktop/builder.rs
+++ b/src-tauri/src/desktop/builder.rs
@@ -1100,7 +1100,67 @@ mod shell_nav_tests {
#[cfg(test)]
mod menu_tests {
- use super::fullscreen_menu_label_key;
+ use super::{fullscreen_menu_label_key, menu_action_window_label};
+ use crate::desktop::pet::PET_WINDOW_LABEL;
+
+ #[test]
+ fn menu_actions_prefer_the_focused_shell_window_in_any_iteration_order() {
+ let windows = [
+ ("main", false),
+ ("window-1", true),
+ (PET_WINDOW_LABEL, false),
+ ];
+ assert_eq!(menu_action_window_label(windows), Some("window-1"));
+ assert_eq!(
+ menu_action_window_label(windows.into_iter().rev()),
+ Some("window-1")
+ );
+ }
+
+ #[test]
+ fn menu_actions_target_main_when_it_is_focused() {
+ assert_eq!(
+ menu_action_window_label([("main", true), ("window-1", false)]),
+ Some("main")
+ );
+ }
+
+ #[test]
+ fn menu_actions_fall_back_to_main_when_only_the_pet_is_focused() {
+ assert_eq!(
+ menu_action_window_label([
+ (PET_WINDOW_LABEL, true),
+ ("window-1", false),
+ ("main", false)
+ ]),
+ Some("main")
+ );
+ }
+
+ #[test]
+ fn menu_actions_fall_back_to_main_when_all_windows_are_unfocused() {
+ assert_eq!(
+ menu_action_window_label([("window-1", false), ("main", false)]),
+ Some("main")
+ );
+ }
+
+ #[test]
+ fn menu_actions_target_a_focused_secondary_window_without_main() {
+ assert_eq!(
+ menu_action_window_label([("window-1", true)]),
+ Some("window-1")
+ );
+ }
+
+ #[test]
+ fn menu_actions_have_no_target_without_a_focused_shell_or_main() {
+ assert_eq!(
+ menu_action_window_label([(PET_WINDOW_LABEL, true), ("window-1", false)]),
+ None
+ );
+ assert_eq!(menu_action_window_label([]), None);
+ }
#[test]
fn fullscreen_menu_label_tracks_native_fullscreen_state() {
@@ -1343,7 +1403,24 @@ pub fn handler() -> impl Fn(Invoke<Wry>) -> bool + Send + Sync + 'static {
]
}
-// configure tauri builder
+fn menu_action_window_label<'a>(
+ windows: impl IntoIterator<Item = (&'a str, bool)>,
+) -> Option<&'a str> {
+ let mut main = None;
+ for (label, focused) in windows {
+ if label == crate::desktop::pet::PET_WINDOW_LABEL {
+ continue;
+ }
+ if focused {
+ return Some(label);
+ }
+ if label == MAIN_WINDOW_LABEL {
+ main = Some(label);
+ }
+ }
+ main
+}
+
pub fn builder() -> tauri::Builder<tauri::Wry> {
let mut builder = tauri::Builder::default();
@@ -1426,8 +1503,19 @@ pub fn builder() -> tauri::Builder<tauri::Wry> {
| "desktop-new-window"
| "desktop-new-chat"
| "desktop-open-folder" => {
- if let Err(error) = app.emit("macos-menu-action", event.id().as_ref()) {
- log::warn!("[menu] failed to emit macOS menu action: {error}");
+ let windows = app.webview_windows();
+ let target = menu_action_window_label(
+ windows
+ .values()
+ .map(|window| (window.label(), window.is_focused().unwrap_or(false))),
+ );
+ if let Some(label) = target {
+ if let Err(error) = app.emit_to(label, "macos-menu-action", event.id().as_ref())
+ {
+ log::warn!("[menu] failed to emit macOS menu action: {error}");
+ }
+ } else {
+ log::warn!("[menu] MENU_WINDOW_NOT_FOUND: {}", event.id().as_ref());
}
}
_ => {}
diff --git a/test/macos-run-menu.test.ts b/test/macos-run-menu.test.ts
index cb5f7afb..7e13c449 100644
--- a/test/macos-run-menu.test.ts
+++ b/test/macos-run-menu.test.ts
@@ -82,7 +82,7 @@ describe('macOS native menu', () => {
])('routes %s from its native item to the %s configuration tab', (action, tab, key, zh, en) => {
expect(builderSource).toMatch(new RegExp(`MenuItem::with_id\\(\\s*app,\\s*"${action}",\\s*crate::config::i18n::t\\("${key}"\\)`))
const eventHandler = builderSource.slice(builderSource.indexOf('.on_menu_event(|app, event|'))
- expect(eventHandler).toMatch(new RegExp(`"${action}"[\\s\\S]*?app.emit\\("macos-menu-action", event.id\\(\\).as_ref\\(\\)\\)`))
+ expect(eventHandler).toMatch(new RegExp(`"${action}"[\\s\\S]*?app.emit_to\\(label, "macos-menu-action", event.id\\(\\).as_ref\\(\\)\\)`))
expect(navbarSource).toMatch(new RegExp(`case '${action}':\\s*handleOpenConfig\\('${tab}'\\)`))
expect(i18nSource).toContain(`"${key}" => ("${zh}", "${en}")`)
})
@@ -136,6 +136,31 @@ describe('macOS native menu', () => {
expect(handler).toMatch(/app\.emit_to\(\s*window.label\(\),\s*"macos-menu-action",\s*event.id\(\).as_ref\(\),?\s*\)/)
})
+ it('routes shell commands to one window instead of broadcasting them', () => {
+ const handler = builderSource.slice(builderSource.indexOf('.on_menu_event(|app, event|'))
+ const shellActions = handler.slice(handler.indexOf('"desktop-config"'), handler.indexOf('_ => {}'))
+
+ for (const action of [
+ 'desktop-config',
+ 'desktop-profiles',
+ 'desktop-plugins',
+ 'desktop-harness',
+ 'desktop-about',
+ 'desktop-copy-run-logs',
+ 'desktop-check-update',
+ 'desktop-restart',
+ 'desktop-keyboard-shortcuts',
+ 'desktop-new-window',
+ 'desktop-new-chat',
+ 'desktop-open-folder',
+ ]) {
+ expect(shellActions).toContain(`"${action}"`)
+ }
+ expect(shellActions).toContain('menu_action_window_label(')
+ expect(shellActions).toContain('app.emit_to(label, "macos-menu-action", event.id().as_ref())')
+ expect(handler).not.toContain('app.emit("macos-menu-action"')
+ })
+
it('requests current window state only after the rebuilt native menu is installed', () => {
const install = builderSource.slice(builderSource.indexOf('pub fn install_macos_menu('), builderSource.indexOf('fn fullscreen_menu_label_key'))
expect(install).toMatch(/app\.set_menu\(menu\)\?[\s\S]*window_menu\.set_as_windows_menu_for_nsapp\(\)\?[\s\S]*app\.emit\("macos-menu-rebuilt", \(\)\)\?/)