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

WebBrowserComponent: Add WebViewLifetimeListener

This commit is contained in:
attila 2024-08-23 09:57:30 +02:00 committed by Attila Szarvas
parent 0802db13ee
commit b395239884
2 changed files with 41 additions and 1 deletions

View file

@ -593,9 +593,17 @@ WebBrowserComponent::WebBrowserComponent (const Options& options)
ignoreUnused (blankPageShown);
ignoreUnused (unloadPageWhenHidden);
#endif
for (const auto& l : options.getLifetimeListeners())
lifetimeListeners.add (l);
lifetimeListeners.call (&WebViewLifetimeListener::webViewConstructed, this);
}
WebBrowserComponent::~WebBrowserComponent() = default;
WebBrowserComponent::~WebBrowserComponent()
{
lifetimeListeners.call (&WebViewLifetimeListener::webViewDestructed, this);
}
void WebBrowserComponent::emitEventIfBrowserIsVisible (const Identifier& eventId, const var& object)
{

View file

@ -43,6 +43,25 @@ namespace juce
#define JUCE_WEB_BROWSER_RESOURCE_PROVIDER_AVAILABLE 0
#endif
class WebBrowserComponent;
/** Type for a listener registered with
WebBrowserComponent::Options::withWebViewLifetimeListener. This can be useful for
types using the WebBrowserComponent::Options::withOptionsFrom function as they have to be
constructed before the WebBrowserComponent.
*/
class WebViewLifetimeListener
{
public:
virtual ~WebViewLifetimeListener() = default;
/** Called from the WebBrowserComponent constructor. */
virtual void webViewConstructed (WebBrowserComponent*) = 0;
/** Called from the WebBrowserComponent destructor. */
virtual void webViewDestructed (WebBrowserComponent*) = 0;
};
//==============================================================================
/**
A component that displays an embedded web browser.
@ -372,6 +391,16 @@ public:
}
#endif
/** Adds an object that will be notified when the WebBrowserComponent is constructed and
destructed.
*/
[[nodiscard]] Options withWebViewLifetimeListener (WebViewLifetimeListener* listener)
{
auto copy = *this;
copy.lifetimeListeners.push_back (listener);
return copy;
}
/** Adds all options provided by the builder to the returned Options object.
*/
template <typename OptionsType>
@ -393,6 +422,7 @@ public:
const auto& getInitialisationData() const { return initialisationData; }
auto getResourceProvider() const { return resourceProvider; }
const auto& getAllowedOrigin() const { return allowedOrigin; }
const auto& getLifetimeListeners() const { return lifetimeListeners; }
private:
//==============================================================================
@ -408,6 +438,7 @@ public:
std::vector<std::pair<String, var>> initialisationData;
ResourceProvider resourceProvider;
std::optional<String> allowedOrigin;
std::vector<WebViewLifetimeListener*> lifetimeListeners;
};
//==============================================================================
@ -615,6 +646,7 @@ private:
String lastURL;
StringArray lastHeaders;
MemoryBlock lastPostData;
ListenerList<WebViewLifetimeListener> lifetimeListeners;
void reloadLastURL();