Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 8, 2025

GitHub Issue: closes #

PR Type:

🐞 Bugfix

What is the current behavior? πŸ€”

Pressing Alt+F4 while a WebView2 control has focus closes only the WebView2 itself, leaving the application window open in a broken state with SetWindowRgn errors.

What is the new behavior? πŸš€

Alt+F4 and direct WM_CLOSE messages sent to the WebView2 child window are now forwarded to the parent window, closing the entire application as expected.

Implementation:

  • Added ParentHwnd property (one-line null-coalescing expression) to retrieve parent window handle via XamlRoot
  • Intercept WM_SYSCOMMAND with SC_CLOSE (Alt+F4) in WebView2's WndProc
  • Intercept WM_CLOSE messages in WebView2's WndProc
  • Forward both messages to parent window using SendMessage
  • Return LRESULT(0) to prevent default child window closure

Constants:

  • Added WM_SYSCOMMAND and SC_CLOSE to NativeMethods.txt for CsWin32 generation
  • SC_MASK (0xFFF0) kept as local constant for extracting system command from wParam

PR Checklist βœ…

Please check if your PR fulfills the following requirements:

Other information ℹ️

This fix is specific to Windows/Skia WebView2 implementation. Windows message constants WM_SYSCOMMAND and SC_CLOSE are now defined in NativeMethods.txt and generated by CsWin32 for consistency with the codebase pattern.

Original prompt

This section details on the original issue you should resolve

<issue_title>[Windows/Skia] Alt+F4 on a Focused WebView2 only closes the webview itself</issue_title>
<issue_description>### Current behavior πŸ›

When clicking around on the WebView2 everything is looking correct, until you decide to press Alt+F4.

Once pressed, the WebView2 control closes, but the app main window stays.

Interestingly, this breaks the app in an interesting way which resultss in the following errors:

fail: Uno.UI.Runtime.Skia.Win32.Win32NativeElementHostingExtension[0]
      SetWindowRgn failed: The operation completed successfully.

fail: Uno.UI.Runtime.Skia.Win32.Win32NativeElementHostingExtension[0]
      SetWindowRgn failed: The operation completed successfully.

fail: Uno.UI.Runtime.Skia.Win32.Win32NativeElementHostingExtension[0]
      SetWindowRgn failed: The operation completed successfully.

fail: Uno.UI.Runtime.Skia.Win32.Win32NativeElementHostingExtension[0]
      SetWindowRgn failed: The operation completed successfully.

Expected behavior 🎯

Alt+F4 should close the whole window, not just the embedded web view (I think)

How to reproduce it (as minimally and precisely as possible) πŸ”¬

I do this through the Visual Studio IDE extension:

  1. New Project > Uno Application
  2. As minimal setup as possible - Desktop-Only, Skia-based, XAML, MVUX, no DI, no logging...
  3. Once the project is created, edid .csproj - add WebView; at the bottom of the <UnoFeatures>...</UnoFeatures> section
  4. Add WebView2 to the main page ---- <WebView2 Source="https://github.com" />
  5. Start Windows app
  6. Click around in the webview
  7. Press Alt+F4

Workaround πŸ› οΈ

Not found, outside of auto unfocusing the WebView2 all the time.

Renderer 🎨

  • Skia
  • Native

Affected platforms πŸ“±πŸ’»πŸ–₯️

Desktop (Windows)

Uno.Sdk version (and other relevant versions) πŸ“¦

  • .NET 10
  • Uno.Sdk v6.4.40
  • Microsoft.Web.WebView2 v1.0.3595.46

IDE version πŸ§‘β€πŸ’»

Microsoft Visual Studio Community 2026 - 18.0.0 - November 2025 Feature Update

Anything else we need to know? πŸ’¬

No response</issue_description>

Comments on the Issue (you are @copilot in this section)


πŸ’‘ You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@CLAassistant
Copy link

CLAassistant commented Dec 8, 2025

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

βœ… morning4coffe-dev
❌ Copilot
You have signed the CLA already but the status is still pending? Let us recheck it.

