Skip to content
Snippets Groups Projects
Commit 2b84bbf3 authored by GyDi's avatar GyDi
Browse files

feat: compatible with macos(wip)

parent 26ef4c99
No related branches found
No related tags found
No related merge requests found
......@@ -17,7 +17,7 @@ use tauri::{api::process::kill_children, AppHandle, State};
#[tauri::command]
pub fn restart_sidecar(app_handle: AppHandle, clash_info: State<'_, ClashInfoState>) {
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() {
*arc = payload;
......
......@@ -16,7 +16,7 @@ use tauri::{
};
/// 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_dir.as_os_str().to_str().unwrap();
......@@ -38,12 +38,13 @@ pub fn run_clash_bin(app_handle: &AppHandle) -> ClashInfoPayload {
Ok((mut rx, _)) => {
log::info!("Successfully execute clash sidecar");
payload.controller = Some(read_clash_controller());
cb(payload.clone()); // callback when run sidecar successfully
tauri::async_runtime::spawn(async move {
while let Some(event) = rx.recv().await {
match event {
CommandEvent::Stdout(line) => log::info!("{}", line),
CommandEvent::Stderr(err) => log::error!("{}", err),
CommandEvent::Stdout(line) => log::info!("[stdout]: {}", line),
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 {
Some(file_name) => file_name.clone(),
None => {
return Err(format!("profile item should have `file` field"));
}
None => return Err(format!("profile item should have `file` field")),
};
let file_path = app_home_dir().join("profiles").join(file_name);
......
......@@ -11,14 +11,13 @@ pub fn resolve_setup(app: &App) {
init::init_app(app.package_info());
// run clash sidecar
let info = clash::run_clash_bin(&app.handle());
// update the profile
let info_ = info.clone();
tauri::async_runtime::spawn(async move {
if let Err(err) = clash::put_clash_profile(&info_).await {
log::error!("failed to put config for `{}`", err);
};
let info = clash::run_clash_bin(&app.handle(), |info_| {
// update the profile
tauri::async_runtime::spawn(async move {
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
......
extern crate warp;
use port_scanner::local_port_available;
use std::sync::{Arc, Mutex};
use tauri::{AppHandle, Manager};
use warp::Filter;
......@@ -22,15 +23,16 @@ pub fn check_singleton() -> Result<(), ()> {
/// The embed server only be used to implement singleton process
/// maybe it can be used as pac server later
pub fn embed_server(app: &AppHandle) {
let window = app.get_window("main").unwrap();
let commands = warp::path!("commands" / "visible").map(move || {
window.show().unwrap();
window.set_focus().unwrap();
return format!("ok");
});
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 win = window.lock().unwrap();
win.show().unwrap();
win.set_focus().unwrap();
return format!("ok");
});
warp::serve(commands)
.bind(([127, 0, 0, 1], SERVER_PORT))
.await;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment