web browser (Servo)
git clone https://git.lucas.co/cce-browser.git
examples/wpe_paste.rs (2.8K)
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 // 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 url = std::env::args().nth(1).unwrap_or_else(|| "http://127.0.0.1:8790/paste.html".into());
37 let mut host = wpe::WebKitHost::new(url::Url::parse(&url).unwrap(), (1200, 800));
38 let settle = |h: &mut wpe::WebKitHost, n: u32| {
39 for _ in 0..n { h.pump(); h.frame_drawn(); std::thread::sleep(std::time::Duration::from_millis(50)); }
40 };
41
42 settle(&mut host, 40);
43 println!("loaded: title={:?}", host.title());
44 host.focus(true);
45 // Click the autofocused input so the page has an editable target.
46 host.mouse_move(400.0, 30.0);
47 host.mouse_button_ui(cce_ui::widget::MouseButton::Left, true, 400.0, 30.0);
48 host.mouse_button_ui(cce_ui::widget::MouseButton::Left, false, 400.0, 30.0);
49 settle(&mut host, 10);
50
51 println!("-- sync clipboard, let it propagate, then paste --");
52 host.sync_clipboard();
53 settle(&mut host, 10);
54 host.editing_action_cmd(EditingCommand::Paste);
55 settle(&mut host, 20);
56
57 let title = host.title().unwrap_or_default();
58 println!(" title={title:?}");
59 let paste_ok = title.starts_with("pasted:") && title.len() > "pasted:".len();
60 println!("paste into page: {}", if paste_ok { "OK" } else { "FAILED" });
61
62 // Other direction: select the field's contents and copy, which routes
63 // through the same `changed` vfunc, then read the system clipboard back.
64 println!("\n-- select all + copy out of the page --");
65 host.editing_action_cmd(EditingCommand::Copy);
66 settle(&mut host, 6);
67 let sys = std::process::Command::new("wl-paste")
68 .output().ok()
69 .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
70 .unwrap_or_default();
71 println!(" system clipboard now: {sys:?}");
72 }