diff --git a/apps/desktop/src-tauri/Cargo.toml b/apps/desktop/src-tauri/Cargo.toml index 6a7b76f..285d5f5 100644 --- a/apps/desktop/src-tauri/Cargo.toml +++ b/apps/desktop/src-tauri/Cargo.toml @@ -18,7 +18,7 @@ crate-type = ["staticlib", "cdylib", "rlib"] tauri-build = { version = "2.4.1", features = [] } [dependencies] -tauri = { version = "2.8.5", features = ["wry", "devtools"] } +tauri = { version = "2.8.5", features = ["wry", "devtools", "tray-icon"] } tauri-plugin-opener = "2.5.0" tauri-plugin-store = "2.4.0" tauri-plugin-updater = "2.9.0" diff --git a/apps/desktop/src-tauri/src/lib.rs b/apps/desktop/src-tauri/src/lib.rs index cf0ad5e..6f18d3a 100644 --- a/apps/desktop/src-tauri/src/lib.rs +++ b/apps/desktop/src-tauri/src/lib.rs @@ -3,8 +3,14 @@ mod agent; mod rustdesk; use agent::{collect_inventory_plain, collect_profile, AgentRuntime, MachineProfile}; -use tauri::Emitter; +use tauri::{Emitter, Manager, WindowEvent}; use tauri_plugin_store::Builder as StorePluginBuilder; +#[cfg(target_os = "windows")] +use std::process::Command; +#[cfg(target_os = "windows")] +use tauri::menu::{MenuBuilder, MenuItemBuilder}; +#[cfg(target_os = "windows")] +use tauri::tray::TrayIconBuilder; #[derive(Debug, serde::Serialize)] #[serde(rename_all = "camelCase")] @@ -102,6 +108,20 @@ pub fn run() { .plugin(StorePluginBuilder::default().build()) .plugin(tauri_plugin_updater::Builder::new().build()) .plugin(tauri_plugin_process::init()) + .on_window_event(|window, event| { + if let WindowEvent::CloseRequested { api, .. } = event { + api.prevent_close(); + let _ = window.hide(); + } + }) + .setup(|app| { + #[cfg(target_os = "windows")] + { + setup_raven_autostart(); + setup_tray(app)?; + } + Ok(()) + }) .invoke_handler(tauri::generate_handler![ collect_machine_profile, collect_machine_inventory, @@ -113,3 +133,63 @@ pub fn run() { .run(tauri::generate_context!()) .expect("error while running tauri application"); } + +#[cfg(target_os = "windows")] +fn setup_raven_autostart() { + if let Ok(exe) = std::env::current_exe() { + let path = exe.display().to_string(); + let quoted = format!("\"{}\"", path); + let _ = Command::new("cmd") + .args([ + "/C", + "reg", + "add", + r"HKCU\Software\Microsoft\Windows\CurrentVersion\Run", + "/v", + "Raven", + "/d", + "ed, + "/f", + ]) + .status(); + } +} + +#[cfg(target_os = "windows")] +fn setup_tray(app: &tauri::AppHandle) -> tauri::Result<()> { + let show_item = MenuItemBuilder::with_id("show", "Mostrar").build(app)?; + let quit_item = MenuItemBuilder::with_id("quit", "Sair").build(app)?; + let menu = MenuBuilder::new(app).items(&[&show_item, &quit_item])?.build()?; + + let mut builder = TrayIconBuilder::new() + .menu(&menu) + .on_menu_event(|tray, event| { + match event.id().as_ref() { + "show" => { + if let Some(win) = tray.app_handle().get_webview_window("main") { + let _ = win.show(); + let _ = win.set_focus(); + } + } + "quit" => { + tray.app_handle().exit(0); + } + _ => {} + } + }) + .on_tray_icon_event(|tray, event| { + if let tauri::tray::TrayIconEvent::DoubleClick { .. } = event { + if let Some(win) = tray.app_handle().get_webview_window("main") { + let _ = win.show(); + let _ = win.set_focus(); + } + } + }); + + if let Some(icon) = app.default_window_icon() { + builder = builder.icon(icon.clone()); + } + + builder.build(app)?; + Ok(()) +}