dioxus/examples/popup.rs

61 lines
1.8 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.
2024-02-14 20:33:07 +00:00
//! Currently Dioxus doesn't support nested renderers, hence the need to create popups as separate windows.
2022-12-31 17:19:21 +00:00
use dioxus::prelude::*;
2024-02-14 20:33:07 +00:00
use std::rc::Rc;
2022-12-31 17:19:21 +00:00
fn main() {
dioxus::LaunchBuilder::desktop().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(move |mut rx: UnboundedReceiver<String>| async move {
2024-02-14 20:33:07 +00:00
use futures_util::StreamExt;
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-19 23:48:21 +00:00
let open_compose_window = move |_evt: MouseEvent| {
let tx = handle.tx();
2024-01-20 00:36:40 +00:00
dioxus::desktop::window().new_window(
2024-02-14 20:33:07 +00:00
VirtualDom::new_with_props(popup, 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-16 19:18:46 +00:00
rsx! {
2024-01-15 22:40:56 +00: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}" }
}
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-02-14 20:33:07 +00:00
fn popup(send: Rc<dyn Fn(String)>) -> Element {
let mut user_input = use_signal(String::new);
2022-12-31 17:19:21 +00:00
2024-01-16 19:18:46 +00:00
rsx! {
2022-12-31 17:19:21 +00:00
div {
h1 { "Compose a new email" }
button {
onclick: move |_| {
send(user_input.cloned());
2024-01-20 00:36:40 +00:00
dioxus::desktop::window().close();
2022-12-31 17:19:21 +00:00
},
2024-02-14 20:33:07 +00:00
"Send"
2022-12-31 17:19:21 +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
}