Files
magpie/internal/gui/traytip_linux.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

112 lines
3.6 KiB
Go

//go:build linux && !nogui
package gui
import (
"log"
"reflect"
"unsafe"
"github.com/godbus/dbus/v5"
"github.com/godbus/dbus/v5/prop"
"github.com/wailsapp/wails/v3/pkg/application"
)
// setTrayTip puts the tooltip on the tray's StatusNotifierItem. Wails
// (v3.0.0-beta.24) leaves SetTooltip on Linux a no-op ("TBD"): the item's
// ToolTip stays the "magpie" it was made with and no NewToolTip is sent, so
// a bar never had the quotas to show (atie on Discord: Omarchy's bar showed
// none on hover with 菜单栏显示额度 on). This sets the property Wails
// exported, through its own prop.Properties (which keeps the (sa(iiay)ss)
// type), and sends NewToolTip from the same connection: waybar reads ToolTip
// again only on that signal, Quickshell on it or PropertiesChanged.
func setTrayTip(tray *application.SystemTray, tip string) bool {
props := trayProps(tray)
if props == nil {
return false
}
v := struct {
V0 string // icon name
V1 []application.PX // icon pixmaps
V2 string // title: what Omarchy's bar shows, and waybar
V3 string // description: waybar only, under the title
}{V2: sniTip(tip), V1: []application.PX{}}
if err := props.Set("org.kde.StatusNotifierItem", "ToolTip", dbus.MakeVariant(v)); err != nil {
log.Println("tray: tooltip:", err)
return false
}
conn, err := dbus.SessionBus() // the connection Wails' tray is on
if err != nil {
return false
}
if err := conn.Emit("/StatusNotifierItem", "org.kde.StatusNotifierItem.NewToolTip"); err != nil {
log.Println("tray: tooltip signal:", err)
}
return true
}
// trayProps is the item's exported properties inside Wails' Linux tray
// (SystemTray.impl.props), nil once a Wails version keeps them elsewhere.
func trayProps(tray *application.SystemTray) (props *prop.Properties) {
defer func() {
if recover() != nil {
props = nil
}
}()
if tray == nil {
return nil
}
impl := trayImpl(reflect.ValueOf(tray).Elem())
if !impl.IsValid() {
return nil
}
f := impl.FieldByName("props")
if !f.IsValid() || f.Type() != reflect.TypeOf((*prop.Properties)(nil)) {
return nil
}
return *(**prop.Properties)(unsafe.Pointer(f.UnsafeAddr()))
}
// trayClick is where the tray host said the icon was clicked, across its
// bar: Wails' Linux tray keeps Activate(x, y)'s coordinates
// (SystemTray.impl.lastClickX) and passes the click handler none. ok is
// false when they aren't there to read.
func trayClick(tray *application.SystemTray) (x int, ok bool) {
if tray == nil {
return 0, false
}
return clickIn(reflect.ValueOf(tray).Elem())
}
// setTrayPassive takes the tray's StatusNotifierItem out of the bar's tray
// (Status "Passive") while magpie's icon is in Omarchy's bar as its widget,
// and puts it back ("Active") when it isn't. Omarchy's tray and waybar's both
// leave a Passive item out. Wails exports Status read-only and has no Hide
// on Linux, so it is set through Wails' own properties, as the tooltip is,
// and NewStatus is sent for the hosts that read it again only on that.
func setTrayPassive(tray *application.SystemTray, passive bool) (ok bool) {
props := trayProps(tray)
if props == nil {
return false
}
status := "Active"
if passive {
status = "Passive"
}
defer func() {
if r := recover(); r != nil {
log.Println("tray: status:", r)
ok = false
}
}()
props.SetMust("org.kde.StatusNotifierItem", "Status", status)
conn, err := dbus.SessionBus() // the connection Wails' tray is on
if err != nil {
return false
}
if err := conn.Emit("/StatusNotifierItem", "org.kde.StatusNotifierItem.NewStatus", status); err != nil {
log.Println("tray: status signal:", err)
}
return true
}