iPlug2 - C++ Audio Plug-in Framework
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 
22 BEGIN_IPLUG_NAMESPACE
23 BEGIN_IGRAPHICS_NAMESPACE
24 
25 class IWebViewControl;
26 
27 using onReadyFunc = std::function<void(IWebViewControl* pWebView)>;
28 using onMessageFunc = std::function<void(IWebViewControl* pWebView, const char* jsonMsg)>;
29 
36 class IWebViewControl : public IControl, public IWebView
37 {
38 public:
46  IWebViewControl(const IRECT& bounds, bool opaque, onReadyFunc readyFunc, onMessageFunc msgFunc = nullptr, const char* dllPath = "", const char* tmpPath = "")
47  : IControl(bounds)
48  , IWebView(opaque)
49  , mOnReadyFunc(readyFunc)
50  , mOnMessageFunc(msgFunc)
51  {
52 #ifdef OS_WIN
53  SetWebViewPaths(dllPath, tmpPath);
54 #endif
55  }
56 
58  {
59  GetUI()->RemovePlatformView(mPlatformView);
60  mPlatformView = nullptr;
61  }
62 
63  void OnAttached() override
64  {
65  mPlatformView = OpenWebView(GetUI()->GetWindow(), mRECT.L, mRECT.T, mRECT.W(), mRECT.H(), GetUI()->GetTotalScale());
66  GetUI()->AttachPlatformView(mRECT, mPlatformView);
67  }
68 
69  void Draw(IGraphics& g) override
70  {
71  /* NO-OP */
72  }
73 
74  void OnWebViewReady() override
75  {
76  if(mOnReadyFunc)
77  mOnReadyFunc(this);
78  }
79 
80  void OnMessageFromWebView(const char* json) override
81  {
82  if(mOnMessageFunc)
83  mOnMessageFunc(this, json);
84  }
85 
86  void OnRescale() override
87  {
88  SetWebViewBounds(mRECT.L, mRECT.T, mRECT.W(), mRECT.H(), GetUI()->GetTotalScale());
89  }
90 
91  void OnResize() override
92  {
93  SetWebViewBounds(mRECT.L, mRECT.T, mRECT.W(), mRECT.H(), GetUI()->GetTotalScale());
94  }
95 
96 private:
97  void* mPlatformView = nullptr;
98  onReadyFunc mOnReadyFunc;
99  onMessageFunc mOnMessageFunc;
100 };
101 
102 END_IGRAPHICS_NAMESPACE
103 END_IPLUG_NAMESPACE
104 
The lowest level base class of an IGraphics control.
Definition: IControl.h:42
Used to manage a rectangular area, independent of draw class/platform.
void Draw(IGraphics &g) override
Draw the control to the graphics context.
A control that allows the embedding of HTML UI inside an IGraphics context using a platform-native we...
virtual void RemovePlatformView(void *pView)
Remove a previously attached platform view from the IGraphics view.
Definition: IGraphics.h:785
This file contains the base IControl implementation, along with some base classes for specific types ...
float H() const
float W() const
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:781
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:86
void OnAttached() override
Called after the control has been attached, and its delegate and graphics member variable set...
void OnRescale() override
Implement to do something when graphics is scaled globally (e.g.
float GetTotalScale() const
Gets the combined draw and screen/display scaling factor.
Definition: IGraphics.h:1076
IGraphics * GetUI()
Definition: IControl.h:452
float L
Left side of the rectangle (X)
IWebViewControl(const IRECT &bounds, bool opaque, onReadyFunc readyFunc, onMessageFunc msgFunc=nullptr, const char *dllPath="", const char *tmpPath="")
Constructs am IWebViewControl.
void OnResize() override
Called when IControl is constructed or resized using SetRect().
float T
Top of the rectangle (Y)