AstrBot-Desktop/src-tauri/src/lib.rs

81 行
2.4 KiB
Rust

pub mod database;
pub mod diagnostics;
pub mod file_transfer;
pub mod networking;
pub mod secure_store;
pub mod windows;
use serde::Serialize;
use tauri::{
Manager, WindowEvent,
menu::{Menu, MenuItem},
tray::TrayIconBuilder,
};
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct BootstrapStatus {
app_version: &'static str,
platform: &'static str,
rust_core: &'static str,
protocol_version: &'static str,
}
#[tauri::command]
fn bootstrap_status() -> BootstrapStatus {
BootstrapStatus {
app_version: env!("CARGO_PKG_VERSION"),
platform: "windows",
rust_core: "ready",
protocol_version: desktop_protocol::PROTOCOL_VERSION,
}
}
pub fn run() {
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
)
.with_target(false)
.compact()
.init();
tauri::Builder::default()
.plugin(tauri_plugin_single_instance::init(|app, _, _| {
if let Some(window) = app.get_webview_window("main") {
let _ = window.show();
let _ = window.set_focus();
}
}))
.setup(|app| {
let show = MenuItem::with_id(app, "show", "Show AstrBot Desktop", true, None::<&str>)?;
let quit = MenuItem::with_id(app, "quit", "Quit", true, None::<&str>)?;
let menu = Menu::with_items(app, &[&show, &quit])?;
TrayIconBuilder::with_id("main")
.menu(&menu)
.on_menu_event(|app, event| match event.id.as_ref() {
"show" => {
if let Some(window) = app.get_webview_window("main") {
let _ = window.show();
let _ = window.set_focus();
}
}
"quit" => app.exit(0),
_ => {}
})
.build(app)?;
Ok(())
})
.on_window_event(|window, event| {
if let WindowEvent::CloseRequested { api, .. } = event {
api.prevent_close();
let _ = window.hide();
}
})
.invoke_handler(tauri::generate_handler![bootstrap_status])
.run(tauri::generate_context!())
.expect("failed to run AstrBot Desktop");
}