1
0

compose.rs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //! This example shows how to create a popup window and send data back to the parent window.
  2. use dioxus::prelude::*;
  3. use futures_util::StreamExt;
  4. fn main() {
  5. dioxus_desktop::launch(app);
  6. }
  7. fn app() -> Element {
  8. let emails_sent = use_signal(Vec::new);
  9. let tx = use_coroutine(|mut rx: UnboundedReceiver<String>| {
  10. to_owned![emails_sent];
  11. async move {
  12. while let Some(message) = rx.next().await {
  13. emails_sent.write().push(message);
  14. }
  15. }
  16. });
  17. rsx! {
  18. div {
  19. h1 { "This is your email" }
  20. button {
  21. onclick: move |_| {
  22. let dom = VirtualDom::new_with_props(compose, ComposeProps { app_tx: tx.clone() });
  23. dioxus_desktop::window().new_window(dom, Default::default());
  24. },
  25. "Click to compose a new email"
  26. }
  27. ul {
  28. for message in emails_sent.read().iter() {
  29. li {
  30. h3 { "email" }
  31. span {"{message}"}
  32. }
  33. }
  34. }
  35. }
  36. }
  37. }
  38. struct ComposeProps {
  39. app_tx: Coroutine<String>,
  40. }
  41. fn compose(cx: Scope<ComposeProps>) -> Element {
  42. let user_input = use_signal(String::new);
  43. rsx! {
  44. div {
  45. h1 { "Compose a new email" }
  46. button {
  47. onclick: move |_| {
  48. cx.props.app_tx.send(user_input.get().clone());
  49. dioxus_desktop::window().close();
  50. },
  51. "Click to send"
  52. }
  53. input { oninput: move |e| user_input.set(e.value()), value: "{user_input}" }
  54. }
  55. }
  56. }