- New icon (voice bars in a ring on the accent gradient; three thicker bars at 32 px and below) for the program file, window, taskbar, installer and a tray icon of its own. - 앱 settings screen with app.closeButton: "quit" (default, as before) or "tray", read each time X is pressed (no restart). Tray mode only hides the window; the bot and services keep running. The tray icon is always there: a left click shows the window, its menu has 열기 and 완전 종료 (the same stop-then-exit as closing). - tauri-plugin-single-instance: starting Jarvis again while it runs shows the running one's window instead of a second app driving the same services. - Version 0.1.8. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
176 lines
7.9 KiB
Rust
176 lines
7.9 KiB
Rust
//! 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. Quitting first stops what the app started; if that
|
|
//! cannot be confirmed the window stays open and says so, and quitting again quits. The close button
|
|
//! quits, or hides the window to the tray when `app.closeButton` is "tray"; the tray icon opens it
|
|
//! again or quits. Starting Jarvis again while it runs shows the running one's window.
|
|
//!
|
|
//! 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::menu::{Menu, MenuItem};
|
|
use tauri::tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent};
|
|
use tauri::{AppHandle, 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<AdminApi>,
|
|
phase: AtomicU8,
|
|
}
|
|
|
|
#[tauri::command]
|
|
async fn admin(app: tauri::AppHandle, state: tauri::State<'_, Api>, command: String, args: Option<Value>) -> Result<Value, String> {
|
|
let out = state.api.call(&command, args.unwrap_or(Value::Null)).await?;
|
|
// An update's installer was started (what ran is stopped): quit so it can replace the app.
|
|
if out["exit"] == Value::Bool(true) {
|
|
state.phase.store(CLOSING, Ordering::SeqCst);
|
|
app.exit(0);
|
|
}
|
|
Ok(out)
|
|
}
|
|
|
|
/// Bring the window back: from the tray, or when Jarvis is started again.
|
|
fn show_window(app: &AppHandle) {
|
|
if let Some(w) = app.get_webview_window("main") {
|
|
let _ = w.unminimize();
|
|
let _ = w.show();
|
|
let _ = w.set_focus();
|
|
}
|
|
}
|
|
|
|
/// Quit (the close button set to quit, or the tray's "완전 종료"): stop what the app started, then
|
|
/// exit; the window shows while that runs. If the stop fails the window stays with the reason, and
|
|
/// quitting again exits anyway.
|
|
fn quit(app: &AppHandle) {
|
|
let state = app.state::<Api>();
|
|
match state.phase.load(Ordering::SeqCst) {
|
|
CLOSE_FAILED => {
|
|
jarvis_app::warn_log!("app", "quitting again: quitting although the stop was not confirmed");
|
|
app.exit(0);
|
|
}
|
|
CLOSING => {}
|
|
_ => {
|
|
state.phase.store(CLOSING, Ordering::SeqCst);
|
|
show_window(app);
|
|
let _ = app.emit("closing", ());
|
|
let (handle, api) = (app.clone(), state.api.clone());
|
|
tauri::async_runtime::spawn(async move {
|
|
jarvis_app::info_log!("app", "quitting: stopping what the app started");
|
|
match api.shutdown().await {
|
|
Ok(()) => handle.exit(0),
|
|
Err(e) => {
|
|
handle.state::<Api>().phase.store(CLOSE_FAILED, Ordering::SeqCst);
|
|
let _ = handle.emit("close-failed", e);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
fn arg(name: &str) -> Option<String> {
|
|
let args: Vec<String> = 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::<Vec<_>>()) {
|
|
std::process::exit(code);
|
|
}
|
|
// Not while an installer installs, updates or removes Jarvis (admin.spec.md, "Installer").
|
|
if jarvis_app::setup_lock::held() {
|
|
jarvis_app::setup_lock::tell();
|
|
std::process::exit(3);
|
|
}
|
|
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()
|
|
// First, so a second start only shows this one's window and exits.
|
|
.plugin(tauri_plugin_single_instance::init(|app, _, _| show_window(app)))
|
|
.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()?;
|
|
let open = MenuItem::with_id(app, "open", "열기", true, None::<&str>)?;
|
|
let quit_item = MenuItem::with_id(app, "quit", "완전 종료", true, None::<&str>)?;
|
|
TrayIconBuilder::with_id("main")
|
|
.tooltip("Jarvis")
|
|
.menu(&Menu::with_items(app, &[&open, &quit_item])?)
|
|
.show_menu_on_left_click(false)
|
|
.on_menu_event(|app, e| match e.id.as_ref() {
|
|
"open" => show_window(app),
|
|
"quit" => quit(app),
|
|
_ => {}
|
|
})
|
|
.on_tray_icon_event(|tray, e| {
|
|
if let TrayIconEvent::Click { button: MouseButton::Left, button_state: MouseButtonState::Up, .. } = e {
|
|
show_window(tray.app_handle());
|
|
}
|
|
})
|
|
.icon(tauri::image::Image::from_bytes(include_bytes!("../icons/tray.png"))?)
|
|
.build(app)?;
|
|
Ok(())
|
|
}
|
|
})
|
|
.on_window_event(|window, event| {
|
|
let WindowEvent::CloseRequested { api: close, .. } = event else { return };
|
|
let state = window.state::<Api>();
|
|
match state.phase.load(Ordering::SeqCst) {
|
|
// After a failed stop the window closes, and with it the app.
|
|
CLOSE_FAILED => jarvis_app::warn_log!("app", "window closed again: quitting although the stop was not confirmed"),
|
|
CLOSING => close.prevent_close(),
|
|
_ if state.api.close_to_tray() => {
|
|
close.prevent_close();
|
|
let _ = window.hide();
|
|
jarvis_app::info_log!("app", "window hidden to the tray");
|
|
}
|
|
_ => {
|
|
close.prevent_close();
|
|
quit(window.app_handle());
|
|
}
|
|
}
|
|
})
|
|
.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::<Api>().phase.load(Ordering::SeqCst) == OPEN => {
|
|
let api = handle.state::<Api>().api.clone();
|
|
let _ = tauri::async_runtime::block_on(api.shutdown());
|
|
}
|
|
_ => {}
|
|
}
|
|
});
|
|
}
|