Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(tauri): expose whether closing the main window only hides it

thinhlpgfd9f0f198f1c
diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs
index 9d3454079..5bcb3c38a 100644
--- a/src-tauri/src/lib.rs
+++ b/src-tauri/src/lib.rs
@@ -142,6 +142,7 @@ macro_rules! invoke_commands_with_extras {
core::downloads::commands::pause_download_task,
// App lifecycle
confirm_exit,
+ main_window_hides_on_close,
// Theme
core::setup::get_system_theme,
core::setup::set_gtk_prefer_dark,
@@ -196,6 +197,35 @@ fn is_proxy_server_running<R: tauri::Runtime>(app: &tauri::AppHandle<R>) -> bool
.unwrap_or(false)
}
+#[cfg(not(feature = "cli"))]
+/// Whether closing the main window only hides it, keeping the app running:
+/// always on macOS (quit is Cmd+Q / dock / tray), and on Windows/Linux while
+/// the Local API Server runs with "keep running in background" on. Never once
+/// a shutdown has started. The window-event handler below and the web app's
+/// close handler both ask this, so they agree on whether a close is a quit.
+fn hides_main_window_on_close<R: tauri::Runtime>(app: &tauri::AppHandle<R>) -> bool {
+ use std::sync::atomic::Ordering;
+ if SHUTTING_DOWN.load(Ordering::SeqCst) {
+ return false;
+ }
+ #[cfg(target_os = "macos")]
+ {
+ let _ = app;
+ true
+ }
+ #[cfg(not(target_os = "macos"))]
+ {
+ is_proxy_server_running(app)
+ && core::server::commands::SERVER_RUN_IN_BACKGROUND.load(Ordering::SeqCst)
+ }
+}
+
+#[cfg(not(feature = "cli"))]
+#[tauri::command]
+fn main_window_hides_on_close<R: tauri::Runtime>(app_handle: tauri::AppHandle<R>) -> bool {
+ hides_main_window_on_close(&app_handle)
+}
+
#[cfg(not(feature = "cli"))]
fn reemit_busy_if_any<R: tauri::Runtime>(app_handle: &tauri::AppHandle<R>) {
let busy = BUSY_MODELS.lock().map(|g| g.clone()).unwrap_or_default();
@@ -514,35 +544,18 @@ pub fn run() {
..
} = &event
{
- if label == "main" && !SHUTTING_DOWN.load(Ordering::SeqCst) {
- // macOS: closing the window hides it; the app (and background
- // services) keep running. Quit happens via Cmd+Q / dock / tray,
- // which routes through RunEvent::ExitRequested.
- #[cfg(target_os = "macos")]
- {
- api.prevent_close();
- if let Some(window) = app.get_webview_window("main") {
- let _ = window.hide();
- }
- return;
- }
- // Windows/Linux: hide to tray only while the Local API Server is
- // running and the user opted into keeping it alive in the
- // background; otherwise fall through to the normal quit-on-close.
- // The llamacpp engine is not a reason to keep the app resident
- // (normal chat usage keeps it alive), so it gets torn down via
- // the ExitRequested path on quit.
- #[cfg(not(target_os = "macos"))]
- if is_proxy_server_running(app)
- && core::server::commands::SERVER_RUN_IN_BACKGROUND
- .load(Ordering::SeqCst)
- {
- api.prevent_close();
- if let Some(window) = app.get_webview_window("main") {
- let _ = window.hide();
- }
- return;
+ // Closing the main window hides it when `hides_main_window_on_close`
+ // says so (macOS always; Windows/Linux only while the Local API
+ // Server runs in the background). Otherwise fall through to the
+ // normal quit-on-close. The llamacpp engine is not a reason to keep
+ // the app resident (normal chat usage keeps it alive), so it gets
+ // torn down via the ExitRequested path on quit.
+ if label == "main" && hides_main_window_on_close(app) {
+ api.prevent_close();
+ if let Some(window) = app.get_webview_window("main") {
+ let _ = window.hide();
}
+ return;
}
}
#[cfg(target_os = "macos")]