This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 342
/
Copy pathsub_menu.go
175 lines (157 loc) · 5.05 KB
/
sub_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
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
169
170
171
172
173
174
175
package astilectron
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/asticode/go-astikit"
)
// Sub menu event names
const (
EventNameSubMenuCmdAppend = "sub.menu.cmd.append"
EventNameSubMenuCmdClosePopup = "sub.menu.cmd.close.popup"
EventNameSubMenuCmdInsert = "sub.menu.cmd.insert"
EventNameSubMenuCmdPopup = "sub.menu.cmd.popup"
EventNameSubMenuEventAppended = "sub.menu.event.appended"
EventNameSubMenuEventClosedPopup = "sub.menu.event.closed.popup"
EventNameSubMenuEventInserted = "sub.menu.event.inserted"
EventNameSubMenuEventPoppedUp = "sub.menu.event.popped.up"
)
// SubMenu represents an exported sub menu
type SubMenu struct {
*subMenu
}
// subMenu represents an internal sub menu
// We use this internal subMenu in SubMenu and Menu since all functions of subMenu should be in SubMenu and Menu but
// some functions of Menu shouldn't be in SubMenu and vice versa
type subMenu struct {
*object
items []*MenuItem
// 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
}
// newSubMenu creates a new sub menu
func newSubMenu(ctx context.Context, rootID string, items []*MenuItemOptions, d *dispatcher, i *identifier, w *writer) *subMenu {
// Init
var m = &subMenu{
object: newObject(ctx, d, i, w, i.new()),
rootID: rootID,
}
// Parse items
for _, o := range items {
m.items = append(m.items, newMenuItem(m.ctx, rootID, o, d, i, w))
}
return m
}
// toEvent returns the sub menu in the proper event format
func (m *subMenu) toEvent() (e *EventSubMenu) {
e = &EventSubMenu{
ID: m.id,
RootID: m.rootID,
}
for _, i := range m.items {
e.Items = append(e.Items, i.toEvent())
}
return
}
// NewItem returns a new menu item
func (m *subMenu) NewItem(o *MenuItemOptions) *MenuItem {
return newMenuItem(m.ctx, m.rootID, o, m.d, m.i, m.w)
}
// SubMenu returns the sub menu at the specified indexes
func (m *subMenu) SubMenu(indexes ...int) (s *SubMenu, err error) {
var is = m
var processedIndexes = []string{}
for _, index := range indexes {
if index >= len(is.items) {
return nil, fmt.Errorf("submenu at %s has %d items, invalid index %d", strings.Join(processedIndexes, ":"), len(is.items), index)
}
s = is.items[index].s
processedIndexes = append(processedIndexes, strconv.Itoa(index))
if s == nil {
return nil, fmt.Errorf("no submenu at %s", strings.Join(processedIndexes, ":"))
}
is = s.subMenu
}
return
}
// Item returns the item at the specified indexes
func (m *subMenu) Item(indexes ...int) (mi *MenuItem, err error) {
var is = m
if len(indexes) > 1 {
var s *SubMenu
if s, err = m.SubMenu(indexes[:len(indexes)-1]...); err != nil {
return
}
is = s.subMenu
}
var index = indexes[len(indexes)-1]
if index >= len(is.items) {
return nil, fmt.Errorf("submenu has %d items, invalid index %d", len(is.items), index)
}
mi = is.items[index]
return
}
// Append appends a menu item into the sub menu
func (m *subMenu) Append(i *MenuItem) (err error) {
if err = m.ctx.Err(); err != nil {
return
}
if _, err = synchronousEvent(m.ctx, m, m.w, Event{Name: EventNameSubMenuCmdAppend, TargetID: m.id, MenuItem: i.toEvent()}, EventNameSubMenuEventAppended); err != nil {
return
}
m.items = append(m.items, i)
return
}
// Insert inserts a menu item to the position of the sub menu
func (m *subMenu) Insert(pos int, i *MenuItem) (err error) {
if err = m.ctx.Err(); err != nil {
return
}
if pos > len(m.items) {
err = fmt.Errorf("submenu has %d items, position %d is invalid", len(m.items), pos)
return
}
if _, err = synchronousEvent(m.ctx, m, m.w, Event{Name: EventNameSubMenuCmdInsert, TargetID: m.id, MenuItem: i.toEvent(), MenuItemPosition: astikit.IntPtr(pos)}, EventNameSubMenuEventInserted); err != nil {
return
}
m.items = append(m.items[:pos], append([]*MenuItem{i}, m.items[pos:]...)...)
return
}
// MenuPopupOptions represents menu pop options
type MenuPopupOptions struct {
PositionOptions
PositioningItem *int `json:"positioningItem,omitempty"`
}
// Popup pops up the menu as a context menu in the focused window
func (m *subMenu) Popup(o *MenuPopupOptions) error {
return m.PopupInWindow(nil, o)
}
// PopupInWindow pops up the menu as a context menu in the specified window
func (m *subMenu) PopupInWindow(w *Window, o *MenuPopupOptions) (err error) {
if err = m.ctx.Err(); err != nil {
return
}
var e = Event{Name: EventNameSubMenuCmdPopup, TargetID: m.id, MenuPopupOptions: o}
if w != nil {
e.WindowID = w.id
}
_, err = synchronousEvent(m.ctx, m, m.w, e, EventNameSubMenuEventPoppedUp)
return
}
// ClosePopup close the context menu in the focused window
func (m *subMenu) ClosePopup() error {
return m.ClosePopupInWindow(nil)
}
// ClosePopupInWindow close the context menu in the specified window
func (m *subMenu) ClosePopupInWindow(w *Window) (err error) {
if err = m.ctx.Err(); err != nil {
return
}
var e = Event{Name: EventNameSubMenuCmdClosePopup, TargetID: m.id}
if w != nil {
e.WindowID = w.id
}
_, err = synchronousEvent(m.ctx, m, m.w, e, EventNameSubMenuEventClosedPopup)
return
}