2022-12-31 12:19:21 -05:00
|
|
|
//! This example shows how to create a popup window and send data back to the parent window.
|
2024-02-14 12:33:07 -08:00
|
|
|
//! Currently Dioxus doesn't support nested renderers, hence the need to create popups as separate windows.
|
2024-01-15 18:02:58 -08:00
|
|
|
|
2022-12-31 12:19:21 -05:00
|
|
|
use dioxus::prelude::*;
|
2024-02-14 12:33:07 -08:00
|
|
|
use std::rc::Rc;
|
2022-12-31 12:19:21 -05:00
|
|
|
|
|
|
|
fn main() {
|
2024-10-10 18:00:58 -05:00
|
|
|
dioxus::LaunchBuilder::desktop().launch(app);
|
2022-12-31 12:19:21 -05:00
|
|
|
}
|
|
|
|
|
2024-01-13 20:51:37 -08:00
|
|
|
fn app() -> Element {
|
2024-01-15 23:24:59 -08:00
|
|
|
let mut emails_sent = use_signal(|| Vec::new() as Vec<String>);
|
2022-12-31 12:19:21 -05:00
|
|
|
|
2024-01-15 14:40:56 -08:00
|
|
|
// Wait for responses to the compose channel, and then push them to the emails_sent signal.
|
2024-10-03 18:48:26 -05:00
|
|
|
let handle = use_coroutine(move |mut rx: UnboundedReceiver<String>| async move {
|
2024-02-14 12:33:07 -08:00
|
|
|
use futures_util::StreamExt;
|
2024-01-15 14:40:56 -08:00
|
|
|
while let Some(message) = rx.next().await {
|
|
|
|
emails_sent.write().push(message);
|
2022-12-31 12:19:21 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-01-19 15:48:21 -08:00
|
|
|
let open_compose_window = move |_evt: MouseEvent| {
|
2024-01-15 18:02:58 -08:00
|
|
|
let tx = handle.tx();
|
2024-01-19 16:36:40 -08:00
|
|
|
dioxus::desktop::window().new_window(
|
2024-02-14 12:33:07 -08:00
|
|
|
VirtualDom::new_with_props(popup, Rc::new(move |s| tx.unbounded_send(s).unwrap())),
|
2024-01-15 14:40:56 -08:00
|
|
|
Default::default(),
|
2024-01-15 17:04:39 -08:00
|
|
|
);
|
2024-01-15 14:40:56 -08:00
|
|
|
};
|
2022-12-31 12:19:21 -05:00
|
|
|
|
2024-01-16 13:18:46 -06:00
|
|
|
rsx! {
|
2024-01-15 14:40:56 -08:00
|
|
|
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-10 19:33:34 -08:00
|
|
|
}
|
2022-12-31 12:19:21 -05:00
|
|
|
}
|
|
|
|
}
|
2024-01-13 21:12:21 -08:00
|
|
|
}
|
2022-12-31 12:19:21 -05:00
|
|
|
}
|
|
|
|
|
2024-02-14 12:33:07 -08:00
|
|
|
fn popup(send: Rc<dyn Fn(String)>) -> Element {
|
2024-01-15 23:24:59 -08:00
|
|
|
let mut user_input = use_signal(String::new);
|
2022-12-31 12:19:21 -05:00
|
|
|
|
2024-01-16 13:18:46 -06:00
|
|
|
rsx! {
|
2022-12-31 12:19:21 -05:00
|
|
|
div {
|
|
|
|
h1 { "Compose a new email" }
|
|
|
|
button {
|
|
|
|
onclick: move |_| {
|
2024-01-15 21:12:44 -08:00
|
|
|
send(user_input.cloned());
|
2024-01-19 16:36:40 -08:00
|
|
|
dioxus::desktop::window().close();
|
2022-12-31 12:19:21 -05:00
|
|
|
},
|
2024-02-14 12:33:07 -08:00
|
|
|
"Send"
|
2022-12-31 12:19:21 -05:00
|
|
|
}
|
2024-01-04 19:02:00 -06:00
|
|
|
input { oninput: move |e| user_input.set(e.value()), value: "{user_input}" }
|
2022-12-31 12:19:21 -05:00
|
|
|
}
|
2024-01-13 21:12:21 -08:00
|
|
|
}
|
2022-12-31 12:19:21 -05:00
|
|
|
}
|