Dusk Audio Framework
Loading...
Searching...
No Matches
WebViewWin32.hpp
1/*
2 * DISTRHO Plugin Framework (DPF)
3 * Copyright (C) 2012-2025 Filipe Coelho <falktx@falktx.com>
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any purpose with
6 * or without fee is hereby granted, provided that the above copyright notice and this
7 * permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
10 * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
11 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
13 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#if !defined(DAF_WEB_VIEW_HPP_INCLUDED) && !defined(DGL_WEB_VIEW_HPP_INCLUDED)
18# define DAF_WEB_VIEW_INCLUDE_IMPLEMENTATION
19# include "WebView.hpp"
20#endif
21
22// --------------------------------------------------------------------------------------------------------------------
23// Web View stuff
24
26
27class WebView;
28
30
31#ifdef WEB_VIEW_DGL_NAMESPACE
32START_NAMESPACE_DGL
33using DAF_NAMESPACE::WebView;
34#else
36#endif
37
38WebView* webview_choc_create(const WEB_VIEW_NAMESPACE::WebViewOptions& opts);
39void webview_choc_destroy(WebView*);
40void* webview_choc_handle(WebView*);
41void webview_choc_eval(WebView*, const char* js);
42void webview_choc_navigate(WebView*, const char* url);
43
44#ifdef WEB_VIEW_DGL_NAMESPACE
45END_NAMESPACE_DGL
46#else
48#endif
49
50// --------------------------------------------------------------------------------------------------------------------
51
52#ifdef DAF_WEB_VIEW_INCLUDE_IMPLEMENTATION
53
54# define WC_ERR_INVALID_CHARS 0x80
55# include "choc/choc_WebView.h"
56
57#ifdef WEB_VIEW_DGL_NAMESPACE
58START_NAMESPACE_DGL
59#else
61#endif
62
63WebView* webview_choc_create(const WEB_VIEW_NAMESPACE::WebViewOptions& opts)
64{
65 WebView::Options wopts;
66 wopts.acceptsFirstMouseClick = true;
67 wopts.enableDebugMode = true;
68
69 std::unique_ptr<WebView> webview = std::make_unique<WebView>(wopts);
70 DAF_SAFE_ASSERT_RETURN(webview->loadedOK(), nullptr);
71
72 if (const WEB_VIEW_NAMESPACE::WebViewMessageCallback callback = opts.callback)
73 {
74 webview->addInitScript("function postMessage(m){window.chrome.webview.postMessage(m);}");
75
76 void* const callbackPtr = opts.callbackPtr;
77 webview->bind([callback, callbackPtr](const std::string& value) {
78 char* const data = strdup(value.data());
79 callback(callbackPtr, data);
80 std::free(data);
81 });
82 }
83 else
84 {
85 webview->addInitScript("function postMessage(m){}");
86 }
87
88 if (opts.initialJS != nullptr)
89 webview->addInitScript(opts.initialJS);
90
91 return webview.release();
92}
93
94void webview_choc_destroy(WebView* const webview)
95{
96 delete webview;
97}
98
99void* webview_choc_handle(WebView* const webview)
100{
101 return webview->getViewHandle();
102}
103
104void webview_choc_eval(WebView* const webview, const char* const js)
105{
106 webview->evaluateJavascript(js);
107}
108
109void webview_choc_navigate(WebView* const webview, const char* const url)
110{
111 webview->navigate(url);
112}
113
114#ifdef WEB_VIEW_DGL_NAMESPACE
115END_NAMESPACE_DGL
116#else
118#endif
119
120#endif // DAF_WEB_VIEW_INCLUDE_IMPLEMENTATION
121
122// --------------------------------------------------------------------------------------------------------------------
#define END_NAMESPACE_DAF
Definition DafInfo.hpp:996
#define START_NAMESPACE_DAF
Definition DafInfo.hpp:990