fix(desktop): 托盘菜单左缘对齐图标、去掉垂直间隙(12A 实测反馈)
ci / server (push) Failing after 9s
ci / design-tokens (push) Failing after 11s

此前菜单水平居中于托盘图标(观感偏左)且下方留 4px 间隙。改为
macOS 系统菜单习惯:左缘对齐图标左缘、紧贴菜单栏;菜单超出屏幕
右缘时整体左收 8px 边距。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
wangjia
2026-07-11 00:41:41 +08:00
parent 4aec0ef6e9
commit c9823ce454
+21 -5
View File
@@ -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, 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(); // 获焦后才能在失焦时自动收起