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

87 lines
2.7 KiB
Go

//go:build !nogui
package gui
import (
"bytes"
"image"
"image/color"
"image/png"
"github.com/godbus/dbus/v5"
)
// A Linux bar's tray icon is the menu bar's bird alone, in one colour on
// transparency, as the icons beside it are (ylorn on Discord): the app's
// own icon, a coloured bird on a white tile, stood out in a row of them, and
// a bar that tints its icons to the theme by their alpha made the tile a
// solid block. A bar that doesn't tint shows the colour given: light, as
// most bars are dark, unless the desktop prefers light (traySchemeLight).
var (
trayGlyphLight = color.NRGBA{0xff, 0xff, 0xff, 0xff}
trayGlyphDark = color.NRGBA{0x1f, 0x1f, 0x1f, 0xff}
)
// trayGlyph is trayIcon, the bird's silhouette (a black glyph on
// transparency), in c: each pixel keeps the glyph's alpha. trayIcon itself
// when it can't be read, which is still a silhouette.
func trayGlyph(c color.NRGBA) []byte {
src, err := png.Decode(bytes.NewReader(trayIcon))
if err != nil {
return trayIcon
}
b := src.Bounds()
out := image.NewNRGBA(b)
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
a := color.NRGBAModel.Convert(src.At(x, y)).(color.NRGBA).A
out.SetNRGBA(x, y, color.NRGBA{c.R, c.G, c.B, uint8(uint(a) * uint(c.A) / 0xff)})
}
}
var buf bytes.Buffer
if png.Encode(&buf, out) != nil {
return trayIcon
}
return buf.Bytes()
}
// trayIconFor is the tray's icon on goos, and whether it is a template
// image the system tints (the Mac's menu bar): the bird alone on the Mac
// and Linux (linuxTrayIcon, light saying the desktop prefers a light
// scheme), the app's coloured icon in Windows' tray, which shows apps' own.
func trayIconFor(goos string, light bool) (icon []byte, template bool) {
switch goos {
case "darwin":
return trayIcon, true
case "linux":
return linuxTrayIcon(light), false
}
return appIcon, false
}
// linuxTrayIcon is the glyph for a desktop whose colour scheme preference
// (the portal's org.freedesktop.appearance color-scheme) is light or not:
// dark only where light is asked for; "no preference" or one that can't be
// read is light, the bars of GNOME, Omarchy and waybar's own style being
// dark.
func linuxTrayIcon(light bool) []byte {
if light {
return trayGlyph(trayGlyphDark)
}
return trayGlyph(trayGlyphLight)
}
// portalLight reads the portal's color-scheme value as linuxTrayIcon takes
// it: light only for 2 (prefer light). Settings.Read wraps the value in a
// second variant and ReadOne and SettingChanged don't, so either is
// unwrapped; anything else is no preference.
func portalLight(v dbus.Variant) bool {
for range 2 {
if in, ok := v.Value().(dbus.Variant); ok {
v = in
}
}
n, ok := v.Value().(uint32)
return ok && n == 2
}