web browser (Servo)
git clone https://git.lucas.co/cce-browser.git
examples/wpe_paste.rs (2.7K)
1 //! Paste into a page: the path Ctrl+V takes through the chrome.
2 //!
3 //! `editing_action_cmd(Paste)` is what `main.rs` calls, so this exercises the
4 //! real route — WebKit's Paste command reads through `WPEClipboard::read`,
5 //! which is only there if the display vends a clipboard at all.
6 //!
7 //! Needs a Wayland display for wl-paste, so run it inside a session:
8 //! cce-shadow --instance <n> run ./target/release/examples/wpe_paste <url>
9
10 #[cfg(not(feature = "wpe"))]
11 fn main() { eprintln!("build with --features wpe"); }
12
13 /// Mirrors main.rs's declaration; the wpe module refers to `crate::` and an
14 /// example is its own crate root. (A lib target would remove this wart.)
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 url = std::env::args().nth(1).unwrap_or_else(|| "http://127.0.0.1:8790/paste.html".into());
32 let mut host = wpe::WebKitHost::new(url::Url::parse(&url).unwrap(), (1200, 800));
33 let settle = |h: &mut wpe::WebKitHost, n: u32| {
34 for _ in 0..n { h.pump(); std::thread::sleep(std::time::Duration::from_millis(50)); }
35 };
36
37 settle(&mut host, 40);
38 println!("loaded: title={:?}", host.title());
39 host.focus(true);
40 // Click the autofocused input so the page has an editable target.
41 host.mouse_move(400.0, 30.0);
42 host.mouse_button_ui(cce_ui::widget::MouseButton::Left, true, 400.0, 30.0);
43 host.mouse_button_ui(cce_ui::widget::MouseButton::Left, false, 400.0, 30.0);
44 settle(&mut host, 10);
45
46 println!("-- sync clipboard, let it propagate, then paste --");
47 host.sync_clipboard();
48 settle(&mut host, 10);
49 host.editing_action_cmd(EditingCommand::Paste);
50 settle(&mut host, 20);
51
52 let title = host.title().unwrap_or_default();
53 println!(" title={title:?}");
54 let paste_ok = title.starts_with("pasted:") && title.len() > "pasted:".len();
55 println!("paste into page: {}", if paste_ok { "OK" } else { "FAILED" });
56
57 // Other direction: select the field's contents and copy, which routes
58 // through the same `changed` vfunc, then read the system clipboard back.
59 println!("\n-- select all + copy out of the page --");
60 host.editing_action_cmd(EditingCommand::Copy);
61 settle(&mut host, 6);
62 let sys = std::process::Command::new("wl-paste")
63 .output().ok()
64 .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
65 .unwrap_or_default();
66 println!(" system clipboard now: {sys:?}");
67 }