fix(gui): skip tray tooltip on macOS (#2919)

On macOS 27, the system occasionally fails to retract the tooltip window of a status item: the tooltip stays on screen and does not disappear when the mouse moves away, and its position may drift far from the icon; it only disappears when the app quits. This is an AppKit-side regression: `NSToolTipManager` does not receive `mouseExited` when the icon enters the menu bar overflow area or when the menu tracking loop is interrupted by Esc. It is unrelated to the UI framework — AppKit, Qt 4.8, Qt 6, and Chromium apps all have reproduction records.

The workaround is to not write tooltips on macOS, while keeping tray title and menu updates unchanged:
- `useTray()` skips the `tooltip` field when creating the tray
- `useTray(true)` skips `setTooltip()` during initialization
- `setTrayTooltip()` only updates `setTitle()` on macOS and does not write tooltips

Windows / Linux behavior is unchanged (Linux's AppIndicator does not display this tooltip anyway).

Refs: #2918
See: https://developer.apple.com/forums/thread/856187
This commit is contained in:
Timskt
2026-11-29 22:34:43 +08:00
committed by GitHub
parent ea2d6be50a
commit dbd665d247
+23 -3
View File
@@ -2,10 +2,26 @@ import { invoke } from '@tauri-apps/api/core'
import { Menu, MenuItem, PredefinedMenuItem } from '@tauri-apps/api/menu'
import { TrayIcon } from '@tauri-apps/api/tray'
import { getCurrentWindow } from '@tauri-apps/api/window'
import { type } from '@tauri-apps/plugin-os'
import pkg from '~/../package.json'
const DEFAULT_TRAY_NAME = 'main'
/**
* macOS 上不给托盘图标设置悬停提示。
*
* macOS 27 起,系统偶尔收不回 status item 的 tooltip 窗口:提示框会一直留在屏幕上,
* 鼠标移开也不消失,位置还可能漂到远离图标的地方,只有退出应用才消失。
* 这是 AppKit 侧的回归(NSToolTipManager 收不到 mouseExited),与框架无关,
* 已有 AppKit / Qt / Chromium 多个应用的复现记录。
*
* 规避办法是不写入 tooltip,托盘的标题与菜单更新保持不变。
* macOS 27.2: NSStatusItem tooltip stays on screen
* https://developer.apple.com/forums/thread/856187
* https://github.com/EasyTier/EasyTier/issues/2918
*/
const supportsTrayTooltip = type() !== 'macos'
interface TrayMenuLabels {
show: string
quit: string
@@ -35,7 +51,8 @@ export async function useTray(init: boolean = false) {
tray = await TrayIcon.getById(DEFAULT_TRAY_NAME)
if (!tray) {
tray = await TrayIcon.new({
tooltip: `EasyTier\n${pkg.version}`,
// Keep the title on macOS so the version stays reachable from the menu bar.
...(supportsTrayTooltip ? { tooltip: `EasyTier\n${pkg.version}` } : {}),
title: `EasyTier\n${pkg.version}`,
id: DEFAULT_TRAY_NAME,
menu: await Menu.new({
@@ -54,7 +71,8 @@ export async function useTray(init: boolean = false) {
}
if (init) {
tray.setTooltip(`EasyTier\n${pkg.version}`)
if (supportsTrayTooltip)
tray.setTooltip(`EasyTier\n${pkg.version}`)
tray.setMenuOnLeftClick(false)
tray.setMenu(await Menu.new({
id: 'main',
@@ -130,7 +148,9 @@ export async function setTrayTooltip(tooltip: string) {
const tray = await useTray()
if (!tray)
return
tray.setTooltip(text)
// macOS 上只更新标题,不写 tooltip(见 supportsTrayTooltip 的说明)。
if (supportsTrayTooltip)
tray.setTooltip(text)
tray.setTitle(text)
}
}