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/mainec104b862(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. Atc0477aa72the 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 onc0477aa72itself. Run before on the same change over origin/main9d08993c8, 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/main9d08993c8copy (#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.
162 lines
4.7 KiB
Go
162 lines
4.7 KiB
Go
//go:build !nogui
|
||
|
||
package gui
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"runtime"
|
||
"strings"
|
||
"time"
|
||
|
||
"github.com/wailsapp/wails/v3/pkg/application"
|
||
"github.com/wailsapp/wails/v3/pkg/events"
|
||
|
||
"github.com/yetone/magpie/internal/provider"
|
||
"github.com/yetone/magpie/internal/settings"
|
||
"github.com/yetone/magpie/internal/usage"
|
||
)
|
||
|
||
// watchTrayUsage keeps the tray's cards up to date, and magpie's name over
|
||
// today's total in the tooltip while there are none (trayIdleTip).
|
||
func (h *host) watchTrayUsage() {
|
||
wake := make(chan struct{}, 1)
|
||
onTrayUsage = func() {
|
||
select {
|
||
case wake <- struct{}{}:
|
||
default:
|
||
}
|
||
}
|
||
// a card read while stale is refreshed behind it: read it again once
|
||
// that lands, not a tick later, so the menu bar says what the panel does
|
||
provider.OnSubscriptionUsage(onTrayUsage)
|
||
// another account answering, or the agent switched to another: a card
|
||
// following the account in use reads it at once (#1516)
|
||
provider.OnServedMoved(onTrayUsage)
|
||
// the taskbar turned dark or light: the figure in its colour
|
||
if runtime.GOOS == "windows" {
|
||
h.app.Event.OnApplicationEvent(events.Windows.SystemThemeChanged, func(*application.ApplicationEvent) { onTrayUsage() })
|
||
}
|
||
// Schedule native cell clicks on the application thread after startup.
|
||
onTrayCellClick = func(id string) {
|
||
h.whenReady(func() { application.InvokeAsync(func() { h.trayCellClick(id) }) })
|
||
}
|
||
go func() {
|
||
<-h.ready // the tray is made once the app runs
|
||
shown, drawn, numbered := "", false, false
|
||
for {
|
||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||
s := settings.Load()
|
||
cells, label, tip := trayCardsView(trayUsageCards(ctx, s.TrayUsages), time.Now(), s.QuotaLeft, s.TrayPace, s.TrayWindows)
|
||
cancel()
|
||
if s.TrayNoLogos {
|
||
cells = trayPlain(cells)
|
||
}
|
||
if tip == "" {
|
||
currency, rate := trayCurrency()
|
||
tip = trayIdleTip(trayMoney(trayTotalSum(usage.Today), s.TrayNoCost), currency, rate)
|
||
}
|
||
// Windows' tray shows no text: the first card's figure is its
|
||
// icon instead (traynumber.go), drawn for the taskbar's size and
|
||
// colour
|
||
fig, figKey := trayIconFigure(runtime.GOOS, s.TrayNoNumber, cells), ""
|
||
if fig != "" {
|
||
figKey = fmt.Sprint(fig, "@", trayIconSize(), trayTaskbarDark())
|
||
}
|
||
if runtime.GOOS != "darwin" {
|
||
cells = nil
|
||
} else if label != "" {
|
||
// the menu bar sets the text hard against the icon
|
||
label = " " + label
|
||
}
|
||
// what is shown, to set it again only when it changes
|
||
now := label + "\x00" + tip + "\x00" + figKey
|
||
for _, c := range cells {
|
||
now += "\x00" + c.Letter + strings.Join(c.Rows, "\x01")
|
||
if c.Plain {
|
||
now += "\x02"
|
||
}
|
||
}
|
||
if now != shown {
|
||
shown = now
|
||
// the Mac's cells as an image, the text where it can't be
|
||
if len(cells) > 0 {
|
||
h.tray.SetLabel("")
|
||
} else {
|
||
if drawn {
|
||
drawn = false
|
||
trayImageHide()
|
||
h.tray.SetTemplateIcon(trayIcon)
|
||
}
|
||
h.tray.SetLabel(label)
|
||
}
|
||
if runtime.GOOS == "windows" {
|
||
numbered = h.setTrayNumber(fig, numbered)
|
||
}
|
||
h.tray.SetTooltip(tip)
|
||
// Wails' is a no-op on Linux; until the tray is up, again next time
|
||
if !setTrayTip(h.tray, tip) {
|
||
shown = ""
|
||
}
|
||
}
|
||
// Reapply even unchanged cells: a system menu-bar rebuild can
|
||
// replace the composed image with Wails' bird icon.
|
||
if len(cells) > 0 {
|
||
bird := trayIcon
|
||
if s.TrayNoBird {
|
||
bird = nil // the cards alone
|
||
}
|
||
if trayImageShow(cells, bird) {
|
||
drawn = true
|
||
} else {
|
||
h.tray.SetLabel(label)
|
||
shown = "" // and try again next time
|
||
}
|
||
}
|
||
// the first answer can take a while; look again soon after it
|
||
next := trayUsageEvery()
|
||
if label == "" && len(s.TrayUsages) > 0 {
|
||
next = 20 * time.Second
|
||
}
|
||
select {
|
||
case <-wake:
|
||
case <-time.After(next):
|
||
}
|
||
}
|
||
}()
|
||
}
|
||
|
||
// setTrayNumber makes Windows' tray icon fig (trayNumberIcon), or the
|
||
// app's icon again when there is none and it was one (numbered); it says
|
||
// whether the icon is a figure now. Wails sets one icon for a dark and a
|
||
// light taskbar alike, so it is drawn again when the taskbar's changes.
|
||
func (h *host) setTrayNumber(fig string, numbered bool) bool {
|
||
if fig != "" {
|
||
if icon := trayNumberIcon(fig, trayIconSize(), trayTaskbarDark()); icon != nil {
|
||
h.tray.SetIcon(icon)
|
||
return true
|
||
}
|
||
}
|
||
if numbered {
|
||
h.tray.SetIcon(winTrayIcon)
|
||
}
|
||
return false
|
||
}
|
||
|
||
// trayCellClick opens the allowance represented by the clicked image cell.
|
||
// A visible panel closes on the next click, as clicking the bird does.
|
||
func (h *host) trayCellClick(id string) {
|
||
if id == "" {
|
||
return
|
||
}
|
||
if settings.Load().Tray == "window" {
|
||
h.ShowMain(quotaView(id))
|
||
return
|
||
}
|
||
w, _ := h.panelWin()
|
||
if !windowOpen(w) {
|
||
w.ExecJS(panelQuotaJS(id))
|
||
}
|
||
h.togglePanel()
|
||
}
|