Files
magpie/internal/gui/quiet_restart.go
T
yetone 244cf49371 update: a restart magpie makes by itself comes back as it was, without taking focus
xyxyxy_15591 (Discord #feedback) asked for a silent auto-update: once an
update is in, the window shouldn't pop up. With "Restart automatically"
(Settings, beside Version) on, the window state was already kept (a
closed window came back as the tray icon only), and What's New stays
quiet after an in-app restart. What did show was magpie taking focus:

- macOS relaunched with `open -n` and no `-g`, so LaunchServices brought
  magpie to the front even when only the menu-bar icon came back;
- with the window open, showHere's Focus() activated the app (every OS);
- joining the Dock activated magpie, and with Dock on Wails' Regular
  policy activated it at launch.

Now a restart magpie makes by itself (AutoRestart, `magpie update` from
a terminal) writes a one-shot restart-quiet mark (read once, good for
10 minutes). The next start reads it: `open -n -g`, no Focus(), the Dock
joined without activating, Accessory policy at launch and the Dock set
afterwards. A restart the user clicks removes any mark and comes to the
front as before. `open` gets -g also whenever no window is wanted. No
new setting: zero-config, the existing switch is the one asked for.

Run (base 841326709, rebased on ced880ef0, no upstream change to
these files):
- mote: gofmt -l clean; go vet ./internal/gui ./internal/update
  ./internal/cli; GOOS=linux go vet -tags nogui ./...; GOOS=windows go
  vet on gui/update/cli: clean. internal/update ok;
  TestRestartBySelfComesBackQuietly, TestUpdateThatWontStart,
  TestWhatsNew*, *Handover* ok, except
  TestHandoverLeavesTheMenuBarIconForTheNextMagpie, killed at 30s the
  same at origin/main 7b09d2c95 (AppKit on mote, #4125).
- Windows (win-run, CGO_ENABLED=0): internal/update ok; internal/gui
  reds the same set as origin/main 7b09d2c95 (#4115, #1827, #2958,
  #4514).
- WSL (wsl-run): full `go test -tags nogui -timeout 45m ./...`, 80 ok;
  reds TestInstalledVersion, TestGrokInstalled, TestOmpDetectedWithPiDir
  (#4033, #3548), TestNoCommandBypassesProc (#4491),
  TestSettingsSaveKeepsWhatItDoesNotSend (#4514) all red the same at
  origin/main 7b09d2c95; TestColdTurnsTaken (#4510) passed there alone.
  internal/gui + internal/update rerun at this head: internal/update ok; internal/gui red only TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and TestColdTurnsTaken (#4510).

Broken by hand: without -g, TestRelaunchOpen and
TestRelaunchOpenTriedAgain fail; without markQuietRestart,
TestRestartBySelfComesBackQuietly fails "window true: the start after a
restart by itself isn't quiet"; without the unmark on a clicked restart,
it fails "a restart the user asked for is marked quiet".

Not tried: a real update restart on any OS (`open` of a GUI app over
ssh on mote fails, RBSRequestErrorDomain 5), so -g and no-focus weren't
seen in a real session. Windows' ShowWindow inside Wails may still
bring the window forward when it was open; Linux window managers were
not tried. No GUI assets changed, so no Playwright run.
2026-10-31 15:56:40 +08:00

74 lines
2.6 KiB
Go

package gui
import (
"log"
"os"
"path/filepath"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/yetone/magpie/internal/settings"
)
// 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: "自动更新
// 安装更新之后,不跳出界面"). 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
// activated it, and the window's Focus did on every system.
//
// The old version writes quietRestartFile before it starts the new one;
// the new one reads it once as it starts (takeQuietRestart), and shows
// its window, if it was up, without Focus or activating (quietShow). The
// Mac's open is -g as well (update.Relaunch's quiet).
// quietRestartFile, in magpie's folder, holds when (Unix seconds) a
// restart by itself began.
const quietRestartFile = "restart-quiet"
// quietFor is how long the mark counts: a handover given up waits for the
// requests in flight before the new one is opened (fallbackWait), and a
// mark older than that is a restart that never came back, not this start.
var quietFor = 10 * time.Minute
// 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) + "\n"
if err := os.WriteFile(filepath.Join(dir, quietRestartFile), []byte(stamp), 0o644); err != nil {
log.Println("quiet restart:", err)
}
}
// unmarkQuietRestart takes the mark back: the restart didn't happen.
func unmarkQuietRestart() { os.Remove(filepath.Join(settings.Dir(), quietRestartFile)) }
// takeQuietRestart is whether this start is a restart by itself, the mark
// read once and removed. One unreadable or stale reads as not: the start
// is then the ordinary one, which shows its window as it always has.
func takeQuietRestart() bool {
f := filepath.Join(settings.Dir(), quietRestartFile)
b, err := os.ReadFile(f)
if err != nil {
return false
}
os.Remove(f)
sec, err := strconv.ParseInt(strings.TrimSpace(string(b)), 10, 64)
if err != nil {
return false
}
age := time.Since(time.Unix(sec, 0))
return age >= -time.Minute && age <= quietFor
}
// quietShow: the next show of the main window is the one a restart by
// itself brings back, shown without taking focus; read once (openMain).
var quietShow atomic.Bool