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
test: keep each script panel's selector and encoding in step
Tw938ef3b692e49c
diff --git a/.claude/rules/tauri-runtime.md b/.claude/rules/tauri-runtime.mdindex a970822a..e62a6b59 100644--- a/.claude/rules/tauri-runtime.md+++ b/.claude/rules/tauri-runtime.md@@ -8,7 +8,7 @@ paths:- Native IPC is gated by the application command manifest in `src-tauri/build.rs`. Keep every registered app command in that manifest and grant only its required permissions. The packaged capability is local-only; `PakeConfig::remote_capability` grants the same permission set to exact configured entry origins (scheme, host, and port) before webviews load. Navigation flags, login redirects, subdomains, and restored URLs must not expand that grant. Keep local pages, secondary windows, loopback/IPv6 URLs, and literal hostname handling covered. ACL-denied downloads may fall back to browser attachments, but transient download anchors must bypass the IPC click interceptor to avoid recursion. Fullscreen must acquire native permission before mutating the player DOM; untrusted frame messages must never gain a top-level navigation fallback. Run `cargo test --lib` and `node tests/integration/ipc-security.mjs` (real macOS WebView); hiding `window.__TAURI__` or narrowing plugin capabilities alone does not gate application commands.- Injected CSS uses `src-tauri/src/inject/styles.js`: preserve DOM stylesheet order on ordinary pages; use constructable sheets only when CSP blocks the style element. `replaceSync` silently drops `@import`, so keep import-bearing CSS on its original DOM path, including escaped/case-insensitive at-keywords. Strict CSP plus imports remains unsupported. Run `node tests/browser/style-injection.mjs` with an installed Playwright runtime; mock CSSStyleSheet tests do not validate CSP or cascade behavior.- The `runtime-template` Cargo feature builds a prebuilt runtime that a separate builder customizes without a compiler: at launch it reads `pake-runtime/{pake.json, app.json, custom.js?, tray.png?}` from `Contents/Resources` on macOS or next to the executable elsewhere, and applies app.json's `identifier` and `productName` to the Tauri context before build. The CLI never enables the feature, so CLI-built apps keep their compiled configuration even if such a folder appears. A present but invalid override exits with a `[Pake]` message instead of falling back to the template's identity. A runtime app keys its WebView profile folder under the config dir (`%APPDATA%\<identifier>\EBWebView` on Windows) by identifier instead of product name (`data_dir_name`), so a rename keeps logins and a generated name cannot share another program's folder; CLI apps keep the product-name folder. On Windows a runtime app also sets the process AppUserModelID to its identifier before any window exists, so shortcuts carrying that ID group with the running window and toasts are attributed to it. Logic and tests live in `src-tauri/src/util.rs` (`apply_runtime_override`, `data_dir_name`, `set_app_user_model_id`); `PakeConfig::runtime_app` is the single runtime-app flag, and `run_app` copies the overridden identity from the returned Tauri config into the context; keep `cargo clippy` and `cargo test --lib` green both with and without `--features runtime-template`.-- A page's `alert()`, `confirm()` and `prompt()` show nothing in a WKWebView unless its UI delegate implements the three `runJavaScript…Panel` methods, and Wry 0.54.2's does not: `alert` returned at once, `confirm` returned false and `prompt` null in a millisecond, so a site that confirms before logging out did nothing (a user's report, 2026-11-17; WebView2 and WebKitGTK show their own). `src-tauri/src/app/script_dialogs.rs` adds the three methods to the delegate's class where it lacks them, for every webview from `build_window`, and then sets the same delegate on the webview again: WebKit records which methods a delegate answers when it is set, and without the reset the added methods are never called. Each panel is an NSAlert sheet on the page's window whose completion calls WebKit's handler exactly once (WebKit raises on zero or two calls), which keeps the script waiting as a browser does. The sheet goes on the window even when it is hidden, as a closed window is with hide-on-close: AppKit then shows the window with its sheet (measured: a window hidden with Cmd+W came back on screen when its page called `confirm()`), where an app-modal panel would float over whatever else the user is doing; only a webview with no window falls back to `runModal`. Cancel carries an explicit Escape key equivalent, because AppKit gives Escape only to a button titled Cancel and the Chinese title is 取消. Verify on a real bundle with a page that calls all three and writes what it got back: OK gives `true`, Cancel `false`, a prompt its field's text or `null`, and each wait is seconds, not a millisecond. Send the keys with a CGEvent tool of your own: Escape must give `false` and `null`, Return `true`. Cover it with `tests/unit/macos-script-dialogs.test.ts`.+- A page's `alert()`, `confirm()` and `prompt()` show nothing in a WKWebView unless its UI delegate implements the three `runJavaScript…Panel` methods, and Wry 0.54.2's does not: `alert` returned at once, `confirm` returned false and `prompt` null in a millisecond, so a site that confirms before logging out did nothing (a user's report, 2026-11-17; WebView2 and WebKitGTK show their own). `src-tauri/src/app/script_dialogs.rs` adds the three methods to the delegate's class where it lacks them, for every webview from `build_window`, and then sets the same delegate on the webview again: WebKit records which methods a delegate answers when it is set, and without the reset the added methods are never called. Each panel is an NSAlert sheet on the page's window whose completion calls WebKit's handler exactly once (WebKit raises on zero or two calls), which keeps the script waiting as a browser does. The sheet goes on the window even when it is hidden, as a closed window is with hide-on-close: AppKit then shows the window with its sheet (measured: a window hidden with Cmd+W came back on screen when its page called `confirm()`), where an app-modal panel would float over whatever else the user is doing; only a webview with no window falls back to `runModal`. Cancel carries an explicit Escape key equivalent, because AppKit gives Escape only to a button titled Cancel and the Chinese title is 取消. Verify on a real bundle with a page that calls all three and writes what it got back: OK gives `true`, Cancel `false`, a prompt its field's text or `null`, and each wait is seconds, not a millisecond. Send the keys with a CGEvent tool of your own: Escape must give `false` and `null`, Return `true`. Cover it with `tests/unit/macos-script-dialogs.test.ts` and the `script_panel_encodings_match_their_selectors` Rust test, which keeps each selector's colons and its type encoding in step.- `navigation_buttons` is a runtime-only macOS window field (no CLI flag; the paid apps set it, off by default): `src-tauri/src/app/nav_buttons.rs` adds Back and Forward to the title bar in a leading accessory beside the traffic lights, for people with a plain mouse who have neither the swipe nor Cmd-[. The buttons call `history_step`, the native step the Navigation menu uses, so they work on a blank error page; their action lives on the window's class as `tab_bar.rs`'s does. Enabled state follows the WKWebView's `canGoBack` and `canGoForward` by key-value observing; do not poll. The webview owns the observer through an associated object, which means the observer drops only while the webview is deallocating, when a weak reference to it already reads nil: removal in `Drop` never runs, so the observer unregisters on `NSWindowWillCloseNotification`, behind one flag because removing an unregistered observer raises (`auth.rs`'s delegate still has only the `Drop` removal). Alignment is to ink, checked on a 2x capture: each chevron's middle goes on the close button's centre line, read from `standardWindowButton:` (the lights sit 16 points down a 32 point bar on macOS 26) and from the cell's `imageRectForBounds:` (NSButton sets a 16 point image half a point above a 22 point button's middle), and redone on resize and around full screen. The chevrons are drawn, not `chevron.left`, because that symbol sits on a text baseline 1.5 points high. Labels are the system's own, 返回 and 前进, 上一頁 and 下一頁, through `UiLanguage`. A real-bundle check measures the rows of the red light and of each chevron in a `screencapture -l` PNG, whose centres must be equal, and opens then closes a Cmd+N window with the app still running. Under `hide_title_bar` the buttons sit over the page beside the lights, and the per-site `immersive` paddings in `style.js` clear only the lights' width. Cover it with `tests/unit/macos-navigation-buttons.test.ts` and the `navigation_buttons_are_opt_in` Rust test.- `transparent_title_bar` is a runtime-only macOS window field (no CLI flag; the paid apps set it): Tauri's `Transparent` title bar keeps the page below the bar, and `src-tauri/src/inject/title_bar.js` paints the window background with the page's top-edge color through `set_title_bar_color`. `hide_title_bar` keeps precedence and its overlay behavior. The color is the majority of three top-edge samples, resolved through a canvas so `oklch()`/`color()` values work; do not skip sampling for `document.hidden`, or windows revealed after load stay uncolored. Cover it with `tests/unit/title-bar-color.test.js`; when checking a real bundle, window captures carry the display profile, so convert the sRGB page color before comparing pixels.- Window/runtime options with platform-sensitive behavior include `--incognito`, `--new-window`, `--min-width`, `--min-height`, `--maximize`, multi-window behavior, notification click handling, and Linux/Wayland WebKit compositing defaults.diff --git a/src-tauri/src/app/script_dialogs.rs b/src-tauri/src/app/script_dialogs.rsindex 3a691ace..4e857d4b 100644--- a/src-tauri/src/app/script_dialogs.rs+++ b/src-tauri/src/app/script_dialogs.rs@@ -18,18 +18,33 @@ use block2::{Block, RcBlock};use objc2::ffi::class_addMethod;use objc2::rc::Retained;use objc2::runtime::{AnyClass, AnyObject, Bool, Imp, Sel};-use objc2::{msg_send, sel, MainThreadMarker};+use objc2::{msg_send, MainThreadMarker};use objc2_app_kit::{NSAlert, NSAlertFirstButtonReturn, NSTextField};use objc2_foundation::{NSPoint, NSRect, NSSize, NSString};use std::cell::Cell;-use std::ffi::c_char;+use std::ffi::CStr;use crate::util::UiLanguage;/// `void (id webView, NSString *message, WKFrameInfo *frame, block)`.-const MESSAGE_PANEL_TYPES: &[u8] = b"v@:@@@@?\0";+const MESSAGE_PANEL_TYPES: &CStr = c"v@:@@@@?";/// The text panel carries the default text before the frame.-const TEXT_PANEL_TYPES: &[u8] = b"v@:@@@@@?\0";+const TEXT_PANEL_TYPES: &CStr = c"v@:@@@@@?";+/// The three WKUIDelegate methods, each with the encoding its handler is+/// installed under. A selector takes one argument per colon, and an encoding+/// that disagrees makes the runtime read the wrong registers.+const ALERT_PANEL: (&CStr, &CStr) = (+ c"webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler:",+ MESSAGE_PANEL_TYPES,+);+const CONFIRM_PANEL: (&CStr, &CStr) = (+ c"webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:completionHandler:",+ MESSAGE_PANEL_TYPES,+);+const TEXT_PANEL: (&CStr, &CStr) = (+ c"webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:completionHandler:",+ TEXT_PANEL_TYPES,+);const PROMPT_FIELD_WIDTH: f64 = 260.0;const PROMPT_FIELD_HEIGHT: f64 = 24.0;@@ -259,23 +274,12 @@ pub fn install(webview: &tauri::webview::PlatformWebview) {),Imp,>(run_prompt);- for (selector, imp, types) in [- (- sel!(webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler:),- alert,- MESSAGE_PANEL_TYPES,- ),- (- sel!(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:completionHandler:),- confirm,- MESSAGE_PANEL_TYPES,- ),- (- sel!(webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:completionHandler:),- prompt,- TEXT_PANEL_TYPES,- ),+ for ((selector, types), imp) in [+ (ALERT_PANEL, alert),+ (CONFIRM_PANEL, confirm),+ (TEXT_PANEL, prompt),] {+ let selector = Sel::register(selector);if class.instance_method(selector).is_some() {continue;}@@ -283,7 +287,7 @@ pub fn install(webview: &tauri::webview::PlatformWebview) {class as *const AnyClass as *mut AnyClass,selector,imp,- types.as_ptr() as *const c_char,+ types.as_ptr(),);if !added.as_bool() {eprintln!("[Pake] Failed to install a script dialog on the UI delegate.");@@ -299,3 +303,39 @@ pub fn install(webview: &tauri::webview::PlatformWebview) {let _: () = msg_send![view, setUIDelegate: &*delegate];}}++#[cfg(test)]+mod tests {+ use super::*;++ /// The arguments an encoding declares after `self` and `_cmd`; a block is+ /// written `@?` and counts once.+ fn declared_arguments(types: &CStr) -> usize {+ let types = types.to_str().unwrap();+ let arguments = types.strip_prefix("v@:").expect("void, self, _cmd");+ arguments.replace("@?", "B").chars().count()+ }++ #[test]+ fn script_panel_encodings_match_their_selectors() {+ for (selector, types) in [ALERT_PANEL, CONFIRM_PANEL, TEXT_PANEL] {+ let selector = selector.to_str().unwrap();+ let colons = selector.matches(':').count();+ assert_eq!(declared_arguments(types), colons, "{selector}");+ // WebKit's last argument is always the completion handler block.+ assert!(types.to_str().unwrap().ends_with("@?"), "{selector}");+ assert!(selector.ends_with("completionHandler:"), "{selector}");+ }+ // The text panel is the one with a default text before the frame.+ assert_eq!(+ declared_arguments(TEXT_PANEL.1),+ declared_arguments(ALERT_PANEL.1) + 1+ );+ }++ #[test]+ fn cancel_and_accept_are_named_in_every_language() {+ let (accept, cancel) = button_titles();+ assert!(!accept.is_empty() && !cancel.is_empty() && accept != cancel);+ }+}diff --git a/tests/unit/macos-script-dialogs.test.ts b/tests/unit/macos-script-dialogs.test.tsindex 3eb9bc9d..7547a8a3 100644--- a/tests/unit/macos-script-dialogs.test.ts+++ b/tests/unit/macos-script-dialogs.test.ts@@ -28,8 +28,9 @@ describe('macOS alert(), confirm() and prompt()', () => {'webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:completionHandler:','webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:completionHandler:',]) {- expect(dialogs).toContain(`sel!(${selector})`);+ expect(dialogs).toContain(`c"${selector}"`);}+ expect(dialogs).toContain('let selector = Sel::register(selector);');// Added only where the delegate's class has no answer of its own.expect(dialogs).toContain('if class.instance_method(selector).is_some() {');});