iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
IWebViewControl.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5
6 See LICENSE.txt for more info.
7
8 ==============================================================================
9*/
10
11#pragma once
12
19#include "IControl.h"
20#include "IPlugWebView.h"
21
22BEGIN_IPLUG_NAMESPACE
23BEGIN_IGRAPHICS_NAMESPACE
24
25class IWebViewControl;
26
27using OnReadyFunc = std::function<void(IWebViewControl* pControl)>;
28using OnMessageFunc = std::function<void(IWebViewControl* pControl, const char* jsonMsg)>;
29
36class IWebViewControl : public IControl, public IWebView
37{
38public:
45 IWebViewControl(const IRECT& bounds, bool opaque, OnReadyFunc readyFunc = nullptr, OnMessageFunc msgFunc = nullptr, bool enableDevTools = false, bool enableScroll = false)
46 : IControl(bounds)
47 , IWebView(opaque, enableDevTools)
48 , mOnReadyFunc(readyFunc)
49 , mOnMessageFunc(msgFunc)
50 , mEnableScroll(enableScroll)
51 {
52 // The IControl itself should never receive mouse messages
53 // they need to go to the webview
54 mIgnoreMouse = true;
55 }
56
58 {
59 GetUI()->RemovePlatformView(mPlatformView);
60 mPlatformView = nullptr;
61 }
62
63 void OnAttached() override
64 {
65 IGraphics* pGraphics = GetUI();
66 mPlatformView = OpenWebView(pGraphics->GetWindow(), mRECT.L, mRECT.T, mRECT.W(), mRECT.H(), pGraphics->GetDrawScale());
67 pGraphics->AttachPlatformView(mRECT, mPlatformView);
68 EnableScroll(mEnableScroll);
69 }
70
71 void Draw(IGraphics& g) override
72 {
73 /* NO-OP */
74 }
75
76 void OnWebViewReady() override
77 {
78 if (mOnReadyFunc)
79 mOnReadyFunc(this);
80 }
81
82 void OnWebContentLoaded() override
83 {
84 EnableInteraction(mEnableInteraction);
85 }
86
87 void OnMessageFromWebView(const char* json) override
88 {
89 if (mOnMessageFunc)
90 mOnMessageFunc(this, json);
91 }
92
93 void OnRescale() override
94 {
95 UpdateWebViewBounds();
96 }
97
98 void OnResize() override
99 {
100 UpdateWebViewBounds();
101 }
102
103 void SetIgnoreMouse(bool ignore) override
104 {
105 // The IControl itself should never receive mouse messages
106 // they need to go to the webview
107 mEnableInteraction = !ignore;
108 EnableInteraction(mEnableInteraction);
109 }
110
111 void Hide(bool hide) override
112 {
113 HideWebView(hide);
114
115 if (mPlatformView)
116 GetUI()->HidePlatformView(mPlatformView, hide);
117
118 IControl::Hide(hide);
119 }
120
121private:
122 void UpdateWebViewBounds()
123 {
124 auto ds = GetUI()->GetDrawScale();
125 SetWebViewBounds(mRECT.L * ds, mRECT.T * ds, mRECT.W() * ds, mRECT.H() * ds, ds);
126 }
127
128 void* mPlatformView = nullptr;
129 OnReadyFunc mOnReadyFunc;
130 OnMessageFunc mOnMessageFunc;
131 bool mEnableInteraction = true;
132 bool mEnableScroll = false;
133};
134
135END_IGRAPHICS_NAMESPACE
136END_IPLUG_NAMESPACE
137
This file contains the base IControl implementation, along with some base classes for specific types ...
The lowest level base class of an IGraphics control.
Definition: IControl.h:49
IGraphics * GetUI()
Definition: IControl.h:472
virtual void Hide(bool hide)
Shows or hides the IControl.
Definition: IControl.cpp:239
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:86
virtual void * GetWindow()=0
Get a pointer to the platform view e.g.
virtual void AttachPlatformView(const IRECT &r, void *pView)
Add an OS view as a sub-view, on top of the IGraphics view.
Definition: IGraphics.h:799
virtual void HidePlatformView(void *pView, bool hide)
Hide a previously attached platform view from the IGraphics view.
Definition: IGraphics.h:808
virtual void RemovePlatformView(void *pView)
Remove a previously attached platform view from the IGraphics view.
Definition: IGraphics.h:803
float GetDrawScale() const
Gets the graphics context scaling factor.
Definition: IGraphics.h:1106
A control that allows the embedding of HTML UI inside an IGraphics context using a platform-native we...
void OnRescale() override
Implement to do something when graphics is scaled globally (e.g.
void OnWebViewReady() override
Called when the web view is ready to receive navigation instructions.
void OnMessageFromWebView(const char *json) override
When a script in the web view posts a message, it will arrive as a UTF8 json string here.
void OnResize() override
Called when IControl is constructed or resized using SetRect().
void SetIgnoreMouse(bool ignore) override
Specify whether the control should respond to mouse events.
void OnWebContentLoaded() override
Called after navigation instructions have been exectued and e.g.
void Hide(bool hide) override
Shows or hides the IControl.
void OnAttached() override
Called after the control has been attached, and its delegate and graphics member variable set.
void Draw(IGraphics &g) override
Draw the control to the graphics context.
IWebViewControl(const IRECT &bounds, bool opaque, OnReadyFunc readyFunc=nullptr, OnMessageFunc msgFunc=nullptr, bool enableDevTools=false, bool enableScroll=false)
Constructs am IWebViewControl.
IWebView is a base interface for hosting a platform web view inside an IPlug plug-in's UI.
Definition: IPlugWebView.h:44
void EnableInteraction(bool enable)
Sets whether the webview is interactive.
void EnableScroll(bool enable)
Enable scrolling on the webview.
void SetWebViewBounds(float x, float y, float w, float h, float scale=1.)
Set the bounds of the webview in the parent window.
Used to manage a rectangular area, independent of draw class/platform.
float W() const
float H() const