agent_hub_runtime/desktop/
lifecycle.rs1use std::sync::atomic::{AtomicBool, Ordering};
4use std::sync::Arc;
5use tauri::Manager;
6
7use crate::{core, window_layout};
8
9pub 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
46pub fn quit_app(app: &tauri::AppHandle) {
55 teardown_before_exit(app);
56 app.exit(0);
57}
58
59pub 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 #[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}