mirror of
https://github.com/clash-verge-rev/clash-verge-rev
synced 2025-05-04 23:33:44 +08:00
feat: additional macos tray event handling option for menu display (#2958)
This commit is contained in:
parent
1bef6d085d
commit
124934b012
@ -66,12 +66,18 @@ impl Tray {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_systray(&self, app: &App) -> Result<()> {
|
pub fn create_systray(&self, app: &App) -> Result<()> {
|
||||||
let builder = TrayIconBuilder::with_id("main")
|
let mut builder = TrayIconBuilder::with_id("main")
|
||||||
.icon(app.default_window_icon().unwrap().clone())
|
.icon(app.default_window_icon().unwrap().clone())
|
||||||
.icon_as_template(false);
|
.icon_as_template(false);
|
||||||
|
|
||||||
#[cfg(any(target_os = "macos", target_os = "windows"))]
|
#[cfg(any(target_os = "macos", target_os = "windows"))]
|
||||||
let builder = builder.show_menu_on_left_click(false);
|
{
|
||||||
|
let tray_event = { Config::verge().latest().tray_event.clone() };
|
||||||
|
let tray_event: String = tray_event.unwrap_or("main_window".into());
|
||||||
|
if tray_event.as_str() != "tray_menu" {
|
||||||
|
builder = builder.show_menu_on_left_click(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let tray = builder.build(app)?;
|
let tray = builder.build(app)?;
|
||||||
|
|
||||||
@ -79,22 +85,6 @@ impl Tray {
|
|||||||
let tray_event = { Config::verge().latest().tray_event.clone() };
|
let tray_event = { Config::verge().latest().tray_event.clone() };
|
||||||
let tray_event: String = tray_event.unwrap_or("main_window".into());
|
let tray_event: String = tray_event.unwrap_or("main_window".into());
|
||||||
|
|
||||||
#[cfg(target_os = "macos")]
|
|
||||||
if let TrayIconEvent::Click {
|
|
||||||
button: MouseButton::Left,
|
|
||||||
button_state: MouseButtonState::Down,
|
|
||||||
..
|
|
||||||
} = event
|
|
||||||
{
|
|
||||||
match tray_event.as_str() {
|
|
||||||
"system_proxy" => feat::toggle_system_proxy(),
|
|
||||||
"tun_mode" => feat::toggle_tun_mode(None),
|
|
||||||
"main_window" => resolve::create_window(),
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(target_os = "macos"))]
|
|
||||||
if let TrayIconEvent::Click {
|
if let TrayIconEvent::Click {
|
||||||
button: MouseButton::Left,
|
button: MouseButton::Left,
|
||||||
button_state: MouseButtonState::Down,
|
button_state: MouseButtonState::Down,
|
||||||
@ -113,6 +103,19 @@ impl Tray {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 更新托盘点击行为
|
||||||
|
pub fn update_click_behavior(&self) -> Result<()> {
|
||||||
|
let app_handle = handle::Handle::global().app_handle().unwrap();
|
||||||
|
let tray_event = { Config::verge().latest().tray_event.clone() };
|
||||||
|
let tray_event: String = tray_event.unwrap_or("main_window".into());
|
||||||
|
let tray = app_handle.tray_by_id("main").unwrap();
|
||||||
|
match tray_event.as_str() {
|
||||||
|
"tray_menu" => tray.set_show_menu_on_left_click(true)?,
|
||||||
|
_ => tray.set_show_menu_on_left_click(false)?,
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
/// 更新托盘菜单
|
/// 更新托盘菜单
|
||||||
pub fn update_menu(&self) -> Result<()> {
|
pub fn update_menu(&self) -> Result<()> {
|
||||||
let app_handle = handle::Handle::global().app_handle().unwrap();
|
let app_handle = handle::Handle::global().app_handle().unwrap();
|
||||||
|
@ -73,6 +73,7 @@ pub async fn patch_verge(patch: IVerge, not_save_file: bool) -> Result<()> {
|
|||||||
let http_port = patch.verge_port;
|
let http_port = patch.verge_port;
|
||||||
let enable_tray_speed = patch.enable_tray_speed;
|
let enable_tray_speed = patch.enable_tray_speed;
|
||||||
let enable_global_hotkey = patch.enable_global_hotkey;
|
let enable_global_hotkey = patch.enable_global_hotkey;
|
||||||
|
let tray_event = patch.tray_event;
|
||||||
|
|
||||||
let res: std::result::Result<(), anyhow::Error> = {
|
let res: std::result::Result<(), anyhow::Error> = {
|
||||||
let mut should_restart_core = false;
|
let mut should_restart_core = false;
|
||||||
@ -84,6 +85,7 @@ pub async fn patch_verge(patch: IVerge, not_save_file: bool) -> Result<()> {
|
|||||||
let mut should_update_hotkey = false;
|
let mut should_update_hotkey = false;
|
||||||
let mut should_update_systray_menu = false;
|
let mut should_update_systray_menu = false;
|
||||||
let mut should_update_systray_tooltip = false;
|
let mut should_update_systray_tooltip = false;
|
||||||
|
let mut should_update_systray_click_behavior = false;
|
||||||
|
|
||||||
if tun_mode.is_some() {
|
if tun_mode.is_some() {
|
||||||
should_update_clash_config = true;
|
should_update_clash_config = true;
|
||||||
@ -145,6 +147,10 @@ pub async fn patch_verge(patch: IVerge, not_save_file: bool) -> Result<()> {
|
|||||||
should_update_systray_icon = true;
|
should_update_systray_icon = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if tray_event.is_some() {
|
||||||
|
should_update_systray_click_behavior = true;
|
||||||
|
}
|
||||||
|
|
||||||
if should_restart_core {
|
if should_restart_core {
|
||||||
CoreManager::global().restart_core().await?;
|
CoreManager::global().restart_core().await?;
|
||||||
}
|
}
|
||||||
@ -180,6 +186,10 @@ pub async fn patch_verge(patch: IVerge, not_save_file: bool) -> Result<()> {
|
|||||||
tray::Tray::global().update_tooltip()?;
|
tray::Tray::global().update_tooltip()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if should_update_systray_click_behavior {
|
||||||
|
tray::Tray::global().update_click_behavior()?;
|
||||||
|
}
|
||||||
|
|
||||||
// 处理轻量模式切换
|
// 处理轻量模式切换
|
||||||
if lite_mode.is_some() {
|
if lite_mode.is_some() {
|
||||||
if let Some(window) = handle::Handle::global().get_window() {
|
if let Some(window) = handle::Handle::global().get_window() {
|
||||||
|
@ -119,6 +119,7 @@ const SettingVergeBasic = ({ onError }: Props) => {
|
|||||||
>
|
>
|
||||||
<Select size="small" sx={{ width: 140, "> div": { py: "7.5px" } }}>
|
<Select size="small" sx={{ width: 140, "> div": { py: "7.5px" } }}>
|
||||||
<MenuItem value="main_window">{t("Show Main Window")}</MenuItem>
|
<MenuItem value="main_window">{t("Show Main Window")}</MenuItem>
|
||||||
|
<MenuItem value="tray_menu">{t("Show Tray Menu")}</MenuItem>
|
||||||
<MenuItem value="system_proxy">{t("System Proxy")}</MenuItem>
|
<MenuItem value="system_proxy">{t("System Proxy")}</MenuItem>
|
||||||
<MenuItem value="tun_mode">{t("Tun Mode")}</MenuItem>
|
<MenuItem value="tun_mode">{t("Tun Mode")}</MenuItem>
|
||||||
<MenuItem value="disable">{t("Disable")}</MenuItem>
|
<MenuItem value="disable">{t("Disable")}</MenuItem>
|
||||||
|
@ -288,6 +288,7 @@
|
|||||||
"theme.system": "System",
|
"theme.system": "System",
|
||||||
"Tray Click Event": "Tray Click Event",
|
"Tray Click Event": "Tray Click Event",
|
||||||
"Show Main Window": "Show Main Window",
|
"Show Main Window": "Show Main Window",
|
||||||
|
"Show Tray Menu": "Show Tray Menu",
|
||||||
"Copy Env Type": "Copy Env Type",
|
"Copy Env Type": "Copy Env Type",
|
||||||
"Copy Success": "Copy Success",
|
"Copy Success": "Copy Success",
|
||||||
"Start Page": "Start Page",
|
"Start Page": "Start Page",
|
||||||
|
@ -287,6 +287,7 @@
|
|||||||
"theme.system": "系统",
|
"theme.system": "系统",
|
||||||
"Tray Click Event": "托盘点击事件",
|
"Tray Click Event": "托盘点击事件",
|
||||||
"Show Main Window": "显示主窗口",
|
"Show Main Window": "显示主窗口",
|
||||||
|
"Show Tray Menu": "显示托盘菜单",
|
||||||
"Copy Env Type": "复制环境变量类型",
|
"Copy Env Type": "复制环境变量类型",
|
||||||
"Copy Success": "复制成功",
|
"Copy Success": "复制成功",
|
||||||
"Start Page": "启动页面",
|
"Start Page": "启动页面",
|
||||||
|
7
src/services/types.d.ts
vendored
7
src/services/types.d.ts
vendored
@ -718,7 +718,12 @@ interface IProxyConfig
|
|||||||
interface IVergeConfig {
|
interface IVergeConfig {
|
||||||
app_log_level?: "trace" | "debug" | "info" | "warn" | "error" | string;
|
app_log_level?: "trace" | "debug" | "info" | "warn" | "error" | string;
|
||||||
language?: string;
|
language?: string;
|
||||||
tray_event?: "main_window" | "system_proxy" | "tun_mode" | string;
|
tray_event?:
|
||||||
|
| "main_window"
|
||||||
|
| "tray_menu"
|
||||||
|
| "system_proxy"
|
||||||
|
| "tun_mode"
|
||||||
|
| string;
|
||||||
env_type?: "bash" | "cmd" | "powershell" | "fish" | string;
|
env_type?: "bash" | "cmd" | "powershell" | "fish" | string;
|
||||||
startup_script?: string;
|
startup_script?: string;
|
||||||
start_page?: string;
|
start_page?: string;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user