Files
magpie/internal/gui/traynumber_test.go
T
yetone da275848b6 tray: Windows shows a card's number in its icon (Benjaminliang_1 on X)
Windows' notification area shows no text beside an icon, so with a card
in the tray the icon itself is now the first card's figure: the first
row of the first card whose first row is a percentage, used or left as
Allowance display has it, its digits alone (trayIconFigure). It is drawn
in Segoe UI Semibold (Go's bold if the font can't be read) at
SM_CXSMICON, white on a dark taskbar, near black on a light one, as tall
as fits, centred by its ink, three digits narrowed into the square
(trayNumberIcon). It is drawn again when the figure, the icon size or
the taskbar theme changes (SystemThemeChanged wakes the loop); the
tooltip still lists every card. A balance or magpie's totals are
skipped; with no such card, or no cards at all, the icon is the bird as
before. Settings > Usage > Tray icon number (settings.TrayNoNumber,
Windows only, on by default, kept per computer) keeps the bird when off;
Menu bar windows is now shown on Windows too, since it picks the row
the number comes from. The number row sits after Menu bar cost, so the
bird > pace > cost order the other tests pin holds. Linux and the Mac
are unchanged. New words in zh, zh-TW, ja and de. app.js stays in the
core budget with one comment shortened, no raise.

