Files
magpie/internal/gui/dock.go
T
yetone 751ebe8f5a gui: magpie leads Command-Tab's order after opening its window on a busy Mac too, not only when the main thread keeps up (#1651)
TestDockJoinLeadsCommandTab failed under a full local go test ./...: the
test app was LaunchServices' front app, yet last in Command-Tab's order
(lsappinfo visibleProcessList), kitty first.

Cause: setDock decided whether to activate magpie again by way of the
Dock (#1381's fix) from [NSApp isActive]. The Mac makes an activated app
the front app at once, but NSApp says it is active only once its main
thread has taken the activation in. On a loaded machine the join came
first, isActive read NO, dockStep chose a plain activate (a no-op for
the front app), and magpie stayed last. A temporary print in setDock
showed active=false in every failing run (3 of 5 at load ~120) and true
in the passing ones. The same can happen to a user when the panel's
focus and Open come close together on a busy Mac.

Fix: joining the Dock to the front always goes by way of the Dock
(activateAgain), whatever NSApp says. When magpie truly isn't active
(the tray's Open), the Dock's turn in front changes nothing: magpie is
activated after it either way. dockStep loses its active argument and
appActive is gone. docs/subsystems/gui-shell.md item 7 says so.

The test now activates magpie, waits for LaunchServices to call it the
front app, and joins, all in one turn of the main thread, so the join
always comes before NSApp knows: the busy-machine order, made certain.

Run:
- New test on the old dockStep: 5 of 5 red (magpie not first, kitty first).
  With the fix: 25 of 25 green at load ~120-135.
- The old test (no injected order) with the fix: 10 of 10 green at load
  ~180; before the fix 3 of 5 red at load ~120.
- TestDockStep (table without active). go test ./internal/gui (GUI
  build, darwin) 5 runs green. One run before them failed and its
  output wasn't kept, so which test failed is unknown. After rebasing
  onto 18e82adc6, one package run failed TestTrayCellClickReleasedPanel
  (its own AppKit child, no setDock on its path); it passed alone and in
  3 package runs after. Filed as #1661, cause not found.
  go test -tags nogui ./internal/gui green. gofmt -l clean; go vet
  ./internal/gui (darwin), GOOS=linux and GOOS=windows go vet -tags nogui
  ./internal/gui clean.

Not tried: a real magpie build with osascript Command-Tabs (as 15872d977
did) after this change; the full-screen join (dockOnFullscreen), which
still reads isActive at NSWindowWillEnterFullScreen, long after any
activation, and is unchanged.
2026-10-12 00:57:27 +08:00

26 lines
1.1 KiB
Go

package gui
// dockMove is what setDock does to the app on the Mac.
type dockMove struct {
policy bool // join the Dock or leave it
again bool // activate magpie again by way of the Dock (activateAgain)
front bool // activate magpie
}
// dockStep: what setDock does for magpie in the Dock (regular) or not, to
// be in it (on) or not, brought to the front or not. Nothing when it is
// where it should be. Leaving deactivates the app, so with front it is
// activated after. Joining to the front, it is activated by way of the Dock:
// an app activated out of the Dock that joins it stays last in Command-Tab's
// order, and one Command-Tab away couldn't come back to the window (#1381).
// That is so whether or not NSApp says it is active yet: the Mac makes it
// the front app at once, and NSApp says so only once the main thread has
// taken the activation in, which on a busy Mac comes after the join (#1651).
// Not active at all, the Dock's turn in front changes nothing.
func dockStep(regular, on, front bool) dockMove {
if regular == on {
return dockMove{}
}
return dockMove{policy: true, again: on && front, front: front}
}