compose.rs 2.2 KB

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