dioxus/examples/all_events.rs

87 lines
2.3 KiB
Rust
Raw Normal View History

2022-11-16 00:37:23 +00:00
use dioxus::{events::*, html::MouseEvent, prelude::*};
fn main() {
launch(app);
}
#[derive(Debug)]
enum Event {
2022-07-03 03:12:26 +00:00
MouseMove(MouseEvent),
MouseClick(MouseEvent),
MouseDoubleClick(MouseEvent),
MouseDown(MouseEvent),
MouseUp(MouseEvent),
2022-07-03 03:12:26 +00:00
Wheel(WheelEvent),
2022-07-03 03:12:26 +00:00
KeyDown(KeyboardEvent),
KeyUp(KeyboardEvent),
KeyPress(KeyboardEvent),
2022-07-03 03:12:26 +00:00
FocusIn(FocusEvent),
FocusOut(FocusEvent),
}
const MAX_EVENTS: usize = 8;
2022-07-03 03:12:26 +00:00
const CONTAINER_STYLE: &str = r#"
display: flex;
flex-direction: column;
align-items: center;
"#;
2022-07-03 03:12:26 +00:00
const RECT_STYLE: &str = r#"
background: deepskyblue;
height: 50vh;
width: 50vw;
color: white;
padding: 20px;
margin: 20px;
text-aligh: center;
"#;
fn app() -> Element {
let mut events = use_signal(std::collections::VecDeque::new);
let mut log_event = move |event: Event| {
2022-07-03 03:17:48 +00:00
let mut events = events.write();
if events.len() >= MAX_EVENTS {
events.pop_front();
}
events.push_back(event);
};
2024-01-14 05:12:21 +00:00
rsx! {
div { style: "{CONTAINER_STYLE}",
div {
2022-07-03 03:12:26 +00:00
style: "{RECT_STYLE}",
2022-06-28 20:58:07 +00:00
// focusing is necessary to catch keyboard events
tabindex: "0",
2022-07-03 03:12:26 +00:00
onmousemove: move |event| log_event(Event::MouseMove(event)),
onclick: move |event| log_event(Event::MouseClick(event)),
ondoubleclick: move |event| log_event(Event::MouseDoubleClick(event)),
2022-07-03 03:12:26 +00:00
onmousedown: move |event| log_event(Event::MouseDown(event)),
onmouseup: move |event| log_event(Event::MouseUp(event)),
2022-06-28 20:58:07 +00:00
2022-07-03 03:12:26 +00:00
onwheel: move |event| log_event(Event::Wheel(event)),
2022-07-03 03:12:26 +00:00
onkeydown: move |event| log_event(Event::KeyDown(event)),
onkeyup: move |event| log_event(Event::KeyUp(event)),
onkeypress: move |event| log_event(Event::KeyPress(event)),
2022-07-03 03:12:26 +00:00
onfocusin: move |event| log_event(Event::FocusIn(event)),
onfocusout: move |event| log_event(Event::FocusOut(event)),
"Hover, click, type or scroll to see the info down below"
}
div {
for event in events.read().iter() {
div { "{event:?}" }
}
}
}
2024-01-14 05:12:21 +00:00
}
}