From c9823ce4545f4a4dc37b00f7e4c4faa4176c03d6 Mon Sep 17 00:00:00 2001 From: wangjia <809946525@qq.com> Date: Sat, 11 Jul 2026 00:41:41 +0800 Subject: [PATCH] =?UTF-8?q?fix(desktop):=20=E6=89=98=E7=9B=98=E8=8F=9C?= =?UTF-8?q?=E5=8D=95=E5=B7=A6=E7=BC=98=E5=AF=B9=E9=BD=90=E5=9B=BE=E6=A0=87?= =?UTF-8?q?=E3=80=81=E5=8E=BB=E6=8E=89=E5=9E=82=E7=9B=B4=E9=97=B4=E9=9A=99?= =?UTF-8?q?=EF=BC=8812A=20=E5=AE=9E=E6=B5=8B=E5=8F=8D=E9=A6=88=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 此前菜单水平居中于托盘图标(观感偏左)且下方留 4px 间隙。改为 macOS 系统菜单习惯:左缘对齐图标左缘、紧贴菜单栏;菜单超出屏幕 右缘时整体左收 8px 边距。 Co-Authored-By: Claude Fable 5 --- desktop/src-tauri/src/lib.rs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/desktop/src-tauri/src/lib.rs b/desktop/src-tauri/src/lib.rs index fe44b10..fc1a876 100644 --- a/desktop/src-tauri/src/lib.rs +++ b/desktop/src-tauri/src/lib.rs @@ -12,17 +12,30 @@ use tauri::tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent} use tauri::Manager; use tauri_plugin_global_shortcut::{Shortcut, ShortcutState}; -/// 托盘图标 rect → 菜单窗口左上角物理坐标(图标正下方、水平居中)。 -fn tray_menu_position(rect: &tauri::Rect, scale: f64, win_width: f64) -> (f64, f64) { +/// 托盘图标 rect → 菜单窗口左上角物理坐标。 +/// macOS 系统菜单习惯:左缘对齐图标左缘、紧贴菜单栏下方; +/// 菜单超出屏幕右缘时整体左收(12A 实测反馈:居中定位偏左、间隙偏大)。 +fn tray_menu_position( + rect: &tauri::Rect, + scale: f64, + win_width: f64, + screen_right: Option, +) -> (f64, f64) { let (x, y) = match rect.position { tauri::Position::Physical(p) => (p.x as f64, p.y as f64), tauri::Position::Logical(p) => (p.x * scale, p.y * scale), }; - let (w, h) = match rect.size { + let (_w, h) = match rect.size { tauri::Size::Physical(s) => (s.width as f64, s.height as f64), tauri::Size::Logical(s) => (s.width * scale, s.height * scale), }; - (x + w / 2.0 - win_width / 2.0, y + h + 4.0 * scale) + let mut mx = x; + if let Some(right) = screen_right { + if mx + win_width > right { + mx = right - win_width - 8.0 * scale; + } + } + (mx, y + h) } /// 左键点击托盘图标 → 在图标位置弹出 / 收起自绘菜单窗(11B)。 @@ -39,7 +52,10 @@ fn toggle_tray_menu(app: &tauri::AppHandle, rect: &tauri::Rect) { .outer_size() .map(|s| s.width as f64) .unwrap_or(248.0 * scale); - let (x, y) = tray_menu_position(rect, scale, win_width); + let screen_right = w.current_monitor().ok().flatten().map(|m| { + m.position().x as f64 + m.size().width as f64 + }); + let (x, y) = tray_menu_position(rect, scale, win_width, screen_right); let _ = w.set_position(tauri::PhysicalPosition::new(x, y)); let _ = w.show(); let _ = w.set_focus(); // 获焦后才能在失焦时自动收起