Files
magpie/internal/gui/trayplace_darwin_test.go
T
yetone 6893cfbe34 gui: the Mac's menu bar icon keeps the place the user gave it across a quit (nb5p)
nb5p on Discord (macOS 26, Ice): after every update, and after a plain
quit and reopen, magpie's icon went back into Ice's hidden section however
often it was moved out. Alma has the same. Other apps keep their place.

Cause: AppKit keeps where a status item sits in the app's defaults by its
autosave name ("NSStatusItem Preferred Position Item-0"), and
-[NSStatusBar removeStatusItem:] deletes that key. Wails' cleanup removes
every tray as the app quits (application.go cleanup -> systray.destroy ->
systemTrayDestroy -> removeStatusItem:), so each quit forgot the place and
the next magpie's item started at the default spot, left of the others,
which in Ice is the hidden section. Apps that just exit keep the key.
(Electron's Tray, Alma's, removes its item the same way.) The autosave
name itself is stable: magpie makes one item, so it is Item-0 every
launch, under the same bundle id; it was never the identity that changed.

Fix: a shutdown task (h.app.OnShutdown, which Wails runs before it
removes trays) renames each of the app's status items to "magpie quitting
N" first, so the removal deletes a key nobody keeps and Item-0's place
stays. The items are found by their button's target, Wails'
StatusItemController, whose statusItem property is the item. Windows and
Linux get a no-op (trayshown_other.go). The handover's old magpie quits
through the same path, so a restart to update keeps the place too.

Evidence, on mote (macOS 27.2) with a 40-line Cocoa probe under its own
defaults domain: position seeded at 400, exit without removing -> key kept;
removeStatusItem on quit -> key gone, next launch has none; a fixed
autosave name "magpie" -> also gone on removal (so a fixed name alone
would not help); autosaveName changed just before removeStatusItem ->
key kept, next launch reads 400, and no key is written for the throwaway
name.

Run: gofmt -l internal/gui (clean); on mote at this head go vet
./internal/gui, GOOS=linux go vet -tags nogui, GOOS=windows go vet,
go build ./..., GOOS=windows go build ./..., GOOS=linux go build -tags
nogui ./... : ok. go test ./internal/gui on mote skipping the runAppKit
tests: everything passes except TestSettingsSaveKeepsWhatItDoesNotSend
(SearchAll, red on main, #4514) and TestMainWindowCorners (AppKit, killed:
mote has no Aqua session).

New test: TestQuitKeepsTheMenuBarIconsPlace (trayplace_darwin_test.go)
runs a Wails app twice under a sandboxed defaults home: Item-0's place
seeded, then tray.Destroy(), the call Wails' cleanup makes. Without
keepTrayPlace it must read "forgotten" (the cause), with it "kept".

Not tried: the new test and TestHandoverLeaves... hang over ssh on mote
(its console sits at the login window: no gui/501 launchd domain, no
Finder or Dock), and this Mac is the user's live one, so the AppKit test
has not run anywhere yet; macOS CI runs it. No real two-launch check with
the built app and Ice, for the same reason. The full suite was not run
(two other sessions' gateway suites were on mote).
2026-10-31 16:53:00 +08:00

92 lines
2.7 KiB
Go

//go:build darwin && cgo && !nogui
package gui
import (
"fmt"
"net/http"
"os"
"strings"
"testing"
"time"
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/pkg/events"
)
// nb5p on Discord (macOS 26): Ice put magpie's menu bar icon back in its
// hidden section after every update, and after a plain quit and reopen,
// however often it was moved out. AppKit keeps where the item sits by its
// autosave name ("NSStatusItem Preferred Position Item-0"), and Wails
// removes the item as the app quits, which forgets it: the next magpie's
// item started at the default spot. Renamed first, the removal forgets
// nothing kept.
func TestQuitKeepsTheMenuBarIconsPlace(t *testing.T) {
for _, c := range []struct{ keep, want string }{
{"0", "tray place: forgotten"}, // the cause, without the fix
{"1", "tray place: kept"},
} {
out, err := runAppKit(t, 30*time.Second, []string{"MAGPIE_TEST_TRAY_PLACE=" + c.keep})
if err != nil || !strings.Contains(string(out), c.want) {
t.Fatalf("keep %s: want %q: %v\n%s", c.keep, c.want, err, out)
}
}
}
func init() {
keep := os.Getenv("MAGPIE_TEST_TRAY_PLACE")
if keep == "" {
return
}
fail := func(format string, a ...any) {
fmt.Printf("tray place: "+format+"\n", a...)
os.Exit(1)
}
// where the user put it, the launch before
setTrayPlace("Item-0", 400)
app := application.New(application.Options{
Name: "magpie tray place regression",
Mac: application.MacOptions{ActivationPolicy: application.ActivationPolicyAccessory},
Assets: application.AssetOptions{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, `<p>magpie</p>`)
})},
})
tray := app.SystemTray.New()
tray.SetTemplateIcon(trayIcon)
app.Event.OnApplicationEvent(events.Common.ApplicationStarted, func(*application.ApplicationEvent) {
go func() {
for deadline := time.Now().Add(5 * time.Second); trayItems() == 0; {
if time.Now().After(deadline) {
fail("no status item came up")
}
time.Sleep(50 * time.Millisecond)
}
// AppKit may move it to where the item really is; kept is
// what counts
if _, ok := trayPlace("Item-0"); !ok {
fail("the place set before the start is gone")
}
// what the app's shutdown does: magpie's task, then Wails'
// removal of every tray
if keep == "1" {
if n := keepTrayPlace(); n != 1 {
fail("kept the place of %d items, not 1", n)
}
}
tray.Destroy()
mainSettled()
if x, ok := trayPlace("Item-0"); ok {
fmt.Printf("tray place: kept (%v)\n", x)
} else {
fmt.Println("tray place: forgotten")
}
os.Exit(0)
}()
})
if err := app.Run(); err != nil {
fail("run: %v", err)
}
os.Exit(1)
}