usage: the menu bar's usage is refreshed as often as asked — every 1, 3, 5, 10 or 30 minutes, 3 as before — and reads as used or left as the Usage page does; that choice is a setting now rather than the browser's, so the page, the panel and the menu bar agree, and Settings has it too; a browser's choice from before is carried over once (#122)
This commit is contained in:
+23
-1
@@ -325,6 +325,8 @@ func Handler(w Windows, gw *gateway.Server) http.Handler {
|
||||
// who sees them, and sharing on the network, set on its own
|
||||
in.Visible, in.ModelNames, in.ModelEfforts = cur.Visible, cur.ModelNames, cur.ModelEfforts
|
||||
in.LAN, in.LANKey = cur.LAN, cur.LANKey
|
||||
// used or left is the Usage page's toggle as much as Settings', set on its own
|
||||
in.QuotaLeft = cur.QuotaLeft
|
||||
if err := settings.Save(in); err != nil {
|
||||
fail(rw, err)
|
||||
return
|
||||
@@ -332,7 +334,27 @@ func Handler(w Windows, gw *gateway.Server) http.Handler {
|
||||
if (in.Dock != cur.Dock || in.DockWindow != cur.DockWindow) && onDock != nil {
|
||||
onDock(in)
|
||||
}
|
||||
if in.TrayUsage != cur.TrayUsage && onTrayUsage != nil {
|
||||
if (in.TrayUsage != cur.TrayUsage || in.TrayUsageEvery != cur.TrayUsageEvery) && onTrayUsage != nil {
|
||||
onTrayUsage()
|
||||
}
|
||||
writeJSON(rw, settingsState())
|
||||
})
|
||||
// whether usage reads as used or left: the Usage page's toggle and
|
||||
// Settings', for every meter and the menu bar alike (#122)
|
||||
mux.HandleFunc("POST /api/settings/quota-left", func(rw http.ResponseWriter, r *http.Request) {
|
||||
var in struct{ On bool }
|
||||
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
||||
fail(rw, err)
|
||||
return
|
||||
}
|
||||
s := settings.Load()
|
||||
changed := s.QuotaLeft != in.On
|
||||
s.QuotaLeft = in.On
|
||||
if err := settings.Save(s); err != nil {
|
||||
fail(rw, err)
|
||||
return
|
||||
}
|
||||
if changed && onTrayUsage != nil {
|
||||
onTrayUsage()
|
||||
}
|
||||
writeJSON(rw, settingsState())
|
||||
|
||||
@@ -3485,10 +3485,9 @@ function accountQuota(data, user) {
|
||||
|
||||
// A window reads as how much of it is used, or — as the vendors' own apps
|
||||
// show it — how much is left, the bar filling with that; one choice for
|
||||
// every meter, kept for next time. The vendor's own count, where it gives
|
||||
// one, stands before the percentage.
|
||||
// every meter and the menu bar, kept in the settings (#122). The vendor's
|
||||
// own count, where it gives one, stands before the percentage.
|
||||
let quotaLeft = false;
|
||||
try { quotaLeft = localStorage.getItem("magpie.quotaLeft") === "1"; } catch {}
|
||||
function quotaFill(w) {
|
||||
const used = Math.round(Math.max(0, Math.min(100, w.used)));
|
||||
return quotaLeft ? 100 - used : used;
|
||||
@@ -3497,10 +3496,16 @@ function quotaText(w) {
|
||||
const pct = t(quotaLeft ? "{n} left" : "{n} used", { n: quotaFill(w) + "%" });
|
||||
return w.display ? w.display + " · " + pct : pct;
|
||||
}
|
||||
function setQuotaLeft(on) {
|
||||
async function setQuotaLeft(on) {
|
||||
quotaLeft = on;
|
||||
try { localStorage.setItem("magpie.quotaLeft", on ? "1" : "0"); } catch {}
|
||||
renderQuotas();
|
||||
try {
|
||||
prefs = await api("settings/quota-left", { on });
|
||||
state.settings = prefs;
|
||||
if (view === "settings") renderSettings();
|
||||
} catch (e) {
|
||||
status(e.message, "err");
|
||||
}
|
||||
}
|
||||
|
||||
// resetClock is when a window starts again, on the clock: "14:30" today,
|
||||
@@ -4445,6 +4450,15 @@ function applyPrefs(s) {
|
||||
}
|
||||
}
|
||||
applyPrefs.ready = true;
|
||||
// this browser's choice from before it was a setting, carried over once
|
||||
let kept = null;
|
||||
try { kept = localStorage.getItem("magpie.quotaLeft"); localStorage.removeItem("magpie.quotaLeft"); } catch {}
|
||||
if (kept === "1" && !s.quotaLeft) { s.quotaLeft = true; setQuotaLeft(true); }
|
||||
if (quotaLeft !== !!s.quotaLeft) {
|
||||
quotaLeft = !!s.quotaLeft;
|
||||
if (applyPrefs.painted) renderQuotas();
|
||||
}
|
||||
applyPrefs.painted = true;
|
||||
const was = locale;
|
||||
setLocale(s.lang);
|
||||
if (was !== locale && mode === "window") queueMicrotask(() => slide($("#nav"), "nav"));
|
||||
@@ -4463,7 +4477,7 @@ function renderSettings() {
|
||||
const keep = { theme: s.theme, lang: s.lang, tray: s.tray, dock: !!s.dock, dockWindow: !!s.dockWindow, proxy: s.proxy || "",
|
||||
redact: !!s.redact, redactPersonal: !!s.redactPersonal, redactWords: s.redactWords || [], codexWarmup: s.codexWarmup || "",
|
||||
claudeWarmup: s.claudeWarmup || "",
|
||||
trayUsage: s.trayUsage || "" };
|
||||
trayUsage: s.trayUsage || "", trayUsageEvery: s.trayUsageEvery || 3 };
|
||||
$("#themeSegs").replaceChildren(segs(THEMES.map(([id, name]) => [id, t(name)]), s.theme, (theme) => savePrefs({ ...keep, theme })));
|
||||
$("#langSegs").replaceChildren(segs(LOCALES.map(([id, name]) => [id, t(name)]), s.lang, (lang) => savePrefs({ ...keep, lang })));
|
||||
$("#traySegs").replaceChildren(segs(TRAYS.map(([id, name]) => [id, t(name)]), s.tray || "panel", (tray) => savePrefs({ ...keep, tray })));
|
||||
@@ -4736,6 +4750,8 @@ const trayCardID = (q) => q.user ? q.provider + "|" + q.user : q.provider;
|
||||
// renderTrayUsage: the subscription or plan whose windows show beside the
|
||||
// tray icon. The cards are the Usage page's, asked for when the menu opens.
|
||||
function renderTrayUsage(s, keep) {
|
||||
$("#quotaLeftSegs").replaceChildren(segs([[false, t("Used")], [true, t("Left")]], !!s.quotaLeft,
|
||||
(on) => { if (on !== quotaLeft) setQuotaLeft(on); }));
|
||||
$("#trayUsageRow").hidden = web;
|
||||
if (web) return;
|
||||
const mac = document.body.classList.contains("mac");
|
||||
@@ -4761,7 +4777,13 @@ function renderTrayUsage(s, keep) {
|
||||
openProtoMenu(pill, opts, id, (v) => { if (v !== id && v !== "\x00") savePrefs({ ...keep, trayUsage: v }); }, "Shown beside the icon");
|
||||
};
|
||||
$("#trayUsagePick").replaceChildren(pill);
|
||||
// how often it is asked for again, and whether it reads as used or left —
|
||||
// the Usage page's choice too (#122)
|
||||
$("#trayEveryRow").hidden = !id;
|
||||
$("#trayEverySegs").replaceChildren(segs(TRAY_EVERY.map((m) => [m, t("{n} min", { n: m })]), s.trayUsageEvery || 3,
|
||||
(trayUsageEvery) => savePrefs({ ...keep, trayUsageEvery })));
|
||||
}
|
||||
const TRAY_EVERY = [1, 3, 5, 10, 30];
|
||||
|
||||
// renderProxy: magpie's own requests to vendors follow the system proxy on
|
||||
// their own; this row says which one, and lets it be turned off or set.
|
||||
|
||||
@@ -95,6 +95,10 @@ const I18N = {
|
||||
"Show a subscription's use beside magpie's icon in the menu bar, refreshed every few minutes": "在菜单栏 magpie 图标旁显示一个订阅的用量,每隔几分钟刷新",
|
||||
"Show a subscription's use when pointing at magpie's tray icon, refreshed every few minutes": "鼠标指向托盘里的 magpie 图标时显示一个订阅的用量,每隔几分钟刷新",
|
||||
"Shown beside the icon": "显示在图标旁",
|
||||
"Menu bar refresh": "菜单栏刷新频率",
|
||||
"How often the usage beside the icon is asked for again": "图标旁的用量多久重新获取一次",
|
||||
"Usage shows": "用量显示",
|
||||
"How much of each window is used, or how much is left, on the Usage page, the panel and the menu bar": "每个窗口显示已用多少或剩余多少,用量页、面板和菜单栏一致",
|
||||
"No subscriptions yet": "还没有订阅",
|
||||
"Sign in to one, or add a plan's key, and it shows on the Usage page": "登录一个订阅或添加套餐的 Key 后,会出现在用量页",
|
||||
"Weekly": "每周",
|
||||
|
||||
@@ -137,6 +137,14 @@
|
||||
<div class="who"><div class="name" data-t>Usage in the menu bar</div><div class="sub" id="trayUsageSub"></div></div>
|
||||
<div id="trayUsagePick"></div>
|
||||
</div>
|
||||
<div class="row pref" id="trayEveryRow" hidden>
|
||||
<div class="who"><div class="name" data-t>Menu bar refresh</div><div class="sub" data-t>How often the usage beside the icon is asked for again</div></div>
|
||||
<div id="trayEverySegs"></div>
|
||||
</div>
|
||||
<div class="row pref">
|
||||
<div class="who"><div class="name" data-t>Usage shows</div><div class="sub" data-t>How much of each window is used, or how much is left, on the Usage page, the panel and the menu bar</div></div>
|
||||
<div id="quotaLeftSegs"></div>
|
||||
</div>
|
||||
<div class="row pref">
|
||||
<div class="who"><div class="name" data-t>Open at login</div><div class="sub" data-t>Start magpie in the tray when you log in, so agents find the gateway from the start</div></div>
|
||||
<div id="loginSegs"></div>
|
||||
|
||||
@@ -18,10 +18,13 @@ import (
|
||||
// opening magpie. The Mac's menu bar shows the text; Windows' tray has no
|
||||
// room for any, and gets it in the icon's tooltip only.
|
||||
|
||||
// trayUsageEvery is how often the text is brought up to date. The vendors
|
||||
// are asked no more than the Usage page asks them: their answers are
|
||||
// cached for a minute, and this reads the cache.
|
||||
const trayUsageEvery = 3 * time.Minute
|
||||
// trayUsageEvery is how often the text is brought up to date, as Settings
|
||||
// says (every 3 minutes unless told otherwise). The vendors are asked no
|
||||
// more than the Usage page asks them: their answers are cached for a
|
||||
// minute, and this reads the cache.
|
||||
func trayUsageEvery() time.Duration {
|
||||
return time.Duration(settings.Load().TrayUsageEvery) * time.Minute
|
||||
}
|
||||
|
||||
// trayCardID names a card for settings.TrayUsage: its provider, and the
|
||||
// account when there is one, as two of one vendor can be signed in.
|
||||
@@ -52,8 +55,9 @@ func trayWindows(q provider.SubscriptionQuota) []provider.QuotaWindow {
|
||||
}
|
||||
|
||||
// trayUsageText is the menu bar's text for a card and the tooltip that
|
||||
// spells it out: each window's use, and when it starts again.
|
||||
func trayUsageText(q provider.SubscriptionQuota, now time.Time) (label, tip string) {
|
||||
// spells it out: each window's use, or what is left of it (left), and when
|
||||
// it starts again.
|
||||
func trayUsageText(q provider.SubscriptionQuota, now time.Time, left bool) (label, tip string) {
|
||||
if q.Error != "" {
|
||||
return "", q.Name + ": " + q.Error
|
||||
}
|
||||
@@ -66,11 +70,16 @@ func trayUsageText(q provider.SubscriptionQuota, now time.Time) (label, tip stri
|
||||
}
|
||||
var short, long []string
|
||||
for _, w := range ws {
|
||||
pct := fmt.Sprintf("%d%%", int(math.Round(math.Max(0, math.Min(100, w.Used)))))
|
||||
n := int(math.Round(math.Max(0, math.Min(100, w.Used))))
|
||||
word := "used"
|
||||
if left {
|
||||
n, word = 100-n, "left"
|
||||
}
|
||||
pct := fmt.Sprintf("%d%%", n)
|
||||
short = append(short, pct)
|
||||
line := w.Name + " " + pct
|
||||
line := w.Name + " " + pct + " " + word
|
||||
if w.Display != "" {
|
||||
line = w.Name + " " + w.Display
|
||||
line = w.Name + " " + w.Display + " · " + pct + " " + word
|
||||
}
|
||||
if at := resetAt(w, now); !at.IsZero() && at.After(now) {
|
||||
line += " · resets in " + until(at.Sub(now))
|
||||
|
||||
@@ -19,13 +19,21 @@ func TestTrayUsageText(t *testing.T) {
|
||||
{Name: "Opus weekly", Used: 90, Model: "opus", Span: 7 * 24 * time.Hour},
|
||||
{Name: "Extra usage", Used: 5, Aside: true},
|
||||
}}
|
||||
label, tip := trayUsageText(q, now)
|
||||
label, tip := trayUsageText(q, now, false)
|
||||
if label != "42% · 18%" {
|
||||
t.Errorf("label %q", label)
|
||||
}
|
||||
if want := "Claude\n5-hour 42% · resets in 2h 10m\nWeekly 18% · resets in 3d 4h"; tip != want {
|
||||
if want := "Claude\n5-hour 42% used · resets in 2h 10m\nWeekly 18% used · resets in 3d 4h"; tip != want {
|
||||
t.Errorf("tip %q, want %q", tip, want)
|
||||
}
|
||||
// or what is left of each, as Settings or the Usage page says (#122)
|
||||
label, tip = trayUsageText(q, now, true)
|
||||
if label != "58% · 82%" {
|
||||
t.Errorf("left label %q", label)
|
||||
}
|
||||
if want := "Claude\n5-hour 58% left · resets in 2h 10m\nWeekly 82% left · resets in 3d 4h"; tip != want {
|
||||
t.Errorf("left tip %q, want %q", tip, want)
|
||||
}
|
||||
if id := trayCardID(q); id != "claude|a@b.c" {
|
||||
t.Errorf("id %q", id)
|
||||
}
|
||||
@@ -36,11 +44,11 @@ func TestTrayUsageText(t *testing.T) {
|
||||
{Name: "Weekly", Used: -3, ResetSecs: 90},
|
||||
{Name: "Monthly", Used: 1},
|
||||
}}
|
||||
label, tip = trayUsageText(q, now)
|
||||
label, tip = trayUsageText(q, now, false)
|
||||
if label != "100% · 0%" {
|
||||
t.Errorf("label %q", label)
|
||||
}
|
||||
if want := "ZCode\n5 小时 1.2k / 1k\nWeekly 0% · resets in 2m\nMonthly 1%"; tip != want {
|
||||
if want := "ZCode\n5 小时 1.2k / 1k · 100% used\nWeekly 0% used · resets in 2m\nMonthly 1% used"; tip != want {
|
||||
t.Errorf("tip %q, want %q", tip, want)
|
||||
}
|
||||
if id := trayCardID(q); id != "zcode" {
|
||||
@@ -48,13 +56,13 @@ func TestTrayUsageText(t *testing.T) {
|
||||
}
|
||||
|
||||
// a balance, an error, nothing
|
||||
if label, _ = trayUsageText(provider.SubscriptionQuota{Name: "DeepSeek", Balance: "¥12.30"}, now); label != "¥12.30" {
|
||||
if label, _ = trayUsageText(provider.SubscriptionQuota{Name: "DeepSeek", Balance: "¥12.30"}, now, false); label != "¥12.30" {
|
||||
t.Errorf("balance label %q", label)
|
||||
}
|
||||
if label, tip = trayUsageText(provider.SubscriptionQuota{Name: "Codex", Error: "signed out"}, now); label != "" || tip != "Codex: signed out" {
|
||||
if label, tip = trayUsageText(provider.SubscriptionQuota{Name: "Codex", Error: "signed out"}, now, false); label != "" || tip != "Codex: signed out" {
|
||||
t.Errorf("error: %q %q", label, tip)
|
||||
}
|
||||
if label, tip = trayUsageText(provider.SubscriptionQuota{Name: "Empty"}, now); label != "" || tip != "" {
|
||||
if label, tip = trayUsageText(provider.SubscriptionQuota{Name: "Empty"}, now, false); label != "" || tip != "" {
|
||||
t.Errorf("empty: %q %q", label, tip)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ func (h *host) watchTrayUsage() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
label, tip := "", "magpie"
|
||||
if q, ok := trayUsageCard(ctx); ok {
|
||||
if label, tip = trayUsageText(q, time.Now()); tip == "" {
|
||||
if label, tip = trayUsageText(q, time.Now(), settings.Load().QuotaLeft); tip == "" {
|
||||
tip = "magpie"
|
||||
}
|
||||
}
|
||||
@@ -37,7 +37,7 @@ func (h *host) watchTrayUsage() {
|
||||
h.tray.SetTooltip(tip)
|
||||
}
|
||||
// the first answer can take a while; look again soon after it
|
||||
next := trayUsageEvery
|
||||
next := trayUsageEvery()
|
||||
if label == "" && settings.Load().TrayUsage != "" {
|
||||
next = 20 * time.Second
|
||||
}
|
||||
|
||||
@@ -57,6 +57,12 @@ type Settings struct {
|
||||
// TrayUsage is the subscription or plan whose windows are shown beside
|
||||
// the tray icon, by its provider and account ("claude|a@b.c"); "" none.
|
||||
TrayUsage string `json:"trayUsage,omitempty"`
|
||||
// TrayUsageEvery is how often, in minutes, that text is brought up to
|
||||
// date; 0 is every 3 (one of TrayEvery).
|
||||
TrayUsageEvery int `json:"trayUsageEvery,omitempty"`
|
||||
// QuotaLeft shows a subscription's windows by how much of each is left,
|
||||
// not used: the Usage page, the tray panel and the menu bar alike.
|
||||
QuotaLeft bool `json:"quotaLeft,omitempty"`
|
||||
// How the agents are listed, by agent id. AgentOrder comes first, as
|
||||
// ordered; an agent it doesn't name (one installed since) follows in
|
||||
// magpie's own order. A hidden agent is folded away at the bottom of the
|
||||
@@ -123,6 +129,8 @@ var (
|
||||
Trays = []string{"panel", "window"}
|
||||
// Warmups are CodexWarmup's and ClaudeWarmup's values, off as "".
|
||||
Warmups = []string{"", "week", "all"}
|
||||
// TrayEvery are TrayUsageEvery's values, in minutes.
|
||||
TrayEvery = []int{1, 3, 5, 10, 30}
|
||||
)
|
||||
|
||||
// Path is the settings file.
|
||||
@@ -164,6 +172,9 @@ func Save(s Settings) error {
|
||||
if !slices.Contains(Warmups, s.ClaudeWarmup) {
|
||||
return fmt.Errorf("claude warm-up must be off, week or all, not %q", s.ClaudeWarmup)
|
||||
}
|
||||
if !slices.Contains(TrayEvery, s.TrayUsageEvery) {
|
||||
return fmt.Errorf("the menu bar's usage is refreshed every %v minutes, not %d", TrayEvery, s.TrayUsageEvery)
|
||||
}
|
||||
s.Proxy = strings.TrimSpace(s.Proxy)
|
||||
if s.Proxy != "" && s.Proxy != "direct" {
|
||||
raw := s.Proxy
|
||||
@@ -202,6 +213,9 @@ func (s Settings) normal() Settings {
|
||||
if s.ClaudeWarmup == "off" {
|
||||
s.ClaudeWarmup = ""
|
||||
}
|
||||
if s.TrayUsageEvery == 0 {
|
||||
s.TrayUsageEvery = 3
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
|
||||
@@ -42,6 +42,16 @@ func TestRoundTrip(t *testing.T) {
|
||||
if Save(Settings{ClaudeWarmup: "hourly"}) == nil {
|
||||
t.Fatal("bad claude warm-up accepted")
|
||||
}
|
||||
// the menu bar's usage: every 3 minutes unless told, left or used
|
||||
if s := Load(); s.TrayUsageEvery != 3 || s.QuotaLeft {
|
||||
t.Fatalf("tray defaults: %+v", s)
|
||||
}
|
||||
if Save(Settings{TrayUsageEvery: 10, QuotaLeft: true}) != nil || Load().TrayUsageEvery != 10 || !Load().QuotaLeft {
|
||||
t.Fatal("tray refresh or left not kept")
|
||||
}
|
||||
if Save(Settings{TrayUsageEvery: 7}) == nil {
|
||||
t.Fatal("bad tray refresh accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrate(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user