Skip to content

fix(window): tell WebView2 when a window leaves the screen - #1433

Open
gitsatra wants to merge 2 commits into
spacering-net:mainfrom
gitsatra:fix/webview-visibility
Open

gitsatra wants to merge 2 commits into
spacering-net:mainfrom
gitsatra:fix/webview-visibility

Conversation

@gitsatra

@gitsatra gitsatra commented Dec 6, 2026

Copy link
Copy Markdown
Contributor

fix(window): tell WebView2 when a window leaves the screen

Why

On Windows the app keeps working behind a window nobody can see. Minimise it,
or hide it to the tray, and document.visibilityState still says "visible":
Chromium goes on producing frames and running the page's timers. Everything the
frontend hangs off visibility therefore never pauses —

  • useDocumentVisible (src/lib/document-visibility.ts) keeps transcript
    rendering and the live elapsed timers running;
  • the animation-play-state rules in src/app/globals.css never fire;
  • the bot face's blink/glance timer and its requestAnimationFrame loop
    (src/components/personal-agent/bot-face-motion.ts) keep ticking.

That is what #1430 was trying to fix in the page, and why its approach could
not work: the page was never told. As @xintaofei put it on that PR, wry 0.57
handles WM_SIZE only while wparam != SIZE_MINIMIZED
(wry/src/webview2/mod.rs:1297), and touches
ICoreWebView2Controller::SetIsVisible only when a webview is created and from
Webview::set_visible — neither a minimised window nor one hidden to the tray
takes either path. The fix belongs where the window state is known.

What it does

src-tauri/src/webview_visibility.rs keeps each window's shown/minimised state
and mirrors it into WebView2:

controller.SetIsVisible(shown && !minimised)   // via with_webview, on the UI thread

Recomputed from the paths that change it:

path why
commands::windows::post_window_setup the window exists (and a window built hidden is corrected here)
WindowEvent::Resized tao forwards WM_SIZE(SIZE_MINIMIZED) as a zero-size resize, and the restore as the next one
WindowEvent::Focused(true) a show that did not go through show_and_focus_window
the close-to-tray handler (lib.rs) hiding sends no resize at all
commands::windows::show_and_focus_window showing sends none either
WindowEvent::Destroyed the label is free to be reused

Only a changed answer is dispatched, so a drag-resize costs one lock per
event and no with_webview message.

What it deliberately does not touch

  • It never shows, hides, minimises or resizes anything — only the webview's
    IsVisible flag. wry's own set_visible (which also shows or hides the
    webview's HWND) cannot fight with it.
  • Browser surfaces keep their own controllers: an embedded tab's child webview
    is built through wry directly (browser::surface_child), and an owned tab's
    window by browser::surface_window — neither goes through
    post_window_setup, so neither is followed.
  • Each window is tracked under its own label: minimising the workspace must not
    stop a second window from drawing.
  • A window that cannot answer keeps its webview visible — a wrong true costs
    frames, a wrong false would leave the page blank.
  • No-op away from Windows.

Measured

App built without this change (Windows 11, WebView2, codeg.exe +
msedgewebview2.exe, CPU share averaged over 15–20 s windows):

state document.visibilityState CPU
on screen visible 0.478 core
minimised visible ← the bug 0.025 core
restored visible 0.365 core

With the change, the same measurement reports hidden while minimised, and the
page's own pause paths (useDocumentVisible, the globals.css rules, the face
loop) take effect instead of running blind. The measured figures from the built
app are in the PR comment below.

Tests

  • cargo test --lib webview_visible — the pure decision
    (shown && !minimised), four cases.
  • cargo check --lib and cargo test --lib clean on Windows.
  • Frontend untouched: no TS change in this PR.

wry 0.57 handles WM_SIZE only while wparam != SIZE_MINIMIZED, and touches
ICoreWebView2Controller::SetIsVisible only at creation and from Webview's own
set_visible. So a minimised window, and a window hidden to the tray, leave
WebView2 believing it is on screen: document.visibilityState stays "visible"
and Chromium keeps rendering behind a window nobody can see.

Measured on a build without this: 0.478 core on screen, 0.025 core minimised,
0.365 core restored — with visibilityState reporting "visible" in all three.

webview_visibility mirrors each window's shown/minimised state into
SetIsVisible(shown && !minimised), per window, from the paths that change it:
window setup, Resized (tao forwards SIZE_MINIMIZED as a zero-size resize),
focus, the tray's hide, and show_and_focus_window. Only a changed answer is
dispatched, so a drag-resize does not queue one message per frame.

It never shows, hides or resizes anything, so wry's own set_visible cannot
fight it; browser surfaces keep their own controllers and windows.
Only `tracked` (Windows) and the unit test call it, so on Linux and macOS the
lib target saw an unused function and CI's `-D warnings` failed the build
before it got as far as anything interesting.
@gitsatra

gitsatra commented Dec 6, 2026

Copy link
Copy Markdown
Contributor Author

The ubuntu desktop job failed on dead_code: webview_should_be_visible is only reached from tracked (Windows) and the unit test, so on other targets the lib target sees it unused and CI builds with -D warnings. Fixed in 686a9ad with #[cfg_attr(not(target_os = "windows"), allow(dead_code))]; the rest of the module is already Windows-gated and watch/sync are no-ops elsewhere.

@gitsatra

gitsatra commented Dec 6, 2026

Copy link
Copy Markdown
Contributor Author

Measured on the build from this PR (Windows 11, WebView2, codeg.exe + msedgewebview2.exe, 60 s windows after a 30 s settle, app idle on the workspace page):

state document.visibilityState CPU
on screen visible 0.292 core
minimised (60 s) hidden 0.031 core
restored visible 0.328 core

Before, the same measurement read visible in all three states (0.478 / 0.025 / 0.365 core). The honest part is the point: the page's own pause paths — useDocumentVisible, the animation-play-state rules, the face's rAF loop and blink timer — now get told, instead of the frontend having to infer it.

Two things worth saying plainly:

  • CPU while minimised was already low on my build because it carried the frontend workarounds from fix(personal-agent): a minimised window is not a visible one #1430 that you rejected — they are not in this PR, and without SetIsVisible the page would have gone on rendering once they were gone. That is the regression this prevents.
  • While measuring I found the app can raise itself out of a minimised state (foreground window, ~40–100 s into a run) on this build, independently of this change; my harness re-minimises and reports the clean window separately. Say the word if you want that chased down next.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant