2021-12-04 09:31:26 +03:00
|
|
|
#![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<()> {
|
2021-12-06 05:31:17 +03:00
|
|
|
clash::run_clash_bin(&clash::get_config_dir().to_str().unwrap());
|
2021-12-04 09:31:26 +03:00
|
|
|
|
|
|
|
let app = tauri::Builder::default()
|
|
|
|
.system_tray(
|
2021-12-06 05:31:17 +03:00
|
|
|
SystemTray::new().with_menu(
|
|
|
|
SystemTrayMenu::new()
|
|
|
|
.add_item(CustomMenuItem::new("event_show", "Show"))
|
|
|
|
.add_item(CustomMenuItem::new("event_quit", "Quit")),
|
|
|
|
),
|
2021-12-04 09:31:26 +03:00
|
|
|
)
|
|
|
|
.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() {
|
2021-12-06 05:31:17 +03:00
|
|
|
"event_show" => {
|
|
|
|
let window = app.get_window("main").unwrap();
|
|
|
|
window.show().unwrap();
|
|
|
|
window.set_focus().unwrap();
|
|
|
|
}
|
|
|
|
"event_quit" => {
|
2021-12-04 09:31:26 +03:00
|
|
|
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(())
|
|
|
|
}
|