menubar.rs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. use std::any::Any;
  2. use tao::window::Window;
  3. #[allow(unused)]
  4. pub fn build_menu(window: &Window, default_menu_bar: bool) -> Option<Box<dyn Any>> {
  5. #[cfg(not(any(target_os = "ios", target_os = "android")))]
  6. {
  7. return Some(Box::new(impl_::build_menu_bar(default_menu_bar, window)) as Box<dyn Any>);
  8. }
  9. None
  10. }
  11. #[cfg(not(any(target_os = "ios", target_os = "android")))]
  12. mod impl_ {
  13. use super::*;
  14. use muda::{Menu, MenuItem, PredefinedMenuItem, Submenu};
  15. /// Builds a standard menu bar depending on the users platform. It may be used as a starting point
  16. /// to further customize the menu bar and pass it to a [`WindowBuilder`](tao::window::WindowBuilder).
  17. /// > Note: The default menu bar enables macOS shortcuts like cut/copy/paste.
  18. /// > The menu bar differs per platform because of constraints introduced
  19. /// > by [`MenuItem`](tao::menu::MenuItem).
  20. #[allow(unused)]
  21. pub fn build_menu_bar(default: bool, window: &Window) -> Menu {
  22. let menu = Menu::new();
  23. #[cfg(target_os = "windows")]
  24. {
  25. use tao::platform::windows::WindowExtWindows;
  26. menu.init_for_hwnd(window.hwnd());
  27. }
  28. #[cfg(target_os = "linux")]
  29. {
  30. use tao::platform::unix::WindowExtUnix;
  31. menu.init_for_gtk_window(window.gtk_window(), window.default_vbox())
  32. .unwrap();
  33. }
  34. #[cfg(target_os = "macos")]
  35. {
  36. use tao::platform::macos::WindowExtMacOS;
  37. menu.init_for_nsapp();
  38. }
  39. if default {
  40. // since it is uncommon on windows to have an "application menu"
  41. // we add a "window" menu to be more consistent across platforms with the standard menu
  42. let window_menu = Submenu::new("Window", true);
  43. window_menu
  44. .append_items(&[
  45. &PredefinedMenuItem::fullscreen(None),
  46. &PredefinedMenuItem::separator(),
  47. &PredefinedMenuItem::hide(None),
  48. &PredefinedMenuItem::hide_others(None),
  49. &PredefinedMenuItem::show_all(None),
  50. &PredefinedMenuItem::maximize(None),
  51. &PredefinedMenuItem::minimize(None),
  52. &PredefinedMenuItem::close_window(None),
  53. &PredefinedMenuItem::separator(),
  54. &PredefinedMenuItem::quit(None),
  55. ])
  56. .unwrap();
  57. let edit_menu = Submenu::new("Edit", true);
  58. edit_menu
  59. .append_items(&[
  60. &PredefinedMenuItem::undo(None),
  61. &PredefinedMenuItem::redo(None),
  62. &PredefinedMenuItem::separator(),
  63. &PredefinedMenuItem::cut(None),
  64. &PredefinedMenuItem::copy(None),
  65. &PredefinedMenuItem::paste(None),
  66. &PredefinedMenuItem::separator(),
  67. &PredefinedMenuItem::select_all(None),
  68. ])
  69. .unwrap();
  70. let help_menu = Submenu::new("Help", true);
  71. help_menu
  72. .append_items(&[&MenuItem::new("Toggle Developer Tools", true, None)])
  73. .unwrap();
  74. menu.append_items(&[&window_menu, &edit_menu, &help_menu])
  75. .unwrap();
  76. #[cfg(target_os = "macos")]
  77. {
  78. window_menu.set_as_windows_menu_for_nsapp();
  79. help_menu.set_as_help_menu_for_nsapp();
  80. }
  81. }
  82. menu
  83. }
  84. }