gui: only a Mac page is told it is hidden, and focus tells it it is back (#4293)

On top of pipi-1997's 9704a0901, which is kept as written.

Off the Mac the panel is shown again by Wails' ToggleWindow, which never
ran showWebview. With hideWebview telling every page it was hidden, an
Escape-closed panel on Windows or Linux would have come back with
document.hidden true, its polling stopped for good. WebView2 and
WebKitGTK mark a hidden window's page hidden themselves, so only the Mac
is told now (pageHiddenJSOn), and a page that takes focus marks itself
visible whichever path showed it.

The page side is the same behaviour in fewer bytes (729 for 1,237), in a
try as before. The core scripts are 1,533,802 bytes, over 1,533,073 at
origin/main ace58117a; the budget is raised to that, with no room added,
and says why.

Run on mote (macOS 27.2):
- TestClosingWindowMarksPageOutOfSight passes. With hideWebview's signal
  taken out by hand it fails: "while hidden the page saw hidden=true
  state=\"hidden\" marked=false play=\"running\"". So on 27.2 WebKit
  does mark the page hidden by itself; the CSS animations still run,
  which is what the reporter's sample shows (updateRendering from the
  RemoteLayerTreeDrawingArea timer). The reporter's 27.0 is not
  tried here.
- TestPageToldHiddenOnTheMacOnly, red with the GOOS check taken out.
- go test ./internal/gui: ok except TestMainWindowCorners (#3949) and
  TestColdTurnsTaken (#4510, reopened with the output); neither touches
  window visibility.
- GOOS=windows go vet ./internal/gui; GOOS=linux -tags nogui vet: ok.
- gofmt -l: clean.
GUI on WSL magpie-desktop (another session's suite was on mote): the
whole suite (make test-ui) ran past its 3500s bound at load ~90 with no
result. The eight Routing files it showed red, plus window-visibility,
were then run at this head and at origin/main df0320156, one after the
other at load ~55: here 28 of 91 red, at main 31 of 89, the same tests
(rule ⋯, request opens, row swap, request model, idle frame, back in
sight), all already filed (#4600, #4585). window-visibility passes in
Chromium and WebKit.
After the rebase onto ace58117a, on mote: the budget tests,
TestClosingWindowMarksPageOutOfSight and TestPageToldHiddenOnTheMacOnly
ok; window-visibility, routing-idle-frame and routing-flood 23 of 23 in
Chromium and WebKit (the last two red on WSL under load).

Not run: the WebContent CPU after closing a real window. mote has no
display, so a shown window's WebContent used 0.01s in 5s there and
there was nothing to compare; the 26-42% is the reporter's measurement
on an M1 Pro.

Co-authored-by: pipi <zhuangpipi01@gmail.com>
This commit is contained in:
yetone
2026-11-01 17:28:30 +08:00
co-authored by pipi
parent f06551c277
commit dfe868a1eb
5 changed files with 59 additions and 26 deletions
+11 -22
View File
@@ -5,30 +5,19 @@ const params = new URLSearchParams(location.search);
const mode = params.get("mode") || "window";
// focus without a scroll, as every focus call here asks
const NO_SCROLL = { preventScroll: true };
// A hidden NSWindow keeps its WKWebView's document visible unless the app
// says otherwise: page timers and SVG animations kept drawing Routing in a
// closed window. Compose the app's state with WebKit's own so a browser tab
// that is really hidden still reads hidden.
let windowOutOfSight = false;
const nativeHidden = Object.getOwnPropertyDescriptor(Document.prototype, "hidden")?.get;
const nativeVisibility = Object.getOwnPropertyDescriptor(Document.prototype, "visibilityState")?.get;
try {
Object.defineProperty(Document.prototype, "hidden", {
configurable: true,
get() { return windowOutOfSight || (nativeHidden ? nativeHidden.call(this) : false); },
});
Object.defineProperty(Document.prototype, "visibilityState", {
configurable: true,
get() { return windowOutOfSight ? "hidden" : (nativeVisibility ? nativeVisibility.call(this) : "visible"); },
});
} catch { /* a WebKit without writable descriptors keeps its own state */ }
window.magpieWindowVisibility = (visible) => {
const next = !visible;
if (next === windowOutOfSight) return;
windowOutOfSight = next;
document.documentElement.classList.toggle("magpie-out-of-sight", next);
// a Mac window hidden by orderOut leaves its page drawing: the app says so (#4293)
let outOfSight = false;
for (const [k, off] of [["hidden", true], ["visibilityState", "hidden"]]) {
const own = Object.getOwnPropertyDescriptor(Document.prototype, k)?.get;
try { own && Object.defineProperty(Document.prototype, k, { configurable: true, get() { return outOfSight ? off : own.call(this); } }); } catch {}
}
window.magpieWindowVisibility = (v) => {
if (outOfSight !== v) return;
outOfSight = !v;
document.documentElement.classList.toggle("magpie-out-of-sight", outOfSight);
document.dispatchEvent(new Event("visibilitychange"));
};
addEventListener("focus", () => magpieWindowVisibility(true)); // focused is on screen
// kept: magpie.<k> in localStorage, read as JSON; d when it holds none, or
// not d's kind, or can't be read. keep writes one, for this time only if
// it can't be kept
File diff suppressed because one or more lines are too long
@@ -73,5 +73,13 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium",
};
});
assert.deepEqual(shown, { hidden: false, state: "visible", marked: false, playState: "running", events: 2 });
// a window that takes focus is on screen, whichever path showed it
const focused = await page.evaluate(() => {
window.magpieWindowVisibility(false);
window.dispatchEvent(new Event("focus"));
return { hidden: document.hidden, marked: document.documentElement.classList.contains("magpie-out-of-sight") };
});
assert.deepEqual(focused, { hidden: false, marked: false });
});
}
+20 -3
View File
@@ -2,10 +2,17 @@
package gui
import "github.com/wailsapp/wails/v3/pkg/application"
import (
"runtime"
"github.com/wailsapp/wails/v3/pkg/application"
)
// A macOS window hidden with orderOut keeps its WKWebView's page visible, so
// tell the page before hiding it and tell it again when it is shown.
// tell the page before hiding it and tell it again when it is shown. Only
// the Mac is told: WebView2 and WebKitGTK mark a hidden window's page hidden
// themselves, and there the panel is also shown by Wails' ToggleWindow,
// which would never tell the page it is back (#4293).
const (
pageHiddenJS = "window.magpieWindowVisibility?.(false)"
pageShownJS = "window.magpieWindowVisibility?.(true)"
@@ -15,7 +22,9 @@ func hideWebview(w *application.WebviewWindow) {
if w == nil {
return
}
w.ExecJS(pageHiddenJS)
if js := pageHiddenJSOn(runtime.GOOS); js != "" {
w.ExecJS(js)
}
w.Hide()
}
@@ -27,3 +36,11 @@ func showWebview(w *application.WebviewWindow) *application.WebviewWindow {
w.ExecJS(pageShownJS)
return w
}
// pageHiddenJSOn is what a page on goos is told as its window hides.
func pageHiddenJSOn(goos string) string {
if goos != "darwin" {
return ""
}
return pageHiddenJS
}
+19
View File
@@ -0,0 +1,19 @@
//go:build !nogui
package gui
import "testing"
// TestPageToldHiddenOnTheMacOnly: off the Mac the panel is shown again by
// Wails' ToggleWindow, which never tells the page it is back, so a page told
// hidden there would stop its polling for good (#4293).
func TestPageToldHiddenOnTheMacOnly(t *testing.T) {
if pageHiddenJSOn("darwin") != pageHiddenJS {
t.Fatal("a Mac window's page isn't told it is out of sight")
}
for _, goos := range []string{"windows", "linux"} {
if js := pageHiddenJSOn(goos); js != "" {
t.Fatalf("%s: a page is told %q as its window hides", goos, js)
}
}
}