fix: app now hides on closing

This commit is contained in:
Leafd 2025-10-10 15:36:20 -04:00
parent dab9a807a5
commit a266acc5d4
No known key found for this signature in database
GPG key ID: D44AE7A3699406BE
3 changed files with 31 additions and 5 deletions

View file

@ -71,6 +71,9 @@ pub fn run() {
push_log("info", "backend", format!("Single instance detected. Args: {:?}, CWD: {}", args, cwd));
if let Some(window) = app.get_webview_window("main") {
#[cfg(target_os = "macos")]
let _ = app.set_activation_policy(tauri::ActivationPolicy::Regular);
let _ = window.show();
let _ = window.set_focus();
push_log("info", "backend", "Brought existing window to front".to_string());
@ -306,22 +309,23 @@ pub fn run() {
push_log("info", "backend", "🪟 Window close requested - hiding to tray".to_string());
api.prevent_close();
// Use the app handle to get the window and hide it asynchronously
// This prevents potential re-entrancy issues
let app_clone = app_handle.clone();
std::thread::spawn(move || {
if let Some(win) = app_clone.get_webview_window("main") {
let _ = win.hide();
push_log("info", "backend", "✅ Window hidden to tray".to_string());
#[cfg(target_os = "macos")]
{
let _ = app_clone.set_activation_policy(tauri::ActivationPolicy::Accessory);
push_log("info", "backend", "✅ App removed from Dock".to_string());
}
}
});
}
WindowEvent::Resized(_) => {
// Handle resize events gracefully - no action needed
// This prevents potential crashes on macOS with transparent windows
}
WindowEvent::Moved(_) => {
// Handle move events gracefully
}
_ => {}
}

View file

@ -54,7 +54,12 @@ pub fn setup_tray(app: &AppHandle) -> Result<(), Box<dyn std::error::Error>> {
if let Some(window) = app.get_webview_window("main") {
if window.is_visible().unwrap_or(false) {
let _ = window.hide();
#[cfg(target_os = "macos")]
let _ = app.set_activation_policy(tauri::ActivationPolicy::Accessory);
} else {
#[cfg(target_os = "macos")]
let _ = app.set_activation_policy(tauri::ActivationPolicy::Regular);
let _ = window.show();
let _ = window.set_focus();
}
@ -65,6 +70,10 @@ pub fn setup_tray(app: &AppHandle) -> Result<(), Box<dyn std::error::Error>> {
..
} => {
let app = tray.app_handle();
#[cfg(target_os = "macos")]
let _ = app.set_activation_policy(tauri::ActivationPolicy::Regular);
if let Some(window) = app.get_webview_window("main") {
let _ = window.show();
let _ = window.set_focus();

View file

@ -2,6 +2,9 @@ use tauri::Manager;
#[tauri::command]
pub async fn show_window(app: tauri::AppHandle) -> Result<(), String> {
#[cfg(target_os = "macos")]
let _ = app.set_activation_policy(tauri::ActivationPolicy::Regular);
if let Some(window) = app.get_webview_window("main") {
window
.show()
@ -20,6 +23,10 @@ pub async fn hide_window(app: tauri::AppHandle) -> Result<(), String> {
.hide()
.map_err(|e| format!("Failed to hide window: {}", e))?;
}
#[cfg(target_os = "macos")]
let _ = app.set_activation_policy(tauri::ActivationPolicy::Accessory);
Ok(())
}
@ -30,7 +37,13 @@ pub async fn toggle_window(app: tauri::AppHandle) -> Result<(), String> {
window
.hide()
.map_err(|e| format!("Failed to hide window: {}", e))?;
#[cfg(target_os = "macos")]
let _ = app.set_activation_policy(tauri::ActivationPolicy::Accessory);
} else {
#[cfg(target_os = "macos")]
let _ = app.set_activation_policy(tauri::ActivationPolicy::Regular);
window
.show()
.map_err(|e| format!("Failed to show window: {}", e))?;