ylorn (Hyprland, 0.1.1241): the StatusNotifierItem's IconPixmap was the
coloured app icon on an opaque white tile, which clashed with a bar's
monochrome icons and became a solid block on bars that tint icons by
alpha; the tooltip said only "magpie".
- Linux now gets the macOS menu bar's template bird (tray.png) in one
colour on transparency (trayIconFor / linuxTrayIcon, trayglyph.go):
light, or dark when xdg-desktop-portal's org.freedesktop.appearance
color-scheme is 2 (prefer light); it is set again on the portal's
SettingChanged (watchTrayScheme, own D-Bus connection). An unreadable
portal counts as no preference (light). No IconName is set: a host
that resolves a -symbolic name but draws it untinted would show a
black bird on a dark bar with no pixmap fallback. macOS keeps its
template icon, Windows the coloured app icon.
- With no cards in the menu bar the tooltip is "magpie" over one line of
today's total, as the panel's Usage tab sums it (trayIdleTip: all
tokens, cost in Settings' currency, requests), refreshed with the
cards' loop. Cards picked: unchanged.
- The menu is unchanged (Open / version / Quit): no info lines. The
panel one click away has the models and usage, and the menu is shared
with Windows and macOS.
Run (sandbox HOME, pinned GOMODCACHE, shared GOCACHE), on origin/main
21aa40285:
- TestTrayIconPerOS fails with Linux given appIcon back ("linux:
(0,0)-(64,64), want the menu bar bird's (0,0)-(44,44)"); TestTrayIdleTip
fails with the tooltip back to plain "magpie". Both pass restored, with
TestPortalLight, TestTrayTotals, TestTrayIconSize, TestSNITipIsSafeMarkup.
- gofmt -l clean; go vet ./... (darwin), GOOS=windows go vet ./...,
GOOS=linux go vet -tags nogui ./... clean; GOOS=windows go build and
GOOS=linux go build -tags nogui OK.
- traytip_linux.go (linux && !nogui, cgo) can't be vetted from macOS: the
two new functions were copied into a darwin build and vetted there.
- go test -p 4 -timeout 40m ./...: exit 0, 53 ok, no FAIL.
- GUI: tray-*.test.cjs (Playwright 1.62.0): 112/112 pass.
Not tried on a real Linux bar (the Windows box with its WSL2/Omarchy VM
was unreachable over ssh): the pixmap, its colour on a light/dark scheme
and the tooltip on waybar/Quickshell/KDE are unverified on hardware.
159 lines
5.0 KiB
Go
159 lines
5.0 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
|
|
}
|
|
|
|
// traySchemeLight says whether the desktop prefers a light colour scheme,
|
|
// as xdg-desktop-portal's Settings has it; false for dark, for no
|
|
// preference, and when there is no portal to ask.
|
|
func traySchemeLight() bool {
|
|
conn, err := dbus.SessionBus()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
obj := conn.Object("org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop")
|
|
var v dbus.Variant
|
|
if err := obj.Call("org.freedesktop.portal.Settings.Read", 0, "org.freedesktop.appearance", "color-scheme").Store(&v); err != nil {
|
|
return false
|
|
}
|
|
return portalLight(v)
|
|
}
|
|
|
|
// watchTrayScheme calls f with the new preference each time the desktop's
|
|
// colour scheme changes (the portal's SettingChanged), on a connection of
|
|
// its own so Wails' signals on the shared one aren't held up by it.
|
|
func watchTrayScheme(f func(light bool)) {
|
|
conn, err := dbus.ConnectSessionBus()
|
|
if err != nil {
|
|
return
|
|
}
|
|
if err := conn.AddMatchSignal(
|
|
dbus.WithMatchObjectPath("/org/freedesktop/portal/desktop"),
|
|
dbus.WithMatchInterface("org.freedesktop.portal.Settings"),
|
|
dbus.WithMatchMember("SettingChanged"),
|
|
); err != nil {
|
|
conn.Close()
|
|
return
|
|
}
|
|
ch := make(chan *dbus.Signal, 8)
|
|
conn.Signal(ch)
|
|
for s := range ch {
|
|
if len(s.Body) < 3 {
|
|
continue
|
|
}
|
|
ns, _ := s.Body[0].(string)
|
|
key, _ := s.Body[1].(string)
|
|
v, ok := s.Body[2].(dbus.Variant)
|
|
if ns == "org.freedesktop.appearance" && key == "color-scheme" && ok {
|
|
f(portalLight(v))
|
|
}
|
|
}
|
|
}
|