gui: an update restart nobody clicked comes back in the tray alone (eating_dream on X)

eating_dream on X (v0.1.4303, macOS, Restart automatically on, a check
every half hour, prerelease channel): after each automatic update the
main window came back over what they were doing. A restart by itself
brought the window back when it was up (#2096), and minimised when it
was (#3495).

Now every restart nobody clicked comes back with the tray icon alone, on
every system, the window closed even when it was up. That covers
Settings' AutoRestart, `magpie update` from a terminal, a Remote
magpie's ask, and another copy replacing this binary. The restart path
is restartBySelf, which updates.autoRestart calls. It still takes no
focus and the Mac's Dock icon joins without coming to the front
(quiet_restart.go). A restart the user clicked (the pill, Settings, the
tray) opens the window as it was. No new setting.

restartInto loses its minimised parameter, and markQuietRestart writes
the seconds alone. An older version restarting into this one still asks
for its window and may write "<seconds> minimised", so the reading side
(takeQuietRestart, quietMinimised, showMinimised) stays. host.mainUp
had no caller left, so it moved into the Windows test that reads it.

Tests:
- New TestRestartBySelfComesBackToTheTray: by itself asks for no
  window, clicked asks for it.
- TestRestartBySelfComesBackQuietly now checks restartBySelf's mark and
  reads an older version's minimised mark (markOldQuietRestart, which
  the Windows minimised and quiet tests use too).

Run, at fd407bc99 + this change:
- mote-run:
  - go test ./internal/gui -count=1 -timeout 45m: ok
  - go vet ./internal/gui, GOOS=windows go vet ./internal/gui,
    GOOS=linux go vet -tags nogui ./internal/gui: clean
  - gofmt -l: clean
- Broken by hand: restartBySelf asking for the window (the old
  behaviour with it up) fails "windows asked for [true true], want
  [false true]". The old code read the window through a live Wails
  window, so the test can't run against it as it was.
- win-run go test ./internal/gui -run 'Quiet|BySelf|Minimised|RestartBy':
  ok. TestRestartMinimisedComesBackMinimised and
  TestRestartBySelfAsksNoAttention skip over ssh (session 0).
- wsl-run go test -tags nogui ./internal/gui -run 'Quiet|BySelf|Beside': ok

Not tried: a real automatic restart of the app on any system, and the
Windows desktop tests in the signed-in session. The first update into
this version is still made by the old one, which brings the window
back once.

Docs: gui-shell.md, restart by itself.
This commit is contained in:
yetone
2026-11-16 16:37:40 +08:00
parent e0f9218f91
commit 269e54de5f
10 changed files with 129 additions and 75 deletions
+12 -1
View File
@@ -1235,7 +1235,18 @@ while the new version starts (#4521). See `TestUpdateInATerminalRestartsTheAppWi
A restart magpie makes by itself (Settings' Restart automatically,
`magpie update` from a terminal, a Remote magpie's ask, another copy
replacing this one's binary) also waits while the user is using one of
replacing this one's binary; `restartBySelf` in
[`update.go`](../../internal/gui/update.go)) comes back in the tray
alone, with no window, on every system, and takes no focus
([`quiet_restart.go`](../../internal/gui/quiet_restart.go)). It used to
bring the window back when it was up (#2096), minimised if it was
(#3495), and eating_dream on X, with checks every half hour, had it put
back over their work after every update. A restart the user clicked
opens the window as it was. An older version restarting into this one
still asks for its window as it was, and its mark (`<seconds>
minimised`) is still read. See `TestRestartBySelfComesBackToTheTray`.
A restart magpie makes by itself also waits while the user is using one of
magpie's own windows (BESTLJP0412 on X: the window closed and came back
in the middle of adding a provider, the gateway idle). Every page (the
window, the tray panel, a browser on `magpie web`) posts
+3 -14
View File
@@ -148,16 +148,6 @@ func (h *host) ShowMain(view string) {
// MainShown says whether the window is up.
func (h *host) MainShown() bool { return application.InvokeSyncWithResult(h.mainShown) }
// mainUp says whether the window is up and whether it is minimised, read
// together on the main thread.
func (h *host) mainUp() (up, minimised bool) {
application.InvokeSync(func() {
up = h.mainShown()
minimised = up && h.main.IsMinimised()
})
return up, minimised
}
// Import opens the window on an import link, for the user to confirm.
func (h *host) Import(link string) {
id := stash(link)
@@ -600,11 +590,10 @@ func Run(version string, showMain bool, link string) error {
})
}
updates.onWait = func() { application.InvokeSync(relabel) }
// Settings' AutoRestart: the window comes back only if it was up
// (#2096), and minimised if it was (#3495)
// Settings' AutoRestart, and the other restarts nobody clicked:
// back in the tray alone (restartBySelf)
updates.autoRestart = func() bool {
up, minimised := h.mainUp()
if restartInto(false, up, "", true, minimised) {
if restartBySelf() {
leave(h.app.Quit)
return true
}
+16 -5
View File
@@ -20,8 +20,9 @@ import (
"github.com/yetone/magpie/internal/settings"
)
// #3495 (Lemonawa): a restart by itself (Settings' AutoRestart) from a
// main window minimised on the taskbar brings it back minimised there: up
// #3495 (Lemonawa): a restart by itself (Settings' AutoRestart) an older
// magpie makes from a main window minimised on the taskbar brings it back
// minimised there (this one restarts by itself to the tray alone): up
// (a later restart and lightweight mode count it so), never shown or made
// the foreground window on the way, and, restored from the taskbar, back
// at its size, maximised if it was. A window up and not minimised still
@@ -122,9 +123,9 @@ func init() {
}
app.Event.OnApplicationEvent(events.Common.ApplicationStarted, func(*application.ApplicationEvent) {
go func() {
// what the old magpie left as it restarted by itself, read as a
// start reads it (app.go)
markQuietRestart(mode != "shown")
// what an older magpie left as it restarted by itself, read as
// a start reads it (app.go)
markOldQuietRestart(mode != "shown")
quiet, minimised := takeQuietRestart()
if !quiet || minimised != (mode != "shown") {
fail("the mark read back quiet=%v minimised=%v", quiet, minimised)
@@ -202,3 +203,13 @@ func init() {
}
os.Exit(0)
}
// mainUp says whether the window is up and whether it is minimised, read
// together on the main thread, as an older magpie's AutoRestart read them.
func (h *host) mainUp() (up, minimised bool) {
application.InvokeSync(func() {
up = h.mainShown()
minimised = up && h.main.IsMinimised()
})
return up, minimised
}
+13 -17
View File
@@ -13,9 +13,9 @@ import (
)
// A restart magpie makes by itself (Settings' AutoRestart, or `magpie
// update` run in a terminal) brings the app back as it was, and leaves
// the user where they were. Its window comes back only if it was up
// (#2096); now it also takes no focus (xyxyxy_15591 on Discord: "自动更新
// update` run in a terminal) brings the app back in the tray alone
// (restartBySelf), and leaves the user where they were. Before, its
// window came back if it was up (#2096), taking no focus (xyxyxy_15591 on Discord: "自动更新
// 安装更新之后,不跳出界面"). The window came up in front of the app the
// user was typing in and took the keyboard, for a restart nobody asked
// for: the Mac's open brought magpie to the front, its Dock icon joining
@@ -26,10 +26,11 @@ import (
// its window, if it was up, without Focus or activating (quietShow). The
// Mac's open is -g as well (update.Relaunch's quiet).
//
// A window that was minimised comes back minimised (#3495, Lemonawa):
// the mark says so ("<seconds> minimised"), and the new one puts it on
// the taskbar as it was, neither shown nor activated (showMinimised). A
// mark of the seconds alone, from a version before, is a window up.
// This version marks the seconds alone, and asks for no window. A version
// before restarts into this one with its window up as it was, and minimised
// if it was (#3495, Lemonawa): its mark then says so ("<seconds>
// minimised"), and this one puts the window on the taskbar as it was,
// neither shown nor activated (showMinimised).
// quietRestartFile, in magpie's folder, holds when (Unix seconds) a
// restart by itself began.
@@ -40,22 +41,17 @@ const quietRestartFile = "restart-quiet"
// mark older than that is a restart that never came back, not this start.
var quietFor = 10 * time.Minute
// quietMinimisedWord follows the mark's seconds when the window was
// minimised.
// quietMinimisedWord follows the mark's seconds when an older version's
// window was minimised.
const quietMinimisedWord = "minimised"
// markQuietRestart notes that the restart under way is one by itself, and
// whether the main window was minimised as it began.
func markQuietRestart(minimised bool) {
// markQuietRestart notes that the restart under way is one by itself.
func markQuietRestart() {
dir := settings.Dir()
if err := os.MkdirAll(dir, 0o755); err != nil {
return
}
stamp := strconv.FormatInt(time.Now().Unix(), 10)
if minimised {
stamp += " " + quietMinimisedWord
}
stamp += "\n"
stamp := strconv.FormatInt(time.Now().Unix(), 10) + "\n"
if err := os.WriteFile(filepath.Join(dir, quietRestartFile), []byte(stamp), 0o644); err != nil {
log.Println("quiet restart:", err)
}
+65 -26
View File
@@ -4,16 +4,18 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"syscall"
"testing"
"time"
"github.com/yetone/magpie/internal/settings"
"github.com/yetone/magpie/internal/update"
)
// xyxyxy_15591 on Discord: an update put in by itself shouldn't put the
// window up. A restart by itself (Settings' AutoRestart) marks itself, so
// the new version comes back as the old one was without taking focus; a
// the new version comes back without taking focus; a
// "Restart to update" the user clicked doesn't, and comes to the front as
// it always has. The mark is read once, and one left by a restart that
// never came back (stale) or refused (#894) doesn't make a later start
@@ -42,7 +44,7 @@ func TestRestartBySelfComesBackQuietly(t *testing.T) {
// clicked: no mark, the new one comes to the front, even after a
// restart by itself that left one (it never came back)
markQuietRestart(false)
markQuietRestart()
if !restartToUpdate(false, true, "") {
t.Fatal("restart to update failed")
}
@@ -53,28 +55,19 @@ func TestRestartBySelfComesBackQuietly(t *testing.T) {
t.Fatal("the start after a clicked restart is quiet")
}
// by itself, window up or not: marked, read once; minimised (#3495)
// only for a window up and minimised
for _, c := range []struct{ window, minimised, want bool }{
{true, false, false}, {false, false, false}, {true, true, true}, {false, true, false},
} {
if !restartInto(false, c.window, "", true, c.minimised) {
t.Fatalf("%+v: restart by itself failed", c)
}
quiet, minimised := takeQuietRestart()
if !quiet {
t.Fatalf("%+v: the start after a restart by itself isn't quiet", c)
}
if minimised != c.want {
t.Fatalf("%+v: minimised %v, want %v", c, minimised, c.want)
}
if quiet, _ := takeQuietRestart(); quiet {
t.Fatalf("%+v: the mark is read twice", c)
}
// by itself: marked, read once, never minimised (no window asked for)
if !restartBySelf() {
t.Fatal("restart by itself failed")
}
if quiet, minimised := takeQuietRestart(); !quiet || minimised {
t.Fatalf("the start after a restart by itself read quiet=%v minimised=%v", quiet, minimised)
}
if quiet, _ := takeQuietRestart(); quiet {
t.Fatal("the mark is read twice")
}
// magpie web restarts in place: nothing to mark
if !restartInto(true, false, "", true, false) {
if !restartInto(true, false, "", true) {
t.Fatal("web restart failed")
}
webReexec.Store(false)
@@ -84,7 +77,7 @@ func TestRestartBySelfComesBackQuietly(t *testing.T) {
// the new version refused (#894): this one runs on, unmarked
refuse = true
if restartInto(false, true, "", true, false) {
if restartBySelf() {
t.Fatal("a refused restart said to quit")
}
if _, err := os.Stat(mark); !os.IsNotExist(err) {
@@ -108,10 +101,56 @@ func TestRestartBySelfComesBackQuietly(t *testing.T) {
}
}
// a mark from a version before #3495, the seconds alone: a window up
os.WriteFile(mark, []byte(quietStampNow()+"\n"), 0o644)
if quiet, minimised := takeQuietRestart(); !quiet || minimised {
t.Fatalf("an older version's mark read quiet=%v minimised=%v", quiet, minimised)
// an older version's mark, which says its window was minimised
// (#3495): it restarts into this one with the window up as it was
markOldQuietRestart(true)
if quiet, minimised := takeQuietRestart(); !quiet || !minimised {
t.Fatalf("an older version's minimised mark read quiet=%v minimised=%v", quiet, minimised)
}
}
// eating_dream on X (v0.1.4303, AutoRestart on, a check every half
// hour): every update put the main window back over what they were
// doing. A restart nobody clicked (restartBySelf: Settings' AutoRestart,
// a terminal's magpie update, a Remote magpie's ask, another copy's
// install) comes back in the tray alone, whether the window was up,
// minimised or closed; one the user clicked opens it as it was.
func TestRestartBySelfComesBackToTheTray(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
exe := filepath.Join(t.TempDir(), "magpie")
os.WriteFile(exe, []byte("old"), 0o755)
self, _ := os.Stat(exe)
os.WriteFile(exe, []byte("newer"), 0o755)
o := updates
updates = &updater{exe: exe, self: self, latest: &update.Release{Version: "0.1.9999"}}
defer func() { updates = o }()
var asked []bool
wasRelaunch := relaunchBinary
relaunchBinary = func(exe, old string, window bool, view string) error {
asked = append(asked, window)
return nil
}
defer func() { relaunchBinary = wasRelaunch }()
if !restartBySelf() {
t.Fatal("restart by itself failed")
}
if !restartToUpdate(false, true, "") {
t.Fatal("restart to update failed")
}
if len(asked) != 2 || asked[0] || !asked[1] {
t.Fatalf("windows asked for %v, want [false true]: by itself the tray alone, clicked the window", asked)
}
}
// markOldQuietRestart writes the mark as a version before this one did,
// minimised when its window was.
func markOldQuietRestart(minimised bool) {
markQuietRestart()
if minimised {
f := filepath.Join(settings.Dir(), quietRestartFile)
b, _ := os.ReadFile(f)
os.WriteFile(f, []byte(strings.TrimSpace(string(b))+" "+quietMinimisedWord+"\n"), 0o644)
}
}
+1 -1
View File
@@ -322,7 +322,7 @@ func restartDrawOld(mode string, report func(string, ...any)) {
time.Sleep(time.Second)
}
if strings.HasPrefix(mode, "quiet") {
markQuietRestart(strings.Contains(mode, "-min"))
markOldQuietRestart(strings.Contains(mode, "-min"))
} else {
unmarkQuietRestart()
}
+1 -1
View File
@@ -166,7 +166,7 @@ func init() {
})
// what the old magpie left as it restarted by itself, read as a
// start reads it (app.go)
markQuietRestart(false)
markQuietRestart()
quiet, _ := takeQuietRestart()
showMain := mode == "window"
quietShow.Store(quiet && showMain)
+16 -8
View File
@@ -1075,15 +1075,23 @@ func restartFrom(w Windows, view string) bool {
// Off the Mac, the new one opens its window on view when window is set, and
// only the tray icon when not; the Mac's always opens its window.
func restartToUpdate(web, window bool, view string) bool {
return restartInto(web, window, view, false, false)
return restartInto(web, window, view, false)
}
// restartInto is restartToUpdate; bySelf, Settings' AutoRestart restarts
// (#2096), and the Mac's new one too opens only its menu bar icon when no
// window was up, rather than putting up one the user had closed. The new
// one, by itself, also takes no focus (quiet_restart.go), and puts the
// window back minimised when it was (minimised, #3495).
func restartInto(web, window bool, view string, bySelf, minimised bool) bool {
// restartBySelf is the restart nobody clicked (Settings' AutoRestart,
// `magpie update` in a terminal, a Remote magpie's ask, another copy
// replacing this one's binary): the new version comes back in the tray
// alone, the window left closed even when it was up (eating_dream on X:
// every half hour's update put the window back over what they were
// doing). It came back as it was before (#2096), minimised if it was
// (#3495); the user opens it from the tray when they want it.
func restartBySelf() bool { return restartInto(false, false, "", true) }
// restartInto is restartToUpdate; bySelf, a restart nobody clicked, and
// the Mac's new one too opens only its menu bar icon when no window is
// asked for. The new one, by itself, also takes no focus
// (quiet_restart.go).
func restartInto(web, window bool, view string, bySelf bool) bool {
if pending.Load() != nil {
return false // a handover under way restarts already
}
@@ -1099,7 +1107,7 @@ func restartInto(web, window bool, view string, bySelf, minimised bool) bool {
}
updatedInApp(version) // its first start leaves the notes to Settings (#525)
if bySelf && !web {
markQuietRestart(window && minimised) // it comes back without taking focus
markQuietRestart() // it comes back without taking focus
} else {
unmarkQuietRestart() // one asked for comes to the front
}
+1 -1
View File
@@ -46,7 +46,7 @@ func TestUpdateInATerminalRestartsTheAppWithoutCuttingIt(t *testing.T) {
updates.self, _ = os.Stat(exe)
quit := make(chan struct{})
updates.autoRestart = func() bool {
if restartInto(false, false, "", true, false) {
if restartBySelf() {
leave(func() { close(quit) })
return true
}
+1 -1
View File
@@ -134,7 +134,7 @@ func StartWeb(addr, version string) (*Web, error) {
go w.srv.Serve(ln)
// Settings' AutoRestart: magpie web runs on as the new version (#2096)
updates.autoRestart = func() bool {
if restartInto(true, false, "", true, false) {
if restartInto(true, false, "", true) {
quit()
return true
}