Events
On this page you'll learn about the available events and how to use them.
Basics
Events are simply callbacks which are invoked for specific actions and can, in certain circumstances, even alter the behavior of the application.
Register Callback
To register an event callback simply call the on
method of your webview.
You'll need to pass the event-type as the template parameter and the callback as the function argument.
Every call to on()
will return an id which can later be used to remove the same callback.
smartview.on<saucer::window_event::resize>([](int width, int height)
{
// ...
});
If you only need an event to be called once, you can also use the once
method.
smartview.once<saucer::window_event::resize>([](int width, int height)
{
// Fired once, then automatically removed.
});
Remove Callback
As previously mentioned you can unregister callbacks by saving their id.
To remove a callback, simply call remove
with the event-type as the first and the id as the second argument.
auto id = smartview.on<saucer::window_event::resize>([](int width, int height)
{
// ...
});
smartview.remove(saucer::window_event::resize, id);
In case you simply want to remove all event callbacks of a specific event-type you can use the clear
method.
smartview.clear(saucer::window_event::resize);
Available Events
- Window
- WebView
- Decorated
- Maximize
- Minimize
- Closed
- Resize
- Focus
- Close
Called when the Window decorations change.
std::function<void(bool)>
smartview.on<saucer::window_event::decorated>([](bool decorated)
{
std::println("WebView is decorated: {}", decorated);
});
Called when the Window is maximized or restored after being maximized.
std::function<void(bool)>
smartview.on<saucer::window_event::maximize>([](bool state)
{
std::println("WebView was maximized: {}", state);
});
Called when the Window is minimized or restored after being minimized.
std::function<void(bool)>
smartview.on<saucer::window_event::minimize>([](bool state)
{
std::println("WebView was minimized: {}", state);
});
Called when the Window is closed.
std::function<void()>
smartview.on<saucer::window_event::closed>([]()
{
std::println("WebView closed");
});
Called when the Window is resized.
std::function<void(int width, int height)>
smartview.on<saucer::window_event::resize>([](int width, int height)
{
std::println("Width: {}, Height: {}", width, height);
});
Called when the Window gains or loses focus.
std::function<void(bool)>
smartview.on<saucer::window_event::focus>([](bool focus)
{
std::println("WebView is focused: {}!", focus);
});
Called when the Window is about to be closed.
Returning saucer::policy::block
from within the callback prevents the close.
std::function<saucer::policy()>
smartview.on<saucer::window_event::close>([]()
{
std::println("Preventing a close!");
return saucer::policy::block;
});
- DOM Ready
- Navigated
- Navigate
- Favicon Changed
- Title Changed
- Load
Called when the DOM Content loaded.
std::function<void()>
smartview.on<saucer::web_event::dom_ready>([]()
{
std::println("DOM is ready!");
});
Called when a new URL was loaded.
std::function<void(const std::string &)>
smartview.on<saucer::web_event::navigated>([](const std::string& url)
{
std::println("Navigated: {}", url);
});
Called when the URL is about to change.
Returning saucer::policy::block
from within the callback prevents the navigation.
std::function<saucer::policy(const saucer::navigation &)>
smartview.on<saucer::web_event::navigate>([](const saucer::navigation& nav)
{
if (!nav.new_window())
{
return saucer::policy::allow;
}
auto new_window = /*...*/;
new_window->set_url(nav.url());
return saucer::policy::block;
});
Called when the favicon changes.
std::function<void(const saucer::icon &)>
smartview.on<saucer::web_event::favicon>([](const saucer::icon& icon)
{
std::println("Favicon changed!");
});
Called when the document title changes.
std::function<void(const std::string &)>
smartview.on<saucer::web_event::title>([](const std::string& title)
{
std::println("Document Title changed: {}", title);
});
Called when the web-page load progresses.
std::function<void(const saucer::state &)>
smartview.on<saucer::web_event::load>([](const saucer::state& state)
{
std::println("Loading progressed");
});