forked from asticode/go-astilectron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.go
57 lines (48 loc) · 1.55 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
package astilectron
import (
"context"
"github.com/asticode/go-astitools/context"
)
// Menu event names
const (
EventNameMenuCmdCreate = "menu.cmd.create"
EventNameMenuCmdDestroy = "menu.cmd.destroy"
EventNameMenuEventCreated = "menu.event.created"
EventNameMenuEventDestroyed = "menu.event.destroyed"
)
// Menu represents a menu
// https://github.com/electron/electron/blob/v1.8.1/docs/api/menu.md
type Menu struct {
*subMenu
}
// newMenu creates a new menu
func newMenu(parentCtx context.Context, rootID string, items []*MenuItemOptions, c *asticontext.Canceller, d *dispatcher, i *identifier, w *writer) (m *Menu) {
// Init
m = &Menu{newSubMenu(parentCtx, rootID, items, c, d, i, w)}
// Make sure the menu's context is cancelled once the destroyed event is received
m.On(EventNameMenuEventDestroyed, func(e Event) (deleteListener bool) {
m.cancel()
return true
})
return
}
// toEvent returns the menu in the proper event format
func (m *Menu) toEvent() *EventMenu {
return &EventMenu{m.subMenu.toEvent()}
}
// Create creates the menu
func (m *Menu) Create() (err error) {
if err = m.isActionable(); err != nil {
return
}
_, err = synchronousEvent(m.c, m, m.w, Event{Name: EventNameMenuCmdCreate, TargetID: m.id, Menu: m.toEvent()}, EventNameMenuEventCreated)
return
}
// Destroy destroys the menu
func (m *Menu) Destroy() (err error) {
if err = m.isActionable(); err != nil {
return
}
_, err = synchronousEvent(m.c, m, m.w, Event{Name: EventNameMenuCmdDestroy, TargetID: m.id, Menu: m.toEvent()}, EventNameMenuEventDestroyed)
return
}