2023-01-01 19:57:33 -05:00
|
|
|
use dioxus::prelude::*;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
fn main() {
|
2024-01-16 11:45:02 -06:00
|
|
|
launch_desktop(app);
|
2023-01-01 19:57:33 -05:00
|
|
|
}
|
|
|
|
|
2024-01-13 20:51:37 -08:00
|
|
|
fn app() -> Element {
|
2024-01-15 23:24:59 -08:00
|
|
|
let mut running = use_signal(|| true);
|
|
|
|
let mut count = use_signal(|| 0);
|
|
|
|
let mut saved_values = use_signal(|| vec![0.to_string()]);
|
2023-01-01 19:57:33 -05:00
|
|
|
|
2024-01-24 15:53:39 -08:00
|
|
|
// use_memo will recompute the value of the signal whenever the captured signals change
|
|
|
|
let doubled_count = use_memo(move || count() * 2);
|
|
|
|
|
|
|
|
// use_effect will subscribe to any changes in the signal values it captures
|
|
|
|
// effects will always run after first mount and then whenever the signal values change
|
2024-01-31 16:26:12 -08:00
|
|
|
use_effect(move || println!("Count changed to {count}"));
|
2024-01-24 15:53:39 -08:00
|
|
|
|
2024-01-29 14:12:05 -08:00
|
|
|
// We can do early returns and conditional rendering which will pause all futures that haven't been polled
|
|
|
|
if count() > 30 {
|
|
|
|
return rsx! {
|
|
|
|
h1 { "Count is too high!" }
|
2024-02-02 13:09:00 -08:00
|
|
|
button { onclick: move |_| count.set(0), "Press to reset" }
|
2024-01-29 14:12:05 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-01-24 15:53:39 -08:00
|
|
|
// use_future will spawn an infinitely running future that can be started and stopped
|
2024-01-31 16:33:53 -08:00
|
|
|
use_future(move || async move {
|
2023-01-01 19:57:33 -05:00
|
|
|
loop {
|
2024-01-15 21:43:30 -08:00
|
|
|
if running() {
|
2023-10-15 17:50:23 -07:00
|
|
|
count += 1;
|
|
|
|
}
|
2023-01-01 22:57:16 -05:00
|
|
|
tokio::time::sleep(Duration::from_millis(400)).await;
|
2023-01-01 19:57:33 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-02-02 13:09:00 -08:00
|
|
|
// use_resource will spawn a future that resolves to a value
|
|
|
|
let _slow_count = use_resource(move || async move {
|
2024-01-24 15:53:39 -08:00
|
|
|
tokio::time::sleep(Duration::from_millis(200)).await;
|
|
|
|
count() * 2
|
|
|
|
});
|
|
|
|
|
2024-01-16 13:18:46 -06:00
|
|
|
rsx! {
|
2023-01-01 19:57:33 -05:00
|
|
|
h1 { "High-Five counter: {count}" }
|
|
|
|
button { onclick: move |_| count += 1, "Up high!" }
|
|
|
|
button { onclick: move |_| count -= 1, "Down low!" }
|
2023-10-15 17:52:01 -07:00
|
|
|
button { onclick: move |_| running.toggle(), "Toggle counter" }
|
2024-01-31 16:26:12 -08:00
|
|
|
button { onclick: move |_| saved_values.push(count.to_string()), "Save this value" }
|
2024-01-26 22:33:41 -08:00
|
|
|
button { onclick: move |_| saved_values.clear(), "Clear saved values" }
|
2023-01-01 22:57:16 -05:00
|
|
|
|
2023-10-15 17:40:56 -07:00
|
|
|
// We can do boolean operations on the current signal value
|
2024-01-15 21:12:44 -08:00
|
|
|
if count() > 5 {
|
2024-01-10 19:33:34 -08:00
|
|
|
h2 { "High five!" }
|
2023-01-01 22:57:16 -05:00
|
|
|
}
|
2023-10-15 17:40:56 -07:00
|
|
|
|
|
|
|
// We can cleanly map signals with iterators
|
2024-01-26 22:33:41 -08:00
|
|
|
for value in saved_values.iter() {
|
2023-10-15 17:40:56 -07:00
|
|
|
h3 { "Saved value: {value}" }
|
|
|
|
}
|
2023-10-17 15:42:21 -07:00
|
|
|
|
|
|
|
// We can also use the signal value as a slice
|
2023-10-17 16:06:43 -07:00
|
|
|
if let [ref first, .., ref last] = saved_values.read().as_slice() {
|
2024-01-10 19:33:34 -08:00
|
|
|
li { "First and last: {first}, {last}" }
|
2023-10-17 15:42:21 -07:00
|
|
|
} else {
|
2024-01-10 19:33:34 -08:00
|
|
|
"No saved values"
|
2023-10-17 15:42:21 -07:00
|
|
|
}
|
2024-01-29 13:36:39 -06:00
|
|
|
|
|
|
|
// You can pass a value directly to any prop that accepts a signal
|
2024-01-29 15:57:23 -06:00
|
|
|
Child { count: doubled_count() }
|
2024-01-31 16:26:12 -08:00
|
|
|
Child { count: doubled_count }
|
2024-01-29 13:36:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[component]
|
2024-01-29 15:57:23 -06:00
|
|
|
fn Child(mut count: ReadOnlySignal<i32>) -> Element {
|
2024-02-01 01:05:28 -08:00
|
|
|
println!("rendering child with count {count}");
|
|
|
|
|
2024-01-29 13:36:39 -06:00
|
|
|
rsx! {
|
|
|
|
h1 { "{count}" }
|
2024-01-13 21:12:21 -08:00
|
|
|
}
|
2023-01-01 19:57:33 -05:00
|
|
|
}
|