clash-verge/src-tauri/src/main.rs
2021-12-06 10:31:56 +08:00

70 lines
1.8 KiB
Rust

#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
extern crate tauri;
mod clash;
mod sysopt;
use tauri::{CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu};
#[tauri::command]
async fn get_config_data(url: String) -> Result<String, String> {
match clash::fetch_url(&url).await {
Ok(_) => Ok(String::from("success")),
Err(_) => Err(String::from("error")),
}
}
fn main() -> std::io::Result<()> {
clash::run_clash_bin(&clash::get_config_dir().to_str().unwrap());
let app = tauri::Builder::default()
.system_tray(
SystemTray::new().with_menu(
SystemTrayMenu::new()
.add_item(CustomMenuItem::new("event_show", "Show"))
.add_item(CustomMenuItem::new("event_quit", "Quit")),
),
)
.on_system_tray_event(move |app, event| match event {
SystemTrayEvent::LeftClick { .. } => {
let window = app.get_window("main").unwrap();
window.show().unwrap();
window.set_focus().unwrap();
}
SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
"event_show" => {
let window = app.get_window("main").unwrap();
window.show().unwrap();
window.set_focus().unwrap();
}
"event_quit" => {
app.exit(0);
}
_ => {}
},
_ => {}
})
.invoke_handler(tauri::generate_handler![get_config_data])
.build(tauri::generate_context!())
.expect("error while running tauri application");
app.run(|app_handle, e| match e {
tauri::Event::CloseRequested { label, api, .. } => {
let app_handle = app_handle.clone();
api.prevent_close();
app_handle.get_window(&label).unwrap().hide().unwrap();
}
tauri::Event::ExitRequested { api, .. } => {
api.prevent_exit();
}
_ => {}
});
Ok(())
}