navigation/
navigation.rs

1use std::cell::RefCell;
2use std::rc::Rc;
3
4use saucers::app::App;
5use saucers::options::AppOptions;
6use saucers::prefs::Preferences;
7use saucers::webview::Webview;
8use saucers::webview::events::NavigateEvent;
9
10/// This example demonstrates how to open navigation requests that creates new windows.
11/// By default, only in-page navigations are allowed. Saucer does not create a new window automatically when the
12/// navigation requests one. To enable such behavior, the user will need to implement custom window managing.
13fn main() {
14    let (_cc, app) = App::new(AppOptions::new("Navigation"));
15
16    let w = Webview::new(&Preferences::new(&app)).unwrap();
17
18    w.set_size(1152, 648);
19    w.set_url("https://saucer.app");
20
21    w.on::<NavigateEvent>(Box::new(move |w, nav| {
22        if nav.is_new_window() && nav.is_user_initiated() {
23            // The event handler is fired on the event thread, so creating a window without posting is fine.
24            let app = w.app();
25            let new_window = Webview::new(&Preferences::new(&app)).unwrap();
26            new_window.set_url(nav.url());
27            new_window.set_size(1152, 648);
28            new_window.show();
29            // The app internally maintains a list of webviews, so dropping `new_window` won't destroy it.
30        }
31        true
32    }));
33
34    w.show();
35
36    app.run();
37}