Files
magpie/internal/gui/space_darwin.go
T
yetone 3cd0b5f78d gui: lightweight mode no longer lets go of a main window that is open under another app's, so Command-Tab back to magpie finds it there (#1381, ttmouse)
Cause: lightweight mode (#580) let go of the main window once two of its
30 s looks found it "not shown", and it asked Wails' IsVisible, which on
the Mac is the window's occlusion state. A window the user had
Command-Tabbed away from, covered by the other app's window, counted as
closed: 30-60 s later its webview was dropped and the window closed, and
Command-Tab back to magpie found a blank shell or no window at all. Hidden
with magpie (Command-H) it went the same way, since hiding orders every
window out.

Change: windowUp says whether a window is up as the user sees it:
ordered in on any Space, covered or not, minimised, or open as magpie was
hidden (watchHide records those on NSApplicationWillHideNotification).
Only a window magpie closed itself is down. lighten (through lightShown)
and mainShown use it, so the Dock-while-open setting and a restart to
update also keep a covered window as open. Elsewhere windowUp is
IsVisible as before (visible there covers covered and minimised).

Run:
- Reproduced with a sandbox build of origin/main (own HOME and
  CFFIXED_USER_HOME, port 3481, dock+lightweight on): with the open
  window covered by a full-screen window of a helper process for 70 s the
  log said "the main window's webview let go" and the window was a blank
  white shell. With this change, the same run let go only the closed
  panel and the window kept its page (screenshot of the window).
- Activation itself was checked by handing activation from the helper to
  magpie (NSRunningApplication activateFromApplication): with the window
  there it came forward. A real Command-Tab keystroke was not sent.
- New TestCoveredWindowStaysUp (AppKit, own process): open, covered by
  another window (until IsVisible says false), hidden with the app,
  minimised -> up; closed by magpie, also with the app hidden -> down.
  With windowUp put back to IsVisible it fails: "open: the window counts
  as up=false, want true"; without the hidden clause: "hidden with
  magpie: the window counts as up=false, want true". Passes -count=10.
- gofmt -l; go vet ./internal/gui; GOOS=linux go vet -tags nogui and
  GOOS=windows go vet (with and without nogui) ./internal/gui; go build,
  GOOS=linux go build -tags nogui, GOOS=windows go build;
  go test ./internal/gui and go test -tags nogui ./internal/gui under a
  temp HOME: ok.
2026-10-09 12:59:29 +08:00

99 lines
3.4 KiB
Go

//go:build !nogui
package gui
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa
#import <Cocoa/Cocoa.h>
static void setMovesToActiveSpace(void *win, int on) {
NSWindow *w = (NSWindow *)win;
if (on) w.collectionBehavior |= NSWindowCollectionBehaviorMoveToActiveSpace;
else w.collectionBehavior &= ~NSWindowCollectionBehaviorMoveToActiveSpace;
}
static int movesToActiveSpace(void *win) {
return (((NSWindow *)win).collectionBehavior & NSWindowCollectionBehaviorMoveToActiveSpace) != 0;
}
// Ordered in and not in the Dock: on a Space of its own, or covered, it is
// still open, where Wails' IsVisible (its occlusion state) says it isn't.
static int windowOpen(void *win) {
NSWindow *w = (NSWindow *)win;
return w.isVisible && !w.isMiniaturized;
}
// The windows that were open as magpie was hidden (Command-H, Hide
// Others): hiding orders every window out, and Command-Tab back orders
// these in again. Weak, so a window let go isn't kept.
static NSHashTable *openWhenHidden;
static void watchHide(void) {
if (openWhenHidden != nil) return;
openWhenHidden = [[NSHashTable weakObjectsHashTable] retain];
[[NSNotificationCenter defaultCenter] addObserverForName:NSApplicationWillHideNotification object:nil queue:nil usingBlock:^(NSNotification *n) {
[openWhenHidden removeAllObjects];
for (NSWindow *w in NSApp.windows) {
if (w.isVisible) [openWhenHidden addObject:w];
}
}];
}
// Up: ordered in on any Space, covered by other windows or not, or
// minimised, or open as magpie was hidden. Only a window magpie closed
// itself is down. The occlusion state, Wails' IsVisible, says a window
// covered by another app's is not visible.
static int windowUp(void *win) {
NSWindow *w = (NSWindow *)win;
return w.isVisible || w.isMiniaturized || (NSApp.isHidden && [openWhenHidden containsObject:w]);
}
static void activateApp(void) {
[NSApp activateIgnoringOtherApps:YES];
}
*/
import "C"
import "github.com/wailsapp/wails/v3/pkg/application"
// setMovesToActiveSpace says whether w, ordered front or its app activated,
// is moved to the Space the user is on, rather than taking the user to its
// own; on the main thread.
func setMovesToActiveSpace(w *application.WebviewWindow, on bool) {
if p := w.NativeWindow(); p != nil {
C.setMovesToActiveSpace(p, C.int(boolInt(on)))
}
}
// movesToActiveSpace says whether it is; on the main thread.
func movesToActiveSpace(w *application.WebviewWindow) bool {
p := w.NativeWindow()
return p != nil && C.movesToActiveSpace(p) != 0
}
// windowOpen says whether w is open: shown, on any Space, and not
// minimised; on the main thread.
func windowOpen(w *application.WebviewWindow) bool {
p := w.NativeWindow()
return p != nil && C.windowOpen(p) != 0
}
// windowUp says whether w is up, as the user sees it: open on any Space,
// covered or not, minimised, or open as magpie was hidden; on the main thread.
// Lightweight mode lets go only of a window that isn't, and the Dock and a
// restart keep the window that is (#1381).
func windowUp(w *application.WebviewWindow) bool {
p := w.NativeWindow()
return p != nil && C.windowUp(p) != 0
}
// watchHide keeps which windows were open as magpie was hidden, for
// windowUp; called once, before the app runs.
func watchHide() { C.watchHide() }
// activateApp brings magpie to the front as the Mac does for a click on its
// Dock icon: to the Space its window is on, as the user's Mission Control
// settings say; on the main thread.
func activateApp() { C.activateApp() }