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(); // 获焦后才能在失焦时自动收起