From 0d8b05e8ee138cdeb091551ff34f70eac820547d Mon Sep 17 00:00:00 2001 From: attila Date: Mon, 3 Jun 2024 18:41:19 +0200 Subject: [PATCH] WebBrowserComponent: Enable click-through behaviour for WkWebView, add option to disable it The default is enabled to behave similarly to NSViewComponentPeer. The behaviour for the old Apple WebView is unchanged. --- .../misc/juce_WebBrowserComponent.h | 14 +++++++++++++ .../native/juce_WebBrowserComponent_mac.mm | 21 ++++++++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/modules/juce_gui_extra/misc/juce_WebBrowserComponent.h b/modules/juce_gui_extra/misc/juce_WebBrowserComponent.h index 2d9b18b437..a2235f79e0 100644 --- a/modules/juce_gui_extra/misc/juce_WebBrowserComponent.h +++ b/modules/juce_gui_extra/misc/juce_WebBrowserComponent.h @@ -229,10 +229,24 @@ public: return withMember (*this, &AppleWkWebView::allowAccessToEnclosingDirectory, x); } + /** If this options is specified, the underlying WebView will return NO from its + acceptsFirstMouse method. + + This disables the click-through behaviour, meaning that clicking a previously + unfocused application window only makes the window focused, but will not pass on the + click to whichever control inside the WebView is under the mouse. + */ + [[nodiscard]] AppleWkWebView withDisabledAcceptsFirstMouse() const + { + return withMember (*this, &AppleWkWebView::acceptsFirstMouse, false); + } + auto getAllowAccessToEnclosingDirectory() const { return allowAccessToEnclosingDirectory; } + auto getAcceptsFirstMouse() const { return acceptsFirstMouse; } private: bool allowAccessToEnclosingDirectory = false; + bool acceptsFirstMouse = true; }; /** Specifies options that apply to the Windows implementation when the WebView2 feature is diff --git a/modules/juce_gui_extra/native/juce_WebBrowserComponent_mac.mm b/modules/juce_gui_extra/native/juce_WebBrowserComponent_mac.mm index c91417a683..3b47e8fbac 100644 --- a/modules/juce_gui_extra/native/juce_WebBrowserComponent_mac.mm +++ b/modules/juce_gui_extra/native/juce_WebBrowserComponent_mac.mm @@ -202,7 +202,7 @@ struct WebViewKeyEquivalentResponder final : public ObjCClass { using Base = ObjCClass; - WebViewKeyEquivalentResponder() + explicit WebViewKeyEquivalentResponder (bool acceptsFirstMouse) : Base ("WebViewKeyEquivalentResponder_") { this->template addIvar (lastFocusChangeMemberName); @@ -283,6 +283,9 @@ struct WebViewKeyEquivalentResponder final : public ObjCClass return result; }); + if (acceptsFirstMouse) + this->addMethod (@selector (acceptsFirstMouse:), [] (id, SEL, NSEvent*) { return YES; }); + this->registerClass(); } }; @@ -680,7 +683,7 @@ class WebBrowserComponent::Impl::Platform::WebViewImpl : public WebBrowserCompo public: WebViewImpl (WebBrowserComponent::Impl& implIn, const String& userAgent) : browser (implIn.owner) { - static WebViewKeyEquivalentResponder webviewClass; + static WebViewKeyEquivalentResponder webviewClass { false }; webView.reset ([webviewClass.createInstance() initWithFrame: NSMakeRect (0, 0, 100.0f, 100.0f) frameName: nsEmptyString() @@ -863,7 +866,19 @@ public: #endif #if JUCE_MAC - static WebViewKeyEquivalentResponder webviewClass; + auto& webviewClass = [&]() -> auto& + { + if (browserOptions.getAppleWkWebViewOptions().getAcceptsFirstMouse()) + { + static WebViewKeyEquivalentResponder juceWebviewClass { true }; + return juceWebviewClass; + } + else + { + static WebViewKeyEquivalentResponder juceWebviewClass { false }; + return juceWebviewClass; + } + }(); webView.reset ([webviewClass.createInstance() initWithFrame: NSMakeRect (0, 0, 100.0f, 100.0f) configuration: config.get()]);