compose.rs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //! This example shows how to create a popup window and send data back to the parent window.
  2. use std::rc::Rc;
  3. use dioxus::prelude::*;
  4. use futures_util::StreamExt;
  5. fn main() {
  6. dioxus_desktop::launch(app);
  7. }
  8. fn app() -> Element {
  9. let mut emails_sent = use_signal(|| Vec::new() as Vec<String>);
  10. // Wait for responses to the compose channel, and then push them to the emails_sent signal.
  11. let handle = use_coroutine(|mut rx: UnboundedReceiver<String>| async move {
  12. while let Some(message) = rx.next().await {
  13. emails_sent.write().push(message);
  14. }
  15. });
  16. let open_compose_window = move |evt: MouseEvent| {
  17. let tx = handle.tx();
  18. dioxus_desktop::window().new_window(
  19. VirtualDom::new_with_props(compose, Rc::new(move |s| tx.unbounded_send(s).unwrap())),
  20. Default::default(),
  21. );
  22. };
  23. rsx! {
  24. h1 { "This is your email" }
  25. button { onclick: open_compose_window, "Click to compose a new email" }
  26. ul {
  27. for message in emails_sent.read().iter() {
  28. li {
  29. h3 { "email" }
  30. span { "{message}" }
  31. }
  32. }
  33. }
  34. }
  35. }
  36. fn compose(send: Rc<dyn Fn(String)>) -> Element {
  37. let mut user_input = use_signal(String::new);
  38. rsx! {
  39. div {
  40. h1 { "Compose a new email" }
  41. button {
  42. onclick: move |_| {
  43. send(user_input.cloned());
  44. dioxus_desktop::window().close();
  45. },
  46. "Click to send"
  47. }
  48. input { oninput: move |e| user_input.set(e.value()), value: "{user_input}" }
  49. }
  50. }
  51. }