This project has been superseded by saucer.
A cross-platform C++17 library that allows you to create a simple webview.
Platform | Used Browser | GUI |
---|---|---|
Windows | Webview2 (Edge Chromium) | WinAPI |
Linux | WebKit2GTK | GTK |
Version | Remarks |
---|---|
11 | Will not require Webview2 Runtime or Canary-Edge build |
10 | Explicit installation of the Webview2 Runtime may be required |
8 | Requires WINDOWS_8 to be set to ON from your CMakeLists |
- Add the library to your project
-
add_subdirectory(/path/to/webviewpp EXCLUDE_FROM_ALL) link_libraries(webview)
-
- Use the library
- See documentation
- See
examples
for examples
- Windows
- (Runtime) Webview2 or Edge Chromium Canary Build
- Linux
- (Runtime & Build) webkit2gtk
#include <webview.hpp>
int main()
{
Webview::Window webview("webview", 800, 900);
webview.expose(Webview::Function("addTen", [](int num) {
return num + 10;
}));
webview.show();
webview.run();
return 0;
}
For more examples see examples
webviewpp supports embedding of all required files.
To embed your files you have to use the embed-helper.
Usage:
- Compile the embed-helper
-
mkdir build && cd build && cmake .. && cmake --build . --config Release
-
- Run the embed-helper
-
./embed_helper <path to folder containing all the required files>
-
- Add the parent folder of the
embedded
folder to your include directories - Change
setUrl
calls toembedded:///<filepath>
on Linuxfile:///embedded/<filepath>
on Windows
For an example see examples/embedded
void hide();
Hides the window
void show();
Shows the window
Window::isHidden
bool isHidden();
Returns:
Whether or the window is hidden
void setSize(std::size_t, std::size_t);
Sets the window size
std::pair<std::size_t, std::size_t> getSize();
Returns:
The width and height in form of an
std::pair
std::string getTitle();
Returns:
The title of the window
void setTitle(std::string);
Sets the window title
void run();
Runs the mainloop
Remarks:
Is blocking
void exit();
Closes the webview
std::string getUrl();
Returns:
The current url
void setUrl(std::string);
Navigates to the given url
void enableContextMenu(bool);
Enables the context menu
void enableDevTools(bool);
Enables the developer tools
void expose(Webview::Function const&);
Exposes the given function
Remarks:
If the given Function is an
AsyncFunction
it will be run in a new thread
template <typename T>
std::future<T> callFunction(Webview::JavaScriptFunction&& function);
Calls the given javascript function
Returns:
The result of the javascript function call as
T
Preconditions
T
must be serializable by nlohmann::json
Remarks:
You should never call
.get()
on the returned future in a non async context as it will freeze the webview
void runCode(std::string const&);
Runs the given javascript code
void injectCode(std::string const&);
Makes the given javascript code run on document load
void setCloseCallback(std::function<bool ()>);
Sets the close-callback to the given callback
Remarks:
If the callback returns
true
the webview will not close
void setNavigateCallback(std::function<void (const std::string &)>);
Sets the navigate-callback to the given callback
void setResizeCallback(std::function<void (std::size_t, std::size_t)>);
Sets the resize-callback to the given callback
This work was originally based on the work of MichaelKim