Repository navigation
Expand file tree
/
Copy pathmenuitem.go
More file actions
543 lines (445 loc) · 13.3 KB
/
Copy pathmenuitem.go
File metadata and controls
543 lines (445 loc) · 13.3 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
package tray
import (
"bytes"
"errors"
"fmt"
"image"
"image/png"
"iter"
"maps"
"slices"
"sync"
"deedles.dev/tray/internal/set"
)
// MenuItem is a single item in the menu of the tray item. The item
// can be a regular single item or can be a sub-menu containing more
// items, recursively.
//
// MenuItems are created by calling the AddChild method on either the
// [Menu] or on another MenuItem.
type MenuItem struct {
menu *Menu
id int
parent int
m sync.RWMutex
props map[string]any
children []int
handler MenuEventHandler
}
func (menu *Menu) newItem(parent int) *MenuItem {
menu.id++
item := MenuItem{
menu: menu,
id: menu.id,
parent: parent,
props: make(map[string]any),
}
menu.nodes[item.id] = &item
return &item
}
// AddChild creates a new MenuItem with the given properties and
// appends it as the last child of the root of the menu hierarchy.
func (menu *Menu) AddChild(props ...MenuItemProp) (*MenuItem, error) {
defer menu.lock()()
child := menu.newItem(0)
defer child.lock()()
dirty, errs := child.applyProps(props)
errs = append(errs, menu.updateLayout(menu))
errs = append(errs, child.emitPropertiesUpdated(dirty))
menu.setChildren(append(menu.children, child.id))
return child, errors.Join(errs...)
}
// AddChild creates a new MenuItem with the given properties and
// append it as the last child of the MenuItem that it is called on.
// If the receiver MenuItem has no children before this, it will
// automatically be converted into a sub-menu MenuItem.
func (item *MenuItem) AddChild(props ...MenuItemProp) (*MenuItem, error) {
defer item.menu.lock()()
defer item.lock()()
child := item.menu.newItem(item.id)
defer child.lock()()
dirty, errs := child.applyProps(props)
errs = append(errs, item.menu.updateLayout(item))
errs = append(errs, item.emitPropertiesUpdated(dirty))
item.setChildren(append(item.children, child.id))
return child, errors.Join(errs...)
}
// Remove removes item from the menu hierarchy completely. If its
// parent is another MenuItem and item is its only child, the parent
// is converted from a sub-menu item back into a regular one.
func (item *MenuItem) Remove() error {
parent := item.getParent()
if parent == nil {
return nil
}
defer parent.lock()()
parent.setChildren(sliceRemove(parent.getChildren(), item.id))
if parent != item.menu {
defer item.menu.lock()()
}
delete(item.menu.nodes, item.id)
return item.menu.updateLayout(parent)
}
func appendChild(menu *Menu, dst menuNode, child *MenuItem) error {
parent := child.getParent()
if parent == nil {
// TODO: Allow appending children who have previously been removed?
return nil
}
defer dst.lock()()
if parent != dst {
defer parent.lock()()
}
parent.setChildren(sliceRemove(parent.getChildren(), child.id))
dst.setChildren(append(dst.getChildren(), child.id))
child.parent = dst.getID()
if dst != menu && parent != menu {
defer menu.lock()()
}
updates := slices.Compact([]menuNode{dst, parent})
return menu.updateLayout(updates...)
}
// AppendChild makes child the last child of the root of the menu
// hiearchy, moving it from wherever it currently is. The same rules
// about sub-menu conversion apply as they do in both [AddChild] and
// [Remove].
func (menu *Menu) AppendChild(child *MenuItem) error {
return appendChild(menu, menu, child)
}
// AppendChild makes child the last child of item, moving it from
// wherever it currently is. The same rules about sub-menu conversion
// apply as they do in both [AddChild] and [Remove].
func (item *MenuItem) AppendChild(child *MenuItem) error {
return appendChild(item.menu, item, child)
}
// MoveBefore makes item the previous sibling of sibling. If
// necessary, this method will transfer item from its current parent
// to sibling's parent.
func (item *MenuItem) MoveBefore(sibling *MenuItem) error {
dst := sibling.getParent()
src := item.getParent()
if dst == nil || src == nil {
// TODO: Allow moving children who have previously been removed?
return nil
}
defer dst.lock()()
if dst != src {
defer src.lock()()
}
dc := dst.getChildren()
i := slices.Index(dc, sibling.id)
if i < 0 {
i = len(dc)
}
src.setChildren(sliceRemove(src.getChildren(), item.id))
dst.setChildren(slices.Insert(dc, i, item.id))
item.parent = dst.getID()
if dst != item.menu && src != item.menu {
defer item.menu.lock()()
}
updates := slices.Compact([]menuNode{dst, src})
return item.menu.updateLayout(updates...)
}
func (item *MenuItem) applyProps(props []MenuItemProp) (iter.Seq[string], []error) {
w := menuItemProps{MenuItem: item, dirty: make(set.Set[string])}
for _, p := range props {
p(&w)
}
return maps.Keys(w.dirty), w.errs
}
func (item *MenuItem) emitPropertiesUpdated(props iter.Seq[string]) error {
type prop struct {
Name string
Value any
}
type updatedProps struct {
ID int
Props []prop
}
type removedProps struct {
ID int
Props []string
}
var updated []prop
for change := range props {
updated = append(updated, prop{
Name: change,
Value: item.props[change],
})
}
item.menu.dirty.Add(item.parent)
return item.menu.item.conn.Emit(
menuPath,
"com.canonical.dbusmenu.ItemsPropertiesUpdated",
[]updatedProps{{
ID: item.id,
Props: updated,
}},
[]removedProps(nil),
)
}
// RequestActivation sends a request to the environment that item be
// activated. What exactly this means is dependent on the environment
// and situation.
func (item *MenuItem) RequestActivation(timestamp uint32) error {
return item.menu.item.conn.Emit(
menuPath,
"com.canonical.dbusmenu.ItemActivationRequested",
item.id,
timestamp,
)
}
// Type returns the current value of the item's "type" property,
func (item *MenuItem) Type() MenuType {
item.m.RLock()
defer item.m.RUnlock()
return mapLookup(item.props, "type", MenuType("standard"))
}
// Label returns the current value of the item's "label" property.
// This is the text that should be displayed by the environment for
// the item in the menu.
func (item *MenuItem) Label() string {
item.m.RLock()
defer item.m.RUnlock()
return mapLookup(item.props, "label", "")
}
// Enabled returns the current value of the item's "enabled" property.
func (item *MenuItem) Enabled() bool {
item.m.RLock()
defer item.m.RUnlock()
return mapLookup(item.props, "enabled", true)
}
// Visible returns the current value of the item's "visible" property.
func (item *MenuItem) Visible() bool {
item.m.RLock()
defer item.m.RUnlock()
return mapLookup(item.props, "visible", true)
}
// IconName returns the current value of the item's "icon-name"
// property. This is one of two properties that determine what icon
// should be displayed in the menu by the environment, the other being
// "icon-data".
func (item *MenuItem) IconName() string {
item.m.RLock()
defer item.m.RUnlock()
return mapLookup(item.props, "icon-name", "")
}
// IconData returns the current value of the item's "icon-data"
// property. This is one of two properties that determine what icon
// should be displayed in the menu by the environment, the other being
// "icon-name".
func (item *MenuItem) IconData() (image.Image, error) {
item.m.RLock()
defer item.m.RUnlock()
data, ok := item.props["icon-data"].([]byte)
if !ok {
return nil, nil
}
return png.Decode(bytes.NewReader(data))
}
// Shortcut returns the current value of the item's "shortcut"
// property. This is used by the environment to determine keyboard
// shortcuts and is of the form
//
// [][]string{
// []string{"Control", "Alt", "e"},
// }
func (item *MenuItem) Shortcut() [][]string {
item.m.RLock()
defer item.m.RUnlock()
return mapLookup(item.props, "shortcut", [][]string(nil))
}
// ToggleType returns the current value of the item's "toggle-type"
// property.
func (item *MenuItem) ToggleType() MenuToggleType {
item.m.RLock()
defer item.m.RUnlock()
return mapLookup(item.props, "toggle-type", MenuToggleType(""))
}
// ToggleState returns the current value of the item's "toggle-state"
// property.
func (item *MenuItem) ToggleState() MenuToggleState {
item.m.RLock()
defer item.m.RUnlock()
return mapLookup(item.props, "toggle-state", MenuToggleState(-1))
}
// VendorProp returns the current value of the vendor-specific custom
// property with the given vendor and property name. It returns false
// as its second return if no such property exists.
func (item *MenuItem) VendorProp(vendor, prop string) (any, bool) {
item.m.RLock()
defer item.m.RUnlock()
v, ok := item.props[vendorPropName(vendor, prop)]
return v, ok
}
// SetProps sets all of the given properties on the item.
func (item *MenuItem) SetProps(props ...MenuItemProp) error {
defer item.lock()()
parent := item.getParent()
defer item.menu.lock()()
dirty, errs := item.applyProps(props)
errs = append(errs, item.emitPropertiesUpdated(dirty))
errs = append(errs, item.menu.updateLayout(parent))
return errors.Join(errs...)
}
// MenuType is the possible values of the type of a menu item.
type MenuType string
const (
Standard MenuType = "standard"
Separator MenuType = "separator"
)
// MenuToggleType is the types of togglability that a menu item can
// have.
type MenuToggleType string
const (
NonToggleable MenuToggleType = ""
Checkmark MenuToggleType = "checkmark"
Radio MenuToggleType = "radio"
)
// MenuToggleState is the two main states that a togglable menu item can be in. All values other than [On] and [Off] are considered to be indeterminate.
type MenuToggleState int
const (
Off MenuToggleState = 0
On MenuToggleState = 1
)
// Indeterminate returns true if state represents an indeterminate state other than [On] and [Off].
func (state MenuToggleState) Indeterminate() bool {
return state != Off && state != On
}
type MenuItemProp func(*menuItemProps)
type menuItemProps struct {
*MenuItem
dirty set.Set[string]
errs []error
}
func (item *menuItemProps) mark(change string) {
item.dirty.Add(change)
}
func (item *menuItemProps) catch(err error) {
item.errs = append(item.errs, err)
}
// MenuItemType sets a MenuItem's "type" property. See
// [MenuItem.Type].
func MenuItemType(t MenuType) MenuItemProp {
return func(item *menuItemProps) {
item.props["type"] = t
}
}
// MenuItemLabel sets a MenuItem's "label" property. See
// [MenuItem.Label].
func MenuItemLabel(label string) MenuItemProp {
return func(item *menuItemProps) {
item.props["label"] = label
item.mark("label")
}
}
// MenuItemEnabled sets a MenuItem's "enabled" property. See
// [MenuItem.Enabled].
func MenuItemEnabled(enabled bool) MenuItemProp {
return func(item *menuItemProps) {
item.props["enabled"] = enabled
item.mark("enabled")
}
}
// MenuItemVisible sets a MenuItem's "visible" property. See
// [MenuItem.Visible].
func MenuItemVisible(visible bool) MenuItemProp {
return func(item *menuItemProps) {
item.props["visible"] = visible
item.mark("visible")
}
}
// MenuItemIconName sets a MenuItem's "icon-name" property. See
// [MenuItem.IconName].
func MenuItemIconName(name string) MenuItemProp {
return func(item *menuItemProps) {
item.props["icon-name"] = name
item.mark("icon-name")
}
}
// MenuItemIconData sets a MenuItem's "icon-data" property. See
// [MenuItem.IconData].
func MenuItemIconData(img image.Image) MenuItemProp {
return func(item *menuItemProps) {
var buf bytes.Buffer
err := png.Encode(&buf, img)
if err != nil {
item.catch(err)
return
}
item.props["icon-data"] = buf.Bytes()
item.mark("icon-data")
}
}
// MenuItemShortcut sets a MenuItem's "shortcut" property. See
// [MenuItem.Shortcut].
func MenuItemShortcut(shortcut [][]string) MenuItemProp {
return func(item *menuItemProps) {
item.props["shortcut"] = shortcut
item.mark("shortcut")
}
}
// MenuItemToggleType sets a MenuItem's "toggle-type" property. See
// [MenuItem.ToggleType].
func MenuItemToggleType(t MenuToggleType) MenuItemProp {
return func(item *menuItemProps) {
item.props["toggle-type"] = t
item.mark("toggle-type")
}
}
// MenuItemToggleState sets a MenuItem's "toggle-state" property. See
// [MenuItem.ToggleState].
func MenuItemToggleState(state MenuToggleState) MenuItemProp {
return func(item *menuItemProps) {
item.props["toggle-state"] = state
item.mark("toggle-state")
}
}
// MenuItemVendorProp sets a vendor-specific custom property with the
// given vendor and property name. See [MenuItem.VendorProp].
func MenuItemVendorProp(vendor, prop string, value any) MenuItemProp {
return func(item *menuItemProps) {
name := vendorPropName(vendor, prop)
item.props[name] = value
item.mark(name)
}
}
// MenuItemHandler sets the event handler for a MenuItem.
func MenuItemHandler(handler MenuEventHandler) MenuItemProp {
return func(item *menuItemProps) {
item.handler = handler
}
}
func vendorPropName(vendor, prop string) string {
return fmt.Sprintf("x-%v-%v", vendor, prop)
}
func (item *MenuItem) getParent() menuNode {
if item.parent == 0 {
return item.menu
}
item.menu.m.RLock()
defer item.menu.m.RUnlock()
p, ok := item.menu.nodes[item.parent]
if !ok {
return nil
}
return p
}
func (item *MenuItem) lock() func() {
item.m.Lock()
return func() { item.m.Unlock() }
}
func (item *MenuItem) getID() int {
return item.id
}
func (item *MenuItem) getChildren() []int {
return item.children
}
func (item *MenuItem) setChildren(c []int) {
item.children = c
if len(item.children) == 0 {
item.props["children-display"] = ""
return
}
item.props["children-display"] = "submenu"
}