2022-06-28 20:36:06 +00:00
|
|
|
use dioxus::prelude::*;
|
2022-06-28 22:24:33 +00:00
|
|
|
use dioxus_html::on::{FocusData, KeyboardData, MouseData, WheelData};
|
2022-06-28 20:36:06 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
dioxus::desktop::launch(app);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum Event {
|
|
|
|
MouseMove(Arc<MouseData>),
|
|
|
|
MouseClick(Arc<MouseData>),
|
|
|
|
MouseDoubleClick(Arc<MouseData>),
|
|
|
|
MouseDown(Arc<MouseData>),
|
|
|
|
MouseUp(Arc<MouseData>),
|
2022-06-28 22:18:09 +00:00
|
|
|
|
|
|
|
Wheel(Arc<WheelData>),
|
|
|
|
|
2022-06-28 20:58:07 +00:00
|
|
|
KeyDown(Arc<KeyboardData>),
|
|
|
|
KeyUp(Arc<KeyboardData>),
|
|
|
|
KeyPress(Arc<KeyboardData>),
|
2022-06-28 22:24:33 +00:00
|
|
|
|
|
|
|
FocusIn(Arc<FocusData>),
|
|
|
|
FocusOut(Arc<FocusData>),
|
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
|
|
|
|
|
|
|
fn app(cx: Scope) -> Element {
|
|
|
|
let container_style = r#"
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
align-items: center;
|
|
|
|
"#;
|
|
|
|
let rect_style = r#"
|
|
|
|
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
|
|
|
"#;
|
|
|
|
|
|
|
|
let events = use_ref(&cx, || Vec::new());
|
|
|
|
|
|
|
|
let events_lock = events.read();
|
|
|
|
let first_index = events_lock.len().saturating_sub(MAX_EVENTS);
|
|
|
|
let events_rendered = events_lock[first_index..]
|
|
|
|
.iter()
|
2022-06-28 20:46:24 +00:00
|
|
|
.map(|event| cx.render(rsx!(div {"{event:?}"})));
|
2022-06-28 20:36:06 +00:00
|
|
|
|
|
|
|
let log_event = move |event: Event| {
|
|
|
|
events.write().push(event);
|
|
|
|
};
|
|
|
|
|
|
|
|
cx.render(rsx! (
|
|
|
|
div {
|
|
|
|
style: "{container_style}",
|
|
|
|
div {
|
|
|
|
style: "{rect_style}",
|
2022-06-28 20:58:07 +00:00
|
|
|
// focusing is necessary to catch keyboard events
|
|
|
|
tabindex: "0",
|
|
|
|
|
2022-06-28 20:36:06 +00:00
|
|
|
onmousemove: move |event| log_event(Event::MouseMove(event.data)),
|
|
|
|
onclick: move |event| log_event(Event::MouseClick(event.data)),
|
|
|
|
ondblclick: move |event| log_event(Event::MouseDoubleClick(event.data)),
|
|
|
|
onmousedown: move |event| log_event(Event::MouseDown(event.data)),
|
|
|
|
onmouseup: move |event| log_event(Event::MouseUp(event.data)),
|
2022-06-28 20:58:07 +00:00
|
|
|
|
2022-06-28 22:18:09 +00:00
|
|
|
onwheel: move |event| log_event(Event::Wheel(event.data)),
|
|
|
|
|
2022-06-28 20:58:07 +00:00
|
|
|
onkeydown: move |event| log_event(Event::KeyDown(event.data)),
|
|
|
|
onkeyup: move |event| log_event(Event::KeyUp(event.data)),
|
|
|
|
onkeypress: move |event| log_event(Event::KeyPress(event.data)),
|
2022-06-28 22:18:09 +00:00
|
|
|
|
2022-06-28 22:24:33 +00:00
|
|
|
onfocusin: move |event| log_event(Event::FocusIn(event.data)),
|
|
|
|
onfocusout: move |event| log_event(Event::FocusOut(event.data)),
|
|
|
|
|
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
|
|
|
}
|
2022-06-28 20:46:24 +00:00
|
|
|
div { events_rendered },
|
2022-06-28 20:36:06 +00:00
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|