MicrosoftEdge / WebView2Feedback

Feedback and discussions about Microsoft Edge WebView2
https://aka.ms/webview2
424 stars 51 forks source link

WebView window position is not set correctly #3659

Open ztextplus opened 11 months ago

ztextplus commented 11 months ago

Description First of all, this problem occurs occasionally. During the test,.it was found that the WebView window was shifted to the upper left.

As shown in the figure below, the webview is shifted to the left, causing the non-webview content on the right to be displayed:

image

Using the window tool, we found that the position of the window below is not set correctly, but the position of the parent window is set correctly

image

Version SDK: .0.1264.42 release Runtime: 114.0.1823.82 Framework: Win32 OS: Win10

novac42 commented 11 months ago

Thanks for reporting the issue and sorry you are running into it. I've assigned this to a dev who can help follow up on this.

bradp0721 commented 11 months ago

@ztextplus What are the steps to reproduce the issue? Does this involve multiple monitors or changing DPI scaling?

So just the Intermediate D3D Window position is wrong, but the Chrome_WidgetWin_1 window is correctly positioned? The positioning of the Intermediate D3D Window is code that is shared with Chromium. Is your app sizing/positioning the WebView2 using the CoreWebView2Controller::put_Bounds API or by directly interacting with the HWNDs?

ztextplus commented 11 months ago

@bradp0721 The answer to your question is as follows: 1.So just the Intermediate D3D Window position is wrong, but the Chrome_WidgetWin_1 window is correctly positioned? The position of the Chrome_WidgetWin_1 window is correct, the position we see live is (0, 0, 955, 906)

  1. Does this involve multiple monitors or changing DPI scaling? We're just using the laptop, not switching between two screens with different DPIs.
  2. Is your app sizing/positioning the WebView2 using the CoreWebView2Controller::put_Bounds API or by directly interacting with the HWNDs? We are using this API CoreWebView2Controller::put_Bounds to set the position of WebView, without directly using the windows window function.
bradp0721 commented 11 months ago

@ztextplus you said this happens occasionally. I haven't seen any other reports of this type of issue, so I'm trying to figure out how to get a repro or get an idea of what could be going wrong. When do you notice this happening? Does the app launch this way? Does this happen after closing and opening the laptop lid? Does it happen after moving/resizing the application?

ztextplus commented 11 months ago

@bradp0721 After careful analysis, we found a problem, The background is like this: we made a loading animation for WebView2, and the loading animation starts to be displayed during a URL loading process. When the URL is loaded successfully (receiving the OnNavigationCompleted callback), we will display WebView2.

This leads to a problem. Before receiving the callback of OnNavigationCompleted, we never call CoreWebView2Controller::put_Bounds to set the position of WebView2. We will not call CoreWebView2Controller::put_Bounds to set the position of WebView2 until we receive the callback of OnNavigationCompleted. .

I don't know if doing this will cause potential problems with WebView2, but we have been doing this before, and occasionally this exception occurs.

bradp0721 commented 11 months ago

Are you showing the WebView2 before receiving OnNavigationCompleted? You need to call put_Bounds before making the WebView2 visible to the user. Otherwise, you're at the mercy of however Windows default positioned the HWNDs during creation. I'm actually a little surprised that any WebView2 content is visible without calling put_Bounds.

Also, changing the size of the WebView2 causes the HTML page to re-layout its content. So currently, you are waiting for the page to completely load, then calling put_Bounds to size it, which causes a relayout. If you know what size the WebView2 will be, you should call put_Bounds before calling Navigate. That way the WebView2 loads and renders its content at the desired size. Then you can show the WebView2 when you receive OnNavigationCompleted.

EnaXnaY commented 7 months ago

Have same issue at 4K monitor ;) at normal notebook display and normal monitor (even if scaled) displayed correctly ;)

bug at 4K monitor