-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwui.h
More file actions
238 lines (188 loc) · 6.25 KB
/
wui.h
File metadata and controls
238 lines (188 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#pragma once
#if defined(__APPLE__)
#define PLATFORM_DARWIN
#elif defined(__unix__)
#define PLATFORM_LINUX
#elif defined(_WIN32)
#define PLATFORM_WINDOWS
#else
#error "Platform not supported"
#endif
#ifdef __cplusplus
#include "internal/bindings_js.h"
#include "internal/base.h"
#include "internal/bindings.h"
#include "internal/utilities.h"
#include "internal/smoljson.h"
#include "internal/platform/mac/wkwebview.h"
#include "internal/platform/win/mswebview2.h"
#include "internal/platform/linux/webkitgtk.h"
class wui final {
static inline wui_internal::osbase* platform() {
static wui_internal::os_impl instance;
return &instance;
}
wui() {}
public:
using asset_t = wui_internal::asset_t;
using window_style = wui_internal::window_style;
class view {
void* m_window_handle{};
public:
view() = default;
view(void* window_handle) : m_window_handle(window_handle) {};
void* native() { return m_window_handle; }
wui_internal::view_base* operator->() {
auto ptr = platform()->from_native(m_window_handle);
if (!ptr)
throw std::runtime_error("webview is no longer valid");
return ptr;
}
bool valid() { return platform()->from_native(m_window_handle) != nullptr; }
};
static inline view create_view() { return view(platform()->create_window()); }
static inline void run() {
while (process_messages()) {}
}
static inline bool process_messages() { return platform()->process_messages(); }
static inline void exit() { platform()->exit(); }
static inline void allow_inspect(bool enabled) { wui_internal::view_base::debug = enabled; }
template <typename Func> static inline auto make_binding(Func func) {
return wui_internal::make_binding(std::function(func));
}
};
#endif
/// c-style api
#ifndef WUI_C_API
#if defined(BUILD_SHARED)
#if defined(_WIN32) || defined(__CYGWIN__)
#if defined(BUILD_SHARED)
#define WUI_C_API __declspec(dllexport)
#else
#define WUI_C_API __declspec(dllimport)
#endif
#else
#define WUI_C_API __attribute__((visibility("default")))
#endif
#elif !defined(BUILD_STATIC) && defined(__cplusplus)
#define WUI_C_API inline
#else
#define WUI_C_API extern
#endif
#endif
#ifdef __cplusplus
#define IMPL(...) __VA_ARGS__
extern "C" {
#else
#define IMPL(...)
#endif
typedef void* wui_view_t;
typedef void* wui_bind_result_t;
typedef void* wui_asset_response_t;
WUI_C_API void wui_glob_allow_inspect(int enabled) IMPL({
wui::allow_inspect(enabled != 0);
});
// view related
WUI_C_API wui_view_t wui_new() IMPL({
return wui::create_view().native();
});
WUI_C_API void wui_close(wui_view_t window_handle) IMPL({
wui::view v(window_handle);
if (v.valid()) v->close();
});
WUI_C_API void wui_set_size(wui_view_t window_handle, int width, int height) IMPL({
wui::view v(window_handle);
if (v.valid()) v->set_size(width, height);
});
WUI_C_API void wui_hide(wui_view_t window_handle) IMPL({
wui::view v(window_handle);
if (v.valid()) v->hide();
});
WUI_C_API void wui_show(wui_view_t window_handle) IMPL({
wui::view v(window_handle);
if (v.valid()) v->show();
});
WUI_C_API void wui_maximize(wui_view_t window_handle) IMPL({
wui::view v(window_handle);
if (v.valid()) v->maximize();
});
WUI_C_API void wui_minimize(wui_view_t window_handle) IMPL({
wui::view v(window_handle);
if (v.valid()) v->minimize();
});
WUI_C_API void wui_navigate(wui_view_t window_handle, const char* url) IMPL({
wui::view v(window_handle);
if (v.valid()) v->navigate(url);
});
WUI_C_API void wui_eval(wui_view_t window_handle, const char* script) IMPL({
wui::view v(window_handle);
if (v.valid()) v->eval(script);
});
WUI_C_API void wui_set_html(wui_view_t window_handle, const char* html) IMPL({
wui::view v(window_handle);
if (v.valid()) v->set_html(html);
});
WUI_C_API void wui_bind_set_result_json(wui_bind_result_t bind_result, const char* json) IMPL({
auto&[result, _] = *reinterpret_cast<std::tuple<std::string*, bool*>*>(bind_result);
*result = json;
});
WUI_C_API void wui_bind_throw_with_message(wui_bind_result_t bind_result, const char* message) IMPL({
auto&[result, errored] = *reinterpret_cast<std::tuple<std::string*, bool*>*>(bind_result);
*result = message;
*errored = true;
});
typedef void (*wui_bind_callback_t)(const char* json_args, wui_bind_result_t result, void* user_data);
WUI_C_API int wui_bind_function(wui_view_t window_handle, const char* name, wui_bind_callback_t callback, void* user_data) IMPL({
wui::view v(window_handle);
if (!v.valid()) return 0;
return v->bind_function_raw(name, [callback, user_data](const wui_internal::json_t& json) -> wui_internal::json_t {
std::string result_str;
bool errored = false;
auto result = std::make_tuple(&result_str, &errored);
callback(json.serialize().c_str(), &result, user_data);
if (errored) throw std::runtime_error(result_str);
return wui_internal::json_t::parse(result_str);
});
});
WUI_C_API void wui_asset_response_set_content_type(wui_asset_response_t response, const char* new_content_type) IMPL({
auto& [content_type, _] = *reinterpret_cast<std::tuple<std::string*, std::vector<uint8_t>*>*>(response);
*content_type = new_content_type;
});
WUI_C_API uint8_t* wui_asset_response_get_buffer(wui_asset_response_t response, size_t buffer_size) IMPL({
auto& [_, buffer] = *reinterpret_cast<std::tuple<std::string*, std::vector<uint8_t>*>*>(response);
buffer->clear();
buffer->resize(buffer_size);
return buffer->data();
});
typedef int (*wui_asset_request_callback_t)(const char* uri, wui_asset_response_t response, void* user_data);
WUI_C_API int wui_on_asset_requested(wui_view_t window_handle, wui_asset_request_callback_t callback, void* user_data) IMPL({
wui::view v(window_handle);
if (!v.valid()) return 0;
if (!callback) {
v->on_asset_requested = nullptr;
return 1;
}
v->on_asset_requested = [callback, user_data](auto uri) -> std::optional<wui::asset_t> {
std::string content_type = "application/octet-stream";
std::vector<uint8_t> buffer;
auto response = std::make_tuple(&content_type, &buffer);
if (callback(uri.c_str(), &response, user_data)) {
return wui::asset_t{content_type, buffer};
}
return std::nullopt;
};
return 1;
});
// os related
WUI_C_API int wui_os_process_messages() IMPL({
return wui::process_messages();
});
WUI_C_API void wui_os_loop() IMPL({
wui::run();
});
WUI_C_API void wui_exit() IMPL({
wui::exit();
});
#ifdef __cplusplus
}
#endif