diff --git a/docs/subsystems/gui-shell.md b/docs/subsystems/gui-shell.md index 202c044a6..584edbcd6 100644 --- a/docs/subsystems/gui-shell.md +++ b/docs/subsystems/gui-shell.md @@ -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 (` +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 diff --git a/internal/gui/app.go b/internal/gui/app.go index 7f98d5df9..3c0ceee28 100644 --- a/internal/gui/app.go +++ b/internal/gui/app.go @@ -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 } diff --git a/internal/gui/minimised_windows_test.go b/internal/gui/minimised_windows_test.go index ca99ce013..65ca4db93 100644 --- a/internal/gui/minimised_windows_test.go +++ b/internal/gui/minimised_windows_test.go @@ -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 +} diff --git a/internal/gui/quiet_restart.go b/internal/gui/quiet_restart.go index 5aabe2d1d..ac8414759 100644 --- a/internal/gui/quiet_restart.go +++ b/internal/gui/quiet_restart.go @@ -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 (" 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 (" +// 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) } diff --git a/internal/gui/quiet_restart_test.go b/internal/gui/quiet_restart_test.go index 2715bb94b..60afd380c 100644 --- a/internal/gui/quiet_restart_test.go +++ b/internal/gui/quiet_restart_test.go @@ -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) } } diff --git a/internal/gui/quiet_restart_windows_test.go b/internal/gui/quiet_restart_windows_test.go index 2ad6b43bc..80201b8d2 100644 --- a/internal/gui/quiet_restart_windows_test.go +++ b/internal/gui/quiet_restart_windows_test.go @@ -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() } diff --git a/internal/gui/quietshow_windows_test.go b/internal/gui/quietshow_windows_test.go index 3236250c3..540a31622 100644 --- a/internal/gui/quietshow_windows_test.go +++ b/internal/gui/quietshow_windows_test.go @@ -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) diff --git a/internal/gui/update.go b/internal/gui/update.go index 64dc15efa..039d40624 100644 --- a/internal/gui/update.go +++ b/internal/gui/update.go @@ -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 } diff --git a/internal/gui/update_beside_unix_test.go b/internal/gui/update_beside_unix_test.go index 595f1160e..ebb703b6f 100644 --- a/internal/gui/update_beside_unix_test.go +++ b/internal/gui/update_beside_unix_test.go @@ -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 } diff --git a/internal/gui/web.go b/internal/gui/web.go index 24195021c..4e551a876 100644 --- a/internal/gui/web.go +++ b/internal/gui/web.go @@ -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 }