gui: with the window open, magpie leads Command-Tab's order, so one Command-Tab away and one back returns to it (#1381, ttmouse)

ttmouse on v0.1.1168: open magpie's window, Command-Tab to another app,
Command-Tab back, and magpie is last in the switcher.

Cause: magpie is out of the Dock (Accessory) until the window opens.
With Settings > Dock set to "while the window is open" (or a full-screen
window), the quick panel's focus activates magpie while it is still
Accessory, and it joins the Dock (Regular) after that. An app activated
before it joins stays last in LaunchServices' front-to-back order
(lsappinfo visibleProcessList), which Command-Tab follows, though it is
frontmost. Wails' Focus only calls activateIgnoringOtherApps when the
app isn't active, and calling it again while active is a no-op. setDock
also ran async (dispatch_async), so openMain's Show/Focus could come
before the policy change.

Fix:
- setDock runs on the main thread at once (InvokeSync), so openMain
  joins before it shows the window.
- A pure dockStep(regular, on, active, front) decides the move. Joining
  while already active activates the Dock and then magpie again
  (activateAgain), which puts magpie first in the order. Joining while
  not active just activates it; leaving never re-activates.
- The full-screen join does the same once the window is in full screen
  (DidEnterFullScreen) if magpie was active when it joined.
- Dock "off" (the default) is unchanged: magpie stays out of the Dock
  and out of Command-Tab, by design. The tray panel's behaviour is
  unchanged.
- docs/subsystems/gui-shell.md: Runtime path item 7 for the Dock setting.

Run:
- TestDockStep (table, pure). TestDockJoinLeadsCommandTab (darwin GUI
  build): a real Accessory Wails app activated, then setDock(true,
  true) + window Show/Focus, asserts it is first in lsappinfo's order.
  Red with `again` forced false (front was the test, kitty first in the
  order), green with the fix.
- go test ./internal/gui (GUI build, darwin) passes; full
  go test -tags nogui ./... passes; gofmt -l clean; go vet ./internal/gui
  (darwin) and GOOS=linux / GOOS=windows go vet -tags nogui clean.
- Real build on this Mac, sandbox HOME, own bundle id, port 3481,
  DockWindow on: launched with the quick panel, window opened through the
  Mac's reopen, then two osascript Command-Tabs. Old build: magpie front
  but 11th of 11 in the order (the report). New build: magpie 1st; after
  Command-Tab, the other app 1st and magpie 2nd; Command-Tab again brings
  magpie back. Dock "always": magpie 1st, round trip works. Dock "off":
  stays UIElement, not in the order.

