Skip to content

Commit

Permalink
fix(tray): should disable click expect Selector and Fallback type
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Feb 26, 2024
1 parent 22ec542 commit f61ba52
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
14 changes: 14 additions & 0 deletions backend/tauri/src/core/clash/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ use std::{
collections::HashMap,
fmt::{self, Display, Formatter},
};
use tracing_attributes::instrument;

/// PUT /configs
/// path 是绝对路径
#[instrument]
pub async fn put_configs(path: &str) -> Result<()> {
let (url, headers) = clash_client_info()?;
let url = format!("{url}/configs");
Expand All @@ -31,6 +33,7 @@ pub async fn put_configs(path: &str) -> Result<()> {
}

/// PATCH /configs
#[instrument]
pub async fn patch_configs(config: &Mapping) -> Result<()> {
let (url, headers) = clash_client_info()?;
let url = format!("{url}/configs");
Expand Down Expand Up @@ -115,6 +118,7 @@ impl From<ProxyProviderItem> for ProxyItem {

/// GET /proxies
/// 获取代理列表
#[instrument]
pub async fn get_proxies() -> Result<ProxiesRes> {
let (url, headers) = clash_client_info()?;
let url = format!("{url}/proxies");
Expand All @@ -132,6 +136,7 @@ pub async fn get_proxies() -> Result<ProxiesRes> {
/// 返回代理的配置
///
#[allow(dead_code)]
#[instrument]
pub async fn get_proxy(name: String) -> Result<ProxyItem> {
let (url, headers) = clash_client_info()?;
let url = format!("{url}/proxies/{name}");
Expand All @@ -147,6 +152,7 @@ pub async fn get_proxy(name: String) -> Result<ProxyItem> {
/// 选择代理
/// group: 代理分组名称
/// name: 代理名称
#[instrument]
pub async fn update_proxy(group: &str, name: &str) -> Result<()> {
let (url, headers) = clash_client_info()?;
let url = format!("{url}/proxies/{group}");
Expand Down Expand Up @@ -214,6 +220,7 @@ pub struct ProvidersProxiesRes {

/// GET /providers/proxies
/// 获取所有代理集合的所有代理信息
#[instrument]
pub async fn get_providers_proxies() -> Result<ProvidersProxiesRes> {
let (url, headers) = clash_client_info()?;
let url = format!("{url}/providers/proxies");
Expand All @@ -229,6 +236,7 @@ pub async fn get_providers_proxies() -> Result<ProvidersProxiesRes> {
/// 获取单个代理集合的所有代理信息
/// group: 代理集合名称
#[allow(dead_code)]
#[instrument]
pub async fn get_providers_proxies_group(group: String) -> Result<ProxyProviderItem> {
let (url, headers) = clash_client_info()?;
let url = format!("{url}/providers/proxies/{group}");
Expand All @@ -243,6 +251,7 @@ pub async fn get_providers_proxies_group(group: String) -> Result<ProxyProviderI
/// PUT /providers/proxies/:name
/// 更新代理集合
/// name: 代理集合名称
#[instrument]
pub async fn update_providers_proxies_group(name: &str) -> Result<()> {
let (url, headers) = clash_client_info()?;
let url = format!("{url}/providers/proxies/{name}");
Expand All @@ -263,6 +272,7 @@ pub async fn update_providers_proxies_group(name: &str) -> Result<()> {
/// 获取代理集合的健康检查
/// name: 代理集合名称
#[allow(dead_code)]
#[instrument]
pub async fn get_providers_proxies_healthcheck(name: String) -> Result<Mapping> {
let (url, headers) = clash_client_info()?;
let url = format!("{url}/providers/proxies/{name}/healthcheck");
Expand All @@ -281,6 +291,7 @@ pub struct DelayRes {

/// GET /proxies/{name}/delay
/// 获取代理延迟
#[instrument]
pub async fn get_proxy_delay(name: String, test_url: Option<String>) -> Result<DelayRes> {
let (url, headers) = clash_client_info()?;
let url = format!("{url}/proxies/{name}/delay");
Expand All @@ -300,6 +311,7 @@ pub async fn get_proxy_delay(name: String, test_url: Option<String>) -> Result<D
}

/// 根据clash info获取clash服务地址和请求头
#[instrument]
fn clash_client_info() -> Result<(String, HeaderMap)> {
let client = { Config::clash().data().get_client_info() };

Expand All @@ -317,6 +329,7 @@ fn clash_client_info() -> Result<(String, HeaderMap)> {
}

/// 缩短clash的日志
#[instrument]
pub fn parse_log(log: String) -> String {
if log.starts_with("time=") && log.len() > 33 {
return log[33..].to_owned();
Expand All @@ -329,6 +342,7 @@ pub fn parse_log(log: String) -> String {

/// 缩短clash -t的错误输出
/// 仅适配 clash p核 8-26、clash meta 1.13.1
#[instrument]
pub fn parse_check_output(log: String) -> String {
let t = log.find("time=");
let m = log.find("msg=");
Expand Down
2 changes: 2 additions & 0 deletions backend/tauri/src/core/clash/proxies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::sync::{Arc, OnceLock};
use tokio::{sync::broadcast, try_join};
use tracing_attributes::instrument;

#[derive(Debug, Clone, Deserialize, Serialize, Default)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -69,6 +70,7 @@ async fn fetch_proxies() -> Result<(api::ProxiesRes, api::ProvidersProxiesRes)>
}

impl Proxies {
#[instrument]
pub async fn fetch() -> Result<Self> {
let (inner_proxies, providers_proxies) = fetch_proxies
.retry(&*CLASH_API_DEFAULT_BACKOFF_STRATEGY)
Expand Down
28 changes: 25 additions & 3 deletions backend/tauri/src/core/tray/proxies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ enum TrayUpdateType {
struct TrayProxyItem {
current: Option<String>,
all: Vec<String>,
r#type: String, // TODO: 转成枚举
}
type TrayProxies = IndexMap<String, TrayProxyItem>;

Expand All @@ -66,13 +67,15 @@ fn to_tray_proxies(mode: &str, raw_proxies: &Proxies) -> TrayProxies {
.into_iter()
.map(|x| x.name)
.collect(),
r#type: "Selector".to_owned(),
};
tray_proxies.insert("global".to_owned(), global);
}
for raw_group in raw_proxies.groups.iter() {
let group = TrayProxyItem {
current: raw_group.now.clone(),
all: raw_group.all.clone().into_iter().map(|x| x.name).collect(),
r#type: raw_group.r#type.clone(),
};
tray_proxies.insert(raw_group.name.to_owned(), group);
}
Expand Down Expand Up @@ -196,6 +199,7 @@ mod platform_impl {
use super::{ProxySelectAction, TrayProxyItem};
use crate::core::{clash::proxies::ProxiesGuard, handle::Handle};
use tauri::{CustomMenuItem, SystemTrayMenu, SystemTraySubmenu};
use tracing::warn;
pub fn generate_group_selector(group_name: &str, group: &TrayProxyItem) -> SystemTraySubmenu {
let mut group_menu = SystemTrayMenu::new();
for item in group.all.iter() {
Expand All @@ -208,6 +212,11 @@ mod platform_impl {
sub_item = sub_item.selected();
}
}

if !matches!(group.r#type.as_str(), "Selector" | "Fallback") {
sub_item = sub_item.disabled();
}

group_menu = group_menu.add_item(sub_item);
}
SystemTraySubmenu::new(group_name.to_string(), group_menu)
Expand Down Expand Up @@ -245,10 +254,23 @@ mod platform_impl {
for action in actions {
let from = format!("select_proxy_{}_{}", action.0, action.1);
let to = format!("select_proxy_{}_{}", action.0, action.2);
if let Some(item) = tray.try_get_item(&from) {
let _ = item.set_selected(false);

match tray.try_get_item(&from) {
Some(item) => {
let _ = item.set_selected(false);
}
None => {
warn!("failed to deselect, item not found: {}", from);
}
}
match tray.try_get_item(&to) {
Some(item) => {
let _ = item.set_selected(true);
}
None => {
warn!("failed to select, item not found: {}", to);
}
}
let _ = tray.get_item(&to).set_selected(true);
}
}
}
Expand Down

0 comments on commit f61ba52

Please sign in to comment.