Nui
Loading...
Searching...
No Matches
window.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <nui/core.hpp>
4#ifdef NUI_BACKEND
5# include <nlohmann/json.hpp>
6# include <boost/asio/any_io_executor.hpp>
7# include <nui/backend/url.hpp>
8# include <filesystem>
9#endif
10
11#include <memory>
12#include <optional>
13#include <string>
14#include <functional>
15#include <unordered_map>
16
17namespace Nui
18{
26
27 // https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/icorewebview2?view=webview2-1.0.1370.28#corewebview2_host_resource_access_kind
29 {
30 Deny,
31 Allow,
33 };
34
55
56#ifdef NUI_BACKEND
57 struct CustomSchemeRequest
58 {
59 std::string scheme{};
60 std::function<std::string const&()> getContent{};
61 std::unordered_multimap<std::string, std::string> headers{};
62 std::string uri{};
63 std::string method{};
64
65 std::optional<Url> parseUrl() const;
66
68 NuiCoreWebView2WebResourceContext resourceContext = NuiCoreWebView2WebResourceContext::All;
69 };
70
71 struct CustomSchemeResponse
72 {
73 int statusCode;
75 std::string reasonPhrase{};
76 std::unordered_multimap<std::string, std::string> headers{};
77 std::string body{};
78 };
79
80 struct CustomScheme
81 {
82 std::string scheme;
84 std::vector<std::string> allowedOrigins = {};
85
86 std::function<CustomSchemeResponse(CustomSchemeRequest const&)> onRequest = {};
87
89 NuiCoreWebView2WebResourceContext resourceContext = NuiCoreWebView2WebResourceContext::All;
90
92 bool treatAsSecure = true;
93
95 bool hasAuthorityComponent = false;
96 };
97
98 struct WindowOptions
99 {
101 std::optional<std::string> title = std::nullopt;
102
104 bool debug = false;
105
107 std::vector<CustomScheme> customSchemes = {};
108
110 std::optional<std::string> browserArguments = std::nullopt;
111
113 bool enableTrackingPrevention = true;
114
116 std::optional<std::string> language = std::nullopt;
117
119 std::optional<std::string> folderMappingScheme = std::string{"assets"};
120
121 // Called when a message from the view cannot be parsed or references an invalid function or has no id.
122 std::function<void(std::string_view)> onRpcError = {};
123 };
124#else
126 {};
127#endif
128
132 class Window
133 {
134 public:
135 // Warning! app.example is intentional to avoid timeout issues.
136 // https://github.com/MicrosoftEdge/WebView2Feedback/issues/2381
137 constexpr static std::string_view windowsServeAuthority = "app.example";
138
142 Window();
143
149 explicit Window(WindowOptions const& options);
150
157 [[deprecated]] explicit Window(bool debug);
158
166 [[deprecated]] explicit Window(std::string const& title, bool debug = false);
167
175 [[deprecated]] explicit Window(char const* title, bool debug = false);
176 ~Window();
177 Window(const Window&) = delete;
178 Window& operator=(const Window&) = delete;
181
187 void setTitle(std::string const& title);
188
196 void setSize(int width, int height, WebViewHint hint = WebViewHint::WEBVIEW_HINT_NONE);
197
205 void setPosition(int x, int y, bool useFrameOrigin = true);
206
211
217 void navigate(const std::string& url);
218
224 void navigate(char const* url);
225
226#ifdef NUI_BACKEND
232 void navigate(const std::filesystem::path& file);
233#endif
234
238 void terminate();
239
240#ifndef APPLE
245 void openDevTools();
246#endif
247
248#ifdef NUI_BACKEND
255 void bind(std::string const& name, std::function<void(nlohmann::json const&)> const& callback);
256
262 void unbind(std::string const& name);
263
264 boost::asio::any_io_executor getExecutor() const;
265
276 void setVirtualHostNameToFolderMapping(
277 std::string const& hostName,
278 std::filesystem::path const& folderPath,
280
284 void run();
285
291 void dispatch(std::function<void()> func);
292
301 void setHtml(
302 std::string_view html,
303 std::optional<std::string> windowsServeThroughAuthority = std::string{windowsServeAuthority});
304
310 void setHtmlThroughFilesystem(std::string_view html);
311
317 void eval(std::string const& js);
318
324 void eval(char const* js);
325
330 void eval(std::filesystem::path const& file);
331
337 void init(std::string const& js);
338
343 void init(std::filesystem::path const& file);
344
351 void* getNativeWebView();
352
358 void* getNativeWindow();
359
363 void setConsoleOutput(bool active);
364#endif
365
366 void runInJavascriptThread(std::function<void()>&& func);
367
368 public:
369 struct Implementation;
370 struct WindowsImplementation;
371 struct LinuxImplementation;
372 struct MacOsImplementation;
373
374 private:
375 std::shared_ptr<Implementation> impl_;
376 };
377}
This class encapsulates the webview.
Definition window.hpp:133
void terminate()
Close the window and exit run.
Definition window.cpp:479
void openDevTools()
Open the dev tools.
Definition window.cpp:630
Window(Window &&)
Window & operator=(const Window &)=delete
Window & operator=(Window &&)
void setTitle(std::string const &title)
Set the Title of the window.
Definition window.cpp:281
~Window()
Definition window.cpp:271
void setSize(int width, int height, WebViewHint hint=WebViewHint::WEBVIEW_HINT_NONE)
Sets the size of the window.
Definition window.cpp:287
void navigate(const std::string &url)
Navigate to url.
Definition window.cpp:418
Window(const Window &)=delete
void setPosition(int x, int y, bool useFrameOrigin=true)
Sets the position of the window.
Definition window.cpp:485
void runInJavascriptThread(std::function< void()> &&func)
Definition window.cpp:551
static constexpr std::string_view windowsServeAuthority
Definition window.hpp:137
Window()
Construct a new Window object.
Definition window.cpp:182
void centerOnPrimaryDisplay()
Center the window on the primary display.
Definition window.cpp:509
Definition file_dialog.hpp:6
HostResourceAccessKind
Definition window.hpp:29
WebViewHint
Definition window.hpp:20
emscripten::val bind(F &&f, Args &&... args)
Equivalent of std::bind returning a javascript functor.
Definition functions.hpp:98
NuiCoreWebView2WebResourceContext
Definition window.hpp:36
Definition window.hpp:126