Files
magpie/internal/gui/trayglyph_test.go
T
yetone 52ee847612 gui: a Linux bar's tray icon is the bird's silhouette, not the app's tile, and the tooltip says today's total (ylorn on Discord)
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.
2026-10-15 05:30:58 +08:00

86 lines
2.8 KiB
Go

//go:build !nogui
package gui
import (
"bytes"
"image/color"
"image/png"
"testing"
"github.com/godbus/dbus/v5"
)
// ylorn on Discord: a Linux bar's tray icon was the app's icon, a bird on
// an opaque white tile, which a bar tinting its icons by their alpha
// turned into a solid block. It is the bird's silhouette alone, in one
// colour on transparency, light unless the desktop prefers light; the Mac
// keeps its template and Windows the app's icon.
func TestTrayIconPerOS(t *testing.T) {
for _, c := range []struct {
light bool
want color.NRGBA
}{{false, trayGlyphLight}, {true, trayGlyphDark}} {
icon, template := trayIconFor("linux", c.light)
if template {
t.Errorf("linux: a template icon, which no Linux bar tints")
}
img, err := png.Decode(bytes.NewReader(icon))
if err != nil {
t.Fatal(err)
}
glyph, _ := png.Decode(bytes.NewReader(trayIcon))
if img.Bounds() != glyph.Bounds() {
t.Fatalf("linux: %v, want the menu bar bird's %v", img.Bounds(), glyph.Bounds())
}
b := img.Bounds()
shown := 0
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
p := color.NRGBAModel.Convert(img.At(x, y)).(color.NRGBA)
g := color.NRGBAModel.Convert(glyph.At(x, y)).(color.NRGBA)
if p.A != g.A {
t.Fatalf("linux light=%v: alpha %d at %d,%d, want the bird's %d (a tile or another shape)", c.light, p.A, x, y, g.A)
}
if p.A > 0 {
shown++
if p.R != c.want.R || p.G != c.want.G || p.B != c.want.B {
t.Fatalf("linux light=%v: %v at %d,%d, want every pixel %v", c.light, p, x, y, c.want)
}
}
}
}
if shown == 0 || shown == b.Dx()*b.Dy() {
t.Fatalf("linux: %d of %d pixels shown, want the silhouette on transparency", shown, b.Dx()*b.Dy())
}
}
if icon, template := trayIconFor("darwin", false); !template || !bytes.Equal(icon, trayIcon) {
t.Errorf("darwin: want the template bird")
}
if icon, template := trayIconFor("windows", false); template || !bytes.Equal(icon, appIcon) {
t.Errorf("windows: want the app's own icon")
}
if img, _ := png.Decode(bytes.NewReader(trayGlyph(trayGlyphLight))); img == nil || img.ColorModel() != color.NRGBAModel {
t.Errorf("trayGlyph: want an NRGBA png, got %T", img)
}
}
// The portal's color-scheme: 1 prefers dark, 2 light, 0 neither; Read
// wraps it in a second variant, ReadOne and SettingChanged don't.
func TestPortalLight(t *testing.T) {
for _, c := range []struct {
v dbus.Variant
want bool
}{
{dbus.MakeVariant(uint32(2)), true},
{dbus.MakeVariant(dbus.MakeVariant(uint32(2))), true},
{dbus.MakeVariant(uint32(1)), false},
{dbus.MakeVariant(dbus.MakeVariant(uint32(0))), false},
{dbus.MakeVariant("light"), false},
} {
if got := portalLight(c.v); got != c.want {
t.Errorf("portalLight(%v) = %v, want %v", c.v, got, c.want)
}
}
}