forked from asticode/go-astilectron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu_item.go
168 lines (153 loc) · 5.95 KB
/
menu_item.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package astilectron
import (
"context"
"github.com/asticode/go-astitools/context"
)
// Menu item event names
const (
EventNameMenuItemCmdSetChecked = "menu.item.cmd.set.checked"
EventNameMenuItemCmdSetEnabled = "menu.item.cmd.set.enabled"
EventNameMenuItemCmdSetLabel = "menu.item.cmd.set.label"
EventNameMenuItemCmdSetVisible = "menu.item.cmd.set.visible"
EventNameMenuItemEventCheckedSet = "menu.item.event.checked.set"
EventNameMenuItemEventClicked = "menu.item.event.clicked"
EventNameMenuItemEventEnabledSet = "menu.item.event.enabled.set"
EventNameMenuItemEventLabelSet = "menu.item.event.label.set"
EventNameMenuItemEventVisibleSet = "menu.item.event.visible.set"
)
// Menu item roles
var (
// All
MenuItemRoleClose = PtrStr("close")
MenuItemRoleCopy = PtrStr("copy")
MenuItemRoleCut = PtrStr("cut")
MenuItemRoleDelete = PtrStr("delete")
MenuItemRoleEditMenu = PtrStr("editMenu")
MenuItemRoleForceReload = PtrStr("forcereload")
MenuItemRoleMinimize = PtrStr("minimize")
MenuItemRolePaste = PtrStr("paste")
MenuItemRolePasteAndMatchStyle = PtrStr("pasteandmatchstyle")
MenuItemRoleQuit = PtrStr("quit")
MenuItemRoleRedo = PtrStr("redo")
MenuItemRoleReload = PtrStr("reload")
MenuItemRoleResetZoom = PtrStr("resetzoom")
MenuItemRoleSelectAll = PtrStr("selectall")
MenuItemRoleToggleDevTools = PtrStr("toggledevtools")
MenuItemRoleToggleFullScreen = PtrStr("togglefullscreen")
MenuItemRoleUndo = PtrStr("undo")
MenuItemRoleWindowMenu = PtrStr("windowMenu")
MenuItemRoleZoomOut = PtrStr("zoomout")
MenuItemRoleZoomIn = PtrStr("zoomin")
// MacOSX
MenuItemRoleAbout = PtrStr("about")
MenuItemRoleHide = PtrStr("hide")
MenuItemRoleHideOthers = PtrStr("hideothers")
MenuItemRoleUnhide = PtrStr("unhide")
MenuItemRoleStartSpeaking = PtrStr("startspeaking")
MenuItemRoleStopSpeaking = PtrStr("stopspeaking")
MenuItemRoleFront = PtrStr("front")
MenuItemRoleZoom = PtrStr("zoom")
MenuItemRoleWindow = PtrStr("window")
MenuItemRoleHelp = PtrStr("help")
MenuItemRoleServices = PtrStr("services")
)
// Menu item types
var (
MenuItemTypeNormal = PtrStr("normal")
MenuItemTypeSeparator = PtrStr("separator")
MenuItemTypeCheckbox = PtrStr("checkbox")
MenuItemTypeRadio = PtrStr("radio")
)
// MenuItem represents a menu item
type MenuItem struct {
*object
o *MenuItemOptions
// We must store the root ID since everytime we update a sub menu we need to set the root menu all over again in electron
rootID string
s *SubMenu
}
// MenuItemOptions represents menu item options
// We must use pointers since GO doesn't handle optional fields whereas NodeJS does. Use PtrBool, PtrInt or PtrStr
// to fill the struct
// https://github.com/electron/electron/blob/v1.8.1/docs/api/menu-item.md
type MenuItemOptions struct {
Accelerator *Accelerator `json:"accelerator,omitempty"`
Checked *bool `json:"checked,omitempty"`
Enabled *bool `json:"enabled,omitempty"`
Icon *string `json:"icon,omitempty"`
Label *string `json:"label,omitempty"`
OnClick Listener `json:"-"`
Position *string `json:"position,omitempty"`
Role *string `json:"role,omitempty"`
SubLabel *string `json:"sublabel,omitempty"`
SubMenu []*MenuItemOptions `json:"-"`
Type *string `json:"type,omitempty"`
Visible *bool `json:"visible,omitempty"`
}
// newMenu creates a new menu item
func newMenuItem(parentCtx context.Context, rootID string, o *MenuItemOptions, c *asticontext.Canceller, d *dispatcher, i *identifier, w *writer) (m *MenuItem) {
m = &MenuItem{
o: o,
object: newObject(parentCtx, c, d, i, w, i.new()),
rootID: rootID,
}
if o.OnClick != nil {
m.On(EventNameMenuItemEventClicked, o.OnClick)
}
if len(o.SubMenu) > 0 {
m.s = &SubMenu{newSubMenu(parentCtx, rootID, o.SubMenu, c, d, i, w)}
}
return
}
// toEvent returns the menu item in the proper event format
func (i *MenuItem) toEvent() (e *EventMenuItem) {
e = &EventMenuItem{
ID: i.id,
Options: i.o,
RootID: i.rootID,
}
if i.s != nil {
e.SubMenu = i.s.toEvent()
}
return
}
// SubMenu returns the menu item sub menu
func (i *MenuItem) SubMenu() *SubMenu {
return i.s
}
// SetChecked sets the checked attribute
func (i *MenuItem) SetChecked(checked bool) (err error) {
if err = i.isActionable(); err != nil {
return
}
i.o.Checked = PtrBool(checked)
_, err = synchronousEvent(i.c, i, i.w, Event{Name: EventNameMenuItemCmdSetChecked, TargetID: i.id, MenuItemOptions: &MenuItemOptions{Checked: i.o.Checked}}, EventNameMenuItemEventCheckedSet)
return
}
// SetEnabled sets the enabled attribute
func (i *MenuItem) SetEnabled(enabled bool) (err error) {
if err = i.isActionable(); err != nil {
return
}
i.o.Enabled = PtrBool(enabled)
_, err = synchronousEvent(i.c, i, i.w, Event{Name: EventNameMenuItemCmdSetEnabled, TargetID: i.id, MenuItemOptions: &MenuItemOptions{Enabled: i.o.Enabled}}, EventNameMenuItemEventEnabledSet)
return
}
// SetLabel sets the label attribute
func (i *MenuItem) SetLabel(label string) (err error) {
if err = i.isActionable(); err != nil {
return
}
i.o.Label = PtrStr(label)
_, err = synchronousEvent(i.c, i, i.w, Event{Name: EventNameMenuItemCmdSetLabel, TargetID: i.id, MenuItemOptions: &MenuItemOptions{Label: i.o.Label}}, EventNameMenuItemEventLabelSet)
return
}
// SetVisible sets the visible attribute
func (i *MenuItem) SetVisible(visible bool) (err error) {
if err = i.isActionable(); err != nil {
return
}
i.o.Visible = PtrBool(visible)
_, err = synchronousEvent(i.c, i, i.w, Event{Name: EventNameMenuItemCmdSetVisible, TargetID: i.id, MenuItemOptions: &MenuItemOptions{Visible: i.o.Visible}}, EventNameMenuItemEventVisibleSet)
return
}