Skip to content

fix(windows): changing WebView visibility on hide/show/minimize - #9246

Merged
amrbashir merged 4 commits into
tauri-apps:devfrom
Legend-Master:fix-visibility-change
Mar 28, 2024
Merged

fix(windows): changing WebView visibility on hide/show/minimize#9246
amrbashir merged 4 commits into
tauri-apps:devfrom
Legend-Master:fix-visibility-change

Conversation

@Legend-Master

@Legend-Master Legend-Master commented Mar 22, 2024

Copy link
Copy Markdown
Contributor

Reference: #6864

We need to call SetIsVisible manually according to https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/winrt/microsoft_web_webview2_core/corewebview2controller?view=webview2-winrt-1.0.2365.46#isvisible

Determines whether to show or hide the WebView. If IsVisible is set to false, the WebView is transparent and is not rendered. However, this does not affect the window containing the WebView (the ParentWindow parameter that was passed to CoreWebView2Environment.CreateCoreWebView2ControllerAsync). WebView as a child window does not get window messages when the top window is minimized or restored. For performance reasons, developers should set the IsVisible property of the WebView to false when the app window is minimized and back to true when the app window is restored. The app window does this by handling SIZE_MINIMIZED and SIZE_RESTORED command upon receiving WM_SIZE message. There are CPU and memory benefits when the page is hidden. For instance Chromium has code that throttles activities on the page like animations and some tasks are run less frequently. Similarly, WebView2 will purge some caches to reduce memory usage.

This is a quick dirty fix to make it work at least for minimize / restore / show / hide (no fully covered by other windows support yet) (Resized fires on minimized and restored, and since we don't have an is visible event yet, the best we can do is to hook onto those methods for now), we probably want to separate this event in https://github.com/tauri-apps/tao (issue tauri-apps/tao#892) in the future

@Legend-Master
Legend-Master requested a review from a team as a code owner March 22, 2024 06:26
Comment thread core/tauri-runtime-wry/src/lib.rs Outdated
window: &Window,
webviews: &[WebviewWrapper],
) -> windows::core::Result<()> {
let is_visible = window.is_visible() && !window.is_minimized();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be passed is_visible as an argument, because in the case of when calling window.hide() we already know that it is not visible and so we don't need to call window.is_visible()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, but I feel like that will put the responsibility of calculating is_visible to the caller, which is more error prone

@amrbashir
amrbashir merged commit 5bd47b4 into tauri-apps:dev Mar 28, 2024
@amrbashir amrbashir changed the title fix(windows): not changing WebView visibility fix(windows): changing WebView visibility on hide/show/minimize Mar 28, 2024
amrbashir added a commit that referenced this pull request Apr 9, 2024
This fixes a regression introduced in #9246 where previously users would wait for `DOMContentLoaded` before showing the window to avoid having a white flash, also introduce a regression for apps that is hidden by default and is shown on global shortcuts or some other action.

Also accoding to the MSDN docs for webview2's `IsVisible` they only recommend to change the webview visibility on when un/minimizing.

closes #9393
ref: https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/winrt/microsoft_web_webview2_core/corewebview2controller?view=webview2-winrt-1.0.2365.46#isvisible
Legend-Master added a commit to Legend-Master/tauri that referenced this pull request Apr 15, 2024
amrbashir pushed a commit that referenced this pull request Apr 15, 2024
* Revert "fix(windows): changing WebView visibility on hide/show/minimize (#9246)"

This reverts commit 5bd47b4.

* Change files

* change file

* Update revert-fix-visibility-change.md
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.

2 participants