-
Notifications
You must be signed in to change notification settings - Fork 832
fix(win32): Forward Alt+F4 and WM_CLOSE from WebView2 to parent window #22039
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
base: master
Are you sure you want to change the base?
Conversation
Co-authored-by: MartinZikmund <[email protected]>
|
|
Co-authored-by: MartinZikmund <[email protected]>
β¦ SC_CLOSE Co-authored-by: MartinZikmund <[email protected]>
Co-authored-by: MartinZikmund <[email protected]>
|
π€ 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 |
|
π€ Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-22039/docs/index.html |
|
/azp run |
|
Azure Pipelines successfully started running 2 pipeline(s). |
|
π€ Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-22039/docs/index.html |
src/Uno.UI.Runtime.Skia.Win32/UI/Xaml/Controls/WebView/Win32NativeWebView.cs
Outdated
Show resolved
Hide resolved
| 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
β¦pression Co-authored-by: ramezgerges <[email protected]>
β¦Win32 generation Co-authored-by: ramezgerges <[email protected]>
| internal class Win32NativeWebView : INativeWebView, ISupportsVirtualHostMapping | ||
| { | ||
| private const string WindowClassName = "UnoPlatformWebViewWindow"; | ||
| private const uint SC_MASK = 0xFFF0; // Mask to extract system command from wParam |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
|
π€ Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-22039/docs/index.html |
|
π€ 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 |
|
π€ Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-22039/docs/index.html |
There was a problem hiding this 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
ParentHwndproperty 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 |
src/Uno.UI.Runtime.Skia.Win32/UI/Xaml/Controls/WebView/Win32NativeWebView.cs
Show resolved
Hide resolved
...plesApp/UITests.Shared/Microsoft_UI_Xaml_Controls/WebView2Tests/WebView2_WindowClose.xaml.cs
Outdated
Show resolved
Hide resolved
...plesApp/UITests.Shared/Microsoft_UI_Xaml_Controls/WebView2Tests/WebView2_WindowClose.xaml.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Copilot <[email protected]>
|
π€ 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 |
|
π€ Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-22039/docs/index.html |
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
SetWindowRgnerrors.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:
ParentHwndproperty (one-line null-coalescing expression) to retrieve parent window handle via XamlRootWM_SYSCOMMANDwithSC_CLOSE(Alt+F4) in WebView2's WndProcWM_CLOSEmessages in WebView2's WndProcSendMessageLRESULT(0)to prevent default child window closureConstants:
WM_SYSCOMMANDandSC_CLOSEtoNativeMethods.txtfor CsWin32 generationSC_MASK(0xFFF0) kept as local constant for extracting system command from wParamPR Checklist β
Please check if your PR fulfills the following requirements:
Screenshots Compare Test Runresults.Other information βΉοΈ
This fix is specific to Windows/Skia WebView2 implementation. Windows message constants
WM_SYSCOMMANDandSC_CLOSEare now defined inNativeMethods.txtand generated by CsWin32 for consistency with the codebase pattern.Original prompt
π‘ 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.