forked from wailsapp/wails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.go
215 lines (189 loc) · 4.48 KB
/
keys.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package application
import (
"fmt"
"runtime"
"slices"
"strconv"
"strings"
)
// modifier is actually a string
type modifier int
const (
// CmdOrCtrlKey represents Command on Mac and Control on other platforms
CmdOrCtrlKey modifier = 0 << iota
// OptionOrAltKey represents Option on Mac and Alt on other platforms
OptionOrAltKey modifier = 1 << iota
// ShiftKey represents the shift key on all systems
ShiftKey modifier = 2 << iota
// SuperKey represents Command on Mac and the Windows key on the other platforms
SuperKey modifier = 3 << iota
// ControlKey represents the control key on all systems
ControlKey modifier = 4 << iota
)
func (m modifier) String() string {
return modifierStringMap[runtime.GOOS][m]
}
var modifierStringMap = map[string]map[modifier]string{
"windows": {
CmdOrCtrlKey: "Ctrl",
ControlKey: "Ctrl",
OptionOrAltKey: "Alt",
ShiftKey: "Shift",
SuperKey: "Win",
},
"darwin": {
CmdOrCtrlKey: "Cmd",
ControlKey: "Ctrl",
OptionOrAltKey: "Option",
ShiftKey: "Shift",
SuperKey: "Cmd",
},
"linux": {
CmdOrCtrlKey: "Ctrl",
ControlKey: "Ctrl",
OptionOrAltKey: "Alt",
ShiftKey: "Shift",
SuperKey: "Super",
},
}
var modifierMap = map[string]modifier{
"cmdorctrl": CmdOrCtrlKey,
"cmd": CmdOrCtrlKey,
"command": CmdOrCtrlKey,
"ctrl": CmdOrCtrlKey,
"optionoralt": OptionOrAltKey,
"alt": OptionOrAltKey,
"option": OptionOrAltKey,
"shift": ShiftKey,
"super": SuperKey,
}
// accelerator holds the keyboard shortcut for a menu item
type accelerator struct {
Key string
Modifiers []modifier
}
func (a *accelerator) String() string {
var result []string
// Sort modifiers
for _, modifier := range a.Modifiers {
result = append(result, modifier.String())
}
slices.Sort(result)
if len(a.Key) > 0 {
result = append(result, a.Key)
}
return strings.ToLower(strings.Join(result, "+"))
}
var namedKeys = map[string]struct{}{
"backspace": {},
"tab": {},
"return": {},
"enter": {},
"escape": {},
"left": {},
"right": {},
"up": {},
"down": {},
"space": {},
"delete": {},
"home": {},
"end": {},
"page up": {},
"page down": {},
"f1": {},
"f2": {},
"f3": {},
"f4": {},
"f5": {},
"f6": {},
"f7": {},
"f8": {},
"f9": {},
"f10": {},
"f11": {},
"f12": {},
"f13": {},
"f14": {},
"f15": {},
"f16": {},
"f17": {},
"f18": {},
"f19": {},
"f20": {},
"f21": {},
"f22": {},
"f23": {},
"f24": {},
"f25": {},
"f26": {},
"f27": {},
"f28": {},
"f29": {},
"f30": {},
"f31": {},
"f32": {},
"f33": {},
"f34": {},
"f35": {},
"numlock": {},
}
func parseKey(key string) (string, bool) {
// Lowercase!
key = strings.ToLower(key)
// Check special case
if key == "plus" {
return "+", true
}
// Handle named keys
_, namedKey := namedKeys[key]
if namedKey {
return key, true
}
// Check we only have a single character
if len(key) != 1 {
return "", false
}
runeKey := rune(key[0])
// This may be too inclusive
if strconv.IsPrint(runeKey) {
return key, true
}
return "", false
}
// parseAccelerator parses a string into an accelerator
func parseAccelerator(shortcut string) (*accelerator, error) {
var result accelerator
// Split the shortcut by +
components := strings.Split(shortcut, "+")
// If we only have one it should be a key
// We require components
if len(components) == 0 {
return nil, fmt.Errorf("no components given to validateComponents")
}
modifiers := map[modifier]struct{}{}
// Check components
for index, component := range components {
// If last component
if index == len(components)-1 {
processedKey, validKey := parseKey(component)
if !validKey {
return nil, fmt.Errorf("'%s' is not a valid key", component)
}
result.Key = processedKey
continue
}
// Not last component - needs to be modifier
lowercaseComponent := strings.ToLower(component)
thisModifier, valid := modifierMap[lowercaseComponent]
if !valid {
return nil, fmt.Errorf("'%s' is not a valid modifier", component)
}
// Save this data
modifiers[thisModifier] = struct{}{}
}
// return the keys as a slice
for thisModifier := range modifiers {
result.Modifiers = append(result.Modifiers, thisModifier)
}
return &result, nil
}