Not tried: the full-screen path in the real build, a human Command-Tab
(only osascript keystrokes), real accounts (not involved).
This commit is contained in:
yetone
2026-10-11 19:30:17 +08:00
parent 0896e9fad1
commit 15872d977a
5 changed files with 203 additions and 14 deletions
+1
View File
@@ -26,6 +26,7 @@ three.
4. The page reads `GET /api/state` and writes through `POST` routes. Each write answers with the new state, built under `held`.
5. Closing the window hides it. On the Mac a full-screen window first leaves full screen (`closeStep`). The system's close on the panel (a title bar's X, Alt+F4, a window manager's close key) hides it as Escape does, so the tray icon opens it again; a panel lightweight mode let go, no longer `h.panel`, closes (`makePanel`, `TestPanelCloseHidesIt`). Lightweight mode lets go only of a window closed this way: one covered by another app's window, minimised, or open as magpie was hidden is still open (`windowUp`, not Wails' `IsVisible`, which is the occlusion state), and so is it for the Dock while the window is open and for a restart (#1381, `TestCoveredWindowStaysUp`).
6. The window opens as it was last left. Its settled size and whether it was maximised are kept in `settings.Window` and `settings.WindowMaximised` (`settle`; per machine, see `KeepOwn`). A maximised window keeps the size it restores to. `makeMain` opens it at that size (`openSize`). On its first show, `placeMain` maximises it again on the Mac and Windows (on Windows once the page has come). On Linux, `makeMain` makes it maximised with `StartState`. On Windows a size larger than the screen's work area is fitted and centred (`fitRoom`), and the larger size stays kept. The window's position is not kept.
7. On the Mac, Settings › Dock puts magpie in the Dock never (the default: out of the Dock, and so out of Command-Tab), while the window is open (`DockWindow`) or always (`Dock`); a full-screen window joins it whatever the setting (`inDock`, `dockOnFullscreen`). `openMain` joins before it shows the window (`setDock` runs on the main thread at once). An app activated out of the Dock (the quick panel's focus) that then joins it stays last in Command-Tab's order though it is in front, so joining while active activates magpie again by way of the Dock (`dockStep`, `activateAgain`; #1381, `TestDockStep`, `TestDockJoinLeadsCommandTab`).
## Constraints and failure behavior
+23
View File
@@ -0,0 +1,23 @@
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, magpie active 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 while active, as it is when the
// quick panel opens the window or the Settings page puts it there, it is
// activated again 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).
func dockStep(regular, on, active, front bool) dockMove {
if regular == on {
return dockMove{}
}
return dockMove{policy: true, again: on && front && active, front: front}
}
+102
View File
@@ -0,0 +1,102 @@
//go:build darwin && cgo && !nogui
package gui
import (
"fmt"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"testing"
"time"
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/pkg/events"
)
// magpie out of the Dock, activated by its quick panel, opens its window
// and joins the Dock (Settings › Dock › while the window is open): it leads
// Command-Tab's order, so one Command-Tab away comes back to it (#1381). It
// joined while active and stayed last, behind every app, though in front.
// The order is the Mac's front-to-back list, which `lsappinfo` prints.
func TestDockJoinLeadsCommandTab(t *testing.T) {
out, err := runAppKit(t, 30*time.Second, []string{"MAGPIE_TEST_DOCK_ORDER=1"})
if strings.Contains(string(out), "dock order: skip") {
t.Skipf("this session can't bring an app to the front:\n%s", out)
}
if err != nil || !strings.Contains(string(out), "dock order: ok") {
t.Fatalf("dock order: %v\n%s", err, out)
}
}
// lsFirst says whether pid's app leads the front-to-back order, and whether
// it is the front app at all.
func lsFirst(pid int) (first, front bool, out string) {
me, err := exec.Command("lsappinfo", "find", "pid="+strconv.Itoa(pid)).Output()
asn := strings.TrimSpace(string(me))
if i := strings.Index(asn, "-\""); i > 0 {
asn = asn[:i]
}
if err != nil || asn == "" {
return false, false, fmt.Sprintf("no ASN for pid %d: %v", pid, err)
}
f, _ := exec.Command("lsappinfo", "front").Output()
list, _ := exec.Command("lsappinfo", "visibleProcessList").Output()
front = strings.HasPrefix(strings.TrimSpace(string(f)), asn)
first = strings.HasPrefix(strings.TrimSpace(string(list)), asn+"-")
return first, front, fmt.Sprintf("me %s, front %s, order %.300s", asn, strings.TrimSpace(string(f)), list)
}
func init() {
if os.Getenv("MAGPIE_TEST_DOCK_ORDER") != "1" {
return
}
app := application.New(application.Options{
Name: "magpie dock order 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>`)
})},
})
app.Event.OnApplicationEvent(events.Common.ApplicationStarted, func(*application.ApplicationEvent) {
go func() {
defer app.Quit()
// the quick panel's Focus activates magpie out of the Dock
application.InvokeSync(activateApp)
deadline := time.Now().Add(5 * time.Second)
for _, front, _ := lsFirst(os.Getpid()); !front; _, front, _ = lsFirst(os.Getpid()) {
if time.Now().After(deadline) {
fmt.Println("dock order: skip")
return
}
time.Sleep(100 * time.Millisecond)
}
// its Open: openMain joins the Dock, then shows the window
application.InvokeSync(func() {
setDock(true, true)
w := app.Window.NewWithOptions(application.WebviewWindowOptions{Name: "main", Width: 300, Height: 200})
w.Show()
w.Focus()
})
var first bool
var got string
for deadline := time.Now().Add(5 * time.Second); time.Now().Before(deadline); time.Sleep(100 * time.Millisecond) {
if first, _, got = lsFirst(os.Getpid()); first {
break
}
}
if !first {
fmt.Printf("dock order: magpie isn't first in Command-Tab's order: %s\n", got)
return
}
fmt.Println("dock order: ok")
}()
})
if err := app.Run(); err != nil {
panic(err)
}
os.Exit(0)
}
+31
View File
@@ -0,0 +1,31 @@
package gui
import "testing"
// Joining the Dock while active is activated again by way of the Dock, so
// magpie leads Command-Tab's order (#1381); anything else is as before.
func TestDockStep(t *testing.T) {
cases := []struct {
regular, on, active, front bool
want dockMove
}{
// already where it should be: nothing, active or not
{true, true, true, true, dockMove{}},
{false, false, true, true, dockMove{}},
// the panel (active) opens the window: joins, activated again
{false, true, true, true, dockMove{policy: true, again: true, front: true}},
// the tray's Open or a second magpie (not active): joins, activated
{false, true, false, true, dockMove{policy: true, front: true}},
// joins for another reason, not to the front: not activated
{false, true, true, false, dockMove{policy: true}},
// the window closed: leaves, stays where it is
{true, false, true, false, dockMove{policy: true}},
// Settings takes it out with the window open: brought back to the front
{true, false, true, true, dockMove{policy: true, front: true}},
}
for _, c := range cases {
if got := dockStep(c.regular, c.on, c.active, c.front); got != c.want {
t.Errorf("dockStep(regular=%v, on=%v, active=%v, front=%v) = %+v, want %+v", c.regular, c.on, c.active, c.front, got, c.want)
}
}
}
+46 -14
View File
@@ -96,29 +96,45 @@ static int tintPanel(void *w, int r, int g, int b, int a, int ms) {
return ok;
}
// A Dock icon is the Regular activation policy, none is Accessory. Leaving
// the Dock deactivates the app, so with front set it is brought back to the
// front after, with the window the Settings page is in; a window just closed
// leaves it where it is.
static void setDock(int on, int front) {
dispatch_async(dispatch_get_main_queue(), ^{
NSApplicationActivationPolicy p = on ? NSApplicationActivationPolicyRegular : NSApplicationActivationPolicyAccessory;
if ([NSApp activationPolicy] == p) return;
[NSApp setActivationPolicy:p];
if (front) [NSApp activateIgnoringOtherApps:YES];
});
// A Dock icon is the Regular activation policy, none is Accessory.
static int appRegular(void) { return [NSApp activationPolicy] == NSApplicationActivationPolicyRegular; }
static int appActive(void) { return [NSApp isActive]; }
static void setRegular(int on) {
[NSApp setActivationPolicy:on ? NSApplicationActivationPolicyRegular : NSApplicationActivationPolicyAccessory];
}
static void activate(void) { [NSApp activateIgnoringOtherApps:YES]; }
// An app activated while out of the Dock that then joins it stays last in
// Command-Tab's order, behind every other app, though it is in front: one
// Command-Tab away can't come back to it (#1381). Activating it again does
// nothing while it is active. Activating the Dock, which has no windows or
// menu bar of its own, and then magpie at once puts magpie first.
static void activateAgain(void) {
NSRunningApplication *dock = [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.dock"].firstObject;
if (dock != nil) [dock activateWithOptions:0];
[NSApp activateIgnoringOtherApps:YES];
}
// A full-screen window's title bar, buttons and all, comes down from the top
// of the screen only with the menu bar, and an app out of the Dock has none.
// It joins the Dock as the window starts going full screen: once the window
// is full screen, joining brings neither back. Wails' window events reach Go
// later than that, so this is the notification's own observer.
// later than that, so this is the notification's own observer. Joining
// while active, as it is then, leaves it last in Command-Tab's order; once
// the window is full screen it is activated again (activateAgain).
static void dockOnFullscreen(void) {
dispatch_async(dispatch_get_main_queue(), ^{
__block BOOL again = NO;
[[NSNotificationCenter defaultCenter] addObserverForName:NSWindowWillEnterFullScreenNotification object:nil queue:nil usingBlock:^(NSNotification *n) {
if ([NSApp activationPolicy] == NSApplicationActivationPolicyRegular) return;
again = [NSApp isActive];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
}];
[[NSNotificationCenter defaultCenter] addObserverForName:NSWindowDidEnterFullScreenNotification object:nil queue:nil usingBlock:^(NSNotification *n) {
if (!again) return;
again = NO;
activateAgain();
}];
});
}
*/
@@ -168,8 +184,24 @@ func dockPolicy(on bool) application.ActivationPolicy {
}
// setDock shows magpie in the Dock or takes it out, at once, bringing it to
// the front after when front.
func setDock(on, front bool) { C.setDock(C.int(boolInt(on)), C.int(boolInt(front))) }
// the front after when front (dockStep); on the main thread or off it. On
// it, it is done before what the caller shows next, so a window shown after
// is shown by an app already in the Dock.
func setDock(on, front bool) {
application.InvokeSync(func() {
m := dockStep(C.appRegular() != 0, on, C.appActive() != 0, front)
if !m.policy {
return
}
C.setRegular(C.int(boolInt(on)))
switch {
case m.again:
C.activateAgain()
case m.front:
C.activate()
}
})
}
// dockOnFullscreen puts magpie in the Dock whenever a window goes full
// screen; leaving it, the window puts magpie back as the settings say.