From 02ba04b5d80f763d7ecdfc532bca9b9bf573de57 Mon Sep 17 00:00:00 2001 From: GyDi Date: Sat, 22 Jul 2023 15:35:32 +0800 Subject: [PATCH] feat: support copy environment variable --- src-tauri/src/core/tray.rs | 135 ++++++++++++++++++++----------------- src-tauri/src/feat.rs | 10 +++ 2 files changed, 85 insertions(+), 60 deletions(-) diff --git a/src-tauri/src/core/tray.rs b/src-tauri/src/core/tray.rs index dd48b56..39fb92d 100644 --- a/src-tauri/src/core/tray.rs +++ b/src-tauri/src/core/tray.rs @@ -13,67 +13,81 @@ impl Tray { let version = app_handle.package_info().version.to_string(); - if zh { - SystemTrayMenu::new() - .add_item(CustomMenuItem::new("open_window", "打开面板")) - .add_native_item(SystemTrayMenuItem::Separator) - .add_item(CustomMenuItem::new("rule_mode", "规则模式")) - .add_item(CustomMenuItem::new("global_mode", "全局模式")) - .add_item(CustomMenuItem::new("direct_mode", "直连模式")) - .add_item(CustomMenuItem::new("script_mode", "脚本模式")) - .add_native_item(SystemTrayMenuItem::Separator) - .add_item(CustomMenuItem::new("system_proxy", "系统代理")) - .add_item(CustomMenuItem::new("tun_mode", "TUN 模式")) - .add_submenu(SystemTraySubmenu::new( - "打开目录", - SystemTrayMenu::new() - .add_item(CustomMenuItem::new("open_app_dir", "应用目录")) - .add_item(CustomMenuItem::new("open_core_dir", "内核目录")) - .add_item(CustomMenuItem::new("open_logs_dir", "日志目录")), - )) - .add_submenu(SystemTraySubmenu::new( - "更多", - SystemTrayMenu::new() - .add_item(CustomMenuItem::new("restart_clash", "重启 Clash")) - .add_item(CustomMenuItem::new("restart_app", "重启应用")) - .add_item( - CustomMenuItem::new("app_version", format!("Version {version}")) - .disabled(), - ), - )) - .add_native_item(SystemTrayMenuItem::Separator) - .add_item(CustomMenuItem::new("quit", "退出").accelerator("CmdOrControl+Q")) - } else { - SystemTrayMenu::new() - .add_item(CustomMenuItem::new("open_window", "Dashboard")) - .add_native_item(SystemTrayMenuItem::Separator) - .add_item(CustomMenuItem::new("rule_mode", "Rule Mode")) - .add_item(CustomMenuItem::new("global_mode", "Global Mode")) - .add_item(CustomMenuItem::new("direct_mode", "Direct Mode")) - .add_item(CustomMenuItem::new("script_mode", "Script Mode")) - .add_native_item(SystemTrayMenuItem::Separator) - .add_item(CustomMenuItem::new("system_proxy", "System Proxy")) - .add_item(CustomMenuItem::new("tun_mode", "Tun Mode")) - .add_submenu(SystemTraySubmenu::new( - "Open Dir", - SystemTrayMenu::new() - .add_item(CustomMenuItem::new("open_app_dir", "App Dir")) - .add_item(CustomMenuItem::new("open_core_dir", "Core Dir")) - .add_item(CustomMenuItem::new("open_logs_dir", "Logs Dir")), - )) - .add_submenu(SystemTraySubmenu::new( - "More", - SystemTrayMenu::new() - .add_item(CustomMenuItem::new("restart_clash", "Restart Clash")) - .add_item(CustomMenuItem::new("restart_app", "Restart App")) - .add_item( - CustomMenuItem::new("app_version", format!("Version {version}")) - .disabled(), - ), - )) - .add_native_item(SystemTrayMenuItem::Separator) - .add_item(CustomMenuItem::new("quit", "Quit").accelerator("CmdOrControl+Q")) + macro_rules! t { + ($en: expr, $zh: expr) => { + if zh { + $zh + } else { + $en + } + }; } + + SystemTrayMenu::new() + .add_item(CustomMenuItem::new( + "open_window", + t!("Dashboard", "打开面板"), + )) + .add_native_item(SystemTrayMenuItem::Separator) + .add_item(CustomMenuItem::new( + "rule_mode", + t!("Rule Mode", "规则模式"), + )) + .add_item(CustomMenuItem::new( + "global_mode", + t!("Global Mode", "全局模式"), + )) + .add_item(CustomMenuItem::new( + "direct_mode", + t!("Direct Mode", "直连模式"), + )) + .add_item(CustomMenuItem::new( + "script_mode", + t!("Script Mode", "脚本模式"), + )) + .add_native_item(SystemTrayMenuItem::Separator) + .add_item(CustomMenuItem::new( + "system_proxy", + t!("System Proxy", "系统代理"), + )) + .add_item(CustomMenuItem::new("tun_mode", t!("TUN Mode", "Tun 模式"))) + .add_item(CustomMenuItem::new( + "copy_env", + t!("Copy Env", "复制环境变量"), + )) + .add_submenu(SystemTraySubmenu::new( + t!("Open Dir", "打开目录"), + SystemTrayMenu::new() + .add_item(CustomMenuItem::new( + "open_app_dir", + t!("App Dir", "应用目录"), + )) + .add_item(CustomMenuItem::new( + "open_core_dir", + t!("Core Dir", "内核目录"), + )) + .add_item(CustomMenuItem::new( + "open_logs_dir", + t!("Logs Dir", "日志目录"), + )), + )) + .add_submenu(SystemTraySubmenu::new( + t!("More", "更多"), + SystemTrayMenu::new() + .add_item(CustomMenuItem::new( + "restart_clash", + t!("Restart Clash", "重启 Clash"), + )) + .add_item(CustomMenuItem::new( + "restart_app", + t!("Restart App", "重启应用"), + )) + .add_item( + CustomMenuItem::new("app_version", format!("Version {version}")).disabled(), + ), + )) + .add_native_item(SystemTrayMenuItem::Separator) + .add_item(CustomMenuItem::new("quit", t!("Quit", "退出")).accelerator("CmdOrControl+Q")) } pub fn update_systray(app_handle: &AppHandle) -> Result<()> { @@ -135,6 +149,7 @@ impl Tray { "open_window" => resolve::create_window(app_handle), "system_proxy" => feat::toggle_system_proxy(), "tun_mode" => feat::toggle_tun_mode(), + "copy_env" => feat::copy_clash_env(), "open_app_dir" => crate::log_err!(cmds::open_app_dir()), "open_core_dir" => crate::log_err!(cmds::open_core_dir()), "open_logs_dir" => crate::log_err!(cmds::open_logs_dir()), diff --git a/src-tauri/src/feat.rs b/src-tauri/src/feat.rs index 1e49117..a3a0768 100644 --- a/src-tauri/src/feat.rs +++ b/src-tauri/src/feat.rs @@ -9,6 +9,7 @@ use crate::core::*; use crate::log_err; use anyhow::{bail, Result}; use serde_yaml::{Mapping, Value}; +use wry::application::clipboard::Clipboard; // 重启clash pub fn restart_clash_core() { @@ -319,3 +320,12 @@ async fn update_core_config() -> Result<()> { } } } + +/// copy env variable +pub fn copy_clash_env() { + let port = { Config::clash().data().get_client_info().port }; + let text = format!("export https_proxy=http://127.0.0.1:{port} http_proxy=http://127.0.0.1:{port} all_proxy=socks5://127.0.0.1:{port}"); + + let mut cliboard = Clipboard::new(); + cliboard.write_text(text); +}