1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Use WebViewLifetimeListener in WebControlRelays

This commit is contained in:
attila 2024-08-23 10:08:01 +02:00
parent b395239884
commit 853e2052ff
4 changed files with 98 additions and 27 deletions

View file

@ -2,6 +2,32 @@
# develop # develop
## Change
The constructors of the WebSliderRelay, WebToggleButtonRelay and
WebComboBoxRelay classes were changed and they no longer accept a reference
parameter to a WebBrowserComponent object.
**Possible Issues**
Code that uses these classes will fail to compile.
**Workaround**
Omit the WebBrowserComponent parameter when constructing the relay objects.
**Rationale**
The relay classes use a new underlying mechanism to obtain a pointer to the
WebBrowserComponent object. When calling the
WebBrowserComponent::Options::withOptionsFrom() function with the relay as a
parameter, the corresponding WebBrowserComponent object will notify the relay
about its creation and destruction.
This avoids the anti-pattern where the relay class required a reference to a
yet uninitialised WebBrowserComponent object.
## Change ## Change
The coefficients of LadderFilter::Mode::BPF12 have been changed, causing a The coefficients of LadderFilter::Mode::BPF12 have been changed, causing a

View file

@ -438,9 +438,9 @@ public:
private: private:
WebViewPluginAudioProcessor& processorRef; WebViewPluginAudioProcessor& processorRef;
WebSliderRelay cutoffSliderRelay { webComponent, "cutoffSlider" }; WebSliderRelay cutoffSliderRelay { "cutoffSlider" };
WebToggleButtonRelay muteToggleRelay { webComponent, "muteToggle" }; WebToggleButtonRelay muteToggleRelay { "muteToggle" };
WebComboBoxRelay filterTypeComboRelay { webComponent, "filterTypeCombo" }; WebComboBoxRelay filterTypeComboRelay { "filterTypeCombo" };
WebControlParameterIndexReceiver controlParameterIndexReceiver; WebControlParameterIndexReceiver controlParameterIndexReceiver;

View file