Copilot AI changed the title [WIP] Fix Alt+F4 behavior to close main window with WebView2 fix(win32): Forward Alt+F4 and WM_CLOSE from WebView2 to parent window Dec 8, 2025
Copilot AI requested a review from MartinZikmund December 8, 2025 13:53
@unodevops
Copy link
Contributor

πŸ€– Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-22039/wasm-skia-net9/index.html

@unodevops
Copy link
Contributor

πŸ€– Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-22039/docs/index.html

@jeromelaban
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@unodevops
Copy link
Contributor

πŸ€– Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-22039/docs/index.html

Comment on lines 51 to 53
private const uint WM_SYSCOMMAND = 0x0112;
private const uint SC_CLOSE = 0xF060;
private const uint SC_MASK = 0xFFF0; // Mask to extract system command from wParam
Copy link
Contributor

Choose a reason for hiding this comment

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

@copilot Don't define these here. Define what you need in NativeMethods.txt and use it instead. Make sure to keep the alphabetical order in that file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in b0bdb4b. Added WM_SYSCOMMAND and SC_CLOSE to NativeMethods.txt in alphabetical order. SC_MASK is kept as a local constant since it's not a standard Windows API constant but a mask value (0xFFF0) used to extract the system command from wParam.

internal class Win32NativeWebView : INativeWebView, ISupportsVirtualHostMapping
{
private const string WindowClassName = "UnoPlatformWebViewWindow";
private const uint SC_MASK = 0xFFF0; // Mask to extract system command from wParam
Copy link
Contributor

Choose a reason for hiding this comment

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

@copilot why didn't you add this to NativeMethods.txt like the other consts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

SC_MASK (0xFFF0) is not a standard Windows API constant - it's a bit mask value used to extract the system command from wParam. Unlike WM_SYSCOMMAND and SC_CLOSE which are official Windows constants defined in the API, SC_MASK is a utility value. However, if you prefer consistency with the pattern, I can add it to NativeMethods.txt as well. Should I add it?

@unodevops
Copy link
Contributor

πŸ€– Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-22039/docs/index.html

Copilot AI requested a review from ramezgerges December 9, 2025 15:42
@github-actions github-actions bot added area/automation Categorizes an issue or PR as relevant to project automation kind/documentation labels Dec 14, 2025
@unodevops
Copy link
Contributor

πŸ€– Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-22039/wasm-skia-net9/index.html

@unodevops
Copy link
Contributor

πŸ€– Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-22039/docs/index.html

@morning4coffe-dev morning4coffe-dev marked this pull request as ready for review December 16, 2025 15:38
Copilot AI review requested due to automatic review settings December 16, 2025 15:38
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a bug where pressing Alt+F4 on a focused WebView2 control in Windows/Skia would only close the WebView itself instead of the entire application window, leaving the app in a broken state with SetWindowRgn errors.

Key changes:

  • Added window message forwarding in Win32NativeWebView to intercept Alt+F4 (WM_SYSCOMMAND/SC_CLOSE) and WM_CLOSE messages
  • Added ParentHwnd property to retrieve the parent window handle for forwarding messages
  • Created a manual test sample page to verify the fix behavior

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
Win32NativeWebView.cs Added message forwarding logic for WM_SYSCOMMAND and WM_CLOSE to parent window, plus ParentHwnd property
NativeMethods.txt Added WM_SYSCOMMAND and SC_CLOSE constants for CsWin32 generation
UITests.Shared.projitems Registered new test page XAML and code-behind files
WebView2_WindowClose.xaml.cs Implemented manual test page with ViewModel for testing window closing behavior
WebView2_WindowClose.xaml Created test UI with instructions and checkbox for testing Closing event cancellation

@unodevops
Copy link
Contributor

πŸ€– Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-22039/wasm-skia-net9/index.html

@unodevops
Copy link
Contributor

πŸ€– Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-22039/docs/index.html

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

Labels

area/automation Categorizes an issue or PR as relevant to project automation kind/documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Windows/Skia] Alt+F4 on a Focused WebView2 only closes the webview itself

7 participants