menubar.rs 3.5 KB

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