From 29ef97d6b590277e8f0c9bb0c86992a170f0eb9b Mon Sep 17 00:00:00 2001 From: Nik Pavlov Date: Tue, 15 Oct 2024 17:58:23 +0000 Subject: [PATCH] mac: views: Always initialize CommandDispatcher (see #3188) ChromeCommandDispatcherDelegate forwards accelerator events to the FocusManager through NativeWidgetMacNSWindowHost:: HandleAccelerator. Always initialize it so that accelerators also work in a CefWindow without a BrowserView. --- libcef/browser/views/native_widget_mac.h | 1 + libcef/browser/views/native_widget_mac.mm | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/libcef/browser/views/native_widget_mac.h b/libcef/browser/views/native_widget_mac.h index bb7e7e354..053021b7e 100644 --- a/libcef/browser/views/native_widget_mac.h +++ b/libcef/browser/views/native_widget_mac.h @@ -52,6 +52,7 @@ class CefNativeWidgetMac : public views::NativeWidgetMac { bool IsCefWindowInitialized() const; raw_ptr browser_view_ = nullptr; + bool initialized_ = false; }; #endif // CEF_LIBCEF_BROWSER_VIEWS_NATIVE_WIDGET_MAC_H_ diff --git a/libcef/browser/views/native_widget_mac.mm b/libcef/browser/views/native_widget_mac.mm index f6167435a..522014ebb 100644 --- a/libcef/browser/views/native_widget_mac.mm +++ b/libcef/browser/views/native_widget_mac.mm @@ -138,7 +138,12 @@ void CefNativeWidgetMac::OnWindowFullscreenTransitionComplete() { } void CefNativeWidgetMac::OnWindowInitialized() { - if (!browser_view_) { + // This connects the native widget with the command dispatcher so accelerators + // work even if a browser_view_ is not created later. + // The initialized_ check is necessary because the method can be called twice: + // 1. From NativeWidgetMac::InitNativeWidget + // 2. From ChromeBrowserFrame::Init + if (initialized_) { return; } @@ -146,10 +151,12 @@ void CefNativeWidgetMac::OnWindowInitialized() { if (auto* bridge = GetInProcessNSWindowBridge()) { bridge->SetCommandDispatcher([[ChromeCommandDispatcherDelegate alloc] init], [[BrowserWindowCommandHandler alloc] init]); - } else { + initialized_ = true; + } else if (browser_view_) { if (auto* host = GetHostForBrowser(browser_view_->browser())) { host->GetAppShim()->CreateCommandDispatcherForWidget( GetNSWindowHost()->bridged_native_widget_id()); + initialized_ = true; } } }