Skip to content

Commit

Permalink
feat(tray): add diff check for system tray partial update (#477)
Browse files Browse the repository at this point in the history
  • Loading branch information
4o3F authored Feb 23, 2024
1 parent 63bb5ea commit 0e38ee9
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 6 deletions.
18 changes: 17 additions & 1 deletion backend/tauri/src/core/tray/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{cmds, config::Config, feat, utils::resolve};
use crate::{cmds, config::Config, core::handle::Handle, feat, utils::resolve};
use anyhow::Result;
use rust_i18n::t;
use tauri::{
Expand Down Expand Up @@ -128,6 +128,22 @@ impl Tray {
Ok(())
}

pub fn update_selected_proxy(old: String, new: String) -> Result<()> {
let tray = Handle::global()
.app_handle
.lock()
.as_ref()
.unwrap()
.tray_handle();
let _ = tray
.get_item(format!("select_proxy_{}", old).as_str())
.set_selected(false);
let _ = tray
.get_item(format!("select_proxy_{}", new).as_str())
.set_selected(true);
Ok(())
}

pub fn on_system_tray_event(app_handle: &AppHandle, event: SystemTrayEvent) {
match event {
SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
Expand Down
110 changes: 105 additions & 5 deletions backend/tauri/src/core/tray/proxies.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use crate::core::tray::Tray;
use crate::core::{
clash::api::ProxyItem,
clash::proxies::{ProxiesGuard, ProxiesGuardExt},
handle::Handle,
};
use log::{debug, error, warn};
use std::collections::BTreeMap;
use tauri::SystemTrayMenu;

async fn loop_task() {
Expand Down Expand Up @@ -30,8 +33,73 @@ async fn loop_task() {
}
}

type GroupName = String;
type FromProxy = String;
type ToProxy = String;
#[derive(PartialEq)]
enum TrayUpdateType {
None,
Full,
Part(Vec<(GroupName, FromProxy, ToProxy)>),
}

fn diff_proxies(
old_proxies: &BTreeMap<String, ProxyItem>,
new_proxies: &BTreeMap<String, ProxyItem>,
) -> TrayUpdateType {
let mut update_mode = TrayUpdateType::None;
let group_matching = new_proxies
.clone()
.into_keys()
.collect::<Vec<String>>()
.iter()
.zip(&old_proxies.clone().into_keys().collect::<Vec<String>>())
.filter(|&(new, old)| new == old)
.count();
if group_matching == old_proxies.len() && group_matching == new_proxies.len() {
let mut action_list = Vec::<(GroupName, FromProxy, ToProxy)>::new();
// Iterate through two btreemap
for (group_key, new_proxy) in new_proxies {
match old_proxies.get(group_key) {
Some(old_proxy) => {
if old_proxy.now.is_some()
&& new_proxy.now.is_some()
&& old_proxy.now != new_proxy.now
{
action_list.insert(
0,
(
group_key.to_owned(),
old_proxy.now.as_ref().unwrap().to_owned(),
new_proxy.now.as_ref().unwrap().to_owned(),
),
);
}
}
None => {
update_mode = TrayUpdateType::Full;
break;
}
}
}
if update_mode != TrayUpdateType::Full && !action_list.is_empty() {
update_mode = TrayUpdateType::Part(action_list);
}
} else {
update_mode = TrayUpdateType::Full
}
update_mode
}

pub async fn proxies_updated_receiver() {
let mut rx = ProxiesGuard::global().read().get_receiver();
let mut old_proxies: BTreeMap<String, ProxyItem> = ProxiesGuard::global()
.read()
.inner()
.to_owned()
.records
.into_iter()
.collect();
loop {
match rx.recv().await {
Ok(_) => {
Expand All @@ -41,12 +109,44 @@ pub async fn proxies_updated_receiver() {
continue;
}
Handle::mutate_proxies();
match Handle::update_systray() {
Ok(_) => {
debug!(target: "tray::proxies", "update systray success");
// Do diff check
let new_proxies: BTreeMap<String, ProxyItem> = ProxiesGuard::global()
.read()
.inner()
.to_owned()
.records
.into_iter()
.collect();

match diff_proxies(&old_proxies, &new_proxies) {
TrayUpdateType::None => {}
TrayUpdateType::Full => {
old_proxies = new_proxies;
// println!("{}", simd_json::to_string_pretty(&proxies).unwrap());
match Handle::update_systray() {
Ok(_) => {
debug!(target: "tray::proxies", "update systray success");
}
Err(e) => {
warn!(target: "tray::proxies", "update systray failed: {:?}", e);
}
}
}
Err(e) => {
warn!(target: "tray::proxies", "update systray failed: {:?}", e);
TrayUpdateType::Part(action_list) => {
old_proxies = new_proxies;
for action in action_list {
match Tray::update_selected_proxy(
format!("{}_{}", action.0, action.1),
format!("{}_{}", action.0, action.2),
) {
Ok(_) => {
debug!(target: "tray::proxies", "update systray part success");
}
Err(e) => {
warn!(target: "tray::proxies", "update systray part failed: {:?}", e);
}
}
}
}
}
}
Expand Down

0 comments on commit 0e38ee9

Please sign in to comment.