Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Problem/Bug]: When intercepting NewWindowRequested, outerHeight matches innerHeight and outerWidth matches innerWidth #4821

Open
pushkin- opened this issue Sep 19, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@pushkin-
Copy link

pushkin- commented Sep 19, 2024

What happened?

When I handle a window.open and create a Form with a certain size, outerHeight incorrectly matches innerHeight and outerWidth matches innerWidth

This doesn't happen if I don't intercept NewWindowRequested and let WebView create the window for me. In that case, if I call window.open(url, name, "width=500,height=500"), outerHeight is 570 and innerHeight is 500.

If I intercept the event and create my own Form with the size passed to window.open, both outerHeight and innerHeight are 461 (where 461 is actually the inner height [based on what the value is in my Electron app])

And both outerWidth and innerWidth are 484.

In my Electron app, the inner height and width are 461 and 484, respectively, but the outer height and width match what I passed into window.open.

It seems like in WebView2, the inner bounds gets set to what was passed into window.open (when intercepting NewWindowRequested or not).
And in the case of intercepting the event and creating my own Form, the outer bounds matches the inner bounds.

Importance

Important. My app's user experience is significantly compromised.

Runtime Channel

Stable release (WebView2 Runtime)

Runtime Version

128.0.2739.67

SDK Version

1.0.2535.41

Framework

Winforms

Operating System

Windows 11

OS Version

22631.4169

Repro steps

  1. run the code below
  2. click Show Form which opens a window with bounds 500,500
  3. open devtools for that window and log outerHeight and innerHeight to see that they match
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.WinForms;

namespace WebView2LostDeferral
{
	public partial class Form1 : Form
	{
		private CoreWebView2Environment? _webView2Environment;

		public Form1()
		{
			InitializeComponent();
		}

		private async void Form1_Load(object sender, EventArgs e)
		{
			_webView2Environment = await CoreWebView2Environment.CreateAsync(null);
			await webView21.EnsureCoreWebView2Async(_webView2Environment, _webView2Environment.CreateCoreWebView2ControllerOptions());
			webView21.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
			webView21.CoreWebView2.Navigate("https://www.msn.com");
		}

		private async void CoreWebView2_NewWindowRequested(object? sender, CoreWebView2NewWindowRequestedEventArgs e)
		{
			e.Handled = true;

			Form secondForm = new()
			{
				StartPosition = FormStartPosition.CenterScreen,
				Size = new(800, (int)e.WindowFeatures.Height)
			};
			WebView2 webView = new()
			{
				Dock = DockStyle.Fill
			};
			secondForm.Controls.Add(webView);
			secondForm.Show();

			using (e.GetDeferral())
			{
				await webView.EnsureCoreWebView2Async(_webView2Environment, _webView2Environment!.CreateCoreWebView2ControllerOptions());
				e.NewWindow = webView.CoreWebView2;  // must do this after calling AddScriptToExecuteOnDocumentCreatedAsync
			}
		}

		private void btnShowForm_Click(object sender, EventArgs e)
		{
			_ = webView21.CoreWebView2.ExecuteScriptAsync("window.open('https://www.example.com', 'a', 'height=500,width=500');");
		}
	}
}

designer file:

namespace WebView2LostDeferral
{
	partial class Form1
	{
		/// <summary>
		///  Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		///  Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code

		/// <summary>
		///  Required method for Designer support - do not modify
		///  the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			webView21 = new Microsoft.Web.WebView2.WinForms.WebView2();
			btnShowForm = new Button();
			((System.ComponentModel.ISupportInitialize)webView21).BeginInit();
			SuspendLayout();
			// 
			// webView21
			// 
			webView21.AllowExternalDrop = true;
			webView21.CreationProperties = null;
			webView21.DefaultBackgroundColor = Color.White;
			webView21.Dock = DockStyle.Bottom;
			webView21.Location = new Point(0, 49);
			webView21.Name = "webView21";
			webView21.Size = new Size(800, 401);
			webView21.TabIndex = 0;
			webView21.ZoomFactor = 1D;
			// 
			// btnShowForm
			// 
			btnShowForm.Location = new Point(12, 12);
			btnShowForm.Name = "btnShowForm";
			btnShowForm.Size = new Size(113, 25);
			btnShowForm.TabIndex = 1;
			btnShowForm.Text = "Show Form";
			btnShowForm.UseVisualStyleBackColor = true;
			btnShowForm.Click += btnShowForm_Click;
			// 
			// Form1
			// 
			AutoScaleDimensions = new SizeF(7F, 15F);
			AutoScaleMode = AutoScaleMode.Font;
			ClientSize = new Size(800, 450);
			Controls.Add(btnShowForm);
			Controls.Add(webView21);
			Name = "Form1";
			Text = "Form1";
			Load += Form1_Load;
			((System.ComponentModel.ISupportInitialize)webView21).EndInit();
			ResumeLayout(false);
		}

		#endregion

		private Microsoft.Web.WebView2.WinForms.WebView2 webView21;
		private Button btnShowForm;
	}
}

Repros in Edge Browser

No, issue does not reproduce in the corresponding Edge version

Regression

No, this never worked

Last working version (if regression)

No response

@pushkin- pushkin- added the bug Something isn't working label Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant