优化调整

This commit is contained in:
zhongluofeng
2026-08-14 17:47:06 +08:00
parent 6c7897bf47
commit 7d49a7395f
50 changed files with 2805 additions and 259 deletions
+76 -45
View File
@@ -21,10 +21,10 @@ use tauri::{
static LAST_SHOW_TIME: Mutex<Option<Instant>> = Mutex::new(None);
/// 保存最近一次右键时计算出的定位参数(物理坐标),供 `tray_menu_ready` 使用
/// (x, tray_top, wa_top, wa_bottom, scale)
/// (x, cursor_y, screen_top, screen_bottom, scale)
static LAST_MENU_LAYOUT: Mutex<Option<(f64, f64, f64, f64, f64)>> = Mutex::new(None);
use crate::win32_util::{get_work_area, get_work_area_at_point, get_dpi_for_point};
use crate::win32_util::{get_work_area, get_monitor_bounds_at_point, get_dpi_for_point};
use crate::mihomo_manager::{MihomoManager, is_pseudo_node};
use crate::monitor_kernel::MonitorKernel;
use crate::process_manager::{ProcessManager, ProcessStatus};
@@ -148,7 +148,25 @@ async fn fetch_proxy_nodes(app: &AppHandle) -> Option<(String, Vec<(String, Opti
// ===== 获取菜单状态 =====
pub async fn get_tray_menu_state(app: &AppHandle) -> TrayMenuState {
/// 基础状态(不含代理节点)。托盘菜单显示不应受 mihomo API 慢/卡死影响,
/// 因此先秒发基础状态让菜单立即出现,代理节点随后异步补充。
fn get_base_tray_state(app: &AppHandle) -> TrayMenuState {
let proxy_running = is_proxy_running(app);
let monitor_running = is_monitor_running(app);
// 读取 Windows 注册表中的真实系统代理状态(不依赖 settings.json 缓存)
let system_proxy = crate::mihomo_manager::get_system_proxy_windows();
TrayMenuState {
proxy_running,
system_proxy,
monitor_running,
proxy_group: None,
proxy_nodes: vec![],
proxy_current: None,
}
}
/// 完整状态(含代理节点)。可能因调用 mihomo /proxies 而耗时(最长 10s)。
async fn get_full_tray_state(app: &AppHandle) -> TrayMenuState {
let proxy_running = is_proxy_running(app);
let monitor_running = is_monitor_running(app);
// 读取 Windows 注册表中的真实系统代理状态(不依赖 settings.json 缓存)
@@ -183,6 +201,10 @@ pub async fn get_tray_menu_state(app: &AppHandle) -> TrayMenuState {
}
}
pub async fn get_tray_menu_state(app: &AppHandle) -> TrayMenuState {
get_full_tray_state(app).await
}
// ===== 显示/隐藏托盘菜单窗口 =====
/// 托盘菜单窗口尺寸(逻辑像素)
@@ -249,29 +271,30 @@ pub fn precreate_tray_menu_window(app: &AppHandle) {
}
/// 右键托盘时调用:计算定位参数、发送状态给前端,但不立即显示窗口。
/// 窗口等待前端测量内容高度后调用 `tray_menu_ready` 才显示,确保底部精确对齐托盘图标
/// 窗口等待前端测量内容高度后调用 `tray_menu_ready` 才显示,确保底部对齐鼠标点击位置
///
/// `cursor_pos`: 事件报告的鼠标物理坐标`tray_rect`: 托盘图标区域(物理像素)
pub fn show_tray_menu(app: &AppHandle, cursor_pos: (f64, f64), tray_rect: (f64, f64, f64, f64)) {
/// `cursor_pos`: 事件报告的鼠标物理坐标。
pub fn show_tray_menu(app: &AppHandle, cursor_pos: (f64, f64)) {
let (mx, my) = (cursor_pos.0, cursor_pos.1);
let tray_top = tray_rect.1;
// 获取光标所在显示器的工作区(物理像素)
let (wa_left, wa_top, wa_right, wa_bottom) = get_work_area_at_point(mx as i32, my as i32)
.unwrap_or((0, 0, 1920, 1040));
// 获取光标所在显示器的完整边界(含任务栏,物理像素)
// 托盘图标位于任务栏上,菜单需在鼠标位置弹出,因此用屏幕边界而非工作区做 clamp,
// 否则会被工作区底部(任务栏顶部)截断,导致菜单整体被推到任务栏上方。
let (scr_left, scr_top, scr_right, scr_bottom) = get_monitor_bounds_at_point(mx as i32, my as i32)
.unwrap_or((0, 0, 1920, 1080));
// 光标所在显示器的 DPI:菜单宽度按物理像素换算
let dpi = get_dpi_for_point(mx as i32, my as i32).unwrap_or(96);
let scale = dpi as f64 / 96.0;
let menu_w_px = MENU_W * scale;
// 水平:菜单左边缘对齐鼠标 X(向右延伸),超出右边界则左移(物理坐标)
let x = mx.max(wa_left as f64).min(wa_right as f64 - menu_w_px);
// 水平:菜单左边缘对齐鼠标 X(向右延伸),超出屏幕右边界则左移(物理坐标)
let x = mx.max(scr_left as f64).min(scr_right as f64 - menu_w_px);
// 保存布局参数(全部物理坐标 + scale,供 tray_menu_ready 换算前端上报的逻辑高度)
{
let mut layout = LAST_MENU_LAYOUT.lock().unwrap_or_else(|e| e.into_inner());
*layout = Some((x, tray_top, wa_top as f64, wa_bottom as f64, scale));
*layout = Some((x, my, scr_top as f64, scr_bottom as f64, scale));
}
// 记录显示时间,用于失焦防抖
@@ -285,10 +308,20 @@ pub fn show_tray_menu(app: &AppHandle, cursor_pos: (f64, f64), tray_rect: (f64,
precreate_tray_menu_window(app);
}
// 发送状态给前端(前端测量内容高度后调用 tray_menu_ready 显示窗口)
// 发送状态给前端(前端测量内容高度后调用 tray_menu_ready 显示窗口)
// 代理节点拉取默认最长 10s;这里用 1.5s 短超时兜底,避免 mihomo API 卡死时
// 菜单迟迟不出现(超时则退回基础状态,节点区留空,后续 refresh 可补)。
let app_clone = app.clone();
tauri::async_runtime::spawn(async move {
let state = get_tray_menu_state(&app_clone).await;
let state = match tokio::time::timeout(
Duration::from_millis(1500),
get_full_tray_state(&app_clone),
)
.await
{
Ok(s) => s,
Err(_) => get_base_tray_state(&app_clone),
};
let _ = app_clone.emit(crate::constants::events::TRAY_MENU_SHOW, state);
});
}
@@ -306,6 +339,22 @@ async fn refresh_and_emit_state(app: &AppHandle) {
let _ = app.emit(crate::constants::events::TRAY_MENU_STATE_UPDATED, state);
}
/// 显示主窗口并强制置为前台。
/// Tauri 的 set_focus 在 Windows 上受前台锁定限制,主窗口被其他应用遮挡时无法到前台;
/// 改用原生 SetForegroundWindow + BringWindowToTop(模拟 Alt 键重置前台锁定)。
pub fn focus_main_window(app: &AppHandle) {
if let Some(window) = app.get_webview_window(crate::constants::windows::MAIN) {
let _ = window.show();
let _ = window.unminimize();
match window.hwnd() {
Ok(hwnd) => crate::win32_util::force_foreground(hwnd.0 as isize),
Err(_) => {
window.set_focus().ok();
}
}
}
}
// ===== Tauri 命令 =====
/// 执行菜单项动作(统一入口)
@@ -367,18 +416,12 @@ pub async fn tray_menu_action(
}
}
"download_new" => {
if let Some(window) = app.get_webview_window(crate::constants::windows::MAIN) {
window.show().ok();
window.set_focus().ok();
}
focus_main_window(&app);
let _ = app.emit(crate::constants::events::TRAY_NEW_DOWNLOAD, ());
hide_tray_menu(&app);
}
"settings" => {
if let Some(window) = app.get_webview_window(crate::constants::windows::MAIN) {
window.show().ok();
window.set_focus().ok();
}
focus_main_window(&app);
let _ = app.emit(crate::constants::events::TRAY_OPEN_SETTINGS, ());
hide_tray_menu(&app);
}
@@ -411,9 +454,9 @@ pub async fn tray_menu_ready(content_height: f64, app: AppHandle) -> Result<(),
let win = app.get_webview_window(TRAY_MENU_LABEL)
.ok_or("tray-menu window not found")?;
let (x, tray_top, wa_top, wa_bottom, scale) = {
let (x, cursor_y, scr_top, scr_bottom, scale) = {
let layout = LAST_MENU_LAYOUT.lock().unwrap_or_else(|e| e.into_inner());
layout.unwrap_or((0.0, 1040.0, 0.0, 1040.0, 1.0))
layout.unwrap_or((0.0, 1040.0, 0.0, 1080.0, 1.0))
};
// 将内容高度限制在合理范围内(前端上报为逻辑像素)
@@ -427,8 +470,10 @@ pub async fn tray_menu_ready(content_height: f64, app: AppHandle) -> Result<(),
height: win_h_px as u32,
}));
// 垂直:菜单下边缘紧贴托盘图标顶部(向上弹出,物理坐标)
let y = (tray_top - win_h_px).max(wa_top).min(wa_bottom - win_h_px);
// 垂直:菜单底部对齐鼠标点击位置(向上弹出,物理坐标)
// 用屏幕边界而非工作区 clamp,使菜单贴近/覆盖任务栏上的鼠标位置,
// 而不是被工作区底部(任务栏顶部)截断后整体出现在任务栏上方。
let y = (cursor_y - win_h_px).max(scr_top).min(scr_bottom - win_h_px);
let pos = tauri::Position::Physical(tauri::PhysicalPosition {
x: x as i32,
@@ -666,31 +711,17 @@ pub fn create_tray_menu(app: &AppHandle) -> Result<(), tauri::Error> {
button: MouseButton::Left,
..
} => {
// 左键:显示主窗口
if let Some(window) = app.get_webview_window(crate::constants::windows::MAIN) {
window.show().ok();
window.set_focus().ok();
}
// 左键:显示主窗口(强制置前,绕过前台锁定)
focus_main_window(&app);
}
TrayIconEvent::Click {
button: MouseButton::Right,
position,
rect,
..
} => {
// 右键:显示自定义菜单窗口(使用事件中的精确坐标)
// 右键:显示自定义菜单窗口(使用事件中的鼠标坐标)
let cursor = (position.x, position.y);
// 从 Rect 的 Position/Size 枚举中提取物理像素值
let (rx, ry) = match rect.position {
tauri::Position::Physical(p) => (p.x as f64, p.y as f64),
tauri::Position::Logical(p) => (p.x, p.y),
};
let (_rw, rh) = match rect.size {
tauri::Size::Physical(s) => (s.width as f64, s.height as f64),
tauri::Size::Logical(s) => (s.width, s.height),
};
let tray_r = (rx, ry, _rw, rh);
show_tray_menu(&app, cursor, tray_r);
show_tray_menu(&app, cursor);
}
_ => {}
}