multiwindow.rs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. //! Multiwindow example
  2. //!
  3. //! This example shows how to implement a simple multiwindow application using dioxus.
  4. //! This works by spawning a new window when the user clicks a button. We have to build a new virtualdom which has its
  5. //! own context, root elements, etc.
  6. use dioxus::prelude::*;
  7. use dioxus::{desktop::Config, desktop::WindowCloseBehaviour};
  8. fn main() {
  9. dioxus::LaunchBuilder::desktop()
  10. // We can choose the close behavior of the last window to hide. See WindowCloseBehaviour for more options.
  11. .with_cfg(Config::new().with_close_behaviour(WindowCloseBehaviour::LastWindowHides))
  12. .launch(app);
  13. }
  14. fn app() -> Element {
  15. let onclick = move |_| {
  16. let dom = VirtualDom::new(popup);
  17. dioxus::desktop::window().new_window(dom, Default::default());
  18. };
  19. rsx! {
  20. button { onclick, "New Window" }
  21. }
  22. }
  23. fn popup() -> Element {
  24. let mut count = use_signal(|| 0);
  25. rsx! {
  26. div {
  27. h1 { "Popup Window" }
  28. p { "Count: {count}" }
  29. button { onclick: move |_| count += 1, "Increment" }
  30. }
  31. }
  32. }