1
0

multiwindow_with_tray_icon.rs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. //! Multiwindow with tray icon example
  2. //!
  3. //! This example shows how to implement a simple multiwindow application and tray icon 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. //!
  7. //! This is useful for apps that incorporate settings panels or persistent windows like Raycast.
  8. use dioxus::desktop::{
  9. trayicon::{default_tray_icon, init_tray_icon},
  10. window, WindowCloseBehaviour,
  11. };
  12. use dioxus::prelude::*;
  13. fn main() {
  14. dioxus::launch(app);
  15. }
  16. fn app() -> Element {
  17. use_hook(|| {
  18. // Set the close behavior for the main window
  19. // This will hide the window instead of closing it when the user clicks the close button
  20. window().set_close_behavior(WindowCloseBehaviour::WindowHides);
  21. // Initialize the tray icon with a default icon and no menu
  22. // This will provide the tray into context for the application
  23. init_tray_icon(default_tray_icon(), None)
  24. });
  25. rsx! {
  26. button {
  27. onclick: move |_| {
  28. window().new_window(VirtualDom::new(popup), Default::default());
  29. },
  30. "New Window"
  31. }
  32. }
  33. }
  34. fn popup() -> Element {
  35. rsx! {
  36. div { "This is a popup window!" }
  37. }
  38. }