compose.rs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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(cx: Scope) -> Element {
  8. let emails_sent = use_ref(cx, Vec::new);
  9. let tx = use_coroutine(cx, |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. cx.render(rsx! {
  18. div {
  19. h1 { "This is your email" }
  20. button {
  21. onclick: move |_| {
  22. let dom = VirtualDom::new_with_props(compose, ComposeProps {
  23. app_tx: tx.clone()
  24. });
  25. // this returns a weak reference to the other window
  26. // Be careful not to keep a strong reference to the other window or it will never be dropped
  27. // and the window will never close.
  28. dioxus_desktop::window().new_window(dom, Default::default());
  29. },
  30. "Click to compose a new email"
  31. }
  32. ul {
  33. emails_sent.read().iter().map(|message| cx.render(rsx! {
  34. li {
  35. h3 { "email" }
  36. span {"{message}"}
  37. }
  38. }))
  39. }
  40. }
  41. })
  42. }
  43. struct ComposeProps {
  44. app_tx: Coroutine<String>,
  45. }
  46. fn compose(cx: Scope<ComposeProps>) -> Element {
  47. let user_input = use_state(cx, String::new);
  48. cx.render(rsx! {
  49. div {
  50. h1 { "Compose a new email" }
  51. button {
  52. onclick: move |_| {
  53. cx.props.app_tx.send(user_input.get().clone());
  54. dioxus_desktop::window().close();
  55. },
  56. "Click to send"
  57. }
  58. input {
  59. oninput: move |e| user_input.set(e.value.clone()),
  60. value: "{user_input}"
  61. }
  62. }
  63. })
  64. }