2022-11-16 00:37:23 +00:00
|
|
|
use dioxus::{events::*, html::MouseEvent, prelude::*};
|
2022-06-28 20:36:06 +00:00
|
|
|
|
|
|
|
fn main() {
|
2022-07-09 19:15:20 +00:00
|
|
|
dioxus_desktop::launch(app);
|
2022-06-28 20:36:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum Event {
|
2022-07-03 03:12:26 +00:00
|
|
|
MouseMove(MouseEvent),
|
|
|
|
MouseClick(MouseEvent),
|
|
|
|
MouseDoubleClick(MouseEvent),
|
|
|
|
MouseDown(MouseEvent),
|
|
|
|
MouseUp(MouseEvent),
|
2022-06-28 22:18:09 +00:00
|
|
|
|
2022-07-03 03:12:26 +00:00
|
|
|
Wheel(WheelEvent),
|
2022-06-28 22:18:09 +00:00
|
|
|
|
2022-07-03 03:12:26 +00:00
|
|
|
KeyDown(KeyboardEvent),
|
|
|
|
KeyUp(KeyboardEvent),
|
|
|
|
KeyPress(KeyboardEvent),
|
2022-06-28 22:24:33 +00:00
|
|
|
|
2022-07-03 03:12:26 +00:00
|
|
|
FocusIn(FocusEvent),
|
|
|
|
FocusOut(FocusEvent),
|
2022-06-28 20:36:06 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 22:24:33 +00:00
|
|
|
const MAX_EVENTS: usize = 8;
|
2022-06-28 20:36:06 +00:00
|
|
|
|
2022-07-03 03:12:26 +00:00
|
|
|
const CONTAINER_STYLE: &str = r#"
|
2022-06-28 20:36:06 +00:00
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
align-items: center;
|
|
|
|
"#;
|
2022-07-03 03:12:26 +00:00
|
|
|
|
|
|
|
const RECT_STYLE: &str = r#"
|
2022-06-28 20:36:06 +00:00
|
|
|
background: deepskyblue;
|
|
|
|
height: 50vh;
|
|
|
|
width: 50vw;
|
2022-06-28 22:18:09 +00:00
|
|
|
color: white;
|
|
|
|
padding: 20px;
|
|
|
|
margin: 20px;
|
|
|
|
text-aligh: center;
|
2022-06-28 20:36:06 +00:00
|
|
|
"#;
|
|
|
|
|
2022-07-03 03:12:26 +00:00
|
|
|
fn app(cx: Scope) -> Element {
|
2022-12-07 21:11:40 +00:00
|
|
|
let events = use_ref(cx, std::collections::VecDeque::new);
|
2022-06-28 20:36:06 +00:00
|
|
|
|
2022-07-03 03:17:48 +00:00
|
|
|
let log_event = move |event: Event| {
|
|
|
|
let mut events = events.write();
|
|
|
|
|
|
|
|
if events.len() >= MAX_EVENTS {
|
|
|
|
events.pop_front();
|
|
|
|
}
|
|
|
|
events.push_back(event);
|
|
|
|
};
|
2022-06-28 20:36:06 +00:00
|
|
|
|
|
|
|
cx.render(rsx! (
|
2024-01-03 20:02:08 +00:00
|
|
|
div { style: "{CONTAINER_STYLE}",
|
2022-06-28 20:36:06 +00:00
|
|
|
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)),
|
2024-01-03 20:02:08 +00:00
|
|
|
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-06-28 22:18:09 +00:00
|
|
|
|
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-06-28 22:18:09 +00:00
|
|
|
|
2022-07-03 03:12:26 +00:00
|
|
|
onfocusin: move |event| log_event(Event::FocusIn(event)),
|
|
|
|
onfocusout: move |event| log_event(Event::FocusOut(event)),
|
2022-06-28 22:24:33 +00:00
|
|
|
|
2022-06-28 22:18:09 +00:00
|
|
|
"Hover, click, type or scroll to see the info down below"
|
2022-06-28 20:36:06 +00:00
|
|
|
}
|
2024-01-03 20:02:08 +00:00
|
|
|
div { events.read().iter().map(|event| rsx!( div { "{event:?}" } )) }
|
|
|
|
}
|
2022-06-28 20:36:06 +00:00
|
|
|
))
|
|
|
|
}
|