MicrosoftEdge / MicrosoftEdge/WebView2Feedback

WebView ZoomFactor Resets on each Navigation

Open
#3,459 14 comments 2 reactions 1 assignee View on GitHub

@bradp0721 is already working on this.

Since May 8, 2023.

bug
Dominant language
PowerShell
Stars
526
Forks
67
PR merge metrics
No merged PRs in 30d

Description

The ZoomFactor property is getting reset every time the WebView instance is navigated when it should maintain the ZoomFactor across navigations. We are after all looking at the same instance.

Observed:
Any time you create a new WebView instance and change the source or refresh the ZoomFactor is reset to its initial value. If you set the value during Initialization to 2 it resets to 2. Default is 1 and it so it usually resets to 1.

Additionally the ZoomFactorChanged event is fired whenever the page is refreshed and receives the now reset value instead of the current active ZoomFactor the user may have changed to during using the active Web page.

Steps:

  • create a WebView
  • Open a page in the WebView
  • Change the Zoom level with Ctrl-Mousewheel or Ctrl+/-
  • Refresh the page

Observed:

Changing zoom level works, but is not persisted across the refresh operation.

Expected:

Since you are setting the ZoomFactor on the WebBrowser instance I would expect the ZoomFactor to stay at what the user sets it to unless it's explicitly overridden at the code level.

Further the fact that ZoomFactorChanged is fired when the value is reset makes it very difficult to maintain this value explicitly. It would be useful to be able to distinguish between:

  • Initial Assignment
  • Reset on Navigation
  • Changed by user / in document

Without this you need to use flag values to disable level capture in NavigationStarting and enable in DomContentLoaded which is a PITA for something that seems like it should be automatic since you set the value at the WebBrowser instance.

FWIW full browsers behave as follows:

  • Refresh: Zoom level is preserved
  • Load a new page: Zoom level is reset to default (in Edge)
  • Go back to original page: Zoom level is back to now zoomed level (ie. change is remembered)
Workaround

For now the workaround I use requires a bunch of extra code:

        /// <summary>
        /// If true, tracks the Zoom factor of this browser instance
        /// over refreshes. By default the WebView looses any user
        /// zooming via Ctrl+ and Ctrl- or Ctrl-Mousewheel whenever
        /// the content is reloaded. This settings explicitly keeps
        /// track of the ZoomFactor and overrides it.
        /// </summary>
        public bool TrackZoomFactor { get; set; }
        
        public double ZoomLevel { get; set;  }= 1;
        private bool CaptureZoom = false;
        
        /// <summary>
        /// Handle keeping the Zoom Factor the same for all preview instances
        /// Without this 
        /// </summary>
        private void CaptureZoomFactor()
        {
            if (WebBrowser.CoreWebView2 == null)
            {
                throw new InvalidOperationException("Please call CaptureZoomFactor after WebView has been initialized");
            }
            
            if (!TrackZoomFactor)
                return;
            
            WebBrowser.ZoomFactorChanged += (s, e) =>
            {
                if (!CaptureZoom)
                {
                    WebBrowser.ZoomFactor = ZoomLevel;
                }
                else
                {
                    ZoomLevel = WebBrowser.ZoomFactor;
                }
            };
            WebBrowser.NavigationStarting += (object sender, CoreWebView2NavigationStartingEventArgs e) => CaptureZoom = false;
            WebBrowser.CoreWebView2.DOMContentLoaded += (s, e) => CaptureZoom = true;
        }

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.