web browser (Servo)
git clone https://git.lucas.co/cce-browser.git
examples/wpe_tabs.rs (2.8K)
1 //! Tabs: several views on one display, and — the point of the change —
2 //! **background tabs updating their own state**.
3 //!
4 //! The old polling read only the active webview, so a tab loading in the
5 //! background stayed titleless until you switched to it. Here tab 1 is opened
6 //! and immediately backgrounded; if signals work it still reports its title.
7 //!
8 //! `cargo run --release -p cce-browser --features wpe --example wpe_tabs`
9
10 #[cfg(not(feature = "wpe"))]
11 fn main() {
12 eprintln!("build with --features wpe");
13 }
14
15 #[cfg(feature = "wpe")]
16 #[derive(Debug, Clone, Copy)]
17 pub enum EditingCommand { Copy, Cut, Paste }
18
19 #[cfg(feature = "wpe")]
20 #[path = "../src/pages.rs"]
21 mod pages;
22 #[cfg(feature = "wpe")]
23 #[path = "../src/downloads.rs"]
24 mod downloads;
25 #[cfg(feature = "wpe")]
26 // The host's vi channel and scripts.
27 #[path = "../src/vi.rs"]
28 #[allow(dead_code)]
29 mod vi;
30
31 #[path = "../src/wpe/mod.rs"]
32 mod wpe;
33
34 #[cfg(feature = "wpe")]
35 fn main() {
36 let mut host = wpe::WebKitHost::new(
37 url::Url::parse("https://example.com").unwrap(),
38 (1200, 800),
39 );
40 let settle = |h: &mut wpe::WebKitHost, n: u32| {
41 for _ in 0..n {
42 h.pump(); h.frame_drawn();
43 std::thread::sleep(std::time::Duration::from_millis(50));
44 }
45 };
46 let dump = |h: &wpe::WebKitHost, label: &str| {
47 println!("{label} (active={} of {})", h.active_index(), h.tab_count());
48 for i in 0..h.tab_count() {
49 let t = h.tab(i).unwrap();
50 println!(
51 " tab{i}: title={:?} url={:?} loading={}",
52 t.title,
53 t.url.as_ref().map(|u| u.as_str()),
54 t.loading
55 );
56 }
57 };
58
59 settle(&mut host, 30);
60 dump(&host, "-- one tab --");
61
62 println!("\n-- open tab 1, then immediately background it --");
63 host.open_tab(url::Url::parse("https://example.org").unwrap());
64 host.activate(0); // switch away before it can finish loading
65 settle(&mut host, 40);
66 dump(&host, " after settling (tab1 was never active again)");
67
68 let bg_ok = host.tab(1).map(|t| t.title.is_some() && !t.loading).unwrap_or(false);
69 println!("\n background tab reported state: {}", if bg_ok { "OK" } else { "MISSING" });
70
71 println!("\n-- switch to tab 1 --");
72 host.activate(1);
73 settle(&mut host, 10);
74 println!(" active title={:?} image={:?}", host.title(), host.image());
75
76 println!("\n-- close tab 1 --");
77 assert!(host.close_tab(1), "should not be the last tab");
78 settle(&mut host, 6);
79 dump(&host, " after close");
80
81 println!("\n-- close the last tab --");
82 let more = host.close_tab(0);
83 println!(" close_tab returned {more} (false = was the last)");
84 assert!(!more);
85 println!("\nOK — no crash through open/background/switch/close");
86 }