Files
magpie/internal/gui/trayclick.go
T
yetone f131117976 gui: on niri the tray panel opens under the bar beside the clicked icon, not in the middle of the screen (#1670, Kenoia)
A Wayland window can't place itself. Under niri the panel opened where niri
floats a new window, the middle of the output. Hyprland already had an
adapter (omarchy/hypr.go); niri now has one too.

niri (checked in niri v26.04's source, niri-ipc/src/lib.rs and
src/ipc/server.rs) can't add window rules at runtime and doesn't report the
pointer over IPC. So omarchy.NiriPanel watches niri's event stream
($NIRI_SOCKET, one line of JSON per message) and moves each window of
magpie's pid titled "magpie panel" once as it opens:
MoveWindowToFloating if it was tiled, then MoveFloatingWindow with SetFixed
x/y, which count from the working area's top-left (below the bar). It goes 6
px under the bar, centred on the x that the tray host passed to the SNI
Activate(x, y), kept inside the output. Wails' Linux tray keeps that x in
private fields (lastClickX/lastClickY), read the same way trayProps already
reads props; TestTrayClickIsWails reads Wails' source to fail if they move.
Noctalia passes bar-surface-local coordinates, so the x is taken across
the focused output. With no click (`magpie panel`), or with one off the
output, it goes to the top right. Wails' HideOnFocusLost closes it when
another window takes the focus.

What was run:
- go test -race ./internal/omarchy/, go test ./internal/gui/ (sandbox
  HOME, pinned GOMODCACHE/GOCACHE). The IPC fixtures are built from
  niri-ipc 26.4.0's Window/Output/Event/Reply types, quoted in the test.
- Broke it by hand: dropping the placed-once map fails TestWatchWindows
  (placed [7 7 9 9 9]); dropping the right-edge clamp fails TestPanelSpot.
- gofmt -l; go vet (darwin), GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./...; linux nogui and windows builds.
- A nested niri 26.04 (winit backend on Xvfb, scale 1.5, Fedora 43
  container, magpie built with -tags gtk3), with SNI Activate sent via
  dbus-send. Activate(300, 14) put the panel at [58, 6] (300 - 484/2).
  A second Activate closed it. `magpie panel` reopened it at the top right
  (853 - 484 - 6 = 363). Activate(800, 14) clamped it to 363. `magpie gui`
  taking the focus closed it.

Not tried: a real niri desktop, Noctalia or another real tray host
(Waybar, ironbar), more than one monitor, a bar at the bottom or the side
(not accounted for), focus-follows-mouse. The panel still opens in the
middle and slides up; how visible that is on real hardware wasn't seen.

Not from this change: omarchy's TestCurrent fails with -count=2 on main
(it passes with -count=1).
2026-10-12 05:21:23 +08:00

82 lines
2.4 KiB
Go

package gui
import (
"reflect"
"runtime"
"strings"
"unsafe"
)
// menuOpenFrame is the Wails function that tells the tray's click handler
// when Linux's tray host opens the icon's menu (com.canonical.dbusmenu's
// "opened" event). KDE Plasma opens the menu on a right-click, so a click
// counted there opened the quick panel too, which took the focus and shut
// the menu on Wayland at once (#1430).
const menuOpenFrame = "pkg/application.(*linuxSystemTray).Event"
// menuOpening says the click handler was called because the tray's menu is
// opening, not because the icon was clicked: Wails calls the same handler
// for both, and only the call stack tells them apart.
func menuOpening() bool {
pcs := make([]uintptr, 32)
n := runtime.Callers(2, pcs)
frames := runtime.CallersFrames(pcs[:n])
var names []string
for {
f, more := frames.Next()
names = append(names, f.Function)
if !more {
break
}
}
return openedBy(names)
}
// openedBy says one of the callers is the menu's "opened" event.
func openedBy(callers []string) bool {
for _, c := range callers {
if strings.HasSuffix(c, menuOpenFrame) {
return true
}
}
return false
}
// trayImpl is the struct behind a Wails SystemTray's impl, the platform's
// tray (Linux's linuxSystemTray), given the SystemTray itself; not valid
// once a Wails version keeps it elsewhere.
func trayImpl(tray reflect.Value) (impl reflect.Value) {
defer func() {
if recover() != nil {
impl = reflect.Value{}
}
}()
i := tray.FieldByName("impl")
if !i.IsValid() || i.Kind() != reflect.Interface || i.IsNil() {
return reflect.Value{}
}
p := i.Elem()
if p.Kind() != reflect.Pointer || p.IsNil() || p.Elem().Kind() != reflect.Struct {
return reflect.Value{}
}
return p.Elem()
}
// clickIn reads the click Wails' Linux tray keeps from the tray host's
// Activate(x, y), lastClickX and lastClickY, given the SystemTray. (0, 0)
// is no click, as Wails reads it too: a host that passes no coordinates
// sends that.
func clickIn(tray reflect.Value) (x int, ok bool) {
impl := trayImpl(tray)
if !impl.IsValid() {
return 0, false
}
fx, fy := impl.FieldByName("lastClickX"), impl.FieldByName("lastClickY")
if !fx.IsValid() || !fy.IsValid() || fx.Kind() != reflect.Int || fy.Kind() != reflect.Int || !fx.CanAddr() {
return 0, false
}
x = *(*int)(unsafe.Pointer(fx.UnsafeAddr()))
y := *(*int)(unsafe.Pointer(fy.UnsafeAddr()))
return x, x != 0 || y != 0
}