diff --git a/internal/gui/app.go b/internal/gui/app.go index 0f2ed29d8..3f676f93f 100644 --- a/internal/gui/app.go +++ b/internal/gui/app.go @@ -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. diff --git a/internal/gui/panel_space_darwin_test.go b/internal/gui/panel_space_darwin_test.go index db588871b..9013f1131 100644 --- a/internal/gui/panel_space_darwin_test.go +++ b/internal/gui/panel_space_darwin_test.go @@ -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, `
magpie
`) })}, }) - 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) diff --git a/internal/gui/panelrefit.go b/internal/gui/panelrefit.go index 540fefc8b..65ee2d181 100644 --- a/internal/gui/panelrefit.go +++ b/internal/gui/panelrefit.go @@ -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() diff --git a/internal/gui/panelside_test.go b/internal/gui/panelside_test.go index 280abc06e..6102a8e25 100644 --- a/internal/gui/panelside_test.go +++ b/internal/gui/panelside_test.go @@ -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) { diff --git a/internal/gui/space_darwin.go b/internal/gui/space_darwin.go index c2ba1a6e8..ad90b5e58 100644 --- a/internal/gui/space_darwin.go +++ b/internal/gui/space_darwin.go @@ -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 { diff --git a/internal/gui/space_other.go b/internal/gui/space_other.go index e68450ecf..c1d16686a 100644 --- a/internal/gui/space_other.go +++ b/internal/gui/space_other.go @@ -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.