forked from richardwilkes/webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.go
128 lines (114 loc) · 3.4 KB
/
menu.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package webapp
import (
"github.com/richardwilkes/cef/cef"
"github.com/richardwilkes/webapp/keys"
)
// Pre-defined menu IDs. Apps should start their IDs at MenuIDUserBase.
const (
MenuIDAppMenu = int(cef.MenuIDUserFirst) + iota
MenuIDFileMenu
MenuIDEditMenu
MenuIDWindowMenu
MenuIDHelpMenu
MenuIDServicesMenu
MenuIDAboutItem
MenuIDDevToolsItem
MenuIDPreferencesItem
MenuIDQuitItem
MenuIDCutItem
MenuIDCopyItem
MenuIDPasteItem
MenuIDDeleteItem
MenuIDSelectAllItem
MenuIDMinimizeItem
MenuIDZoomItem
MenuIDBringAllWindowsToFrontItem
MenuIDCloseItem
MenuIDHideItem
MenuIDHideOthersItem
MenuIDShowAllItem
MenuIDUserBase = MenuIDAppMenu + 250
)
// Menu represents a set of menu items.
type Menu struct {
PlatformData interface{}
ID int
Title string
}
// NewMenu creates a new menu.
func NewMenu(id int, title string) *Menu {
menu := &Menu{
Title: title,
ID: id,
}
driver.MenuInit(menu)
return menu
}
// ItemAtIndex returns the menu item at the specified index within the menu.
func (menu *Menu) ItemAtIndex(index int) *MenuItem {
return driver.MenuItemAtIndex(menu, index)
}
// Item returns the menu item with the specified id anywhere in the menu and
// and its sub-menus.
func (menu *Menu) Item(id int) *MenuItem {
for i := menu.Count() - 1; i >= 0; i-- {
item := menu.ItemAtIndex(i)
if item.ID == id {
return item
}
if item.SubMenu != nil {
if item = item.SubMenu.Item(id); item != nil {
return item
}
}
}
return nil
}
// SetItemAtIndexTitle sets the title of the menu item at the specified index
// within the menu.
func (menu *Menu) SetItemAtIndexTitle(index int, title string) {
driver.MenuItemAtIndexSetTitle(menu, index, title)
}
// SetItemTitle sets the title of the menu item with the specified id anywhere
// in the menu and its sub-menus.
func (menu *Menu) SetItemTitle(id int, title string) {
if item := menu.Item(id); item != nil {
item.Owner.SetItemAtIndexTitle(item.Index, title)
}
}
// InsertSeparator inserts a menu separator at the specified item index within
// this menu. Pass in a negative index to append to the end.
func (menu *Menu) InsertSeparator(beforeIndex int) {
driver.MenuInsertSeparator(menu, beforeIndex)
}
// InsertItem inserts a menu item at the specified item index within this
// menu. Pass in a negative index to append to the end. Both 'validator' and
// 'handler' may be nil for default behavior.
func (menu *Menu) InsertItem(beforeIndex, id int, title string, key *keys.Key, keyModifiers keys.Modifiers, validator func() bool, handler func()) {
if validator == nil {
validator = func() bool { return true }
}
if handler == nil {
handler = func() {}
}
driver.MenuInsertItem(menu, beforeIndex, id, title, key, keyModifiers, validator, handler)
}
// InsertMenu inserts a new sub-menu at the specified item index within this
// menu. Pass in a negative index to append to the end.
func (menu *Menu) InsertMenu(beforeIndex, id int, title string) *Menu {
return driver.MenuInsertMenu(menu, beforeIndex, id, title)
}
// Remove the menu item at the specified index from this menu.
func (menu *Menu) Remove(index int) {
if index >= 0 && index < menu.Count() {
driver.MenuRemove(menu, index)
}
}
// Count of menu items in this menu.
func (menu *Menu) Count() int {
return driver.MenuCount(menu)
}
// Dispose releases any operating system resources associated with this menu.
func (menu *Menu) Dispose() {
driver.MenuDispose(menu)
}