diff --git a/docs/subsystems/gui-shell.md b/docs/subsystems/gui-shell.md index 275280611..45d559f12 100644 --- a/docs/subsystems/gui-shell.md +++ b/docs/subsystems/gui-shell.md @@ -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 diff --git a/internal/gui/app.go b/internal/gui/app.go index b8933164f..dfde5dfd8 100644 --- a/internal/gui/app.go +++ b/internal/gui/app.go @@ -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 diff --git a/internal/gui/trayshown_darwin.go b/internal/gui/trayshown_darwin.go new file mode 100644 index 000000000..5445f657e --- /dev/null +++ b/internal/gui/trayshown_darwin.go @@ -0,0 +1,100 @@ +//go:build darwin && cgo && !nogui + +package gui + +/* +#cgo CFLAGS: -x objective-c +#cgo LDFLAGS: -framework Cocoa +#import +#include + +// 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 +} diff --git a/internal/gui/trayshown_darwin_test.go b/internal/gui/trayshown_darwin_test.go new file mode 100644 index 000000000..54c8ac99d --- /dev/null +++ b/internal/gui/trayshown_darwin_test.go @@ -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, `

magpie

`) + })}, + }) + 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) +} diff --git a/internal/gui/trayshown_other.go b/internal/gui/trayshown_other.go new file mode 100644 index 000000000..dba733ccb --- /dev/null +++ b/internal/gui/trayshown_other.go @@ -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 }