Files
magpie/internal/gui/trayplace_darwin_test.go
T
yetone 2545bce6e7 gui: an update keeps the Mac's menu bar icon where the user put it (imty42 on X, #3873)
imty42 on X, #3873 (wei9312, hey-yx, EXHades): after every update the
menu bar icon the user had Cmd-dragged (or Ice had put in its visible
section) went back to the default spot, far left, which in Ice is the
hidden section. EXHades' defaults after an update held only "magpie tray
brought back after a handover", no "NSStatusItem Preferred Position
Item-0".

Cause: 6893cfbe3 kept the place across a quit (the item renamed before
Wails' removeStatusItem:), but a restart to update never gets that far
with the item in place. The magpie handing the gateway over hides its
item first (leaveMenuBar, setVisible:NO), and hiding deletes the
Preferred Position key just as removal does; forgetTrayHiddenLater then
removes the VisibleCC key the hide wrote, leaving exactly what EXHades
saw. The new version's item is Item-0 in the same defaults domain, so
its place was gone with it.

Fix: leaveMenuBar renames the item (keepTrayPlace) before the hide, so
the hide forgets "magpie quitting 0", which nobody keeps, and the
VisibleCC key it writes for that name is a hidden one forgetTrayHidden
already removes.

Evidence on mote (macOS 27.2, GUI session) with a Cocoa probe under its
own defaults domain, position seeded at 400, old and new process each
with Item-0 up: the old one's setVisible:NO -> domain holds only
"VisibleCC Item-0" = 0, Preferred Position gone. Renamed before the
hide -> "Preferred Position Item-0" = 400 kept through both exits, and a
third launch comes up there.

Test: TestQuitKeepsTheMenuBarIconsPlace gets a "handover" case that runs
the real leaveMenuBar on a Wails tray with the place seeded and wants it
kept and nothing left hidden. Broken by hand (keepTrayPlace dropped from
leaveMenuBar): "tray place: forgotten", FAIL; with it: PASS.

Run on mote at this head: gofmt -l internal/gui clean; go vet
./internal/gui, GOOS=linux go vet -tags nogui, GOOS=windows go vet, go
build ./...: ok; go test -count=1 ./internal/gui: ok (131s), the AppKit
tests included.

Not tried: a real restart to update of a built bundle with Ice (mote's
magpie processes belong to other sessions); the AppKit behaviour was
shown with the probe and the Wails test instead. No GUI files changed,
so no GUI suite run. Full suite runs after the tag.
2026-11-10 10:14:55 +08:00

110 lines
3.3 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"},
// imty42 on X, #3873: the magpie handing over to the new version
// hides its item, and setVisible:NO forgets the place the new
// version's Item-0 shares
{"handover", "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")
}
if keep == "handover" {
h := &host{app: app, tray: tray}
h.leaveMenuBar()
mainSettled()
mainSettled() // the hidden visibility forgotten after
if x, ok := trayPlace("Item-0"); !ok {
fmt.Println("tray place: forgotten")
} else if n := trayHidden(); n != 0 {
fail("handing over kept %d items hidden", n)
} else {
fmt.Printf("tray place: kept (%v)\n", x)
}
os.Exit(0)
}
// 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)
}