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

examples/wpe_select.rs (2.3K)

 1 //! Proves a press-drag-release over text selects it.
 2 //!
 3 //! WebKit decides whether a pointer move is a drag from the button bits in
 4 //! the move event's modifiers, not from the press it saw earlier; a move
 5 //! that reports no held button is a hover, and nothing is selected. The page
 6 //! mirrors `getSelection()` into `document.title`, which page-state sync
 7 //! already reads back.
 8 //!
 9 //! `cargo run --release -p cce-browser --example wpe_select`
10 
11 #[cfg(not(feature = "wpe"))]
12 fn main() {
13     eprintln!("build with --features wpe");
14 }
15 
16 #[cfg(feature = "wpe")]
17 #[derive(Debug, Clone, Copy)]
18 pub enum EditingCommand { Copy, Cut, Paste }
19 
20 #[cfg(feature = "wpe")]
21 #[path = "../src/pages.rs"]
22 mod pages;
23 #[cfg(feature = "wpe")]
24 #[path = "../src/downloads.rs"]
25 mod downloads;
26 #[cfg(feature = "wpe")]
27 // The host's vi channel and scripts.
28 #[path = "../src/vi.rs"]
29 #[allow(dead_code)]
30 mod vi;
31 
32 #[path = "../src/wpe/mod.rs"]
33 mod wpe;
34 
35 #[cfg(feature = "wpe")]
36 fn main() {
37     use cce_ui::widget::MouseButton;
38 
39     let page = "data:text/html,<html><body style='margin:0;font:40px monospace'>\
40         <p id=t style='margin:0'>alpha bravo charlie delta echo</p>\
41         <script>document.title='sel:';\
42         document.addEventListener('selectionchange',()=>\
43         document.title='sel:'+getSelection().toString())</script></body></html>";
44     let mut host = wpe::WebKitHost::new(url::Url::parse(page).unwrap(), (1200, 800));
45     let settle = |h: &mut wpe::WebKitHost, n: u32| {
46         for _ in 0..n {
47             h.pump(); h.frame_drawn();
48             std::thread::sleep(std::time::Duration::from_millis(50));
49         }
50     };
51     settle(&mut host, 40);
52     host.focus(true);
53 
54     // Press at the start of the line, drag right in steps, release.
55     host.mouse_move(2.0, 20.0);
56     settle(&mut host, 2);
57     host.mouse_button_ui(MouseButton::Left, true, 2.0, 20.0);
58     settle(&mut host, 2);
59     for x in (20..=500).step_by(40) {
60         host.mouse_move(x as f32, 20.0);
61         settle(&mut host, 1);
62     }
63     host.mouse_button_ui(MouseButton::Left, false, 500.0, 20.0);
64     settle(&mut host, 8);
65 
66     let title = host.title().unwrap_or_default();
67     println!("title={title:?}");
68     let selected = title.strip_prefix("sel:").unwrap_or("");
69     println!("{}", if selected.trim().is_empty() { "FAIL: nothing selected" } else { "OK: drag selected text" });
70 }