Skip to main content

agent_hub_runtime/desktop/
lifecycle.rs

1//! Shared window restoration and application shutdown lifecycle.
2
3use std::sync::atomic::{AtomicBool, Ordering};
4use std::sync::Arc;
5use tauri::Manager;
6
7use crate::{core, window_layout};
8
9/// Shared flag: when true, CloseRequested should NOT be prevented.
10pub static QUITTING: AtomicBool = AtomicBool::new(false);
11
12pub(super) fn restore_main_window(app: &tauri::AppHandle) {
13    let app_for_main = app.clone();
14    if let Err(err) = app.run_on_main_thread(move || {
15        #[cfg(target_os = "macos")]
16        {
17            if let Err(err) = app_for_main.set_dock_visibility(true) {
18                log::error!("Failed to show Dock icon on macOS: {err}");
19            }
20            if let Err(err) = app_for_main.set_activation_policy(tauri::ActivationPolicy::Regular) {
21                log::error!("Failed to set activation policy to Regular on macOS: {err}");
22            }
23            if let Err(err) = app_for_main.show() {
24                log::error!("Failed to show app on macOS: {err}");
25            }
26        }
27
28        if let Some(w) = app_for_main.get_window("main") {
29            if let Err(err) = w.show() {
30                log::error!("Failed to show main window: {err}");
31            }
32            if let Err(err) = w.unminimize() {
33                log::error!("Failed to unminimize main window: {err}");
34            }
35            if let Err(err) = w.set_focus() {
36                log::error!("Failed to focus main window: {err}");
37            }
38        } else {
39            log::error!("Main window not found while restoring from tray");
40        }
41    }) {
42        log::error!("Failed to schedule restore_main_window on main thread: {err}");
43    }
44}
45
46/// Quit the application cleanly: destroy the main window, then exit.
47///
48/// Do NOT signal our process group here (e.g. `kill(-pgid, SIGTERM)`).
49/// On Linux the app inherits the launcher's pgid — that may be the user's
50/// desktop session (issue #47, tearing down GNOME) or the developer's shell
51/// (terminating the parent terminal and its sibling jobs). Either is
52/// catastrophic and not worth the convenience of auto-cleaning a stray
53/// `tauri dev` vite process.
54pub fn quit_app(app: &tauri::AppHandle) {
55    teardown_before_exit(app);
56    app.exit(0);
57}
58
59/// Restart the process, running the same teardown `quit_app` does.
60///
61/// Called after an in-app update has replaced the bundle on disk. Reuses
62/// `teardown_before_exit` rather than restarting outright: skipping it would
63/// drop the exit-time local backup commit, so a user who restarts instead of
64/// quitting would silently lose it.
65///
66/// `request_restart`, not `restart`: on the main thread the latter spawns the
67/// replacement process and exits without ever emitting `RunEvent::Exit`, and
68/// `tauri-plugin-single-instance` removes its socket only on that event. The
69/// old process usually exits before the new one gets far enough to connect —
70/// leaving a stale socket file the next launch cleans up on `ConnectionRefused`
71/// — but nothing enforces that ordering, and losing the race means the new
72/// instance sees a live singleton and exits immediately, taking the app down
73/// instead of restarting it. Going through the exit event closes the window.
74pub fn restart_app(app: &tauri::AppHandle) {
75    teardown_before_exit(app);
76    app.request_restart();
77}
78
79fn teardown_before_exit(app: &tauri::AppHandle) {
80    QUITTING.store(true, Ordering::SeqCst);
81    if let Some(terminals) = app.try_state::<Arc<core::terminal::TerminalManager>>() {
82        terminals.close_all();
83    }
84    if let Some(w) = app.get_window("main") {
85        window_layout::save(&w);
86        if let Err(err) = w.destroy() {
87            log::error!("Failed to destroy main window while quitting: {err}");
88        }
89    }
90    for (label, window) in app.windows() {
91        if label != "main" {
92            if let Err(error) = window.destroy() {
93                log::warn!("Cannot close workspace: {error}");
94            }
95        }
96    }
97    if let Some(preview) = app.try_state::<Arc<core::preview::Preview>>() {
98        preview.clear();
99    }
100    if let Some(previews) = app.try_state::<Arc<core::file_preview::FilePreviews>>() {
101        if let Err(error) = previews.clear() {
102            log::warn!("Cannot clear preview copies: {error}");
103        }
104    }
105    // 退出前 auto-backup (§3.4): local commit only, fail-fast on a busy lock,
106    // after the window is gone so quitting feels instant.
107    #[cfg(feature = "cloud-sync")]
108    if let Some(store) = app.try_state::<Arc<core::skill_store::SkillStore>>() {
109        core::auto_backup::commit_on_exit(&store);
110    }
111}