2021-12-04 09:31:26 +03:00
|
|
|
#![cfg_attr(
|
|
|
|
all(not(debug_assertions), target_os = "windows"),
|
|
|
|
windows_subsystem = "windows"
|
|
|
|
)]
|
|
|
|
|
|
|
|
extern crate tauri;
|
|
|
|
|
2021-12-08 18:40:52 +03:00
|
|
|
mod cmd;
|
2021-12-12 21:29:48 +03:00
|
|
|
mod config;
|
2021-12-14 13:37:03 +03:00
|
|
|
mod events;
|
2021-12-14 11:07:15 +03:00
|
|
|
mod utils;
|
2021-12-04 09:31:26 +03:00
|
|
|
|
2021-12-14 16:58:29 +03:00
|
|
|
use crate::{events::state::ClashInfoState, utils::clash::put_clash_profile};
|
2021-12-14 13:37:03 +03:00
|
|
|
use std::sync::{Arc, Mutex};
|
2021-12-08 18:40:52 +03:00
|
|
|
use tauri::{
|
2021-12-14 13:37:03 +03:00
|
|
|
api, CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem,
|
2021-12-08 18:40:52 +03:00
|
|
|
SystemTraySubmenu,
|
|
|
|
};
|
2021-12-04 09:31:26 +03:00
|
|
|
|
|
|
|
fn main() -> std::io::Result<()> {
|
2021-12-08 18:40:52 +03:00
|
|
|
let sub_menu = SystemTraySubmenu::new(
|
|
|
|
"出站规则",
|
|
|
|
SystemTrayMenu::new()
|
|
|
|
.add_item(CustomMenuItem::new("rway_global", "全局连接"))
|
|
|
|
.add_item(CustomMenuItem::new("rway_rule", "规则连接").selected())
|
|
|
|
.add_item(CustomMenuItem::new("rway_direct", "直接连接")),
|
|
|
|
);
|
|
|
|
let menu = SystemTrayMenu::new()
|
|
|
|
.add_submenu(sub_menu)
|
|
|
|
.add_native_item(SystemTrayMenuItem::Separator)
|
|
|
|
.add_item(CustomMenuItem::new("syste_proxy", "设置为系统代理"))
|
|
|
|
.add_item(CustomMenuItem::new("self_startup", "开机启动").selected())
|
|
|
|
.add_item(CustomMenuItem::new("open_window", "显示应用"))
|
|
|
|
.add_native_item(SystemTrayMenuItem::Separator)
|
|
|
|
.add_item(CustomMenuItem::new("quit", "退出").accelerator("CmdOrControl+Q"));
|
2021-12-04 09:31:26 +03:00
|
|
|
|
|
|
|
let app = tauri::Builder::default()
|
2021-12-08 18:40:52 +03:00
|
|
|
.system_tray(SystemTray::new().with_menu(menu))
|
2021-12-04 09:31:26 +03:00
|
|
|
.on_system_tray_event(move |app, event| match event {
|
|
|
|
SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
|
2021-12-08 18:40:52 +03:00
|
|
|
"open_window" => {
|
2021-12-06 05:31:17 +03:00
|
|
|
let window = app.get_window("main").unwrap();
|
|
|
|
window.show().unwrap();
|
|
|
|
window.set_focus().unwrap();
|
|
|
|
}
|
2021-12-08 18:40:52 +03:00
|
|
|
"quit" => {
|
2021-12-14 13:37:03 +03:00
|
|
|
api::process::kill_children();
|
2021-12-04 09:31:26 +03:00
|
|
|
app.exit(0);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
2021-12-08 18:40:52 +03:00
|
|
|
SystemTrayEvent::LeftClick { .. } => {
|
|
|
|
let window = app.get_window("main").unwrap();
|
|
|
|
window.show().unwrap();
|
|
|
|
window.set_focus().unwrap();
|
|
|
|
}
|
2021-12-04 09:31:26 +03:00
|
|
|
_ => {}
|
|
|
|
})
|
2021-12-08 18:40:52 +03:00
|
|
|
.invoke_handler(tauri::generate_handler![
|
2021-12-14 13:37:03 +03:00
|
|
|
cmd::import_profile,
|
|
|
|
cmd::restart_sidebar,
|
|
|
|
cmd::get_clash_info,
|
2021-12-08 18:40:52 +03:00
|
|
|
])
|
2021-12-04 09:31:26 +03:00
|
|
|
.build(tauri::generate_context!())
|
|
|
|
.expect("error while running tauri application");
|
|
|
|
|
2021-12-12 12:46:16 +03:00
|
|
|
// init app config
|
2021-12-14 11:07:15 +03:00
|
|
|
utils::init::init_app(app.package_info());
|
2021-12-14 13:37:03 +03:00
|
|
|
// run clash sidecar
|
|
|
|
let info = utils::clash::run_clash_bin(&app.handle());
|
2021-12-14 16:58:29 +03:00
|
|
|
// update the profile
|
|
|
|
let info_copy = info.clone();
|
|
|
|
tauri::async_runtime::spawn(async move {
|
|
|
|
match put_clash_profile(&info_copy).await {
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(err) => log::error!("failed to put config for `{}`", err),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2021-12-14 13:37:03 +03:00
|
|
|
app.manage(ClashInfoState(Arc::new(Mutex::new(info))));
|
2021-12-12 12:46:16 +03:00
|
|
|
|
2021-12-04 09:31:26 +03:00
|
|
|
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();
|
|
|
|
}
|
2021-12-14 13:37:03 +03:00
|
|
|
tauri::Event::Exit => {
|
|
|
|
api::process::kill_children();
|
|
|
|
}
|
2021-12-04 09:31:26 +03:00
|
|
|
_ => {}
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|