Skip to content
56 changes: 56 additions & 0 deletions packages/desktop-shell/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use url::Url;
const BOOTSTRAP_URL: &str = "http://tauri.localhost";
#[cfg(not(target_os = "windows"))]
const BOOTSTRAP_URL: &str = "tauri://localhost";
static FULLSCREEN_HIDE_PENDING: AtomicBool = AtomicBool::new(false);
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
Comment thread
yiliang114 marked this conversation as resolved.
Resolved

#[derive(Clone, Serialize)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -102,6 +103,30 @@ fn main() {
.window_dirty
.store(true, Ordering::Relaxed);
}
#[cfg(target_os = "macos")]
WindowEvent::CloseRequested { api, .. } => {
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
save_window_state(app_handle);
api.prevent_close();
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
if let Some(window) = app_handle.get_webview_window("main") {
if FULLSCREEN_HIDE_PENDING.load(Ordering::Relaxed) {
return;
}
if window.is_fullscreen().unwrap_or(false) {
FULLSCREEN_HIDE_PENDING.store(true, Ordering::Relaxed);
let _ = window.set_fullscreen(false);
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
// ponytail: remove this delay when Tauri exposes fullscreen-exit events.
std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_secs(2));
if FULLSCREEN_HIDE_PENDING.swap(false, Ordering::Relaxed) {
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
let _ = window.hide();
}
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
});
} else {
let _ = window.hide();
}
}
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
}
#[cfg(not(target_os = "macos"))]
WindowEvent::CloseRequested { .. } => save_window_state(app_handle),
_ => {}
},
Expand All @@ -114,6 +139,19 @@ fn main() {
save_window_state(app_handle);
stop_runtime(app_handle);
}
#[cfg(target_os = "macos")]
RunEvent::Reopen {
has_visible_windows,
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
..
} if should_restore_main_window(
has_visible_windows,
app_handle.get_webview_window("main").is_some_and(|window| {
!window.is_visible().unwrap_or(false) || window.is_minimized().unwrap_or(false)
}),
) =>
{
focus_main_window(app_handle)
}
_ => {}
});
}
Expand Down Expand Up @@ -518,13 +556,19 @@ fn spawn_window_state_flusher(app: AppHandle) {
}

fn focus_main_window(app: &AppHandle) {
FULLSCREEN_HIDE_PENDING.store(false, Ordering::Relaxed);
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
if let Some(window) = app.get_webview_window("main") {
let _ = window.unminimize();
let _ = window.show();
let _ = window.set_focus();
}
}

#[cfg(target_os = "macos")]
fn should_restore_main_window(has_visible_windows: bool, main_needs_restore: bool) -> bool {
!has_visible_windows || main_needs_restore || FULLSCREEN_HIDE_PENDING.load(Ordering::Relaxed)
}

fn show_local_control_window(app: &AppHandle) -> Result<(), String> {
if let Some(window) = app.get_webview_window("local-control") {
window.show().map_err(|error| error.to_string())?;
Expand Down Expand Up @@ -660,9 +704,21 @@ mod tests {
is_allowed_navigation, is_bootstrap_url, is_safe_external_url, is_same_origin, origin_of,
BOOTSTRAP_URL,
};
#[cfg(target_os = "macos")]
use super::{should_restore_main_window, FULLSCREEN_HIDE_PENDING};
#[cfg(target_os = "macos")]
use std::sync::atomic::Ordering;
use std::sync::Mutex;
use url::Url;

#[cfg(target_os = "macos")]
#[test]
fn pending_fullscreen_hide_forces_reopen_restore() {
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
FULLSCREEN_HIDE_PENDING.store(true, Ordering::Relaxed);
assert!(should_restore_main_window(true, false));
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
FULLSCREEN_HIDE_PENDING.store(false, Ordering::Relaxed);
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
}

#[test]
fn allows_only_the_daemon_origin_in_the_main_window() {
let origin = Url::parse("http://127.0.0.1:49152/").expect("origin");
Expand Down
Loading