//! Jarvis management app: one Tauri 2 window over the app core (`app/src/admin.spec.md`, "Window //! and Windows build"). The screens (`app/ui`) call `invoke("admin", {command, args})`, passed to the //! same `AdminApi` the development server uses. Closing the window first stops what the app started; //! if that cannot be confirmed the window stays open and says so, and closing it again quits. //! //! jarvis [--location FILE] //! jarvis --display-driver install|uninstall --result FILE (elevated, started by the app) #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] use std::path::PathBuf; use std::sync::atomic::{AtomicU8, Ordering}; use std::sync::Arc; use jarvis_app::admin_api::AdminApi; use serde_json::Value; use tauri::{Emitter, Manager, RunEvent, WebviewUrl, WebviewWindowBuilder, WindowEvent}; /// Where closing stands: open, closing (stopping what runs), or the stop could not be confirmed. const OPEN: u8 = 0; const CLOSING: u8 = 1; const CLOSE_FAILED: u8 = 2; struct Api { api: Arc, phase: AtomicU8, } #[tauri::command] async fn admin(state: tauri::State<'_, Api>, command: String, args: Option) -> Result { state.api.call(&command, args.unwrap_or(Value::Null)).await } fn arg(name: &str) -> Option { let args: Vec = std::env::args().collect(); args.iter().position(|a| a == name).and_then(|i| args.get(i + 1).cloned()) } fn main() { // The elevated half of installing or removing the virtual monitor's driver: no window. if let Some(code) = jarvis_app::vdisplay::elevated_main(&std::env::args().collect::>()) { std::process::exit(code); } let location = arg("--location").map(PathBuf::from).or_else(jarvis_app::datadir::default_location_file) .expect("no location file path: pass --location"); // Log files go to the data folder once there is one; until then memory only. let logs = jarvis_app::datadir::DataDir::locate(&location).map(|d| d.logs()); let hub = jarvis_app::logs::LogHub::new(logs.as_deref()); jarvis_app::logs::install(hub.clone()); let api = Arc::new(AdminApi::new(&location, hub)); // The window's browser data: in the data folder, or a scratch folder in the system temp folder // before there is one. WebView2 holds that folder until this process has ended, so scratch // folders of earlier runs are removed here, at the next start. jarvis_app::datadir::remove_scratch_webview(&std::env::temp_dir(), None); let (webview_data, _) = jarvis_app::datadir::webview_data_dir(&location); let app = tauri::Builder::default() .manage(Api { api, phase: AtomicU8::new(OPEN) }) .invoke_handler(tauri::generate_handler![admin]) .setup({ move |app| { WebviewWindowBuilder::new(app, "main", WebviewUrl::App("index.html".into())) .title("Jarvis 관리") .inner_size(1200.0, 820.0) .min_inner_size(900.0, 600.0) .data_directory(webview_data) .build()?; Ok(()) } }) .on_window_event(|window, event| { let WindowEvent::CloseRequested { api: close, .. } = event else { return }; let state = window.state::(); match state.phase.load(Ordering::SeqCst) { CLOSE_FAILED => { jarvis_app::warn_log!("app", "window closed again: quitting although the stop was not confirmed"); } CLOSING => close.prevent_close(), _ => { close.prevent_close(); state.phase.store(CLOSING, Ordering::SeqCst); let _ = window.emit("closing", ()); let (handle, api) = (window.app_handle().clone(), state.api.clone()); tauri::async_runtime::spawn(async move { jarvis_app::info_log!("app", "window closing: stopping what the app started"); match api.shutdown().await { Ok(()) => handle.exit(0), Err(e) => { handle.state::().phase.store(CLOSE_FAILED, Ordering::SeqCst); let _ = handle.emit("close-failed", e); } } }); } } }) .build(tauri::generate_context!()) .expect("the window did not start"); app.run(move |handle, event| { match event { // Quitting another way (the system shutting down) still stops what runs. RunEvent::ExitRequested { .. } if handle.state::().phase.load(Ordering::SeqCst) == OPEN => { let api = handle.state::().api.clone(); let _ = tauri::async_runtime::block_on(api.shutdown()); } _ => {} } }); }