Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

fix: close a sign-in popup's window on Windows when its page closes it

Tw936fccc825817a
diff --git a/.claude/rules/tauri-runtime.md b/.claude/rules/tauri-runtime.md
index 66b3e42c..5b45cc8f 100644
--- a/.claude/rules/tauri-runtime.md
+++ b/.claude/rules/tauri-runtime.md
@@ -18,7 +18,7 @@ paths:
- `--new-window` and `--multi-window` do not bypass every provider policy. Google OAuth and similar embedded-WebView restrictions may still require a normal browser or native client.
- macOS Basic Auth keeps only an enablement boolean in CLI/config. Usernames and passwords are runtime-only input for the target host and session; never put them in CLI arguments, config, generated artifacts, logs, or persistence. Native dialog changes need a real macOS smoke covering the icon, button alignment, label/input baselines, field width, and outer margins; compile and unit tests do not prove the layout.
- A sized `window.open` to a sign-in URL, or to the app's own site, is a popup flow: the popup signs in, answers `window.opener` and closes itself (the Google button on TikTok and X, Notion's own redirect page, Sign in with Apple). On macOS and Windows it gets a real window under every `--new-window` setting. Navigating the opener there instead, which macOS did from 0419e982 to avoid a WebKit abort that 63863b7e's target-configuration popups later removed, left the callback page with nobody to answer: a blank window after the Google password (user reports, 2026-11-18), and a frame's popup (Google's button on X is a frame) was refused outright. `requestsPopupWindow` and `openPopupThroughBlank` in `link_policy.js` open the popup blank and send it to its address, from `event.js` and from `frame_links.js`, because the native handler builds a blank popup under every setting while one that starts at its address exists only with `--new-window`. "Sized" follows the window features as a browser reads them: commas or spaces separate, an explicit `popup` decides, `noopener` and `noreferrer` rule a popup out, since it could not be sent to its address. Every other sign-in `window.open` keeps its route through `openAuthNavigation` (no size, `noopener`, a `_self` target, a popup the engine refused): the current window on macOS and Linux, a native popup first on Windows. A sized popup of another site goes to the system browser unless its URL or its window name (`login`, `oauth` and the rest of `isAuthPopup`) marks a sign-in. WebView2 runs `event.js` in subframes too, where the app's own site is `pakeConfig.url`, not the frame's address. Linux is left out by `opensSignInPopups` until a self-closing popup has been run on a real WebKitGTK desktop: Wry destroys the web view at once there, which nobody has measured against an IPC call in flight. URL matching lives in `src-tauri/src/inject/auth.js` (`matchesAuthUrl`), covered by `tests/unit/auth-sso-patterns.test.js`; routing changes need `tests/unit/event-link-guard.test.js`, `tests/unit/frame-links.test.js` and `tests/browser/popup-routing.mjs`, whose frame cases pass on any code because Chromium never refuses a popup.
-- A popup's page closes it with `window.close()`. WKWebView closes nothing for that call unless its UI delegate implements `webViewDidClose:`, which Wry's does not; `src-tauri/src/app/popup_close.rs` closes the window, and only for windows a page opened (`page_popup`), since WebKit sends the same message for a main window whose page calls `window.close()`. WebView2 closes the window itself; on Linux the window stays with its web view destroyed, as before. On macOS the timing is load-bearing twice. WebKit cancels a closing page's requests, a callback page closes in its first script, and Tauri's notification plugin has an IPC call out in every document (`is_permission_granted`); the answer to a cancelled `ipc://` request makes WebKit raise, and under the release profile's `panic = "abort"` Wry's `objc2::exception::catch` cannot hold, so the whole app quit on every sign-in (measured on macOS 26.7; with `panic = "unwind"` the same run passed, at 2.0 MB more per architecture, 11.8 to 13.8 MB). So `event.js` delays every top-level page's own `window.close()` by `WINDOW_CLOSE_DELAY_MS` (WebKit runs the same cancel for any window a script may close, a first page in the main window included), and the native side hides the window at once and destroys it after `CLOSE_GRACE`, because destroying the web view cancels requests too. Not covered: an opener that closes a cross-origin popup calls the engine's own `close`, a page that navigates within the delay after `close()` loses the close, and the same abort is possible wherever a page with an IPC call in flight is torn down. Page-opened popups take `pake-popup-N` labels (`POPUP_WINDOW_LABEL_PREFIX`) and the window-state plugin filters them out, so a popup opens at the size its page asked for and a Cmd+N window never inherits one. Verify on a real bundle with `tests/integration/signin-popup-server.mjs`: each of its three buttons must end with the app's page in place, the answer received, the popup gone and the app still running, several times in a row, and its `/selfclose` page must leave the main window open.
+- A popup's page closes it with `window.close()`. WKWebView closes nothing for that call unless its UI delegate implements `webViewDidClose:`, which Wry's does not; `src-tauri/src/app/popup_close.rs` closes the window, and only for windows a page opened (`page_popup`), since WebKit sends the same message for a main window whose page calls `window.close()`. WebView2 raises `WindowCloseRequested`, which Wry answers by destroying only the web view's container, so the Windows arm in `build_window` closes the window too (measured in a Windows 11 VM: the popup had answered its opener and stayed on screen titled `callback`). On Linux the window stays with its web view destroyed, as before. On macOS the timing is load-bearing twice. WebKit cancels a closing page's requests, a callback page closes in its first script, and Tauri's notification plugin has an IPC call out in every document (`is_permission_granted`); the answer to a cancelled `ipc://` request makes WebKit raise, and under the release profile's `panic = "abort"` Wry's `objc2::exception::catch` cannot hold, so the whole app quit on every sign-in (measured on macOS 26.7; with `panic = "unwind"` the same run passed, at 2.0 MB more per architecture, 11.8 to 13.8 MB). So `event.js` delays every top-level page's own `window.close()` by `WINDOW_CLOSE_DELAY_MS` (every page, because a `noopener` popup has no `window.opener` to tell it by; in the main window it is a precaution, since a self-closing first page there did not abort in three runs without it), and the native side hides the window at once and destroys it after `CLOSE_GRACE`, because destroying the web view cancels requests too. Not covered: an opener that closes a cross-origin popup calls the engine's own `close`, a page that navigates within the delay after `close()` loses the close, and the same abort is possible wherever a page with an IPC call in flight is torn down. Page-opened popups take `pake-popup-N` labels (`POPUP_WINDOW_LABEL_PREFIX`) and the window-state plugin filters them out, so a popup opens at the size its page asked for and a Cmd+N window never inherits one. Verify on a real bundle with `tests/integration/signin-popup-server.mjs`: each of its three buttons must end with the app's page in place, the answer received, the popup gone and the app still running, several times in a row, and its `/selfclose` page must leave the main window open.
- Auth routing matches provider hostnames and endpoint-shaped paths separately, never query strings or fragments. Tracking links carrying a login URL must still open externally. Cover main-page and subframe clicks in `tests/browser/popup-routing.mjs`; classifier-only tests do not prove routing.
- Blank/two-stage popups need a real WindowProxy, not the main window. Normalize omitted/empty URLs to `about:blank` in both JS entry points: Tauri rejects WebKit's empty URL before calling `on_new_window`. The default native handler must create a managed blank popup with Pake's download delegate even without `--new-window`; `NewWindowResponse::Allow` does not provide that delegate. Verify actual macOS attachment bytes and preservation of the main URL, not only Chromium popup creation.
- `--download-dir` / `downloadDir` is embedded as trusted `pake.download_dir`, shared by native and IPC downloads through `get_download_dir`. Empty means system Downloads; quoted `~/path` expands against the runtime user home. Reject relative paths and absolute tilde suffixes, create missing directories on download, and surface directory creation plus file open/write failures without redirecting to another folder. Do not accept a directory from remote-page IPC arguments.
diff --git a/src-tauri/src/app/window.rs b/src-tauri/src/app/window.rs
index 6a65f185..eaf42be8 100644
--- a/src-tauri/src/app/window.rs
+++ b/src-tauri/src/app/window.rs
@@ -482,7 +482,7 @@ fn build_window(
new_window_features,
} = opts;
// A window a page opened: a sign-in popup, a blank two-stage one.
- #[cfg(target_os = "macos")]
+ #[cfg(any(target_os = "macos", target_os = "windows"))]
let page_popup = new_window_features.is_some();
#[cfg(target_os = "macos")]
let use_native_window_tabbing = config.multi_window && new_window_features.is_none();
@@ -1093,6 +1093,39 @@ fn build_window(
}
}
+ // WebView2 raises WindowCloseRequested for the same call, and Wry answers
+ // by destroying the web view's own container, which left the popup's frame
+ // on screen with nothing in it (measured in a Windows 11 VM).
+ #[cfg(target_os = "windows")]
+ if page_popup {
+ let popup = window.clone();
+ if let Err(error) = window.with_webview(move |webview| {
+ use webview2_com::WindowCloseRequestedEventHandler;
+ let result = unsafe {
+ (move || -> windows_core::Result<()> {
+ let core = webview.controller().CoreWebView2()?;
+ let mut token = 0;
+ core.add_WindowCloseRequested(
+ &WindowCloseRequestedEventHandler::create(Box::new(move |_, _| {
+ if let Err(error) = popup.close() {
+ eprintln!(
+ "[Pake] Failed to close the popup its page closed: {error}"
+ );
+ }
+ Ok(())
+ })),
+ &mut token,
+ )
+ })()
+ };
+ if let Err(error) = result {
+ eprintln!("[Pake] Failed to watch for a popup closing itself: {error}");
+ }
+ }) {
+ eprintln!("[Pake] Failed to access WebView2 for its popup close: {error}");
+ }
+ }
+
// A page's alert(), confirm() and prompt() show nothing in a WKWebView
// unless its UI delegate answers, and Wry's does not: every window's does.
#[cfg(target_os = "macos")]
diff --git a/src-tauri/src/inject/event.js b/src-tauri/src/inject/event.js
index 645771fa..07146608 100644
--- a/src-tauri/src/inject/event.js
+++ b/src-tauri/src/inject/event.js
@@ -680,9 +680,10 @@ function openAuthNavigation(originalWindowOpen, url, name, specs) {
// cancelled IPC request ends the whole app, because the release profile
// cannot catch the exception WebKit raises for it (measured: every sign-in
// popup that closed itself took the app with it). The answer takes a couple
-// of milliseconds, so the close waits them out. WebKit runs the same cancel
-// for any window a script may close, a first page in the main window
-// included, so every top-level page gets the wait, not only popups.
+// of milliseconds, so the close waits them out. Every top-level page gets
+// the wait: a popup opened with noopener has no window.opener to tell it by.
+// In the main window it is a precaution only; a first page that closed
+// itself there did not end the app in three runs without it.
const WINDOW_CLOSE_DELAY_MS = 150;
function deferWindowClose() {
if (window !== window.top) return;
diff --git a/tests/unit/popup-close.test.ts b/tests/unit/popup-close.test.ts
index 07b53f48..024419b1 100644
--- a/tests/unit/popup-close.test.ts
+++ b/tests/unit/popup-close.test.ts
@@ -59,7 +59,14 @@ describe('a popup its page closes', () => {
expect(waited).toBeGreaterThan(hidden);
expect(closed).toBeGreaterThan(waited);
expect(buildWindow()).toMatch(
- /#\[cfg\(target_os = "macos"\)\]\s+let page_popup = new_window_features\.is_some\(\);/,
+ /#\[cfg\(any\(target_os = "macos", target_os = "windows"\)\)\]\s+let page_popup = new_window_features\.is_some\(\);/,
+ );
+ });
+
+ it('closes the window on Windows, where Wry destroys only the web view', () => {
+ // The popup had answered its opener and stayed on screen, empty.
+ expect(buildWindow()).toMatch(
+ /#\[cfg\(target_os = "windows"\)\]\s+if page_popup \{[\s\S]+?core\.add_WindowCloseRequested\([\s\S]+?if let Err\(error\) = popup\.close\(\)/,
);
});