@ -37,9 +37,8 @@ namespace juce
#if JUCE_WEB_BROWSER #if JUCE_WEB_BROWSER
WebSliderRelay::WebSliderRelay (WebBrowserComponent& browserIn, StringRef nameIn) WebSliderRelay::WebSliderRelay (StringRef nameIn)
: browser (browserIn), : name (nameIn)
name (nameIn)
{ {
} }
@ -75,12 +74,25 @@ WebBrowserComponent::Options WebSliderRelay::buildOptions (const WebBrowserCompo
{ {
return initialOptions return initialOptions
.withEventListener (eventId, [this] (auto object) { handleEvent (object); }) .withEventListener (eventId, [this] (auto object) { handleEvent (object); })
.withInitialisationData ("__juce__sliders", name); .withInitialisationData ("__juce__sliders", name)
.withWebViewLifetimeListener (this);
} }
void WebSliderRelay::emitEvent (const var& payload) void WebSliderRelay::emitEvent (const var& payload)
{ {
browser.emitEventIfBrowserIsVisible (eventId, payload); if (browser != nullptr)
browser->emitEventIfBrowserIsVisible (eventId, payload);
}
void WebSliderRelay::webViewConstructed (WebBrowserComponent* browserIn)
{
browser = browserIn;
listeners.call (&Listener::initialUpdateRequested, this);
}
void WebSliderRelay::webViewDestructed (WebBrowserComponent*)
{
browser = nullptr;
} }
void WebSliderRelay::handleEvent (const var& event) void WebSliderRelay::handleEvent (const var& event)
@ -122,9 +134,8 @@ void WebSliderRelay::handleEvent (const var& event)
} }
//============================================================================== //==============================================================================
WebToggleButtonRelay::WebToggleButtonRelay ([[maybe_unused]] WebBrowserComponent& browserIn, StringRef nameIn) WebToggleButtonRelay::WebToggleButtonRelay (StringRef nameIn)
: browser (browserIn), : name (nameIn)
name (nameIn)
{ {
} }
@ -154,12 +165,25 @@ WebBrowserComponent::Options WebToggleButtonRelay::buildOptions (const WebBrowse
{ {
return initialOptions return initialOptions
.withEventListener (eventId, [this] (auto object) { handleEvent (object); }) .withEventListener (eventId, [this] (auto object) { handleEvent (object); })
.withInitialisationData ("__juce__toggles", name); .withInitialisationData ("__juce__toggles", name)
.withWebViewLifetimeListener (this);
} }
void WebToggleButtonRelay::emitEvent (const var& payload) void WebToggleButtonRelay::emitEvent (const var& payload)
{ {
browser.emitEventIfBrowserIsVisible (eventId, payload); if (browser != nullptr)
browser->emitEventIfBrowserIsVisible (eventId, payload);
}
void WebToggleButtonRelay::webViewConstructed (WebBrowserComponent* browserIn)
{
browser = browserIn;
listeners.call (&Listener::initialUpdateRequested);
}
void WebToggleButtonRelay::webViewDestructed (WebBrowserComponent*)
{
browser = nullptr;
} }
void WebToggleButtonRelay::handleEvent (const var& event) void WebToggleButtonRelay::handleEvent (const var& event)
@ -187,9 +211,8 @@ void WebToggleButtonRelay::handleEvent (const var& event)
} }
//============================================================================== //==============================================================================
WebComboBoxRelay::WebComboBoxRelay ([[maybe_unused]] WebBrowserComponent& browserIn, StringRef nameIn) WebComboBoxRelay::WebComboBoxRelay (StringRef nameIn)
: browser (browserIn), : name (nameIn)
name (nameIn)
{ {
} }
@ -219,12 +242,25 @@ WebBrowserComponent::Options WebComboBoxRelay::buildOptions (const WebBrowserCom
{ {
return initialOptions return initialOptions
.withEventListener (eventId, [this] (auto object) { handleEvent (object); }) .withEventListener (eventId, [this] (auto object) { handleEvent (object); })
.withInitialisationData ("__juce__comboBoxes", name); .withInitialisationData ("__juce__comboBoxes", name)
.withWebViewLifetimeListener (this);
} }
void WebComboBoxRelay::emitEvent (const var& payload) void WebComboBoxRelay::emitEvent (const var& payload)
{ {
browser.emitEventIfBrowserIsVisible (eventId, payload); if (browser != nullptr)
browser->emitEventIfBrowserIsVisible (eventId, payload);
}
void WebComboBoxRelay::webViewConstructed (WebBrowserComponent* browserIn)
{
browser = browserIn;
listeners.call (&Listener::initialUpdateRequested);
}
void WebComboBoxRelay::webViewDestructed (WebBrowserComponent*)
{
browser = nullptr;
} }
void WebComboBoxRelay::handleEvent (const var& event) void WebComboBoxRelay::handleEvent (const var& event)

View file

@ -62,14 +62,15 @@ namespace juce
@tags{GUI} @tags{GUI}
*/ */
class JUCE_API WebSliderRelay : public OptionsBuilder<WebBrowserComponent::Options> class JUCE_API WebSliderRelay : public OptionsBuilder<WebBrowserComponent::Options>,
private WebViewLifetimeListener
{ {
public: public:
/** Creating a relay will ensure that a Javascript object under the provided name will be /** Creating a relay will ensure that a Javascript object under the provided name will be
available in the specified WebBrowserComponent's context. Use the frontend framework's available in the specified WebBrowserComponent's context. Use the frontend framework's
getSliderState function with the same name to get a hold of this object. getSliderState function with the same name to get a hold of this object.
*/ */
WebSliderRelay (WebBrowserComponent& browserIn, StringRef nameIn); WebSliderRelay (StringRef nameIn);
//============================================================================== //==============================================================================
/** @internal */ /** @internal */
@ -98,8 +99,10 @@ public:
private: private:
void handleEvent (const var& event); void handleEvent (const var& event);
void webViewConstructed (WebBrowserComponent*) override;
void webViewDestructed (WebBrowserComponent*) override;
WebBrowserComponent& browser; WebBrowserComponent* browser = nullptr;
String name; String name;
float value{}; float value{};
Identifier eventId { "__juce__slider" + name }; Identifier eventId { "__juce__slider" + name };
@ -134,14 +137,15 @@ private:
@tags{GUI} @tags{GUI}
*/ */
class JUCE_API WebToggleButtonRelay : public OptionsBuilder<WebBrowserComponent::Options> class JUCE_API WebToggleButtonRelay : public OptionsBuilder<WebBrowserComponent::Options>,
private WebViewLifetimeListener
{ {
public: public:
/** Creating a relay will ensure that a Javascript object under the provided name will be /** Creating a relay will ensure that a Javascript object under the provided name will be
available in the specified WebBrowserComponent's context. Use the frontend framework's available in the specified WebBrowserComponent's context. Use the frontend framework's
getToggleState function with the same name to get a hold of this object. getToggleState function with the same name to get a hold of this object.
*/ */
WebToggleButtonRelay (WebBrowserComponent& browserIn, StringRef nameIn); WebToggleButtonRelay (StringRef nameIn);
//============================================================================== //==============================================================================
/** @internal */ /** @internal */
@ -169,8 +173,10 @@ public:
private: private:
void handleEvent (const var& event); void handleEvent (const var& event);
void webViewConstructed (WebBrowserComponent*) override;
void webViewDestructed (WebBrowserComponent*) override;
WebBrowserComponent& browser; WebBrowserComponent* browser = nullptr;
String name; String name;
Identifier eventId { "__juce__toggle" + name }; Identifier eventId { "__juce__toggle" + name };
ListenerList<Listener> listeners; ListenerList<Listener> listeners;
@ -204,14 +210,15 @@ private:
@tags{GUI} @tags{GUI}
*/ */
class JUCE_API WebComboBoxRelay : public OptionsBuilder<WebBrowserComponent::Options> class JUCE_API WebComboBoxRelay : public OptionsBuilder<WebBrowserComponent::Options>,
private WebViewLifetimeListener
{ {
public: public:
/** Creating a relay will ensure that a Javascript object under the provided name will be /** Creating a relay will ensure that a Javascript object under the provided name will be
available in the specified WebBrowserComponent's context. Use the frontend framework's available in the specified WebBrowserComponent's context. Use the frontend framework's
getComboBoxState function with the same name to get a hold of this object. getComboBoxState function with the same name to get a hold of this object.
*/ */
WebComboBoxRelay (WebBrowserComponent& browserIn, StringRef nameIn); WebComboBoxRelay (StringRef nameIn);
//============================================================================== //==============================================================================
/** @internal */ /** @internal */
@ -239,8 +246,10 @@ public:
private: private:
void handleEvent (const var& event); void handleEvent (const var& event);
void webViewConstructed (WebBrowserComponent*) override;
void webViewDestructed (WebBrowserComponent*) override;
WebBrowserComponent& browser; WebBrowserComponent* browser = nullptr;
String name; String name;
Identifier eventId { "__juce__comboBox" + name }; Identifier eventId { "__juce__comboBox" + name };
ListenerList<Listener> listeners; ListenerList<Listener> listeners;