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

examples/wpe_dark.rs (1.6K)

 1 //! Verifies force-dark actually inverts, by sampling the rendered frame.
 2 //! `cargo run --release -p cce-browser --features wpe --example wpe_dark`
 3 
 4 #[cfg(not(feature = "wpe"))]
 5 fn main() { eprintln!("build with --features wpe"); }
 6 
 7 #[cfg(feature = "wpe")]
 8 #[derive(Debug, Clone, Copy)]
 9 pub enum EditingCommand { Copy, Cut, Paste }
10 
11 #[cfg(feature = "wpe")]
12 #[path = "../src/pages.rs"]
13 mod pages;
14 #[cfg(feature = "wpe")]
15 #[path = "../src/downloads.rs"]
16 mod downloads;
17 #[cfg(feature = "wpe")]
18 #[path = "../src/wpe/mod.rs"]
19 mod wpe;
20 
21 #[cfg(feature = "wpe")]
22 fn main() {
23     // A deliberately hardcoded-white page: the case force-dark exists for.
24     let url = "data:text/html,<body style='background:%23ffffff'><h1 style='color:%23000'>hello</h1></body>";
25     let mut host = wpe::WebKitHost::new(url::Url::parse(url).unwrap(), (400, 300));
26     let settle = |h: &mut wpe::WebKitHost, n: u32| {
27         for _ in 0..n { h.pump(); std::thread::sleep(std::time::Duration::from_millis(50)); }
28     };
29 
30     settle(&mut host, 30);
31     let before = host.sample_pixel();
32     println!("light: top-left pixel = {before:?}");
33 
34     println!("-- enabling force-dark --");
35     host.set_force_dark(true);
36     settle(&mut host, 40);
37     let after = host.sample_pixel();
38     println!("dark:  top-left pixel = {after:?}");
39 
40     match (before, after) {
41         (Some(b), Some(a)) => {
42             let lum = |p: (u8, u8, u8)| p.0 as u32 + p.1 as u32 + p.2 as u32;
43             println!("\nluminance {} -> {}", lum(b), lum(a));
44             println!("force-dark: {}", if lum(a) < lum(b) / 2 { "OK (page darkened)" } else { "NO EFFECT" });
45         }
46         _ => println!("no frame sampled"),
47     }
48 }