dioxus/examples/compose.rs

63 lines
1.7 KiB
Rust
Raw Normal View History

2022-12-31 17:19:21 +00:00
//! This example shows how to create a popup window and send data back to the parent window.
use std::rc::Rc;
2022-12-31 17:19:21 +00:00
use dioxus::prelude::*;
use futures_util::StreamExt;
fn main() {
launch(app);
2022-12-31 17:19:21 +00:00
}
fn app() -> Element {
let mut emails_sent = use_signal(|| Vec::new() as Vec<String>);
2022-12-31 17:19:21 +00:00
2024-01-15 22:40:56 +00:00
// Wait for responses to the compose channel, and then push them to the emails_sent signal.
let handle = use_coroutine(|mut rx: UnboundedReceiver<String>| async move {
2024-01-15 22:40:56 +00:00
while let Some(message) = rx.next().await {
emails_sent.write().push(message);
2022-12-31 17:19:21 +00:00
}
});
2024-01-15 22:40:56 +00:00
let open_compose_window = move |evt: MouseEvent| {
let tx = handle.tx();
2024-01-15 22:40:56 +00:00
dioxus_desktop::window().new_window(
VirtualDom::new_with_props(compose, Rc::new(move |s| tx.unbounded_send(s).unwrap())),
2024-01-15 22:40:56 +00:00
Default::default(),
);
2024-01-15 22:40:56 +00:00
};
2022-12-31 17:19:21 +00:00
2024-01-15 22:40:56 +00:00
rsx! {
h1 { "This is your email" }
button { onclick: open_compose_window, "Click to compose a new email" }
ul {
for message in emails_sent.read().iter() {
li {
h3 { "email" }
span { "{message}" }
}
2022-12-31 17:19:21 +00:00
}
}
2024-01-14 05:12:21 +00:00
}
2022-12-31 17:19:21 +00:00
}
fn compose(send: Rc<dyn Fn(String)>) -> Element {
let mut user_input = use_signal(String::new);
2022-12-31 17:19:21 +00:00
2024-01-14 05:12:21 +00:00
rsx! {
2022-12-31 17:19:21 +00:00
div {
h1 { "Compose a new email" }
button {
onclick: move |_| {
send(user_input.cloned());
dioxus_desktop::window().close();
2022-12-31 17:19:21 +00:00
},
"Click to send"
}
2024-01-06 07:15:30 +00:00
2024-01-05 01:02:00 +00:00
input { oninput: move |e| user_input.set(e.value()), value: "{user_input}" }
2022-12-31 17:19:21 +00:00
}
2024-01-14 05:12:21 +00:00
}
2022-12-31 17:19:21 +00:00
}