|
1 | 1 | //! A WebAssembly example demonstrating button driver usage in the browser. |
2 | | -//! This example shows how to implement a custom `Instant` provider using JavaScript's `Date.now()`. |
3 | 2 | //! |
4 | 3 | //! To run this example: |
5 | 4 | //! 1. Install trunk: `cargo install trunk` |
6 | 5 | //! 2. Run: `trunk serve` |
7 | 6 | //! 3. Open your browser at `http://localhost:8080` |
| 7 | +//! |
| 8 | +//! Required features: `wasm` (which includes `std` and `js-sys`). |
8 | 9 |
|
9 | | -use std::{cell::RefCell, ops::Sub, rc::Rc, time::Duration}; |
| 10 | +use std::{cell::RefCell, rc::Rc}; |
10 | 11 |
|
11 | | -use button_driver::{Button, ButtonConfig, InstantProvider, Mode, PinWrapper}; |
| 12 | +use button_driver::{Button, ButtonConfig, Mode, PinWrapper}; |
12 | 13 | use log::info; |
13 | 14 | use wasm_bindgen::{closure::Closure, JsCast}; |
14 | 15 | use web_sys::{js_sys::Date, MouseEvent}; |
15 | 16 |
|
16 | | -#[derive(PartialEq, Clone)] |
17 | | -struct WasmInstant(u64); |
18 | | - |
19 | | -impl WasmInstant { |
20 | | - fn now() -> WasmInstant { |
21 | | - WasmInstant(Date::now() as u64) |
22 | | - } |
23 | | -} |
24 | | - |
25 | | -impl Sub for WasmInstant { |
26 | | - type Output = Duration; |
27 | | - |
28 | | - fn sub(self, rhs: Self) -> Self::Output { |
29 | | - let delta_ms = self.0 - rhs.0; |
30 | | - return Duration::from_millis(delta_ms); |
31 | | - } |
32 | | -} |
33 | | - |
34 | | -impl InstantProvider<Duration> for WasmInstant { |
35 | | - fn now() -> Self { |
36 | | - WasmInstant::now() |
37 | | - } |
38 | | -} |
39 | | - |
40 | 17 | #[derive(Clone, Default)] |
41 | 18 | struct ButtonInput(Rc<RefCell<bool>>); |
42 | 19 |
|
@@ -77,7 +54,7 @@ pub fn main() { |
77 | 54 | mouse_up_handler.forget(); |
78 | 55 | } |
79 | 56 | { |
80 | | - let mut button = Button::<_, WasmInstant>::new( |
| 57 | + let mut button = Button::<_, Date>::new( |
81 | 58 | pin_state, |
82 | 59 | ButtonConfig { |
83 | 60 | mode: Mode::PullDown, |
|
0 commit comments