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.
|
|
|
|
|
2024-01-16 02:02:58 +00:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
2022-12-31 17:19:21 +00:00
|
|
|
use dioxus::prelude::*;
|
|
|
|
use futures_util::StreamExt;
|
|
|
|
|
|
|
|
fn main() {
|
2024-01-16 14:42:16 +00:00
|
|
|
launch(app);
|
2022-12-31 17:19:21 +00:00
|
|
|
}
|
|
|
|
|
2024-01-14 04:51:37 +00:00
|
|
|
fn app() -> Element {
|
2024-01-16 07:24:59 +00:00
|
|
|
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.
|
2024-01-16 02:02:58 +00:00
|
|
|
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| {
|
2024-01-16 02:02:58 +00:00
|
|
|
let tx = handle.tx();
|
2024-01-15 22:40:56 +00:00
|
|
|
dioxus_desktop::window().new_window(
|
2024-01-16 05:12:44 +00:00
|
|
|
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-16 01:04:39 +00:00
|
|
|
);
|
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}" }
|
2024-01-11 03:33:34 +00:00
|
|
|
}
|
2022-12-31 17:19:21 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-14 05:12:21 +00:00
|
|
|
}
|
2022-12-31 17:19:21 +00:00
|
|
|
}
|
|
|
|
|
2024-01-16 02:02:58 +00:00
|
|
|
fn compose(send: Rc<dyn Fn(String)>) -> Element {
|
2024-01-16 07:24:59 +00:00
|
|
|
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 |_| {
|
2024-01-16 05:12:44 +00:00
|
|
|
send(user_input.cloned());
|
2023-10-23 20:26:10 +00:00
|
|
|
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
|
|
|
}
|