iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
IPlugWebView.h
1 /*
2 ==============================================================================
3
4 MIT License
5
6 iPlug2 WebView Library
7 Copyright (c) 2024 Oliver Larkin
8
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in all
17 copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 SOFTWARE.
26
27 ==============================================================================
28*/
29
30#pragma once
31
32#include "IPlugPlatform.h"
33#include "IPlugLogger.h"
34#include "wdlstring.h"
35#include <functional>
36#include <memory>
37
38BEGIN_IPLUG_NAMESPACE
39
40class IWebViewImpl;
41
44{
45public:
46 using completionHandlerFunc = std::function<void(const char* result)>;
47
53 IWebView(bool opaque = true, bool enableDevTools = false, const char* customUrlScheme = "");
54 virtual ~IWebView();
55
56 void* OpenWebView(void* pParent, float x, float y, float w, float h, float scale = 1.0f);
57 void CloseWebView();
58 void HideWebView(bool hide);
59
61 void LoadHTML(const char* html);
62
64 void LoadURL(const char* url);
65
70 void LoadFile(const char* fileName, const char* bundleID = "");
71
74
78 void EvaluateJavaScript(const char* scriptStr, completionHandlerFunc func = nullptr);
79
81 void EnableScroll(bool enable);
82
84 void EnableInteraction(bool enable);
85
87 void SetWebViewBounds(float x, float y, float w, float h, float scale = 1.);
88
90 void GetWebRoot(WDL_String& path) const;
91
93 const char* GetCustomUrlScheme() const { return mCustomUrlScheme.Get(); }
94
96 bool GetEnableDevTools() const { return mEnableDevTools; }
97
99 bool IsOpaque() const { return mOpaque; }
100
102 void SetCustomUrlScheme(const char* customUrlScheme) { mCustomUrlScheme.Set(customUrlScheme); }
103
105 void SetEnableDevTools(bool enable) { mEnableDevTools = enable; }
106
107#pragma mark -
108
110 virtual void OnWebViewReady() {}
111
113 virtual void OnWebContentLoaded() {}
114
116 virtual void OnMessageFromWebView(const char* json) {}
117
119 virtual bool OnCanNavigateToURL(const char* url) { return true; }
120
122 virtual bool OnCanDownloadMIMEType(const char* mimeType) { return false; }
123
125 virtual void OnGetLocalDownloadPathForFile(const char* fileName, WDL_String& localPath);
126
128 virtual void OnDownloadedFile(const char* path) { DBGMSG("Downloaded %s\n", path);}
129
131 virtual void OnFailedToDownloadFile(const char* path) { DBGMSG("Downloading %s failed\n", path); }
132
134 virtual void OnReceivedData(size_t numBytesReceived, size_t totalNumBytes) {}
135
136private:
137 std::unique_ptr<IWebViewImpl> mpImpl;
138 bool mOpaque;
139 bool mEnableDevTools = false;
140 WDL_String mCustomUrlScheme;
141};
142
143END_IPLUG_NAMESPACE
IPlug logging a.k.a tracing functionality.
Include to get consistently named preprocessor macros for different platforms and logging functionali...
IWebView is a base interface for hosting a platform web view inside an IPlug plug-in's UI.
Definition: IPlugWebView.h:44
virtual bool OnCanDownloadMIMEType(const char *mimeType)
Override to filter MIME types that should be downloaded.
Definition: IPlugWebView.h:122
void EnableInteraction(bool enable)
Sets whether the webview is interactive.
void EvaluateJavaScript(const char *scriptStr, completionHandlerFunc func=nullptr)
Runs some JavaScript in the webview.
virtual bool OnCanNavigateToURL(const char *url)
Override to filter URLs.
Definition: IPlugWebView.h:119
void EnableScroll(bool enable)
Enable scrolling on the webview.
void LoadFile(const char *fileName, const char *bundleID="")
Load a file on disk into the web view.
virtual void OnDownloadedFile(const char *path)
Override to handle file download success.
Definition: IPlugWebView.h:128
const char * GetCustomUrlScheme() const
Returns the custom URL scheme, if set.
Definition: IPlugWebView.h:93
virtual void OnFailedToDownloadFile(const char *path)
Override to handle file download failure.
Definition: IPlugWebView.h:131
void LoadHTML(const char *html)
Load an HTML string into the webview.
void SetCustomUrlScheme(const char *customUrlScheme)
Used to set the URL scheme after the IWebView has been contstructed;.
Definition: IPlugWebView.h:102
bool IsOpaque() const
True if the webview was configured opaque.
Definition: IPlugWebView.h:99
virtual void OnWebContentLoaded()
Called after navigation instructions have been exectued and e.g.
Definition: IPlugWebView.h:113
void LoadURL(const char *url)
Instruct the webview to load an external URL.
virtual void OnGetLocalDownloadPathForFile(const char *fileName, WDL_String &localPath)
Override to download the file to a specific location other than e.g.
void SetWebViewBounds(float x, float y, float w, float h, float scale=1.)
Set the bounds of the webview in the parent window.
virtual void OnWebViewReady()
Called when the web view is ready to receive navigation instructions.
Definition: IPlugWebView.h:110
void SetEnableDevTools(bool enable)
Used to toggle devtools after the IWebView has been contstructed.
Definition: IPlugWebView.h:105
void GetWebRoot(WDL_String &path) const
Fills the path where web content is being served from, when LoadFile() is used.
virtual void OnMessageFromWebView(const char *json)
When a script in the web view posts a message, it will arrive as a UTF8 json string here.
Definition: IPlugWebView.h:116
void ReloadPageContent()
Trigger a reload of the webview's content.
virtual void OnReceivedData(size_t numBytesReceived, size_t totalNumBytes)
Override to handle file download progress.
Definition: IPlugWebView.h:134
IWebView(bool opaque=true, bool enableDevTools=false, const char *customUrlScheme="")
Constructs an IWebView.
bool GetEnableDevTools() const
Are developer tools enabled on this webview.
Definition: IPlugWebView.h:96