menu.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package fyne
  2. type systemTrayDriver interface {
  3. Driver
  4. SetSystemTrayMenu(*Menu)
  5. SystemTrayMenu() *Menu
  6. }
  7. // Menu stores the information required for a standard menu.
  8. // A menu can pop down from a MainMenu or could be a pop out menu.
  9. type Menu struct {
  10. Label string
  11. Items []*MenuItem
  12. }
  13. // NewMenu creates a new menu given the specified label (to show in a MainMenu) and list of items to display.
  14. func NewMenu(label string, items ...*MenuItem) *Menu {
  15. return &Menu{Label: label, Items: items}
  16. }
  17. // Refresh will instruct this menu to update its display.
  18. //
  19. // Since: 2.2
  20. func (m *Menu) Refresh() {
  21. for _, w := range CurrentApp().Driver().AllWindows() {
  22. main := w.MainMenu()
  23. if main != nil {
  24. for _, menu := range main.Items {
  25. if menu == m {
  26. w.SetMainMenu(main)
  27. break
  28. }
  29. }
  30. }
  31. }
  32. if d, ok := CurrentApp().Driver().(systemTrayDriver); ok {
  33. if m == d.SystemTrayMenu() {
  34. d.SetSystemTrayMenu(m)
  35. }
  36. }
  37. }
  38. // MenuItem is a single item within any menu, it contains a display Label and Action function that is called when tapped.
  39. type MenuItem struct {
  40. ChildMenu *Menu
  41. // Since: 2.1
  42. IsQuit bool
  43. IsSeparator bool
  44. Label string
  45. Action func()
  46. // Since: 2.1
  47. Disabled bool
  48. // Since: 2.1
  49. Checked bool
  50. // Since: 2.2
  51. Icon Resource
  52. // Since: 2.2
  53. Shortcut Shortcut
  54. }
  55. // NewMenuItem creates a new menu item from the passed label and action parameters.
  56. func NewMenuItem(label string, action func()) *MenuItem {
  57. return &MenuItem{Label: label, Action: action}
  58. }
  59. // NewMenuItemSeparator creates a menu item that is to be used as a separator.
  60. func NewMenuItemSeparator() *MenuItem {
  61. return &MenuItem{IsSeparator: true, Action: func() {}}
  62. }
  63. // MainMenu defines the data required to show a menu bar (desktop) or other appropriate top level menu.
  64. type MainMenu struct {
  65. Items []*Menu
  66. }
  67. // NewMainMenu creates a top level menu structure used by fyne.Window for displaying a menubar
  68. // (or appropriate equivalent).
  69. func NewMainMenu(items ...*Menu) *MainMenu {
  70. return &MainMenu{Items: items}
  71. }
  72. // Refresh will instruct any rendered menus using this struct to update their display.
  73. //
  74. // Since: 2.2
  75. func (m *MainMenu) Refresh() {
  76. for _, w := range CurrentApp().Driver().AllWindows() {
  77. menu := w.MainMenu()
  78. if menu != nil && menu == m {
  79. w.SetMainMenu(m)
  80. }
  81. }
  82. }