dioxus/examples/keys.rs

66 lines
1.9 KiB
Rust
Raw Normal View History

2022-02-04 21:03:55 +00:00
use crossterm::event::{KeyCode, KeyEvent, MouseEvent};
2022-01-12 14:40:36 +00:00
use dioxus::prelude::*;
fn main() {
rink::launch(app);
}
fn app(cx: Scope) -> Element {
2022-02-04 21:03:55 +00:00
let (key, set_key) = use_state(&cx, || KeyCode::Null);
let (mouse, set_mouse) = use_state(&cx, || (0, 0));
let (size, set_size) = use_state(&cx, || (0, 0));
2022-02-04 21:09:26 +00:00
let (count, set_count) = use_state(&cx, || 0);
2022-01-12 14:40:36 +00:00
cx.render(rsx! {
div {
width: "100%",
height: "10px",
background_color: "red",
justify_content: "center",
align_items: "center",
2022-02-04 21:03:55 +00:00
flex_direction: "column",
2022-01-12 14:40:36 +00:00
2022-02-04 20:52:01 +00:00
rink::InputHandler {
onkeydown: move |evt: KeyEvent| {
2022-02-04 21:09:26 +00:00
use crossterm::event::KeyCode::*;
match evt.code {
Left => set_count(count + 1),
Right => set_count(count - 1),
Up => set_count(count + 10),
Down => set_count(count - 10),
_ => {},
}
2022-02-04 21:03:55 +00:00
set_key(evt.code);
2022-02-04 20:52:01 +00:00
},
2022-02-04 21:03:55 +00:00
onmousedown: move |evt: MouseEvent| {
set_mouse((evt.row, evt.column));
2022-02-04 20:52:01 +00:00
},
onresize: move |dims| {
2022-02-04 21:03:55 +00:00
set_size(dims);
2022-02-04 20:52:01 +00:00
},
},
2022-02-04 21:09:26 +00:00
"count: {count:?}",
"key: {key:?}",
2022-02-04 21:03:55 +00:00
"mouse: {mouse:?}",
"resize: {size:?}",
2022-01-12 14:40:36 +00:00
}
})
}
fn app2<'a>(cx: Scope<'a>) -> Element<'a> {
let (count, set_count) = use_state(&cx, || 0);
2022-01-12 14:40:36 +00:00
cx.render(rsx! {
div {
width: "100%",
height: "10px",
background_color: "red",
justify_content: "center",
align_items: "center",
oninput: move |_| set_count(count + 1),
2022-01-12 14:40:36 +00:00
"Hello world!",
h1 {},
}
})
}