custom_menu.rs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //! This example shows how to use a custom menu bar with Dioxus desktop.
  2. //! This example is not supported on the mobile or web renderers.
  3. use dioxus::desktop::{muda::*, use_muda_event_handler};
  4. use dioxus::prelude::*;
  5. fn main() {
  6. // Create a menu bar that only contains the edit menu
  7. let menu = Menu::new();
  8. let edit_menu = Submenu::new("Edit", true);
  9. edit_menu
  10. .append_items(&[
  11. &PredefinedMenuItem::undo(None),
  12. &PredefinedMenuItem::redo(None),
  13. &PredefinedMenuItem::separator(),
  14. &PredefinedMenuItem::cut(None),
  15. &PredefinedMenuItem::copy(None),
  16. &PredefinedMenuItem::paste(None),
  17. &PredefinedMenuItem::select_all(None),
  18. &MenuItem::with_id("switch-text", "Switch text", true, None),
  19. ])
  20. .unwrap();
  21. menu.append(&edit_menu).unwrap();
  22. // Create a desktop config that overrides the default menu with the custom menu
  23. let config = dioxus::desktop::Config::new().with_menu(menu);
  24. // Launch the app with the custom menu
  25. dioxus::LaunchBuilder::new().with_cfg(config).launch(app)
  26. }
  27. fn app() -> Element {
  28. let mut text = use_signal(String::new);
  29. // You can use the `use_muda_event_handler` hook to run code when a menu event is triggered.
  30. use_muda_event_handler(move |muda_event| {
  31. if muda_event.id() == "switch-text" {
  32. text.set("Switched to text".to_string());
  33. }
  34. });
  35. rsx! {
  36. div {
  37. h1 { "Custom Menu" }
  38. p { "Text: {text}" }
  39. }
  40. }
  41. }