aquaarboraurum: on Windows the window that comes back after every
auto-update restart is white. Its title bar and status bar are there,
and its page is alive. A restart by hand or a reload draws it.
AutoRestart only restarts once nothing is going through magpie and the
window has sat unused, which is when the user is away, often with the
screen locked or the display off. A WebView2 window first shown then
can keep a white surface: Chromium's native occlusion tracking takes
every window as covered while the screen is locked, and a surface first
drawn in that state may not be drawn again. Closing the window to the
tray and opening it again, which the reporter did by hand, draws it.
- windowsOptions starts WebView2 with CalculateNativeWinOcclusion
disabled (occlusionOff), after the user's own features. A user who
enables it in WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS keeps it on.
Wails still tells a hidden or minimised window so (put_IsVisible).
- Wails' main-thread window registers for session and display-state
notifications (watchScreenBack). The app's WndProcInterceptor
(appWndProc, which still calls screensWndProc) draws each open window
again (redrawShown) when the session is unlocked or the display comes
on after being off, as mendOnWake already did on wake.
Not reproduced: the white window never showed on xiaomi-gpu (Windows
11, WebView2 157.0.4341.46). The tries:
- quiet and asked restarts, behind other windows, maximised, under a
full-screen cover, cloaked, minimised then restored;
- the real sandboxed AutoRestart update path (v0.1.9000 to v0.1.9001
from a local feed).
The box's screen can't be locked for a test, so the locked-screen case
the fix is aimed at wasn't tried.
Run:
- Windows (xiaomi-gpu, signed-in session):
- TestRestartedWindowIsDrawn: all 13 modes pass on the fix. The test
is new and plays the update restart (old process, markQuietRestart,
AwaitPredecessor, openMain). Each mode now also posts an unlock to
the app's own main-thread window, checks that the window is drawn
again and stays drawn, and checks that the msedgewebview2 browser's
command line has CalculateNativeWinOcclusion disabled.
- Broken by hand (the interceptor ignoring the unlock, occlusionOff
returning the list unchanged): red on both checks.
- go vet ./internal/gui; TestScreenBackFrom,
TestAppWndProcSaysTheScreenIsBack, TestWebView2* pass.
- The real update path with exes built from this tree: the new
process's window came up drawn (10 of 360 sampled points white,
the same as the old window) over 22s of samples.
- mote:
- gofmt -l;
- go vet ./internal/gui/..., GOOS=linux -tags nogui, GOOS=windows;
- GOOS=windows go build ./..., GOOS=linux go build -tags nogui ./...;
- go test ./internal/gui (142s) and -tags nogui (95s): ok.
Not run: the full ./... suite. mote and mica were both over their load
(mote's suite.lock had four suites queued). The change is in
internal/gui only.
132 lines
4.7 KiB
Go
132 lines
4.7 KiB
Go
//go:build windows && !nogui
|
|
|
|
package gui
|
|
|
|
import (
|
|
"log"
|
|
"sync/atomic"
|
|
"unsafe"
|
|
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
// A WebView2 window shown while the screen is locked or the display off
|
|
// can stay white once the user is back: Chromium's own occlusion tracking
|
|
// takes every window as covered then, and a surface first drawn in that
|
|
// state may not be drawn again. aquaarboraurum on X saw a white window
|
|
// after every restart to update. AutoRestart makes that restart once no
|
|
// request is going through magpie and its window has sat unused, as when
|
|
// the user is away and the screen locked; a restart by hand, with the
|
|
// user there, came up drawn. Not reproduced: the screen of the Windows box
|
|
// the tests run on can't be locked for them. magpie's windows are drawn
|
|
// again when the session is unlocked or the display comes on (mendOnWake),
|
|
// as closing the window to the tray and opening it again did by hand, and
|
|
// windowsOptions turns Chromium's occlusion tracking off
|
|
// (webViewOcclusion).
|
|
|
|
// The messages that say the screen is back (WinUser.h, WtsApi32.h).
|
|
const (
|
|
wmWTSSessionChange = 0x02B1
|
|
wtsSessionUnlock = 0x8
|
|
wmPowerBroadcast = 0x0218
|
|
pbtPowerSettingChange = 0x8013
|
|
notifyForThisSession = 0
|
|
deviceNotifyWindowHndl = 0
|
|
)
|
|
|
|
// guidConsoleDisplayState is GUID_CONSOLE_DISPLAY_STATE: the display off
|
|
// (0), on (1) or dimmed (2).
|
|
var guidConsoleDisplayState = windows.GUID{Data1: 0x6fe69556, Data2: 0x704a, Data3: 0x47a0,
|
|
Data4: [8]byte{0x8f, 0x24, 0xc2, 0x8d, 0x93, 0x6f, 0xda, 0x47}}
|
|
|
|
// powerBroadcastSetting is POWERBROADCAST_SETTING with the display state's
|
|
// DWORD as its data.
|
|
type powerBroadcastSetting struct {
|
|
PowerSetting windows.GUID
|
|
DataLength uint32
|
|
Data uint32
|
|
}
|
|
|
|
var (
|
|
procWTSRegisterSessionNotification = windows.NewLazySystemDLL("wtsapi32.dll").NewProc("WTSRegisterSessionNotification")
|
|
procRegisterPowerSettingNotification = windows.NewLazySystemDLL("user32.dll").NewProc("RegisterPowerSettingNotification")
|
|
procFindWindowExW = windows.NewLazySystemDLL("user32.dll").NewProc("FindWindowExW")
|
|
)
|
|
|
|
// displayOff: the display was last said to be off.
|
|
var displayOff atomic.Bool
|
|
|
|
// watchScreenBack has Wails' main-thread window told when the session is
|
|
// unlocked and when the display goes off or on; on the main thread, once.
|
|
func watchScreenBack() {
|
|
hwnd := ownMainThreadWindow()
|
|
if hwnd == 0 {
|
|
log.Println("webview: Wails' main-thread window not found; an unlock doesn't draw the windows again")
|
|
return
|
|
}
|
|
if r, _, err := procWTSRegisterSessionNotification.Call(hwnd, notifyForThisSession); r == 0 {
|
|
log.Printf("webview: an unlock doesn't draw the windows again: %v", err)
|
|
}
|
|
if r, _, err := procRegisterPowerSettingNotification.Call(hwnd, uintptr(unsafe.Pointer(&guidConsoleDisplayState)), deviceNotifyWindowHndl); r == 0 {
|
|
log.Printf("webview: the display coming on doesn't draw the windows again: %v", err)
|
|
}
|
|
}
|
|
|
|
// ownMainThreadWindow is this process's Wails main-thread window: another
|
|
// magpie's has the same title, during a restart's handover too.
|
|
func ownMainThreadWindow() uintptr {
|
|
title, err := windows.UTF16PtrFromString(wailsMainThread)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
pid := windows.GetCurrentProcessId()
|
|
for after := uintptr(0); ; {
|
|
w, _, _ := procFindWindowExW.Call(0, after, 0, uintptr(unsafe.Pointer(title)))
|
|
if w == 0 {
|
|
return 0
|
|
}
|
|
var p uint32
|
|
if _, err := windows.GetWindowThreadProcessId(windows.HWND(w), &p); err == nil && p == pid {
|
|
return w
|
|
}
|
|
after = w
|
|
}
|
|
}
|
|
|
|
// screenBackFrom says why msg brings the screen back ("" when it doesn't):
|
|
// the session unlocked, or the display on after it was off. Windows sends
|
|
// the display's state as it is on registering, which, on, is no change.
|
|
func screenBackFrom(msg uint32, wParam, lParam uintptr) string {
|
|
switch {
|
|
case msg == wmWTSSessionChange && wParam == wtsSessionUnlock:
|
|
return "the session was unlocked"
|
|
case msg == wmPowerBroadcast && wParam == pbtPowerSettingChange && lParam != 0:
|
|
// lParam is a pointer Windows owns for the message's length.
|
|
s := *(**powerBroadcastSetting)(unsafe.Pointer(&lParam))
|
|
if s.PowerSetting != guidConsoleDisplayState || s.DataLength < 4 {
|
|
return ""
|
|
}
|
|
switch s.Data {
|
|
case 0:
|
|
displayOff.Store(true)
|
|
case 1:
|
|
if displayOff.Swap(false) {
|
|
return "the display came on"
|
|
}
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// appWndProc is the app's WndProcInterceptor (windowsOptions): the screen
|
|
// coming back draws the windows again, and the screens are read
|
|
// (screensWndProc). Neither keeps a message from Wails but the screens'.
|
|
var appWndProc = func(hwnd uintptr, msg uint32, wParam, lParam uintptr) (uintptr, bool) {
|
|
if why := screenBackFrom(msg, wParam, lParam); why != "" {
|
|
if f := screenBack.Load(); f != nil {
|
|
(*f)(why)
|
|
}
|
|
}
|
|
return screensWndProc(hwnd, msg, wParam, lParam)
|
|
}
|