forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortcut.go
85 lines (69 loc) · 1.91 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
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
package fyne
import (
"sync"
)
// ShortcutHandler is a default implementation of the shortcut handler
// for the canvasObject
type ShortcutHandler struct {
mu sync.RWMutex
entry map[string]func(Shortcut)
}
// TypedShortcut handle the registered shortcut
func (sh *ShortcutHandler) TypedShortcut(shortcut Shortcut) {
if _, ok := sh.entry[shortcut.ShortcutName()]; !ok {
return
}
sh.entry[shortcut.ShortcutName()](shortcut)
}
// AddShortcut register an handler to be executed when the shortcut action is triggered
func (sh *ShortcutHandler) AddShortcut(shortcut Shortcut, handler func(shortcut Shortcut)) {
sh.mu.Lock()
defer sh.mu.Unlock()
if sh.entry == nil {
sh.entry = make(map[string]func(Shortcut))
}
sh.entry[shortcut.ShortcutName()] = handler
}
// RemoveShortcut removes a registered shortcut
func (sh *ShortcutHandler) RemoveShortcut(shortcut Shortcut) {
sh.mu.Lock()
defer sh.mu.Unlock()
if sh.entry == nil {
return
}
delete(sh.entry, shortcut.ShortcutName())
}
// Shortcut is the interface used to describe a shortcut action
type Shortcut interface {
ShortcutName() string
}
// ShortcutPaste describes a shortcut paste action.
type ShortcutPaste struct {
Clipboard Clipboard
}
// ShortcutName returns the shortcut name
func (se *ShortcutPaste) ShortcutName() string {
return "Paste"
}
// ShortcutCopy describes a shortcut copy action.
type ShortcutCopy struct {
Clipboard Clipboard
}
// ShortcutName returns the shortcut name
func (se *ShortcutCopy) ShortcutName() string {
return "Copy"
}
// ShortcutCut describes a shortcut cut action.
type ShortcutCut struct {
Clipboard Clipboard
}
// ShortcutName returns the shortcut name
func (se *ShortcutCut) ShortcutName() string {
return "Cut"
}
// ShortcutSelectAll describes a shortcut selectAll action.
type ShortcutSelectAll struct{}
// ShortcutName returns the shortcut name
func (se *ShortcutSelectAll) ShortcutName() string {
return "SelectAll"
}