gui: a restart to update leaves the Mac's menu bar icon for the next magpie (Mitsuha, audie)

From v0.1.1354 a restart to update hands the gateway over, and the old
magpie hides its own menu bar item with Wails' Hide, which is
[statusItem setVisible:NO]. AppKit keeps that in the app's defaults by
the item's autosave name ("NSStatusItem VisibleCC Item-0" = 0 on macOS
26), and the next item made under that name starts hidden. So after the
update the icon was gone, and quitting and reopening didn't bring it
back (Mitsuha on v0.1.1364; audie on two Macs, macOS 27.0.1).

Evidence: a 30-line Cocoa probe made a status item, setVisible:NO, and
every later launch of the same program started with visible=0 and the
key = 0; removeStatusItem kept nothing; removing the key brought it back
on the next launch. This machine's com.yetone.magpie.sbhandover.plist
(the handover commit's own sandbox run) holds
"NSStatusItem VisibleCC Item-0" => false.

Fix (internal/gui/trayshown_darwin.go, app.go):
- leaveMenuBar (was the onHandedOver closure) queues forgetTrayHidden
  on the main thread after Wails' hide block, so the hide is made on
  screen but not kept.
- bringTrayBack, once per computer (a flag in the Mac's defaults, never
  in a WebDAV backup), forgets a kept hide before the tray is made: that
  repairs everyone an older magpie already hit. A hide kept after that
  is the user's and is left alone.
- A magpie handed over to by an older one (which still hides for good)
  forgets the hide once that pid has quit and shows its item, unless a
  hide was already kept when it started.
docs/subsystems/gui-shell.md says so under the handover.

Run (worktree at origin/main ed91aa04e):
- TestHandoverLeavesTheMenuBarIconForTheNextMagpie (AppKit child with a
  real Wails tray, CFFIXED_USER_HOME sandbox): ok, -count=3.
- Broken by hand: leaveMenuBar without forgetTrayHiddenLater -> "handing
  over kept the menu bar icon hidden for the next magpie (1 kept)";
  bringTrayBack without forgetTrayHidden -> "the icon an older magpie hid
  wasn't brought back (still hidden true, 1 kept)".
- go test ./internal/gui/ -count=1: ok 86.7s; go test -tags nogui
  ./internal/gui/: ok 67.2s.
- gofmt -l: nothing. go vet ./internal/gui/ (darwin), GOOS=linux go vet
  -tags nogui, GOOS=windows go vet: ok. go build, GOOS=linux go build
  -tags nogui, GOOS=windows go build: ok.
- No GUI asset changed, so no Playwright run.

Not tried: a real signed update from v0.1.1367 to this build on the
user's bundle id; whether Control Center hides the new version's running
item live when the old one writes the key (the probe's running item kept
visible=1; the on-screen window list needs screen recording rights). Not
run: the full go test ./... (one package touched; other sessions are
queued for the suite). audie's WebDAV sync carries no tray setting and
doesn't restart magpie; the icon going there is taken to be the same
update's restart, not confirmed with them.
This commit is contained in:
yetone
2026-10-24 15:20:37 +08:00
parent ed91aa04ef
commit ccbbcbffcb
5 changed files with 245 additions and 11 deletions
+12 -1
View File
@@ -505,7 +505,18 @@ then calls `beginHandover` instead of the relaunch-after-exit. The steps:
Two magpies never show at once. On a Mac the old one lets go of its
instance lock before it gives the socket (`releaseInstance`), so the new
one's `runningAlready` takes it, and once the ack comes the old one hides
its menu bar icon, panel and window and leaves the Dock (`onHandedOver`).
its menu bar icon, panel and window and leaves the Dock (`onHandedOver`,
`leaveMenuBar`). AppKit keeps a status item's `setVisible:NO` in the
app's defaults by its autosave name (`NSStatusItem VisibleCC Item-0` on
macOS 26), which the new one's item and every later one start from, so
the hide is forgotten once it is made (`forgetTrayHiddenLater`,
[`trayshown_darwin.go`](../../internal/gui/trayshown_darwin.go)). From
v0.1.1354 to v0.1.1367 it wasn't, and the icon stayed gone after the
update, through a quit and reopen too (Mitsuha, audie). `bringTrayBack`
forgets a kept hide once per computer before the tray is made, and a
magpie handed over to by an older one forgets it again once that one has
quit (`awaitExit`), unless a hide was kept when it started: one kept after
the bring-back is the user's (`TestHandoverLeavesTheMenuBarIconForTheNextMagpie`).
On Linux the D-Bus name Wails holds can't be let go, so the new one serves
the gateway at once but waits for the old pid to end (`awaitExit`, at most
`handedOffWait`, 16 minutes) before it makes its app and tray; the old one
+32 -10
View File
@@ -512,6 +512,8 @@ func Run(version string, showMain bool, link string) error {
library.KeepLinked(30 * time.Second)
}()
// before the item is made, which starts as hidden as AppKit kept it
hiddenAtStart := bringTrayBack()
h.tray = h.app.SystemTray.New()
if runtime.GOOS == "linux" {
// set before the tray starts, it is the item's id too, which
@@ -536,17 +538,18 @@ func Run(version string, showMain bool, link string) error {
// a Mac that handed the gateway to the new version (handover.go)
// leaves the menu bar to it while it finishes its requests: one bird
if runtime.GOOS == "darwin" {
onHandedOver = func() {
application.InvokeSync(func() {
h.tray.Hide()
if h.panel != nil {
h.panel.Hide()
onHandedOver = h.leaveMenuBar
// a magpie before this fix hid its item for good as it handed
// over (leaveMenuBar): once it has quit, bring the icon back
if before != 0 && !hiddenAtStart {
go func() {
awaitExit(before, handedOffWait)
<-h.ready
if forgetTrayHidden() > 0 {
log.Println("tray: brought back the menu bar icon the magpie before hid")
}
if h.main != nil {
h.main.Hide()
}
setDock(false, false)
})
h.tray.Show()
}()
}
}
h.watchTrayUsage()
@@ -769,6 +772,25 @@ var OpenView string
// said to have started); until then a signal is magpie's to handle.
var Started func()
// leaveMenuBar is a Mac's magpie that handed the gateway to the new
// version leaving the menu bar, panel, window and Dock to it while it
// finishes its requests: one bird. Hidden, the item's visibility is kept by
// AppKit for the app, the new version's item and every later one too, so it
// is forgotten once the hide is made.
func (h *host) leaveMenuBar() {
application.InvokeSync(func() {
h.tray.Hide()
forgetTrayHiddenLater()
if h.panel != nil {
h.panel.Hide()
}
if h.main != nil {
h.main.Hide()
}
setDock(false, false)
})
}
// setTrayInBar has the tray's item leave the bar's tray while magpie's icon
// is in Omarchy's bar as its widget, and come back once it isn't. The item
// is there to be set a moment after the app has started; the newest call
+100
View File
@@ -0,0 +1,100 @@
//go:build darwin && cgo && !nogui
package gui
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa
#import <Cocoa/Cocoa.h>
#include <stdlib.h>
// AppKit keeps a status item's visibility in the app's defaults, by its
// autosave name ("NSStatusItem VisibleCC Item-0" on macOS 26, "NSStatusItem
// Visible Item-0" before), whoever set it: the user dragging it out, or the
// app's own setVisible:NO. The next item made under that name starts hidden.
static int mpHiddenKey(NSString *k, id v) {
return [k hasPrefix:@"NSStatusItem Visible"] && [v respondsToSelector:@selector(boolValue)] && ![v boolValue];
}
// mpForgetHidden removes every status item visibility kept as hidden, and
// says how many it removed.
static int mpForgetHidden(void) {
NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
int n = 0;
for (NSString *k in [[d persistentDomainForName:[[NSBundle mainBundle] bundleIdentifier] ?: [[NSProcessInfo processInfo] processName]] allKeys]) {
if (mpHiddenKey(k, [d objectForKey:k])) {
[d removeObjectForKey:k];
n++;
}
}
if (n) [d synchronize];
return n;
}
static int mpHiddenCount(void) {
NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
int n = 0;
for (NSString *k in [[d persistentDomainForName:[[NSBundle mainBundle] bundleIdentifier] ?: [[NSProcessInfo processInfo] processName]] allKeys]) {
if (mpHiddenKey(k, [d objectForKey:k])) n++;
}
return n;
}
// After the blocks already queued on the main thread (Wails' setVisible:NO
// is one), so the hide is undone in the defaults only once it is made.
static void mpForgetHiddenLater(void) {
dispatch_async(dispatch_get_main_queue(), ^{ mpForgetHidden(); });
}
static int mpFlag(const char *k) {
return [[NSUserDefaults standardUserDefaults] boolForKey:[NSString stringWithUTF8String:k]];
}
static void mpSetFlag(const char *k) {
NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
[d setBool:YES forKey:[NSString stringWithUTF8String:k]];
[d synchronize];
}
// Waits, off the main thread, for what is queued on it to have run.
static void mpMainSettled(void) {
dispatch_sync(dispatch_get_main_queue(), ^{});
}
*/
import "C"
import "unsafe"
// trayHidden is how many of this app's status items macOS keeps as hidden.
func trayHidden() int { return int(C.mpHiddenCount()) }
// forgetTrayHidden has the next status item start shown: it removes the
// hidden visibility AppKit keeps for it. How many it removed.
func forgetTrayHidden() int { return int(C.mpForgetHidden()) }
// forgetTrayHiddenLater is forgetTrayHidden once the main thread has run
// what is queued on it, the tray's Hide among them.
func forgetTrayHiddenLater() { C.mpForgetHiddenLater() }
// mainSettled waits for the main thread to run what is queued on it. Not
// on the main thread.
func mainSettled() { C.mpMainSettled() }
// trayBroughtBackKey marks, in the Mac's defaults (this computer's, never
// in a backup), that the hidden icon a handover left was brought back.
const trayBroughtBackKey = "magpie tray brought back after a handover"
// bringTrayBack, once per computer, brings back the menu bar icon a restart
// to update hid: from v0.1.1354 the magpie before hid its own item with
// setVisible:NO, which AppKit keeps for the app, so every later magpie
// started with no icon. A hide kept after that is the user's. Whether one
// stays hidden, to be left alone.
func bringTrayBack() (stillHidden bool) {
k := C.CString(trayBroughtBackKey)
defer C.free(unsafe.Pointer(k))
if C.mpFlag(k) == 0 {
forgetTrayHidden()
C.mpSetFlag(k)
}
return trayHidden() > 0
}
+93
View File
@@ -0,0 +1,93 @@
//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"
)
// Mitsuha on Discord (v0.1.1364) and audie: after an update the Mac's menu
// bar icon was gone, and quitting and reopening didn't bring it back. From
// v0.1.1354 a restart to update hands the gateway over, and the magpie
// before hid its item with setVisible:NO. AppKit keeps that in the app's
// defaults ("NSStatusItem VisibleCC Item-0" = 0), so the new version's item
// and every later one started hidden. The hide leaves nothing kept, and a
// hide an older magpie kept is undone once per computer; a later one is
// the user's and stays.
func TestHandoverLeavesTheMenuBarIconForTheNextMagpie(t *testing.T) {
out, err := runAppKit(t, 30*time.Second, []string{"MAGPIE_TEST_TRAY_SHOWN=1"})
if err != nil || !strings.Contains(string(out), "tray shown: ok") {
t.Fatalf("tray shown: %v\n%s", err, out)
}
}
func init() {
if os.Getenv("MAGPIE_TEST_TRAY_SHOWN") != "1" {
return
}
app := application.New(application.Options{
Name: "magpie tray shown 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>`)
})},
})
h := &host{app: app}
h.tray = app.SystemTray.New()
h.tray.SetTemplateIcon(trayIcon)
fail := func(format string, a ...any) {
fmt.Printf("tray shown: "+format+"\n", a...)
os.Exit(1)
}
app.Event.OnApplicationEvent(events.Common.ApplicationStarted, func(*application.ApplicationEvent) {
go func() {
// what a magpie before this fix did as it handed over: the hide
// is kept for the app (the item is up a moment after the start)
for deadline := time.Now().Add(5 * time.Second); trayHidden() == 0; {
if time.Now().After(deadline) {
fail("Hide kept nothing in the defaults; the cause isn't what this test is about")
}
h.tray.Hide()
mainSettled()
time.Sleep(50 * time.Millisecond)
}
if still := bringTrayBack(); still || trayHidden() != 0 {
fail("the icon an older magpie hid wasn't brought back (still hidden %v, %d kept)", still, trayHidden())
}
h.tray.Show()
mainSettled()
// this magpie handing over
h.leaveMenuBar()
mainSettled()
if n := trayHidden(); n != 0 {
fail("handing over kept the menu bar icon hidden for the next magpie (%d kept)", n)
}
// the user's own hide, after the one-time bring-back (shown
// again first: a hidden item's Hide keeps nothing)
h.tray.Show()
mainSettled()
h.tray.Hide()
mainSettled()
if still := bringTrayBack(); !still || trayHidden() != 1 {
fail("the user's own hide was undone (still hidden %v, %d kept)", still, trayHidden())
}
fmt.Println("tray shown: ok")
os.Exit(0)
}()
})
if err := app.Run(); err != nil {
fail("run: %v", err)
}
os.Exit(1)
}
+8
View File
@@ -0,0 +1,8 @@
//go:build (!darwin || !cgo) && !nogui
package gui
// Only the Mac keeps a status item's visibility (trayshown_darwin.go).
func forgetTrayHidden() int { return 0 }
func forgetTrayHiddenLater() {}
func bringTrayBack() (stillHidden bool) { return false }