feat: enhance clash caller & support more commands

This commit is contained in:
GyDi
2021-12-14 18:37:03 +08:00
parent 4719649bf4
commit 881a95520a
6 changed files with 121 additions and 32 deletions

View File

@@ -1,17 +1,31 @@
use tauri::api::process::kill_children;
use crate::utils::{clash, import};
use crate::{
events::{emit::ClashInfoPayload, state::ClashInfoState},
utils::{clash, import},
};
use tauri::{api::process::kill_children, AppHandle, State};
#[tauri::command]
pub fn cmd_restart_sidebar() {
pub fn restart_sidebar(app_handle: AppHandle, clash_info: State<'_, ClashInfoState>) {
kill_children();
clash::run_clash_bin();
let payload = clash::run_clash_bin(&app_handle);
if let Ok(mut arc) = clash_info.0.lock() {
*arc = payload;
}
}
#[tauri::command]
pub async fn cmd_import_profile(url: String) -> Result<String, String> {
pub async fn import_profile(url: String) -> Result<String, String> {
match import::import_profile(&url).await {
Ok(_) => Ok(String::from("success")),
Err(_) => Err(String::from("error")),
}
}
#[tauri::command]
pub fn get_clash_info(clash_info: State<'_, ClashInfoState>) -> Option<ClashInfoPayload> {
match clash_info.0.lock() {
Ok(arc) => Some(arc.clone()),
_ => None,
}
}