Run at this commit on origin/main ec104b862 (traywindows.js merged with
94f6f6092's trayExtra pills; its "not on Windows" note dropped), mote:
gofmt -l empty; go vet and GOOS=windows go vet of gui and settings ok;
go test -count=1 -timeout 45m ./internal/settings ./internal/davsync
ok; ./internal/gui red only on TestMainWindowCorners (#3949),
TestCoreScriptBudget ok. GUI tray-windows, tray-cost, quota-pace,
i18n-keys, tray-bird, tray-logos, tray-usages, quit-ask: 140 pass,
0 fail. win-run on xiaomi-gpu: TestTrayNumberIcon and
TestTrayIconFigure ok. At c0477aa72 the same GUI set once had
quota-pace webkit "On moved the page" (439 !== 120), a late wheel
(#4784); green twice after, and quota-pace alone 3 of 3 green there
and on c0477aa72 itself.

Run before on the same change over origin/main 9d08993c8, mote:
- gofmt -l on gui/settings/davsync: empty; go vet ./internal/gui
  ./internal/settings and GOOS=windows go vet: ok.
- go test -count=1 -timeout 45m ./internal/gui ./internal/settings
  ./internal/davsync: settings ok, davsync ok; gui red only on
  TestMainWindowCorners/macOS_26_SDK (#3949) and TestCoreScriptBudget
  (5 bytes over after the rebase), which is green after the comment
  trim, rerun with TestTrayNumberIcon and TestTrayIconFigure: ok.
- GUI (both engines): tray-windows, tray-cost, quota-pace: 56 pass,
  0 fail. Earlier at this tree: tray-bird, tray-logos, tray-usages,
  i18n-keys green; quota-pace webkit de 420 red once, from a late wheel
  scroll (#4784), green on the rerun; segs-press-redraw red on
  searchAllRow, the same on a clean origin/main 9d08993c8 copy (#4566).
- A 184-file GUI run (the files touching Settings, the tray or i18n,
  before the rebase) under mote load 40-90: 3095 pass, 224 fail. My
  files' reds there (tray-cost: the number row was between pace and
  cost) are fixed above. The rest are in files this change doesn't
  reach (account-alias, model chips, menu-input-scroll, mobile-web,
  omarchy, plugin-github, key-replace, context-map and others; #4600
  lists several). Each was not compared on main one by one.
Windows (win-run on xiaomi-gpu, real Segoe UI): TestTrayNumberIcon and
TestTrayIconFigure ok.
Real Windows 11 run: a production build in a portable sandbox (own
data\, USERPROFILE/APPDATA/LOCALAPPDATA redirected, gateway on a free
port, a fake remote magpie serving one Claude card). The dark taskbar
showed a white "42", then a narrowed "100" when the card read 100%,
and the bird with trayNoNumber set. Only my processes, tasks and
NotifyIconSettings entry were removed afterwards.

Broken by hand: trayIconFigure always "" turns TestTrayIconFigure red
(6 cases); the dark and light colours swapped turn TestTrayNumberIcon
red; the old advance-width centring turns it red too.

Not tried: a light taskbar on the real box (it would change the user's
theme; covered by the test only), hovering the tooltip (screen locked),
other DPIs on real hardware, Windows on ARM64. Linux's code is
unchanged and was not run.
2026-11-01 21:31:55 +08:00

111 lines
4.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//go:build !nogui
package gui
import (
"bytes"
"image"
"image/color"
"image/png"
"testing"
"time"
)
// Windows' tray icon is the first card's figure (Benjaminliang_1 on X),
// the percentage as the Mac's menu bar shows it, used or left; the
// next card's when the first has none; the bird with no cards, off
// Windows, or with Settings › Tray icon number off.
func TestTrayIconFigure(t *testing.T) {
now := time.Date(2026, 9, 30, 12, 0, 0, 0, time.UTC)
cards := trayCards()
view := func(ids []string, left bool, pick map[string][]string) []trayCell {
cells, _, _ := trayCardsView(trayPick(cards, ids, now), now, left, false, pick)
return cells
}
for _, tc := range []struct {
name string
goos string
noNumber bool
cells []trayCell
want string
}{
{"the first card's first window", "windows", false, view([]string{"claude|a@b.c", "codex|x@y.z"}, false, nil), "42"},
{"what is left, as Settings has it", "windows", false, view([]string{"claude|a@b.c"}, true, nil), "58"},
{"the window picked for the menu bar", "windows", false, view([]string{"claude|a@b.c"}, false, map[string][]string{"claude": {"7d"}}), "18"},
{"a full window", "windows", false, view([]string{"codex|x@y.z"}, false, map[string][]string{"codex": {"7d"}}), "100"},
{"a balance first: the next card's", "windows", false, view([]string{"deepseek", "copilot"}, false, nil), "63"},
{"a balance alone: the bird", "windows", false, view([]string{"deepseek"}, false, nil), ""},
{"no cards: the bird", "windows", false, view(nil, false, nil), ""},
{"turned off: the bird", "windows", true, view([]string{"claude|a@b.c"}, false, nil), ""},
{"the Mac draws its cells", "darwin", false, view([]string{"claude|a@b.c"}, false, nil), ""},
{"Linux shows the label", "linux", false, view([]string{"claude|a@b.c"}, false, nil), ""},
{"with Pace beside it", "windows", false, []trayCell{{Rows: []string{"42% +3%"}}}, "42"},
{"a balance's own text", "windows", false, []trayCell{{Rows: []string{"1234%x"}}, {Rows: []string{"Σ1.2M"}}}, ""},
} {
if got := trayIconFigure(tc.goos, tc.noNumber, tc.cells); got != tc.want {
t.Errorf("%s: %q, want %q", tc.name, got, tc.want)
}
}
}
// The figure is drawn at the size Windows asks for, in the taskbar's text
// colour on transparency, as tall as fits with room above and below, and
// "100" narrowed into the square, not cut.
func TestTrayNumberIcon(t *testing.T) {
// off by more than the rounding of a half-covered pixel
far := func(a, b uint8) bool { d := int(a) - int(b); return d < -4 || d > 4 }
for _, size := range []int{16, 20, 24, 32} {
for _, dark := range []bool{true, false} {
for _, fig := range []string{"7", "42", "100"} {
b := trayNumberIcon(fig, size, dark)
img, err := png.Decode(bytes.NewReader(b))
if err != nil {
t.Fatalf("%s at %d: %v", fig, size, err)
}
if img.Bounds() != image.Rect(0, 0, size, size) {
t.Fatalf("%s at %d: %v", fig, size, img.Bounds())
}
want := trayNumberColour(dark)
// white on a dark taskbar, near black on a light one
if lum := int(want.R) + int(want.G) + int(want.B); dark != (lum > 3*200) || !dark && lum > 3*64 || want.A != 0xff {
t.Fatalf("dark=%v: drawn in %v", dark, want)
}
ink := image.Rectangle{}
for y := range size {
for x := range size {
c := color.NRGBAModel.Convert(img.At(x, y)).(color.NRGBA)
if c.A < 0x80 {
continue
}
if far(c.R, want.R) || far(c.G, want.G) || far(c.B, want.B) {
t.Fatalf("%s at %d dark=%v: (%d,%d) is %v, want %v", fig, size, dark, x, y, c, want)
}
ink = ink.Union(image.Rect(x, y, x+1, y+1))
}
}
if ink.Empty() {
t.Fatalf("%s at %d: nothing drawn", fig, size)
}
// legible: the digits most of the square's height, a pixel
// or more clear of its top and bottom
if h := ink.Dy(); h < size*6/10 || ink.Min.Y < 1 || ink.Max.Y > size-1 {
t.Errorf("%s at %d: ink %v", fig, size, ink)
}
// centred, and three digits fill the width without leaving it
if d := ink.Min.X - (size - ink.Max.X); d < -2 || d > 2 {
t.Errorf("%s at %d: off centre, ink %v", fig, size, ink)
}
if fig == "100" && ink.Dx() < size*8/10 {
t.Errorf("100 at %d: %d wide", size, ink.Dx())
}
}
}
}
if bytes.Equal(trayNumberIcon("42", 16, true), trayNumberIcon("17", 16, true)) {
t.Fatal("42 and 17 drawn alike")
}
if trayNumberIcon("", 16, true) != nil || trayNumberIcon("42", 0, true) != nil {
t.Fatal("drawn with no figure or no size")
}
}