all_events.rs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //! This example shows how to listen to all events on a div and log them to the console.
  2. //!
  3. //! The primary demonstration here is the properties on the events themselves, hoping to give you some inspiration
  4. //! on adding interactivity to your own application.
  5. use dioxus::prelude::*;
  6. use std::{collections::VecDeque, fmt::Debug, rc::Rc};
  7. fn main() {
  8. launch(app);
  9. }
  10. fn app() -> Element {
  11. // Using a VecDeque so its cheap to pop old events off the front
  12. let mut events = use_signal(VecDeque::new);
  13. // All events and their data implement Debug, so we can re-cast them as Rc<dyn Debug> instead of their specific type
  14. let mut log_event = move |event: Rc<dyn Debug>| {
  15. // Only store the last 20 events
  16. if events.read().len() >= 20 {
  17. events.write().pop_front();
  18. }
  19. events.write().push_back(event);
  20. };
  21. rsx! {
  22. style { {include_str!("./assets/events.css")} }
  23. div { id: "container",
  24. // focusing is necessary to catch keyboard events
  25. div { id: "receiver", tabindex: 0,
  26. onmousemove: move |event| log_event(event.data()),
  27. onclick: move |event| log_event(event.data()),
  28. ondoubleclick: move |event| log_event(event.data()),
  29. onmousedown: move |event| log_event(event.data()),
  30. onmouseup: move |event| log_event(event.data()),
  31. onwheel: move |event| log_event(event.data()),
  32. onkeydown: move |event| log_event(event.data()),
  33. onkeyup: move |event| log_event(event.data()),
  34. onkeypress: move |event| log_event(event.data()),
  35. onfocusin: move |event| log_event(event.data()),
  36. onfocusout: move |event| log_event(event.data()),
  37. "Hover, click, type or scroll to see the info down below"
  38. }
  39. div { id: "log",
  40. for event in events.read().iter() {
  41. div { "{event:?}" }
  42. }
  43. }
  44. }
  45. }
  46. }