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: cancel startup reveal after user visibility control

Tw93db6429634166
diff --git a/src-tauri/src/app/setup.rs b/src-tauri/src/app/setup.rs
index 12edfc29..8d6e14df 100644
--- a/src-tauri/src/app/setup.rs
+++ b/src-tauri/src/app/setup.rs
@@ -1,6 +1,7 @@
use crate::app::window::{open_additional_window_safe, reapply_window_icon};
+use crate::cancel_startup_reveal;
use std::str::FromStr;
-use std::sync::{Arc, Mutex};
+use std::sync::{atomic::AtomicBool, Arc, Mutex};
use std::time::{Duration, Instant};
use tauri::{
menu::{MenuBuilder, MenuItemBuilder},
@@ -16,6 +17,7 @@ pub fn set_system_tray(
tray_icon_path: &str,
_init_fullscreen: bool,
allow_multi_window: bool,
+ startup_revealed: Arc<AtomicBool>,
) -> tauri::Result<()> {
if !show_system_tray {
app.remove_tray_by_id("pake-tray");
@@ -42,6 +44,8 @@ pub fn set_system_tray(
app.app_handle().remove_tray_by_id("pake-tray");
+ let menu_revealed = startup_revealed.clone();
+ let click_revealed = startup_revealed;
let mut tray_builder = TrayIconBuilder::new()
.menu(&menu)
.on_menu_event(move |app, event| match event.id().as_ref() {
@@ -50,11 +54,13 @@ pub fn set_system_tray(
}
"hide_app" => {
if let Some(window) = app.get_webview_window("pake") {
+ cancel_startup_reveal(&menu_revealed);
let _ = window.minimize();
}
}
"show_app" => {
if let Some(window) = app.get_webview_window("pake") {
+ cancel_startup_reveal(&menu_revealed);
let _ = window.show();
reapply_window_icon(&window);
#[cfg(target_os = "linux")]
@@ -79,6 +85,8 @@ pub fn set_system_tray(
if let TrayIconEvent::Click { button, .. } = event {
if button == tauri::tray::MouseButton::Left {
if let Some(window) = tray.app_handle().get_webview_window("pake") {
+ // Any tray toggle claims visibility control from startup reveal.
+ cancel_startup_reveal(&click_revealed);
let is_visible = window.is_visible().unwrap_or(false);
if is_visible {
let _ = window.hide();
@@ -120,6 +128,7 @@ pub fn set_global_shortcut(
app: &AppHandle,
shortcut: String,
_init_fullscreen: bool,
+ startup_revealed: Arc<AtomicBool>,
) -> tauri::Result<()> {
if shortcut.is_empty() {
return Ok(());
@@ -139,6 +148,7 @@ pub fn set_global_shortcut(
tauri_plugin_global_shortcut::Builder::new()
.with_handler({
let last_triggered = Arc::clone(&last_triggered);
+ let startup_revealed = startup_revealed.clone();
move |app, event, _shortcut| {
let Ok(mut last_triggered) = last_triggered.lock() else {
return;
@@ -150,6 +160,7 @@ pub fn set_global_shortcut(
if shortcut_hotkey.eq(event) {
if let Some(window) = app.get_webview_window("pake") {
+ cancel_startup_reveal(&startup_revealed);
let is_visible = window.is_visible().unwrap_or(false);
if is_visible {
let _ = window.hide();
diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs
index bf07dc92..73740426 100644
--- a/src-tauri/src/lib.rs
+++ b/src-tauri/src/lib.rs
@@ -42,8 +42,20 @@ fn is_placeholder_startup_url(url: &Url) -> bool {
url.scheme().eq_ignore_ascii_case("about")
}
+/// First automatic reveal wins. Returns true if the caller should show the window.
+fn claim_startup_reveal(revealed: &AtomicBool) -> bool {
+ !revealed.swap(true, Ordering::AcqRel)
+}
+
+/// User took control of main-window visibility (tray, shortcut, dock, second
+/// instance, hide-on-close). Drop any pending page-load / fallback reveal so a
+/// slow cold start cannot re-open a window the user just hid.
+pub(crate) fn cancel_startup_reveal(revealed: &AtomicBool) {
+ revealed.store(true, Ordering::Release);
+}
+
fn reveal_startup_window(window: WebviewWindow, init_fullscreen: bool, revealed: &Arc<AtomicBool>) {
- if revealed.swap(true, Ordering::AcqRel) {
+ if !claim_startup_reveal(revealed) {
return;
}
@@ -216,11 +228,13 @@ pub fn run_app() {
// Only add single instance plugin if multiple instances are not allowed
if !multi_instance {
+ let instance_revealed = startup_window_revealed.clone();
app_builder = app_builder.plugin(tauri_plugin_single_instance::init(
move |app, _args, _cwd| {
if multi_window {
open_additional_window_safe(app);
} else if let Some(window) = app.get_webview_window("pake") {
+ cancel_startup_reveal(&instance_revealed);
let _ = window.unminimize();
let _ = window.show();
reapply_window_icon(&window);
@@ -254,6 +268,11 @@ pub fn run_app() {
});
}
+ // Clone before setup moves the Arc into tray / shortcut / fallback handlers.
+ let close_revealed = startup_window_revealed.clone();
+ #[cfg(target_os = "macos")]
+ let reopen_revealed = startup_window_revealed.clone();
+
app_builder
.invoke_handler(tauri::generate_handler![
download_file,
@@ -290,22 +309,30 @@ pub fn run_app() {
&pake_config.system_tray_path,
init_fullscreen,
multi_window,
+ startup_window_revealed.clone(),
+ )?;
+ set_global_shortcut(
+ app.app_handle(),
+ activation_shortcut,
+ init_fullscreen,
+ startup_window_revealed.clone(),
)?;
- set_global_shortcut(app.app_handle(), activation_shortcut, init_fullscreen)?;
// Show window after state restoration to prevent position flashing
// once its first page finishes. A fallback keeps offline or stalled
// pages reachable without exposing a blank webview during normal startup.
if !start_to_tray {
let window_clone = window.clone();
- let fallback_revealed = startup_window_revealed.clone();
tauri::async_runtime::spawn(async move {
tokio::time::sleep(tokio::time::Duration::from_millis(
STARTUP_WINDOW_FALLBACK_DELAY,
))
.await;
- reveal_startup_window(window_clone, init_fullscreen, &fallback_revealed);
+ reveal_startup_window(window_clone, init_fullscreen, &startup_window_revealed);
});
+ } else {
+ // Tray/shortcut already hold clones that cancel user-driven toggles.
+ drop(startup_window_revealed);
}
Ok(())
@@ -313,6 +340,8 @@ pub fn run_app() {
.on_window_event(move |_window, _event| {
if let tauri::WindowEvent::CloseRequested { api, .. } = _event {
if hide_on_close && _window.label() == "pake" {
+ // User dismissed the window; do not let startup reveal reopen it.
+ cancel_startup_reveal(&close_revealed);
// Hide window when hide_on_close is enabled (regardless of tray status)
let window = _window.clone();
tauri::async_runtime::spawn(async move {
@@ -347,7 +376,7 @@ pub fn run_app() {
eprintln!("[Pake] Fatal error while building Tauri application: {error}");
std::process::exit(1);
})
- .run(|_app, _event| {
+ .run(move |_app, _event| {
// Handle macOS dock icon click to reopen hidden window
#[cfg(target_os = "macos")]
if let tauri::RunEvent::Reopen {
@@ -357,6 +386,7 @@ pub fn run_app() {
{
if !has_visible_windows {
if let Some(window) = _app.get_webview_window("pake") {
+ cancel_startup_reveal(&reopen_revealed);
let _ = window.show();
reapply_window_icon(&window);
let _ = window.set_focus();
@@ -387,6 +417,33 @@ mod tests {
assert!(!is_placeholder_startup_url(&tauri));
}
+ #[test]
+ fn first_claim_wins_startup_reveal() {
+ let revealed = AtomicBool::new(false);
+ assert!(claim_startup_reveal(&revealed));
+ assert!(!claim_startup_reveal(&revealed));
+ }
+
+ #[test]
+ fn user_show_then_hide_blocks_automatic_startup_reveal() {
+ // Slow page load: user opens from tray/shortcut, then hides again.
+ // Page-load finish and the 3s fallback must not reopen the window.
+ let revealed = AtomicBool::new(false);
+ cancel_startup_reveal(&revealed); // explicit show
+ cancel_startup_reveal(&revealed); // explicit hide
+ assert!(
+ !claim_startup_reveal(&revealed),
+ "automatic reveal must stay cancelled after user visibility control"
+ );
+ }
+
+ #[test]
+ fn cancel_before_any_claim_blocks_reveal() {
+ let revealed = AtomicBool::new(false);
+ cancel_startup_reveal(&revealed);
+ assert!(!claim_startup_reveal(&revealed));
+ }
+
#[test]
fn linux_webkit_safe_mode_stays_on_by_default() {
assert!(should_enable_linux_webkit_safe_mode_from_values(
diff --git a/tests/unit/startup-window-reveal.test.ts b/tests/unit/startup-window-reveal.test.ts
index bbf00262..ac887ad6 100644
--- a/tests/unit/startup-window-reveal.test.ts
+++ b/tests/unit/startup-window-reveal.test.ts
@@ -6,12 +6,16 @@ const libSource = fs.readFileSync(
path.join(process.cwd(), 'src-tauri', 'src', 'lib.rs'),
'utf8',
);
+const setupSource = fs.readFileSync(
+ path.join(process.cwd(), 'src-tauri', 'src', 'app', 'setup.rs'),
+ 'utf8',
+);
describe('startup window reveal', () => {
it('waits for the first real page finish instead of a fixed short delay', () => {
expect(libSource).toContain('.on_page_load(');
expect(libSource).toContain('PageLoadEvent::Finished');
- expect(libSource).toContain('revealed.swap(true');
+ expect(libSource).toContain('claim_startup_reveal');
expect(libSource).toContain('STARTUP_WINDOW_FALLBACK_DELAY');
expect(libSource).toContain('is_placeholder_startup_url');
expect(libSource).toMatch(
@@ -26,4 +30,19 @@ describe('startup window reveal', () => {
/url\.scheme\(\)\.eq_ignore_ascii_case\("about"\)/,
);
});
+
+ it('cancels automatic reveal when the user controls visibility during startup', () => {
+ expect(libSource).toContain('cancel_startup_reveal');
+ expect(libSource).toMatch(
+ /user_show_then_hide_blocks_automatic_startup_reveal/,
+ );
+ // Tray, shortcut, second-instance, hide-on-close, and dock reopen.
+ expect(setupSource).toMatch(/cancel_startup_reveal\(&menu_revealed\)/);
+ expect(setupSource).toMatch(/cancel_startup_reveal\(&click_revealed\)/);
+ expect(setupSource).toMatch(/cancel_startup_reveal\(&startup_revealed\)/);
+ expect(libSource).toMatch(
+ /cancel_startup_reveal\(&instance_revealed\)/,
+ );
+ expect(libSource).toMatch(/cancel_startup_reveal\(&close_revealed\)/);
+ });
});