|
| 1 | +//! A WebAssembly example demonstrating button driver usage in the browser. |
| 2 | +//! This example uses the built-in `JsInstant` type which wraps `js_sys::Date::now()`. |
| 3 | +//! |
| 4 | +//! To run this example: |
| 5 | +//! 1. Install trunk: `cargo install trunk` |
| 6 | +//! 2. Run: `trunk serve` |
| 7 | +//! 3. Open your browser at `http://localhost:8080` |
| 8 | +//! |
| 9 | +//! Required features: `wasm` (which includes `std` and `js-sys`). |
| 10 | +
|
| 11 | +use std::{cell::RefCell, rc::Rc}; |
| 12 | + |
| 13 | +use button_driver::{Button, ButtonConfig, JsInstant, Mode, PinWrapper}; |
| 14 | +use log::info; |
| 15 | +use wasm_bindgen::{closure::Closure, JsCast}; |
| 16 | +use web_sys::MouseEvent; |
| 17 | + |
| 18 | +#[derive(Clone, Default)] |
| 19 | +struct ButtonInput(Rc<RefCell<bool>>); |
| 20 | + |
| 21 | +impl PinWrapper for ButtonInput { |
| 22 | + fn is_high(&mut self) -> bool { |
| 23 | + *self.0.borrow() |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +#[wasm_bindgen::prelude::wasm_bindgen(start)] |
| 28 | +pub fn main() { |
| 29 | + wasm_logger::init(wasm_logger::Config::default()); |
| 30 | + let document = web_sys::window().unwrap().document().unwrap(); |
| 31 | + let clickable_area = document.query_selector(".clickable-area").unwrap().unwrap(); |
| 32 | + let pin_state = ButtonInput::default(); |
| 33 | + { |
| 34 | + let pin_state_ref = pin_state.clone(); |
| 35 | + let mouse_down_handler = Closure::wrap(Box::new(move |_: MouseEvent| { |
| 36 | + *pin_state_ref.0.borrow_mut() = true; |
| 37 | + }) as Box<dyn FnMut(_)>); |
| 38 | + clickable_area |
| 39 | + .add_event_listener_with_callback( |
| 40 | + "mousedown", |
| 41 | + mouse_down_handler.as_ref().unchecked_ref(), |
| 42 | + ) |
| 43 | + .unwrap(); |
| 44 | + mouse_down_handler.forget(); |
| 45 | + } |
| 46 | + { |
| 47 | + let pin_state_ref = pin_state.clone(); |
| 48 | + let mouse_up_handler = Closure::wrap(Box::new(move |_: MouseEvent| { |
| 49 | + *pin_state_ref.0.borrow_mut() = false; |
| 50 | + }) as Box<dyn FnMut(_)>); |
| 51 | + |
| 52 | + clickable_area |
| 53 | + .add_event_listener_with_callback("mouseup", mouse_up_handler.as_ref().unchecked_ref()) |
| 54 | + .unwrap(); |
| 55 | + mouse_up_handler.forget(); |
| 56 | + } |
| 57 | + { |
| 58 | + let mut button = Button::<_, JsInstant>::new( |
| 59 | + pin_state, |
| 60 | + ButtonConfig { |
| 61 | + mode: Mode::PullDown, |
| 62 | + ..Default::default() |
| 63 | + }, |
| 64 | + ); |
| 65 | + let callback = Closure::wrap(Box::new(move || { |
| 66 | + button.tick(); |
| 67 | + |
| 68 | + if let Some(dur) = button.held_time() { |
| 69 | + info!("Total holding time {:?}", dur); |
| 70 | + if button.is_clicked() { |
| 71 | + info!("Clicked + held"); |
| 72 | + } else if button.is_double_clicked() { |
| 73 | + info!("Double clicked + held"); |
| 74 | + } else if button.holds() == 2 && button.clicks() > 0 { |
| 75 | + info!("Held twice with {} clicks", button.clicks()); |
| 76 | + } else if button.holds() == 2 { |
| 77 | + info!("Held twice"); |
| 78 | + } |
| 79 | + } else { |
| 80 | + if button.is_clicked() { |
| 81 | + info!("Click"); |
| 82 | + } else if button.is_double_clicked() { |
| 83 | + info!("Double click"); |
| 84 | + } else if button.is_triple_clicked() { |
| 85 | + info!("Triple click"); |
| 86 | + } else if let Some(dur) = button.current_holding_time() { |
| 87 | + info!("Held for {:?}", dur); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + button.reset(); |
| 92 | + }) as Box<dyn FnMut()>); |
| 93 | + |
| 94 | + web_sys::window() |
| 95 | + .unwrap() |
| 96 | + .set_interval_with_callback_and_timeout_and_arguments_0( |
| 97 | + callback.as_ref().unchecked_ref(), |
| 98 | + 20, |
| 99 | + ) |
| 100 | + .unwrap(); |
| 101 | + callback.forget(); |
| 102 | + } |
| 103 | +} |
0 commit comments