-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add wasm support and add example on browser #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| /target | ||
| /dist |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| [package] | ||
| name = "wasm" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| [dependencies] | ||
| wasm-bindgen = { version = "0.2" } | ||
| web-sys = { version = "0.3.69", features = ["Window", "Document", "Element", "MouseEvent"] } | ||
| button-driver = { path = "../../", features = ["wasm"] } | ||
| log = "0.4.22" | ||
| wasm-logger = "0.2.0" | ||
|
|
||
| [lib] | ||
| path = "src/lib.rs" | ||
| crate-type = ["cdylib"] | ||
|
|
||
| [features] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <html> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <title>Button example</title> | ||
| <style> | ||
| .clickable-area { | ||
| display: flex; | ||
| justify-content: center; | ||
| align-items: center; | ||
| width: 200px; | ||
| height: 100px; | ||
| } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <button class="clickable-area" type="button">Button</button> | ||
| <br> | ||
| <p> | ||
| Please open developer tools console to view log. | ||
| </p> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| //! A WebAssembly example demonstrating button driver usage in the browser. | ||
| //! This example uses the built-in `JsInstant` type which wraps `js_sys::Date::now()`. | ||
| //! | ||
| //! To run this example: | ||
| //! 1. Install trunk: `cargo install trunk` | ||
| //! 2. Run: `trunk serve` | ||
| //! 3. Open your browser at `http://localhost:8080` | ||
| //! | ||
| //! Required features: `wasm` (which includes `std` and `js-sys`). | ||
|
|
||
| use std::{cell::RefCell, rc::Rc}; | ||
|
|
||
| use button_driver::{Button, ButtonConfig, JsInstant, Mode, PinWrapper}; | ||
| use log::info; | ||
| use wasm_bindgen::{closure::Closure, JsCast}; | ||
| use web_sys::MouseEvent; | ||
|
|
||
| #[derive(Clone, Default)] | ||
| struct ButtonInput(Rc<RefCell<bool>>); | ||
|
|
||
| impl PinWrapper for ButtonInput { | ||
| fn is_high(&mut self) -> bool { | ||
| *self.0.borrow() | ||
| } | ||
| } | ||
|
|
||
| #[wasm_bindgen::prelude::wasm_bindgen(start)] | ||
| pub fn main() { | ||
| wasm_logger::init(wasm_logger::Config::default()); | ||
| let document = web_sys::window().unwrap().document().unwrap(); | ||
| let clickable_area = document.query_selector(".clickable-area").unwrap().unwrap(); | ||
| let pin_state = ButtonInput::default(); | ||
| { | ||
| let pin_state_ref = pin_state.clone(); | ||
| let mouse_down_handler = Closure::wrap(Box::new(move |_: MouseEvent| { | ||
| *pin_state_ref.0.borrow_mut() = true; | ||
| }) as Box<dyn FnMut(_)>); | ||
| clickable_area | ||
| .add_event_listener_with_callback( | ||
| "mousedown", | ||
| mouse_down_handler.as_ref().unchecked_ref(), | ||
| ) | ||
| .unwrap(); | ||
| mouse_down_handler.forget(); | ||
| } | ||
| { | ||
| let pin_state_ref = pin_state.clone(); | ||
| let mouse_up_handler = Closure::wrap(Box::new(move |_: MouseEvent| { | ||
| *pin_state_ref.0.borrow_mut() = false; | ||
| }) as Box<dyn FnMut(_)>); | ||
|
|
||
| clickable_area | ||
| .add_event_listener_with_callback("mouseup", mouse_up_handler.as_ref().unchecked_ref()) | ||
| .unwrap(); | ||
| mouse_up_handler.forget(); | ||
| } | ||
| { | ||
| let mut button = Button::<_, JsInstant>::new( | ||
| pin_state, | ||
| ButtonConfig { | ||
| mode: Mode::PullDown, | ||
| ..Default::default() | ||
| }, | ||
| ); | ||
| let callback = Closure::wrap(Box::new(move || { | ||
| button.tick(); | ||
|
|
||
| if let Some(dur) = button.held_time() { | ||
| info!("Total holding time {:?}", dur); | ||
| if button.is_clicked() { | ||
| info!("Clicked + held"); | ||
| } else if button.is_double_clicked() { | ||
| info!("Double clicked + held"); | ||
| } else if button.holds() == 2 && button.clicks() > 0 { | ||
| info!("Held twice with {} clicks", button.clicks()); | ||
| } else if button.holds() == 2 { | ||
| info!("Held twice"); | ||
| } | ||
| } else { | ||
| if button.is_clicked() { | ||
| info!("Click"); | ||
| } else if button.is_double_clicked() { | ||
| info!("Double click"); | ||
| } else if button.is_triple_clicked() { | ||
| info!("Triple click"); | ||
| } else if let Some(dur) = button.current_holding_time() { | ||
| info!("Held for {:?}", dur); | ||
| } | ||
| } | ||
|
|
||
| button.reset(); | ||
| }) as Box<dyn FnMut()>); | ||
|
|
||
| web_sys::window() | ||
| .unwrap() | ||
| .set_interval_with_callback_and_timeout_and_arguments_0( | ||
| callback.as_ref().unchecked_ref(), | ||
| 20, | ||
| ) | ||
| .unwrap(); | ||
| callback.forget(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.