dioxus/packages/tui/examples/tui_keys.rs

67 lines
2.1 KiB
Rust
Raw Normal View History

2022-03-09 18:36:30 +00:00
use dioxus::events::WheelEvent;
use dioxus::prelude::*;
use dioxus_html::geometry::ScreenPoint;
2022-05-12 11:36:52 +00:00
use dioxus_html::input_data::keyboard_types::Code;
use dioxus_html::input_data::MouseButtonSet;
2022-03-09 18:36:30 +00:00
use dioxus_html::on::{KeyboardEvent, MouseEvent};
fn main() {
dioxus_tui::launch(app);
2022-03-09 18:36:30 +00:00
}
fn app(cx: Scope) -> Element {
let key = use_state(&cx, || "".to_string());
let mouse = use_state(&cx, ScreenPoint::zero);
2022-03-09 18:36:30 +00:00
let count = use_state(&cx, || 0);
let buttons = use_state(&cx, MouseButtonSet::empty);
2022-03-09 18:36:30 +00:00
let mouse_clicked = use_state(&cx, || false);
2022-05-12 11:36:52 +00:00
let key_down_handler = move |evt: KeyboardEvent| {
match evt.data.code() {
Code::ArrowLeft => count.set(count + 1),
Code::ArrowRight => count.set(count - 1),
Code::ArrowUp => count.set(count + 10),
Code::ArrowDown => count.set(count - 10),
_ => {}
}
key.set(format!(
"{:?} repeating: {:?}",
evt.key(),
evt.is_auto_repeating()
));
};
2022-03-09 18:36:30 +00:00
cx.render(rsx! {
div {
width: "100%",
height: "10px",
background_color: "red",
justify_content: "center",
align_items: "center",
flex_direction: "column",
2022-05-12 11:36:52 +00:00
onkeydown: key_down_handler,
2022-03-09 18:36:30 +00:00
onwheel: move |evt: WheelEvent| {
count.set(count + evt.data.delta().strip_units().y as i64);
2022-03-09 18:36:30 +00:00
},
ondrag: move |evt: MouseEvent| {
mouse.set(evt.data.screen_coordinates());
2022-03-09 18:36:30 +00:00
},
onmousedown: move |evt: MouseEvent| {
mouse.set(evt.data.screen_coordinates());
buttons.set(evt.data.held_buttons());
2022-03-09 18:36:30 +00:00
mouse_clicked.set(true);
},
onmouseup: move |evt: MouseEvent| {
buttons.set(evt.data.held_buttons());
2022-03-09 18:36:30 +00:00
mouse_clicked.set(false);
},
"count: {count:?}",
"key: {key}",
"mouse buttons: {buttons:?}",
2022-03-09 18:36:30 +00:00
"mouse pos: {mouse:?}",
"mouse button pressed: {mouse_clicked}"
}
})
}