forked from FyshOS/fynedesk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortcut.go
58 lines (48 loc) · 1.28 KB
/
shortcut.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
package wm
import (
"sync"
"fyne.io/fyne/v2"
"fyne.io/fynedesk"
)
// ShortcutHandler is a simple implementation for tracking registered shortcuts
type ShortcutHandler struct {
mu sync.RWMutex
entry map[*fynedesk.Shortcut]func()
}
// TypedShortcut handle the registered shortcut
func (sh *ShortcutHandler) TypedShortcut(shortcut fyne.Shortcut) {
var matched func()
for s, f := range sh.entry {
if s.ShortcutName() == shortcut.ShortcutName() {
matched = f
}
}
if matched == nil {
return
}
matched()
}
// AddShortcut register an handler to be executed when the shortcut action is triggered
func (sh *ShortcutHandler) AddShortcut(shortcut *fynedesk.Shortcut, handler func()) {
sh.mu.Lock()
defer sh.mu.Unlock()
if sh.entry == nil {
sh.entry = make(map[*fynedesk.Shortcut]func())
}
sh.entry[shortcut] = handler
}
// Shortcuts returns the list of all registered shortcuts
func (sh *ShortcutHandler) Shortcuts() []*fynedesk.Shortcut {
sh.mu.Lock()
defer sh.mu.Unlock()
var shorts []*fynedesk.Shortcut
for s := range sh.entry {
shorts = append(shorts, s)
}
return shorts
}
// ShortcutManager is an interface that we can use to check for the handler capabilities of a desktop
type ShortcutManager interface {
Shortcuts() []*fynedesk.Shortcut
TypedShortcut(fyne.Shortcut)
}