Files
magpie/internal/gui/traymenu.go
T
yetone 52e7e4fc8d update: a restart to update waits until the gateway is idle instead of cutting off what the agents have in flight (#577: 正式版更新时保持网关请求与工具链不中断 — a Codex reply streaming through magpie, or a tool chain under way, was cut off by Restart to update) — the app serves the gateway in its own process and a restart to update is install, quit, relaunch, so whatever was in flight ended with it: a stream mid-reply, and a Claude Code turn of the subscription bridge waiting on its caller's tool results, whose Claude Code lives only in this process. The gateway now says what a restart would cut short (Server.Busy): its requests in flight (all but GETs and the bridge's own MCP callbacks), the bridge's runs parked on tool results, and when the last request ended. Restart to update (Settings → Version, the header's Update pill, the tray's item) restarts at once only when nothing is in flight, no turn waits on tools and nothing ended in the last 10 s (a tool chain's gap, the agent running a tool between two requests); otherwise it waits, saying what for: Settings shows "restarts once the gateway is idle · 2 in flight · 1 awaiting tool results" (网关空闲后自动重启 · 2 个请求进行中 · 1 个工具调用待返回) with Restart now and Cancel, the pill reads "Waiting to update" (等待更新) and its next click (not a double click's second half) restarts now, the tray item becomes "Restart Now to Update (3 in flight)" (立即重启以更新(3 个进行中)). The wait asks the feed again once idle, so versions out meanwhile collapse into the newest, downloaded before the restart; it gives up after an hour, saying so, the update then going in at the next restart or quit. A gateway served by another magpie (magpie serve, an older one) is not this one's to wait for. Handing the gateway over to the new version instead (the dev handover, e0c6892) is not used for releases: it rests on SO_REUSEPORT, which Windows lacks, and the new app would have to start beside the old one (single-instance, the Mac's bundle swapped under it). TestBusy (a stream counts until it ends, a GET never, a parked bridge run until it is answered or ends), TestRestartToUpdateWaitsForTheGateway (fails without the change: the click restarted with 2 requests in flight), TestRestartWhenIdle (not while streaming, parked or in a tool chain's gap; then once; an hour's wait gives up), TestRestartWhenIdleIntoTheNewest (0.1.11 staged, 0.1.12 and 0.1.13 out while waiting: restarted into 0.1.13) and TestTrayRestartNow are new; update-wait.test.cjs (WebKit, en and zh: what is in flight, waiting with Restart now and Cancel, counts following the gateway, the hour's give-up, the pill's wait and restart now, an idle click restarting at once, no click scrolling, no left border) fails without the change and passes with it. update-hide, update-check, sync-update, update-auto and click-scroll pass in WebKit. go vet ./internal/..., go build (darwin GUI), go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui -count=1 ./... under a temp HOME pass (TestFXInState's known TempDir cleanup flake aside; passes on rerun).
2026-10-02 23:36:43 +08:00

102 lines
3.1 KiB
Go

package gui
import (
"fmt"
"strings"
"github.com/yetone/magpie/internal/gateway"
)
// trayZh is the tray menu in Chinese; the page has its own words
// (i18n.js), and these few are all the menu shows (#301).
var trayZh = map[string]string{
"Open magpie": "打开 magpie",
"Version %s": "版本 %s",
"Restart to Update": "重启以更新",
"Restart to Update to %s": "重启以更新到 %s",
"Quit magpie": "退出 magpie",
"Restart Now to Update": "立即重启以更新",
"Restart Now to Update (%d in flight)": "立即重启以更新(%d 个进行中)",
}
// onLang relabels the tray menu when the Settings page changes the
// language; set by the process that has the tray.
var onLang func()
// trayLang is the language the tray menu is in, as the page picks its own:
// the setting, or with "system" (or none) the system's, Chinese for any zh.
func trayLang(pref string, system func() string) string {
switch pref {
case "en", "zh":
return pref
}
if strings.HasPrefix(strings.ToLower(system()), "zh") {
return "zh"
}
return "en"
}
// trayText is a menu line in the language, English where it has none.
func trayText(lang, key string, args ...any) string {
if lang == "zh" {
if s, ok := trayZh[key]; ok {
key = s
}
}
if len(args) == 0 {
return key
}
return fmt.Sprintf(key, args...)
}
// trayLabels are the tray menu's lines: open, version, restart and quit.
type trayLabels struct{ open, version, restart, quit string }
// trayMenuLabels are the lines in the language; update is the version
// waiting for a restart, if there is one.
func trayMenuLabels(lang, version, update string) trayLabels {
l := trayLabels{
open: trayText(lang, "Open magpie"),
version: trayText(lang, "Version %s", version),
restart: trayText(lang, "Restart to Update"),
quit: trayText(lang, "Quit magpie"),
}
if update != "" {
l.restart = trayText(lang, "Restart to Update to %s", update)
}
return l
}
// trayRestartNow is the restart item while a restart waits for the
// gateway (#577): a click restarts at once, cutting short what b has in
// flight.
func trayRestartNow(lang string, b gateway.Busy) string {
if n := b.Requests + b.Tools; n > 0 {
return trayText(lang, "Restart Now to Update (%d in flight)", n)
}
return trayText(lang, "Restart Now to Update")
}
// envLang is the language the environment names: the first of LC_ALL,
// LC_MESSAGES, LANG and LANGUAGE set to one.
func envLang(getenv func(string) string) string {
for _, k := range []string{"LC_ALL", "LC_MESSAGES", "LANG", "LANGUAGE"} {
if v := getenv(k); v != "" && v != "C" && v != "POSIX" && !strings.HasPrefix(v, "C.") {
return v
}
}
return ""
}
// firstAppleLang is the first entry `defaults read -g AppleLanguages`
// prints: ( "zh-Hans-CN", "en-US" ), one to a line, quoted or not.
func firstAppleLang(out string) string {
for _, line := range strings.Split(out, "\n") {
s := strings.Trim(strings.TrimSpace(line), `",`)
if s != "" && s != "(" && s != ")" {
return s
}
}
return ""
}