git.lucas.co / cce-browser
web browser (Servo)
git clone https://git.lucas.co/cce-browser.git

examples/wpe_tabs.rs (2.7K)

 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 #[path = "../src/wpe/mod.rs"]
27 mod wpe;
28 
29 #[cfg(feature = "wpe")]
30 fn main() {
31     let mut host = wpe::WebKitHost::new(
32         url::Url::parse("https://example.com").unwrap(),
33         (1200, 800),
34     );
35     let settle = |h: &mut wpe::WebKitHost, n: u32| {
36         for _ in 0..n {
37             h.pump();
38             std::thread::sleep(std::time::Duration::from_millis(50));
39         }
40     };
41     let dump = |h: &wpe::WebKitHost, label: &str| {
42         println!("{label}  (active={} of {})", h.active_index(), h.tab_count());
43         for i in 0..h.tab_count() {
44             let t = h.tab(i).unwrap();
45             println!(
46                 "   tab{i}: title={:?} url={:?} loading={}",
47                 t.title,
48                 t.url.as_ref().map(|u| u.as_str()),
49                 t.loading
50             );
51         }
52     };
53 
54     settle(&mut host, 30);
55     dump(&host, "-- one tab --");
56 
57     println!("\n-- open tab 1, then immediately background it --");
58     host.open_tab(url::Url::parse("https://example.org").unwrap());
59     host.activate(0); // switch away before it can finish loading
60     settle(&mut host, 40);
61     dump(&host, "   after settling (tab1 was never active again)");
62 
63     let bg_ok = host.tab(1).map(|t| t.title.is_some() && !t.loading).unwrap_or(false);
64     println!("\n   background tab reported state: {}", if bg_ok { "OK" } else { "MISSING" });
65 
66     println!("\n-- switch to tab 1 --");
67     host.activate(1);
68     settle(&mut host, 10);
69     println!("   active title={:?} image={:?}", host.title(), host.image());
70 
71     println!("\n-- close tab 1 --");
72     assert!(host.close_tab(1), "should not be the last tab");
73     settle(&mut host, 6);
74     dump(&host, "   after close");
75 
76     println!("\n-- close the last tab --");
77     let more = host.close_tab(0);
78     println!("   close_tab returned {more} (false = was the last)");
79     assert!(!more);
80     println!("\nOK — no crash through open/background/switch/close");
81 }