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,15 +11,14 @@ 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
let info_ = info.clone();
tauri::async_runtime::spawn(async move { tauri::async_runtime::spawn(async move {
if let Err(err) = clash::put_clash_profile(&info_).await { if let Err(err) = clash::put_clash_profile(&info_).await {
log::error!("failed to put config for `{}`", err); log::error!("failed to put config for `{}`", err);
}; };
}); });
});
// resolve the verge config - enable system proxy // resolve the verge config - enable system proxy
let mut original: Option<sysopt::SysProxyConfig> = None; let mut original: Option<sysopt::SysProxyConfig> = None;

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()));
tauri::async_runtime::spawn(async move {
let commands = warp::path!("commands" / "visible").map(move || { let commands = warp::path!("commands" / "visible").map(move || {
window.show().unwrap(); let win = window.lock().unwrap();
window.set_focus().unwrap(); win.show().unwrap();
win.set_focus().unwrap();
return format!("ok"); return format!("ok");
}); });
tauri::async_runtime::spawn(async move {
warp::serve(commands) warp::serve(commands)
.bind(([127, 0, 0, 1], SERVER_PORT)) .bind(([127, 0, 0, 1], SERVER_PORT))
.await; .await;