-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
shortcut.go
198 lines (160 loc) · 4.41 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
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
package fyne
import (
"sync"
)
// ShortcutHandler is a default implementation of the shortcut handler
// for [CanvasObject].
type ShortcutHandler struct {
entry sync.Map // map[string]func(Shortcut)
}
// TypedShortcut handle the registered shortcut
func (sh *ShortcutHandler) TypedShortcut(shortcut Shortcut) {
val, ok := sh.entry.Load(shortcut.ShortcutName())
if !ok {
return
}
f := val.(func(Shortcut))
f(shortcut)
}
// AddShortcut register a handler to be executed when the shortcut action is triggered
func (sh *ShortcutHandler) AddShortcut(shortcut Shortcut, handler func(shortcut Shortcut)) {
sh.entry.Store(shortcut.ShortcutName(), handler)
}
// RemoveShortcut removes a registered shortcut
func (sh *ShortcutHandler) RemoveShortcut(shortcut Shortcut) {
sh.entry.Delete(shortcut.ShortcutName())
}
// Shortcut is the interface used to describe a shortcut action
type Shortcut interface {
ShortcutName() string
}
// KeyboardShortcut describes a shortcut meant to be triggered by a keyboard action.
type KeyboardShortcut interface {
Shortcut
Key() KeyName
Mod() KeyModifier
}
// ShortcutPaste describes a shortcut paste action.
type ShortcutPaste struct {
Clipboard Clipboard
}
var _ KeyboardShortcut = (*ShortcutPaste)(nil)
// Key returns the [KeyName] for this shortcut.
//
// Implements: [KeyboardShortcut]
func (se *ShortcutPaste) Key() KeyName {
return KeyV
}
// Mod returns the [KeyModifier] for this shortcut.
//
// Implements: [KeyboardShortcut]
func (se *ShortcutPaste) Mod() KeyModifier {
return KeyModifierShortcutDefault
}
// ShortcutName returns the shortcut name
func (se *ShortcutPaste) ShortcutName() string {
return "Paste"
}
// ShortcutCopy describes a shortcut copy action.
type ShortcutCopy struct {
Clipboard Clipboard
}
var _ KeyboardShortcut = (*ShortcutCopy)(nil)
// Key returns the [KeyName] for this shortcut.
//
// Implements: [KeyboardShortcut]
func (se *ShortcutCopy) Key() KeyName {
return KeyC
}
// Mod returns the [KeyModifier] for this shortcut.
//
// Implements: [KeyboardShortcut]
func (se *ShortcutCopy) Mod() KeyModifier {
return KeyModifierShortcutDefault
}
// ShortcutName returns the shortcut name
func (se *ShortcutCopy) ShortcutName() string {
return "Copy"
}
// ShortcutCut describes a shortcut cut action.
type ShortcutCut struct {
Clipboard Clipboard
}
var _ KeyboardShortcut = (*ShortcutCut)(nil)
// Key returns the [KeyName] for this shortcut.
//
// Implements: [KeyboardShortcut]
func (se *ShortcutCut) Key() KeyName {
return KeyX
}
// Mod returns the [KeyModifier] for this shortcut.
//
// Implements: [KeyboardShortcut]
func (se *ShortcutCut) Mod() KeyModifier {
return KeyModifierShortcutDefault
}
// ShortcutName returns the shortcut name
func (se *ShortcutCut) ShortcutName() string {
return "Cut"
}
// ShortcutSelectAll describes a shortcut selectAll action.
type ShortcutSelectAll struct{}
var _ KeyboardShortcut = (*ShortcutSelectAll)(nil)
// Key returns the [KeyName] for this shortcut.
//
// Implements: [KeyboardShortcut]
func (se *ShortcutSelectAll) Key() KeyName {
return KeyA
}
// Mod returns the [KeyModifier] for this shortcut.
//
// Implements: [KeyboardShortcut]
func (se *ShortcutSelectAll) Mod() KeyModifier {
return KeyModifierShortcutDefault
}
// ShortcutName returns the shortcut name
func (se *ShortcutSelectAll) ShortcutName() string {
return "SelectAll"
}
// ShortcutUndo describes a shortcut undo action.
//
// Since: 2.5
type ShortcutUndo struct{}
var _ KeyboardShortcut = (*ShortcutUndo)(nil)
// Key returns the [KeyName] for this shortcut.
//
// Implements: [KeyboardShortcut]
func (se *ShortcutUndo) Key() KeyName {
return KeyZ
}
// Mod returns the [KeyModifier] for this shortcut.
//
// Implements: [KeyboardShortcut]
func (se *ShortcutUndo) Mod() KeyModifier {
return KeyModifierShortcutDefault
}
// ShortcutName returns the shortcut name
func (se *ShortcutUndo) ShortcutName() string {
return "Undo"
}
// ShortcutRedo describes a shortcut redo action.
//
// Since: 2.5
type ShortcutRedo struct{}
var _ KeyboardShortcut = (*ShortcutRedo)(nil)
// Key returns the [KeyName] for this shortcut.
//
// Implements: [KeyboardShortcut]
func (se *ShortcutRedo) Key() KeyName {
return KeyY
}
// Mod returns the [KeyModifier] for this shortcut.
//
// Implements: [KeyboardShortcut]
func (se *ShortcutRedo) Mod() KeyModifier {
return KeyModifierShortcutDefault
}
// ShortcutName returns the shortcut name
func (se *ShortcutRedo) ShortcutName() string {
return "Redo"
}