Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.
Rust chatgptclaudeclidesktopdesktop-appelectron-alternativegeminilinuxmacosnativefierno-electronrusttauriweb-to-desktopwebsite-to-appwebviewwindowsyoutube
fix: target macos menu commands at focused window
Tw9379eedbc43a87
diff --git a/src-tauri/src/app/menu.rs b/src-tauri/src/app/menu.rsindex ad4fd830..42b608a9 100644--- a/src-tauri/src/app/menu.rs+++ b/src-tauri/src/app/menu.rs@@ -1,7 +1,7 @@// Menu functionality is only used on macOS; the module is gated in app/mod.rs.use crate::app::window::{open_additional_window_safe, MultiWindowState};use tauri::menu::{AboutMetadata, Menu, MenuItem, PredefinedMenuItem, Submenu};-use tauri::{AppHandle, Manager, Wry};+use tauri::{AppHandle, Manager, WebviewWindow, Wry};use tauri_plugin_opener::OpenerExt;pub fn set_app_menu(@@ -245,6 +245,15 @@ fn home_url(app: &AppHandle) -> Option<tauri::Url> {resolve_home_url(&window_config.url_type, &window_config.url)}+fn focused_webview_window(app_handle: &AppHandle) -> Option<WebviewWindow> {+ let windows = app_handle.webview_windows();+ windows+ .values()+ .find(|window| window.is_focused().unwrap_or(false))+ .cloned()+ .or_else(|| windows.get("pake").cloned())+}+pub fn handle_menu_click(app_handle: &AppHandle, id: &str) {match id {"new_window" => {@@ -256,13 +265,13 @@ pub fn handle_menu_click(app_handle: &AppHandle, id: &str) {.open_url("https://github.com/tw93/Pake", None::<&str>);}"reload" => {- if let Some(window) = app_handle.get_webview_window("pake") {+ if let Some(window) = focused_webview_window(app_handle) {let _ = window.eval("window.location.reload()");}}"toggle_devtools" => {#[cfg(debug_assertions)] // Only allow in debug builds- if let Some(window) = app_handle.get_webview_window("pake") {+ if let Some(window) = focused_webview_window(app_handle) {if window.is_devtools_open() {window.close_devtools();} else {@@ -271,32 +280,32 @@ pub fn handle_menu_click(app_handle: &AppHandle, id: &str) {}}"zoom_in" => {- if let Some(window) = app_handle.get_webview_window("pake") {+ if let Some(window) = focused_webview_window(app_handle) {let _ = window.eval("zoomIn()");}}"zoom_out" => {- if let Some(window) = app_handle.get_webview_window("pake") {+ if let Some(window) = focused_webview_window(app_handle) {let _ = window.eval("zoomOut()");}}"zoom_reset" => {- if let Some(window) = app_handle.get_webview_window("pake") {+ if let Some(window) = focused_webview_window(app_handle) {let _ = window.eval("setZoom('100%')");}}"go_back" => {- if let Some(window) = app_handle.get_webview_window("pake") {+ if let Some(window) = focused_webview_window(app_handle) {let _ = window.eval("window.history.back()");}}"go_forward" => {- if let Some(window) = app_handle.get_webview_window("pake") {+ if let Some(window) = focused_webview_window(app_handle) {let _ = window.eval("window.history.forward()");}}"go_home" => {- if let Some(window) = app_handle.get_webview_window("pake") {+ if let Some(window) = focused_webview_window(app_handle) {// Native navigation works even from a blank error page (where// eval cannot run) and resolves local-file apps to the correct// bundled asset instead of a path on the current origin.@@ -311,39 +320,39 @@ pub fn handle_menu_click(app_handle: &AppHandle, id: &str) {}}"copy_url" => {- if let Some(window) = app_handle.get_webview_window("pake") {+ if let Some(window) = focused_webview_window(app_handle) {let _ = window.eval("navigator.clipboard.writeText(window.location.href)");}}"paste_and_match_style" => {- if let Some(window) = app_handle.get_webview_window("pake") {+ if let Some(window) = focused_webview_window(app_handle) {let _ = window.eval("triggerPasteAsPlainText()");}}"find" => {- if let Some(window) = app_handle.get_webview_window("pake") {+ if let Some(window) = focused_webview_window(app_handle) {let _ = window.eval("window.pakeFind?.open()");}}"find_next" => {- if let Some(window) = app_handle.get_webview_window("pake") {+ if let Some(window) = focused_webview_window(app_handle) {let _ = window.eval("window.pakeFind?.next()");}}"find_previous" => {- if let Some(window) = app_handle.get_webview_window("pake") {+ if let Some(window) = focused_webview_window(app_handle) {let _ = window.eval("window.pakeFind?.previous()");}}"clear_cache_restart" => {- if let Some(window) = app_handle.get_webview_window("pake") {+ if let Some(window) = focused_webview_window(app_handle) {if window.clear_all_browsing_data().is_ok() {app_handle.restart();}}}"always_on_top" => {- if let Some(window) = app_handle.get_webview_window("pake") {+ if let Some(window) = focused_webview_window(app_handle) {let is_on_top = window.is_always_on_top().unwrap_or(false);let _ = window.set_always_on_top(!is_on_top);}diff --git a/src-tauri/src/inject/event.js b/src-tauri/src/inject/event.jsindex d3246154..d0b980be 100644--- a/src-tauri/src/inject/event.js+++ b/src-tauri/src/inject/event.js@@ -60,6 +60,20 @@ function handleShortcut(event) {}}+function handleWebShortcut(event) {+ if (isNonMacDesktop() && event.ctrlKey) {+ handleShortcut(event);+ return;+ }++ const isMac = /mac/i.test(getDesktopPlatform());+ const isMacScrollShortcut =+ event.key === "ArrowUp" || event.key === "ArrowDown";+ if (isMac && event.metaKey && isMacScrollShortcut) {+ handleShortcut(event);+ }+}+function toggleNativeFullscreen(appWindow) {appWindow.isFullscreen()@@ -622,14 +636,7 @@ document.addEventListener("DOMContentLoaded", () => {if (window["pakeConfig"]?.disabled_web_shortcuts !== true) {document.addEventListener("keydown", handleWindowFullscreenShortcut, true);- document.addEventListener("keyup", (event) => {- if (/windows|linux/i.test(navigator.userAgent) && event.ctrlKey) {- handleShortcut(event);- }- if (/macintosh|mac os x/i.test(navigator.userAgent) && event.metaKey) {- handleShortcut(event);- }- });+ document.addEventListener("keyup", handleWebShortcut);}document.addEventListener("keydown", handleClipboardShortcut, true);diff --git a/src-tauri/src/inject/find.js b/src-tauri/src/inject/find.jsindex a749d9b7..6c09dd34 100644--- a/src-tauri/src/inject/find.js+++ b/src-tauri/src/inject/find.js@@ -663,9 +663,11 @@function getFindShortcutAction(event) {const userAgent = navigator.userAgent || "";const isMac = /macintosh|mac os x/i.test(userAgent);- const hasModifier = isMac- ? event.metaKey && !event.ctrlKey- : event.ctrlKey && !event.metaKey;+ if (isMac) {+ return "";+ }++ const hasModifier = event.ctrlKey && !event.metaKey;if (!hasModifier || event.altKey) {return "";diff --git a/tests/unit/event-fullscreen-shortcuts.test.js b/tests/unit/event-fullscreen-shortcuts.test.jsindex 64ee551f..c857685b 100644--- a/tests/unit/event-fullscreen-shortcuts.test.js+++ b/tests/unit/event-fullscreen-shortcuts.test.js@@ -42,6 +42,7 @@ function loadEventHelpers({const eventListeners = {};const elementsById = new Map();const fullscreenCalls = [];+ const reloadCalls = [];const body = createElement("body");body.appendChild = (child) => {body.children.push(child);@@ -81,7 +82,7 @@ function loadEventHelpers({href: "https://example.com/app",origin: "https://example.com",pathname: "/app",- reload: () => {},+ reload: () => reloadCalls.push(true),},localStorage: {getItem: () => null,@@ -145,6 +146,7 @@ function loadEventHelpers({...context,eventListeners,fullscreenCalls,+ reloadCalls,get isFullscreen() {return isFullscreen;},@@ -157,6 +159,12 @@ function getFullscreenShortcutHandler(context) {).handler;}+function getWebShortcutHandler(context) {+ return context.eventListeners.keyup.find(+ ({ handler }) => handler.name === "handleWebShortcut",+ ).handler;+}+function createKeyEvent(key, overrides = {}) {return {key,@@ -173,6 +181,43 @@ function createKeyEvent(key, overrides = {}) {}describe("event fullscreen shortcuts", () => {+ it("leaves macOS reload shortcuts to the native menu", () => {+ const context = loadEventHelpers({+ userAgent:+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15",+ platform: "MacIntel",+ });+ const event = createKeyEvent("r", { metaKey: true });++ getWebShortcutHandler(context)(event);++ expect(context.reloadCalls).toEqual([]);+ expect(event.preventDefault).not.toHaveBeenCalled();+ });++ it("keeps Ctrl+R web shortcuts on Windows and Linux", () => {+ const context = loadEventHelpers();+ const event = createKeyEvent("r", { ctrlKey: true });++ getWebShortcutHandler(context)(event);++ expect(context.reloadCalls).toEqual([true]);+ expect(event.preventDefault).toHaveBeenCalled();+ });++ it("keeps macOS scroll-to-edge shortcuts in the webview", () => {+ const context = loadEventHelpers({+ userAgent:+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15",+ platform: "MacIntel",+ });+ const event = createKeyEvent("ArrowUp", { metaKey: true });++ getWebShortcutHandler(context)(event);++ expect(event.preventDefault).toHaveBeenCalled();+ });+it("toggles native fullscreen on F11 for Windows", async () => {const context = loadEventHelpers();const handler = getFullscreenShortcutHandler(context);diff --git a/tests/unit/find-shortcuts.test.js b/tests/unit/find-shortcuts.test.jsindex 278c9353..c6de79c3 100644--- a/tests/unit/find-shortcuts.test.js+++ b/tests/unit/find-shortcuts.test.js@@ -240,7 +240,7 @@ describe("Find injection", () => {expect(previousEvent.propagationStopped).toBe(true);});- it("uses the macOS modifier for Find shortcuts", () => {+ it("leaves macOS Find shortcuts to the native menu", () => {const context = loadFindScript({enabled: true,userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",@@ -252,7 +252,7 @@ describe("Find injection", () => {listener.handler(createKeyboardEvent("f", { ctrlKey: true }));listener.handler(createKeyboardEvent("f", { metaKey: true }));- expect(calls).toEqual(["open"]);+ expect(calls).toEqual([]);});it("counts text matches and skips input and script content", () => {diff --git a/tests/unit/menu-focused-window.test.ts b/tests/unit/menu-focused-window.test.tsnew file mode 100644index 00000000..1880aed1--- /dev/null+++ b/tests/unit/menu-focused-window.test.ts@@ -0,0 +1,18 @@+import fs from 'node:fs';+import path from 'node:path';+import { describe, expect, it } from 'vitest';++describe('macOS menu window targeting', () => {+ it('routes window commands through the focused webview helper', () => {+ const source = fs.readFileSync(+ path.join(process.cwd(), 'src-tauri/src/app/menu.rs'),+ 'utf8',+ );+ const handler = source.slice(source.indexOf('pub fn handle_menu_click'));++ expect(source).toContain('fn focused_webview_window');+ expect(source).toContain('window.is_focused()');+ expect(handler).not.toContain('get_webview_window("pake")');+ expect(handler).toContain('focused_webview_window(app_handle)');+ });+});