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
- Resize
- Closed
- Close
- Maximize
- Minimize
- Focus
std::function<void(int width, int height)>
smartview.on<saucer::window_event::resize>([](int width, int height)
{
std::print("Width: {}, Height: {}\n", width, height);
});
std::function<void()>
smartview.on<saucer::window_event::closed>([]()
{
std::print("WebView is closed!\n", width, height);
});
true
from within the callback prevents the close.std::function<bool()>
smartview.on<saucer::window_event::close>([]()
{
std::print("Preventing a close!\n", width, height);
return true;
});
std::function<void(bool)>
smartview.on<saucer::window_event::maximize>([](bool state)
{
std::print("WebView is maximized: {}!\n", state);
});
std::function<void(bool)>
smartview.on<saucer::window_event::minimize>([](bool state)
{
std::print("WebView is minimized: {}!\n", state);
});
std::function<void(bool)>
smartview.on<saucer::window_event::focus>([](bool focus)
{
std::print("WebView is focused: {}!\n", focus);
});
- URL Changed
- Load Finished
- Load Started
- DOM Ready
std::function<void(const std::string &)>
smartview.on<saucer::web_event::url_changed>([](const std::string& url)
{
std::print("New URL loaded: {}\n", url);
});
std::function<void()>
smartview.on<saucer::web_event::load_finished>([]()
{
std::print("Web-Page finished loading!\n");
});
std::function<void()>
smartview.on<saucer::web_event::load_started>([]()
{
std::print("Web-Page started loading!\n");
});
std::function<void()>
smartview.on<saucer::web_event::dom_ready>([]()
{
std::print("DOM is ready!\n");
});