gui: the Mac's tray panel sits at a menu bar extra's level, so a screenshot tool draws over it (zyh6036 on X)

zyh6036 on X: the menu bar panel stays so far in front that even
WeChat's screenshot can't cover it, so it's hard to take a picture of.

The panel asked for AlwaysOnTop (NSFloatingWindowLevel, 3), but Wails'
SystemTray.PositionWindow sets the window it places to
NSPopUpMenuWindowLevel (101) every time (systemtray_darwin.m), and magpie
places the panel with it on each open and each resize. The live app's
panel read layer 101 in CGWindowList. Nothing in magpie's history chose
101 (git log -S finds only AlwaysOnTop, from the first commit).

placeByIcon now does PositionWindow and then sets NSStatusWindowLevel
(25), the level of the status bar itself: still over normal windows,
floating ones, the Dock (20) and the menu bar (24), and still on every
Space and over full-screen apps (the collection behavior is unchanged,
#2141), but under pop-up menus and anything a screenshot tool puts up at
25 or above. Both PositionWindow calls (togglePanelNow, sizeOpenPanel)
go through it. Other systems: setPanelLevel is a no-op.

What was run (mica, macOS 27, at 48fffb60c + this change; the panel tests again at the rebased head):
- TestPanelJoinsFullScreenSpaces now has a real SystemTray, places the
  panel through placeByIcon and checks the level is
  NSStatusWindowLevel. Broken by hand (setPanelLevel call removed): red,
  "the panel is at level 101". Restored: green.
- A real build in a sandbox HOME (MAGPIE_ADDR 127.0.0.1:3536),
  `magpie panel`, with a tiny AppKit helper: CGWindowList front to back
  read the panel (439x521) at layer 25 over a normal titled window
  (layer 0); a full-screen borderless overlay at NSScreenSaverWindowLevel
  (1000) shown after it was in front of the panel, and so was one at
  NSStatusWindowLevel (25) shown after it, as a screenshot overlay is.
  Before the change the panel was at 101, over the one at 25.
- go test -count=1 -timeout 45m ./internal/gui: ok (714s). An earlier
  run of it had two source guards (panelside_test.go) reading the old
  PositionWindow call, updated here, and TestGatewayModeListsNoOwnSessions
  red under load ("listing":true, the search asked before the list was
  read), green alone: filed as #7064.
- go vet ./internal/gui on darwin, GOOS=windows, and Linux in WSL
  (magpie-desktop, -tags gtk3): ok. gofmt -l: clean.

Not tried: the panel over a real full-screen app. mica's and mote's
screens are locked, and a window there doesn't enter full screen; the
full-screen guarantee is the unchanged collection behavior, which the
same test still checks. WeChat itself: not installed on any test Mac,
so its overlay's level isn't known; if it is below 25 this doesn't
reach it, and the reply asks the reporter to say.
This commit is contained in:
yetone
2026-11-18 16:30:23 +08:00
parent 534b85bf99
commit a2d402afe0
6 changed files with 60 additions and 6 deletions
+9 -1
View File
@@ -1027,7 +1027,7 @@ func (h *host) togglePanelNow() {
hideWebview(h.panel)
return
}
_ = h.tray.PositionWindow(h.panel, 6)
h.placeByIcon(h.panel)
showWebview(h.panel).Focus()
h.tellPanelSide()
return
@@ -1038,6 +1038,14 @@ func (h *host) togglePanelNow() {
}
}
// placeByIcon places the panel by the tray icon. On the Mac, Wails puts
// the window it places at a pop-up menu's level, over a screenshot tool's
// (zyh6036 on X), so it goes back to a menu bar extra's (setPanelLevel).
func (h *host) placeByIcon(w *application.WebviewWindow) {
_ = h.tray.PositionWindow(w, 6)
setPanelLevel(w)
}
// tellPanelSide tells the panel, just placed, which of its edges opens the
// main window: the one nearest the tray (panelOpenAt). Wayland doesn't say
// where the compositor put a window, so there it goes by the system.
+9 -3
View File
@@ -20,7 +20,10 @@ import (
// real window: the panel, made at start and made again after lightweight
// mode let it go, is on every Space and over a full-screen app's, and is
// shown and hidden with that set, without activating magpie (activated,
// the Mac took the user to the Space of magpie's other windows).
// the Mac took the user to the Space of magpie's other windows). Placed by
// the tray icon, it is at a menu bar extra's level, not the pop-up menu
// level Wails' PositionWindow gives it, where WeChat's screenshot couldn't
// draw over it (zyh6036 on X).
func TestPanelJoinsFullScreenSpaces(t *testing.T) {
out, err := runAppKit(t, 30*time.Second, []string{"MAGPIE_TEST_PANEL_SPACE=1"})
if err != nil || !strings.Contains(string(out), "panel space: ok") {
@@ -40,7 +43,7 @@ func init() {
fmt.Fprint(w, `<p>magpie</p>`)
})},
})
h := &host{app: app}
h := &host{app: app, tray: app.SystemTray.New()}
fail := func(format string, a ...any) {
fmt.Printf("panel space: "+format+"\n", a...)
os.Exit(1)
@@ -64,7 +67,10 @@ func init() {
if application.InvokeSyncWithResult(appFrontmost) {
fail("%s: magpie is frontmost before the icon is clicked; the test can't tell", how)
}
application.InvokeSync(func() { w.Show().Focus() }) // the icon clicked
application.InvokeSync(func() { h.placeByIcon(w); w.Show().Focus() }) // the icon clicked
if !application.InvokeSyncWithResult(func() bool { return panelLevelOK(w) }) {
fail("%s: placed by the icon, the panel is at level %d, not a menu bar extra's (NSStatusWindowLevel), so a screenshot tool's overlay can't cover it", how, application.InvokeSyncWithResult(func() int { return panelLevel(w) }))
}
waitFor(how+": the panel shows", func() bool { return application.InvokeSyncWithResult(func() bool { return windowOpen(w) }) })
// activation is asked for and comes a moment later: give it time
time.Sleep(500 * time.Millisecond)
+1 -1
View File
@@ -14,7 +14,7 @@ import (
func (h *host) sizeOpenPanel(w *application.WebviewWindow, width, height int) {
if runtime.GOOS != "windows" {
w.SetSize(width, height)
_ = h.tray.PositionWindow(w, 6)
h.placeByIcon(w)
return
}
b := w.Bounds()
+1 -1
View File
@@ -54,7 +54,7 @@ func TestPanelToldItsSideWhenShown(t *testing.T) {
if body == nil {
t.Fatal("togglePanelNow not found")
}
if !regexp.MustCompile(`(?s)PositionWindow\(h\.panel, 6\)\s*showWebview\(h\.panel\)\.Focus\(\)\s*h\.tellPanelSide\(\)\s*return`).Match(body) {
if !regexp.MustCompile(`(?s)h\.placeByIcon\(h\.panel\)\s*showWebview\(h\.panel\)\.Focus\(\)\s*h\.tellPanelSide\(\)\s*return`).Match(body) {
t.Error("the Mac's panel isn't told its side after it is placed")
}
if !regexp.MustCompile(`(?s)h\.tray\.ToggleWindow\(\)\s*if h\.panel\.IsVisible\(\) \{\s*h\.tellPanelSide\(\)`).Match(body) {
+37
View File
@@ -30,6 +30,21 @@ static int nonActivating(void *win) {
return [w isKindOfClass:[NSPanel class]] && (w.styleMask & NSWindowStyleMaskNonactivatingPanel) != 0;
}
// The quick panel's level, a menu bar extra's (NSStatusWindowLevel):
// over normal and floating windows, the Dock and the menu bar, and under
// pop-up menus and a screenshot tool's overlay above it. Wails'
// PositionWindow puts the window it places at NSPopUpMenuWindowLevel,
// where WeChat's screenshot couldn't cover the panel (zyh6036 on X).
static void setPanelLevel(void *win) {
[(NSWindow *)win setLevel:NSStatusWindowLevel];
}
static long statusLevel(void) { return (long)NSStatusWindowLevel; }
static long windowLevel(void *win) {
return (long)((NSWindow *)win).level;
}
// Can be made key: a borderless window can't unless its class says so,
// as Wails' panel class does.
static int canBeKey(void *win) {
@@ -121,6 +136,28 @@ func joinsFullScreenSpaces(w *application.WebviewWindow) bool {
return p != nil && C.joinsFullScreenSpaces(p) != 0
}
// setPanelLevel puts the quick panel at a menu bar extra's level, after
// Wails' PositionWindow put it at a pop-up menu's; on the main thread.
func setPanelLevel(w *application.WebviewWindow) {
if p := w.NativeWindow(); p != nil {
C.setPanelLevel(p)
}
}
// panelLevel is w's window level, -1 with no window; on the main thread.
func panelLevel(w *application.WebviewWindow) int {
if p := w.NativeWindow(); p != nil {
return int(C.windowLevel(p))
}
return -1
}
// panelLevelOK says whether w is at a menu bar extra's level; on the
// main thread.
func panelLevelOK(w *application.WebviewWindow) bool {
return panelLevel(w) == int(C.statusLevel())
}
// canBeKey says whether w can be made key, take the keyboard; on the
// main thread.
func canBeKey(w *application.WebviewWindow) bool {
+3
View File
@@ -7,6 +7,9 @@ import "github.com/wailsapp/wails/v3/pkg/application"
// Spaces are the Mac's: elsewhere a window is shown where it was.
func setMovesToActiveSpace(*application.WebviewWindow, bool) {}
// Only the Mac's tray puts the panel at a level of its own.
func setPanelLevel(*application.WebviewWindow) {}
func windowOpen(w *application.WebviewWindow) bool { return w.IsVisible() && !w.IsMinimised() }
// Elsewhere a window is visible covered or minimised alike.