Fix macOS app half-activation state and menu closure issue
Moderate
Commit Hash:
4b4dec9460a4fe927c90d57e49d43fa25e2ec327
Commit Time: 2025-11-14 19:48:35+00:00
Impact Level: Moderate
Generated By: webview2-upstream-sentry
Upstream Review:
View Upstream Review 🔗
📋 Summary
This commit fixes two related issues on macOS: (1) When clicking a menu item while the app is inactive, AppKit can enter a "half-activated" state where the app won't fully activate; (2) The NSWindowDidBecomeKey notification triggered by the first click activation causes the menu to close prematurely, preventing menu item execution. The fix consists of three parts: In mouse_capture.mm's local event monitor, when the app is inactive and about to consume a left mouse down event, explicitly call [NSApp activateIgnoringOtherApps:YES] to activate the app, avoiding the half-activated state; Add SetIgnoreWindowKeyNotificationOnce() method and ignore_window_key_notification_once_ flag in MenuCocoaWatcherMac to ignore a single NSWindowDidBecomeKeyNotification; In MenuController::OnMousePressed(), detect inactive app state and set the flag to ignore the next window-became-key notification in this scenario, ensuring menu items can execute properly.
🎯 Impact Analysis
This change has a moderate impact on WebView2Mac. While the modified code involves Remote Cocoa's mouse capture mechanism and menu controller, which are core components that WebView2Mac depends on, the changes primarily affect application-level activation state handling and menu behavior rather than core window bridging functionality. Specific impacts include: (1) Changes in mouse_capture.mm modify the local event monitor's behavior to explicitly activate the app when clicked while inactive. WebView2Mac uses mouse capture functionality through Remote Cocoa to handle user input, and this change may affect click behavior and activation logic for WebView2 controls when the host application is inactive; (2) Changes in MenuCocoaWatcherMac add functionality to ignore a single window key notification. WebView2Mac may use this class to monitor menu state changes, and the new ignore logic could affect menu dismissal timing; (3) MenuController changes affect menu behavior in inactive applications. If WebView2Mac displays context menus or other menus within embedded web content, this change may improve the reliability of menu item execution. Overall, these changes aim to improve user experience by fixing edge cases in activation and menu behavior, and should have a positive impact on WebView2Mac. However, testing is needed to ensure that application activation and menu interactions still work properly in WebView2 scenarios, particularly when users interact with WebView2 controls while the host application is in the background.
Impacted Classes:
remote_cocoa::CocoaMouseCapture
remote_cocoa::CocoaMouseCapture::ActiveEventTap
views::MenuCocoaWatcherMac
views::MenuController