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)
48 , mOnReadyFunc(readyFunc)
49 , mOnMessageFunc(msgFunc)
50 , mEnableDevTools(enableDevTools)
51 , mEnableScroll(enableScroll)
52 {
53 // The IControl itself should never receive mouse messages
54 // they need to go to the webview
55 mIgnoreMouse = true;
56 }
57
59 {
60 GetUI()->RemovePlatformView(mPlatformView);
61 mPlatformView = nullptr;
62 }
63
64 void OnAttached() override
65 {
66 IGraphics* pGraphics = GetUI();
67 mPlatformView = OpenWebView(pGraphics->GetWindow(), mRECT.L, mRECT.T, mRECT.W(), mRECT.H(), pGraphics->GetDrawScale(), mEnableDevTools);
68 pGraphics->AttachPlatformView(mRECT, mPlatformView);
69 EnableScroll(mEnableScroll);
70 }
71
72 void Draw(IGraphics& g) override
73 {
74 /* NO-OP */
75 }
76
77 void OnWebViewReady() override
78 {
79 if (mOnReadyFunc)
80 mOnReadyFunc(this);
81 }
82
83 void OnWebContentLoaded() override
84 {
85 EnableInteraction(mEnableInteraction);
86 }
87
88 void OnMessageFromWebView(const char* json) override
89 {
90 if (mOnMessageFunc)
91 mOnMessageFunc(this, json);
92 }
93
94 void OnRescale() override
95 {
96 UpdateWebViewBounds();
97 }
98
99 void OnResize() override
100 {
101 UpdateWebViewBounds();
102 }
103
104 void SetIgnoreMouse(bool ignore) override
105 {
106 // The IControl itself should never receive mouse messages
107 // they need to go to the webview
108 mEnableInteraction = !ignore;
109 EnableInteraction(mEnableInteraction);
110 }
111
112 void Hide(bool hide) override
113 {
114 HideWebView(hide);
115
116 if (mPlatformView)
117 GetUI()->HidePlatformView(mPlatformView, hide);
118
119 IControl::Hide(hide);
120 }
121
122private:
123 void UpdateWebViewBounds()
124 {
125 auto ds = GetUI()->GetDrawScale();
126 SetWebViewBounds(mRECT.L * ds, mRECT.T * ds, mRECT.W() * ds, mRECT.H() * ds, ds);
127 }
128
129 void* mPlatformView = nullptr;
130 OnReadyFunc mOnReadyFunc;
131 OnMessageFunc mOnMessageFunc;
132 bool mEnableDevTools = false;
133 bool mEnableInteraction = true;
134 bool mEnableScroll = false;
135};
136
137END_IGRAPHICS_NAMESPACE
138END_IPLUG_NAMESPACE
139
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:467
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:1101
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 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 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.
Used to manage a rectangular area, independent of draw class/platform.
float W() const
float H() const