Files
magpie/internal/gui/windowvisibility.go
T
yetone 2ba044aa66 gui: the Mac panel's page is told when Wails hides it, so it follows Settings' units (blrbrzkj)
blrbrzkj on Discord: Settings said ¥ 人民币 and 中文(万 / 亿), while the
tray panel's Usage tab still showed ≈$7.18 and 16M.

On the Mac a page in an ordered-out window keeps drawing, so magpie tells
it when it is hidden (hideWebview) and shown (showWebview), and the page
reloads its state, settings included, on the shown signal. The panel is
mostly closed by a click elsewhere, which Wails' HideOnFocusLost does
with a plain Hide, and by Escape (HideOnEscape), likewise. The page was
never told it was hidden there, so the shown signal was a no-op
(magpieWindowVisibility returns early when it was never told otherwise),
and the panel kept the currency and number units it was first drawn
with until magpie restarted.

sayWailsHides (windowvisibility.go), on the panel: on the Mac the page is
told on WindowLostFocus, as hideWebview tells it, and Escape hides the
panel through hideWebview. Other systems are unchanged: WebView2 and
WebKitGTK mark a hidden window's page hidden themselves.

Tests:
- TestPanelHiddenByWailsMarksPageOutOfSight (AppKit, darwin): makes the
  real panel (h.makePanel), closes it by a WindowLostFocus event and by
  the Escape key binding, as Wails does, shows it with showWebview, and
  checks the page saw itself hidden and marked. The probe page now
  reports what it saw when it is told it is shown, so a native hidden
  event before the told one can't be the record it sends.
- panel-units-follow.test.cjs: the page half. A panel open on its Usage
  tab in $ and K/M; Settings picks ¥ and 万 / 亿; hidden and shown, the
  panel re-reads the state and shows ≈¥185, 15.4 亿 (億 in zh-TW) and a
  万 axis, then back to ≈$26.40 and 1.54B. zh, en, zh-TW, ja, de;
  Chromium and WebKit.

Run on mote (macOS 27.2), at this tree:
- go test -run 'TestClosingWindowMarksPageOutOfSight|TestPanelHiddenByWailsMarksPageOutOfSight' ./internal/gui/: ok
- go vet ./internal/gui/, GOOS=linux go vet -tags nogui, GOOS=windows go vet: ok; gofmt -l: nothing
- node --test panel-units-follow, window-visibility, number-units: 20/20

Broken by hand:
- sayWailsHides(w) not called: the AppKit test times out with no hidden
  report.
- only its Escape binding removed: red, "hidden=true marked=false
  play=running".
- the page's visibilitychange without loadBack (a copy of the assets):
  panel-units-follow red, "shown again, the panel reads the state again".
- The panel test written to show without a told hide (the old Mac path)
  was 0/12 on the unchanged page, which is how the cause was found.

Not tried: the real tray panel by hand on a Mac (the AppKit test makes
the real panel window but no status item); blrbrzkj's system isn't
known, and on Windows and Linux the page is marked hidden by the webview
itself, so this changes nothing there.
2026-11-04 16:32:55 +08:00

61 lines
1.8 KiB
Go

//go:build !nogui
package gui
import (
"runtime"
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/pkg/events"
)
// 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. 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)"
)
func hideWebview(w *application.WebviewWindow) {
if w == nil {
return
}
if js := pageHiddenJSOn(runtime.GOOS); js != "" {
w.ExecJS(js)
}
w.Hide()
}
func showWebview(w *application.WebviewWindow) *application.WebviewWindow {
if w == nil {
return nil
}
w.Show()
w.ExecJS(pageShownJS)
return w
}
// sayWailsHides tells the panel's page when Wails hides the panel itself:
// on a click elsewhere (HideOnFocusLost), the way it is mostly closed, and
// on Escape (HideOnEscape), both a plain Hide that hideWebview never sees.
// A page never told it was hidden isn't told it is back either, so it kept
// the settings it was drawn with: Settings' ¥ and 万 / 亿 never reached the
// panel's Usage tab (blrbrzkj on Discord).
func sayWailsHides(w *application.WebviewWindow) {
if js := pageHiddenJSOn(runtime.GOOS); js != "" {
w.OnWindowEvent(events.Common.WindowLostFocus, func(*application.WindowEvent) { w.ExecJS(js) })
}
w.RegisterKeyBinding("escape", func(application.Window) { hideWebview(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
}