feat: event emit when clash config update

This commit is contained in:
GyDi
2022-03-13 01:37:41 +08:00
parent e1793f57ef
commit c1eb539a5c
5 changed files with 31 additions and 34 deletions

View File

@@ -264,7 +264,7 @@ impl Clash {
/// activate the profile
/// generate a new profile to the temp_dir
/// then put the path to the clash core
fn _activate(info: ClashInfo, config: Mapping) -> Result<()> {
fn _activate(info: ClashInfo, config: Mapping, window: Option<Window>) -> Result<()> {
let temp_path = dirs::profiles_temp_path();
config::save_yaml(temp_path.clone(), &config, Some("# Clash Verge Temp File"))?;
@@ -294,6 +294,12 @@ impl Clash {
if resp.status() != 204 {
log::error!("failed to activate clash for status \"{}\"", resp.status());
}
// emit the window to update something
if let Some(window) = window {
window.emit("verge://refresh-clash-config", "yes").unwrap();
}
// do not retry
break;
}
@@ -325,6 +331,7 @@ impl Clash {
// generate the payload
let payload = profiles.gen_enhanced(event_name.clone())?;
let window = self.window.clone();
win.once(&event_name, move |event| {
if let Some(result) = event.payload() {
@@ -359,7 +366,7 @@ impl Clash {
log::info!("profile enhanced status {}", result.status);
Self::_activate(info, config).unwrap();
Self::_activate(info, config, window).unwrap();
}
if let Some(error) = result.error {
@@ -390,7 +397,7 @@ impl Clash {
config.insert(key, value);
}
Self::_activate(info, config)?;
Self::_activate(info, config, self.window.clone())?;
self.activate_enhanced(profiles, delay)
}
}

View File

@@ -45,13 +45,8 @@ fn main() -> std::io::Result<()> {
let profiles_state = app_handle.state::<states::ProfilesState>();
let mut clash = clash_state.0.lock().unwrap();
let mut profiles = profiles_state.0.lock().unwrap();
match clash.restart_sidecar(&mut profiles) {
Ok(_) => {
let window = app_handle.get_window("main").unwrap();
window.emit("restart_clash", "yes").unwrap();
}
Err(err) => log::error!("{}", err),
}
crate::log_if_err!(clash.restart_sidecar(&mut profiles));
}
"quit" => {
resolve::resolve_reset(app_handle);