web browser (Servo)
git clone https://git.lucas.co/cce-browser.git
examples/wpe_dark.rs (1.7K)
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 // The host's vi channel and scripts.
19 #[path = "../src/vi.rs"]
20 #[allow(dead_code)]
21 mod vi;
22
23 #[path = "../src/wpe/mod.rs"]
24 mod wpe;
25
26 #[cfg(feature = "wpe")]
27 fn main() {
28 // A deliberately hardcoded-white page: the case force-dark exists for.
29 let url = "data:text/html,<body style='background:%23ffffff'><h1 style='color:%23000'>hello</h1></body>";
30 let mut host = wpe::WebKitHost::new(url::Url::parse(url).unwrap(), (400, 300));
31 let settle = |h: &mut wpe::WebKitHost, n: u32| {
32 for _ in 0..n { h.pump(); h.frame_drawn(); std::thread::sleep(std::time::Duration::from_millis(50)); }
33 };
34
35 settle(&mut host, 30);
36 let before = host.sample_pixel();
37 println!("light: top-left pixel = {before:?}");
38
39 println!("-- enabling force-dark --");
40 host.set_force_dark(true);
41 settle(&mut host, 40);
42 let after = host.sample_pixel();
43 println!("dark: top-left pixel = {after:?}");
44
45 match (before, after) {
46 (Some(b), Some(a)) => {
47 let lum = |p: (u8, u8, u8)| p.0 as u32 + p.1 as u32 + p.2 as u32;
48 println!("\nluminance {} -> {}", lum(b), lum(a));
49 println!("force-dark: {}", if lum(a) < lum(b) / 2 { "OK (page darkened)" } else { "NO EFFECT" });
50 }
51 _ => println!("no frame sampled"),
52 }
53 }