feat: compatible with macos(wip)

This commit is contained in:
GyDi 2021-12-29 18:49:38 +08:00
parent 26ef4c9961
commit 2b84bbf3a8
4 changed files with 22 additions and 22 deletions

View File

@ -17,7 +17,7 @@ use tauri::{api::process::kill_children, AppHandle, State};
#[tauri::command] #[tauri::command]
pub fn restart_sidecar(app_handle: AppHandle, clash_info: State<'_, ClashInfoState>) { pub fn restart_sidecar(app_handle: AppHandle, clash_info: State<'_, ClashInfoState>) {
kill_children(); kill_children();
let payload = run_clash_bin(&app_handle); let payload = run_clash_bin(&app_handle, |_| {});
if let Ok(mut arc) = clash_info.0.lock() { if let Ok(mut arc) = clash_info.0.lock() {
*arc = payload; *arc = payload;

View File

@ -16,7 +16,7 @@ use tauri::{
}; };
/// Run the clash bin /// Run the clash bin
pub fn run_clash_bin(app_handle: &AppHandle) -> ClashInfoPayload { pub fn run_clash_bin(app_handle: &AppHandle, cb: fn(info: ClashInfoPayload)) -> ClashInfoPayload {
let app_dir = app_home_dir(); let app_dir = app_home_dir();
let app_dir = app_dir.as_os_str().to_str().unwrap(); let app_dir = app_dir.as_os_str().to_str().unwrap();
@ -38,12 +38,13 @@ pub fn run_clash_bin(app_handle: &AppHandle) -> ClashInfoPayload {
Ok((mut rx, _)) => { Ok((mut rx, _)) => {
log::info!("Successfully execute clash sidecar"); log::info!("Successfully execute clash sidecar");
payload.controller = Some(read_clash_controller()); payload.controller = Some(read_clash_controller());
cb(payload.clone()); // callback when run sidecar successfully
tauri::async_runtime::spawn(async move { tauri::async_runtime::spawn(async move {
while let Some(event) = rx.recv().await { while let Some(event) = rx.recv().await {
match event { match event {
CommandEvent::Stdout(line) => log::info!("{}", line), CommandEvent::Stdout(line) => log::info!("[stdout]: {}", line),
CommandEvent::Stderr(err) => log::error!("{}", err), CommandEvent::Stderr(err) => log::error!("[stderr]: {}", err),
_ => {} _ => {}
} }
} }
@ -86,9 +87,7 @@ pub async fn put_clash_profile(payload: &ClashInfoPayload) -> Result<(), String>
{ {
let file_name = match profile.file { let file_name = match profile.file {
Some(file_name) => file_name.clone(), Some(file_name) => file_name.clone(),
None => { None => return Err(format!("profile item should have `file` field")),
return Err(format!("profile item should have `file` field"));
}
}; };
let file_path = app_home_dir().join("profiles").join(file_name); let file_path = app_home_dir().join("profiles").join(file_name);

View File

@ -11,14 +11,13 @@ pub fn resolve_setup(app: &App) {
init::init_app(app.package_info()); init::init_app(app.package_info());
// run clash sidecar // run clash sidecar
let info = clash::run_clash_bin(&app.handle()); let info = clash::run_clash_bin(&app.handle(), |info_| {
// update the profile
// update the profile tauri::async_runtime::spawn(async move {
let info_ = info.clone(); if let Err(err) = clash::put_clash_profile(&info_).await {
tauri::async_runtime::spawn(async move { log::error!("failed to put config for `{}`", err);
if let Err(err) = clash::put_clash_profile(&info_).await { };
log::error!("failed to put config for `{}`", err); });
};
}); });
// resolve the verge config - enable system proxy // resolve the verge config - enable system proxy

View File

@ -1,6 +1,7 @@
extern crate warp; extern crate warp;
use port_scanner::local_port_available; use port_scanner::local_port_available;
use std::sync::{Arc, Mutex};
use tauri::{AppHandle, Manager}; use tauri::{AppHandle, Manager};
use warp::Filter; use warp::Filter;
@ -22,15 +23,16 @@ pub fn check_singleton() -> Result<(), ()> {
/// The embed server only be used to implement singleton process /// The embed server only be used to implement singleton process
/// maybe it can be used as pac server later /// maybe it can be used as pac server later
pub fn embed_server(app: &AppHandle) { pub fn embed_server(app: &AppHandle) {
let window = app.get_window("main").unwrap(); let window = Arc::new(Mutex::new(app.get_window("main").unwrap()));
let commands = warp::path!("commands" / "visible").map(move || {
window.show().unwrap();
window.set_focus().unwrap();
return format!("ok");
});
tauri::async_runtime::spawn(async move { tauri::async_runtime::spawn(async move {
let commands = warp::path!("commands" / "visible").map(move || {
let win = window.lock().unwrap();
win.show().unwrap();
win.set_focus().unwrap();
return format!("ok");
});
warp::serve(commands) warp::serve(commands)
.bind(([127, 0, 0, 1], SERVER_PORT)) .bind(([127, 0, 0, 1], SERVER_PORT))
.await; .await;