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; } } }