2023-01-02 00:57:33 +00:00
|
|
|
use dioxus::prelude::*;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
dioxus_desktop::launch(app);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn app(cx: Scope) -> Element {
|
2023-10-16 00:50:23 +00:00
|
|
|
let running = dioxus_signals::use_signal(cx, || true);
|
2023-07-14 19:52:49 +00:00
|
|
|
let mut count = dioxus_signals::use_signal(cx, || 0);
|
2023-10-17 22:42:21 +00:00
|
|
|
let saved_values = dioxus_signals::use_signal(cx, || vec![0.to_string()]);
|
2023-01-02 00:57:33 +00:00
|
|
|
|
2023-10-17 22:42:21 +00:00
|
|
|
// Signals can be used in async functions without an explicit clone since they're 'static and Copy
|
|
|
|
// Signals are backed by a runtime that is designed to deeply integrate with Dioxus apps
|
2023-01-02 03:09:08 +00:00
|
|
|
use_future!(cx, || async move {
|
2023-01-02 00:57:33 +00:00
|
|
|
loop {
|
2023-10-16 00:50:23 +00:00
|
|
|
if running.value() {
|
|
|
|
count += 1;
|
|
|
|
}
|
2023-01-02 03:57:16 +00:00
|
|
|
tokio::time::sleep(Duration::from_millis(400)).await;
|
2023-01-02 00:57:33 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
cx.render(rsx! {
|
|
|
|
h1 { "High-Five counter: {count}" }
|
|
|
|
button { onclick: move |_| count += 1, "Up high!" }
|
|
|
|
button { onclick: move |_| count -= 1, "Down low!" }
|
2023-10-16 00:52:01 +00:00
|
|
|
button { onclick: move |_| running.toggle(), "Toggle counter" }
|
2023-10-17 22:42:21 +00:00
|
|
|
button { onclick: move |_| saved_values.push(count.value().to_string()), "Save this value" }
|
2023-10-17 22:52:13 +00:00
|
|
|
button { onclick: move |_| saved_values.write().clear(), "Clear saved values" }
|
2023-01-02 03:57:16 +00:00
|
|
|
|
2023-10-16 00:40:56 +00:00
|
|
|
// We can do boolean operations on the current signal value
|
2023-08-07 21:31:03 +00:00
|
|
|
if count.value() > 5 {
|
2023-01-02 03:57:16 +00:00
|
|
|
rsx!{ h2 { "High five!" } }
|
|
|
|
}
|
2023-10-16 00:40:56 +00:00
|
|
|
|
|
|
|
// We can cleanly map signals with iterators
|
|
|
|
for value in saved_values.read().iter() {
|
|
|
|
h3 { "Saved value: {value}" }
|
|
|
|
}
|
2023-10-17 22:42:21 +00:00
|
|
|
|
|
|
|
// We can also use the signal value as a slice
|
2023-10-17 23:06:43 +00:00
|
|
|
if let [ref first, .., ref last] = saved_values.read().as_slice() {
|
2023-10-17 22:52:13 +00:00
|
|
|
rsx! { li { "First and last: {first}, {last}" } }
|
2023-10-17 22:42:21 +00:00
|
|
|
} else {
|
|
|
|
rsx! { "No saved values" }
|
|
|
|
}
|
2023-01-02 00:57:33 +00:00
|
|
|
})
|
|
|
|
}
|