/
QuickSwitchAcceleratorHandler.js
277 lines (244 loc) · 8.89 KB
/
QuickSwitchAcceleratorHandler.js
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
import { EventEmitter } from 'events'
import WaveboxAppCommandKeyTracker from './WaveboxAppCommandKeyTracker'
const privNextAccelerator = Symbol('privNextAccelerator')
const privPrevAccelerator = Symbol('privPrevAccelerator')
const privNextCommandKeys = Symbol('privNextCommandKeys')
const privPrevCommandKeys = Symbol('privPrevCommandKeys')
const privInLongPress = Symbol('privInLongPress')
const privPresentOptionsTO = Symbol('privPresentOptionsTO')
const privSentPresentOptions = Symbol('privSentPresentOptions')
const privDirection = Symbol('privDirection')
const DIRECTIONS = {
NONE: 'NONE',
NEXT: 'NEXT',
PREV: 'PREV'
}
const COMMAND_KEYS = {
META: 'META',
CONTROL: 'CONTROL',
ALT: 'ALT',
SHIFT: 'SHIFT'
}
const ACCELERATOR_TO_COMMAND_KEY = {
CMD: COMMAND_KEYS.META,
COMMAND: COMMAND_KEYS.META,
CTRL: COMMAND_KEYS.CONTROL,
CONTROL: COMMAND_KEYS.CONTROL,
ALT: COMMAND_KEYS.ALT,
ALTGR: COMMAND_KEYS.ALT,
OPTION: COMMAND_KEYS.ALT,
SHIFT: COMMAND_KEYS.SHIFT,
SUPER: COMMAND_KEYS.META,
COMMANDORCONTROL: process.platform === 'darwin' ? COMMAND_KEYS.META : COMMAND_KEYS.CONTROL,
CMDORCTRL: process.platform === 'darwin' ? COMMAND_KEYS.META : COMMAND_KEYS.CONTROL
}
class QuickSwitchAcceleratorHandler extends EventEmitter {
/* ****************************************************************************/
// Lifecycle
/* ****************************************************************************/
/**
* @param nextAccelerator: the next accelerator string
* @param prevAccelerator: the previous accelerator string
*/
constructor (nextAccelerator, prevAccelerator) {
super()
this[privInLongPress] = false
this[privPresentOptionsTO] = 0
this[privDirection] = DIRECTIONS.NONE
this[privNextAccelerator] = nextAccelerator
this[privNextCommandKeys] = this._parseCommandKeys(this[privNextAccelerator])
this[privPrevAccelerator] = prevAccelerator
this[privPrevCommandKeys] = this._parseCommandKeys(this[privPrevAccelerator])
}
/* ****************************************************************************/
// Data Lifecycle
/* ****************************************************************************/
/**
* Changes the accelerator
* @param nextAccelerator: the new next accelerator string
* @param prevAccelerator: the new prev accelerator string
*/
changeAccelerator (nextAccelerator, prevAccelerator) {
if (this[privNextAccelerator] !== nextAccelerator) {
this[privNextAccelerator] = nextAccelerator
this[privNextCommandKeys] = this._parseCommandKeys(this[privNextAccelerator])
this[privInLongPress] = false
}
if (this[privPrevAccelerator] !== prevAccelerator) {
this[privPrevAccelerator] = prevAccelerator
this[privPrevCommandKeys] = this._parseCommandKeys(this[privPrevAccelerator])
this[privInLongPress] = false
}
}
/* ****************************************************************************/
// Key Lifecycle
/* ****************************************************************************/
/**
* Handles the command keys changing by checking if we exit the repeat cycle
*/
_handleCommandKeysChanged = (evt, prev, next) => {
if (!this._commandKeysArePressed()) {
WaveboxAppCommandKeyTracker.removeListener('changed', this._handleCommandKeysChanged)
this[privInLongPress] = false
clearTimeout(this[privPresentOptionsTO])
if (this[privSentPresentOptions]) {
this.emit('select-option', {})
} else {
this._emitFastSwitchEvent()
}
}
}
/* ****************************************************************************/
// Key Utils
/* ****************************************************************************/
/**
* Parses the command keys from an accelerator
* @param accel: the accelerator to parse
* @return a list of command keys
*/
_parseCommandKeys (accel) {
if (typeof (accel) !== 'string') { return [] }
const keys = new Set(accel
.toUpperCase()
.split('+')
.map((k) => ACCELERATOR_TO_COMMAND_KEY[k])
.filter((k) => !!k)
)
return Array.from(keys)
}
/**
* Checks if a command key is pressed
* @param key: the key to press
* @return true if it's pressed, false otherwise
*/
_commandKeyIsPressed (key) {
switch (key) {
case COMMAND_KEYS.META: return WaveboxAppCommandKeyTracker.metaPressed
case COMMAND_KEYS.CONTROL: return WaveboxAppCommandKeyTracker.controlPressed
case COMMAND_KEYS.ALT: return WaveboxAppCommandKeyTracker.altPressed
case COMMAND_KEYS.SHIFT: return WaveboxAppCommandKeyTracker.shiftPressed
default: return false
}
}
/**
* Checks if the command keys are pressed
* @return true if the command keys are pressed
*/
_commandKeysArePressed () {
// Look for the intersection of keys between the two accelerators
const commonCommandKeys = Array.from(
[].concat(
this[privNextCommandKeys],
this[privPrevCommandKeys]
).reduce((acc, k) => {
acc.set(k, (acc.get(k) || 0) + 1)
return acc
}, new Map()).entries()
).filter((ent) => ent[1] > 1).map((ent) => ent[0])
if (commonCommandKeys.length) {
// If there are common keys, we expect the common ones to always be pressed
// For example Ctrl+Tab & Ctrl+Shift+Tab we expect Ctrl to always be pressed
const notPressed = commonCommandKeys.find((k) => !this._commandKeyIsPressed(k))
return notPressed === undefined
} else {
// If there are no common keys, we expect one of the control keys to always
// be pressed. For example Ctrl+Tab & Alt+Tab we expect either Ctrl or Alt to
// always be pressed
const nextNotPressed = this[privNextCommandKeys].find((k) => !this._commandKeyIsPressed(k))
if (nextNotPressed === undefined) { return true }
const prevNotPressed = this[privPrevCommandKeys].find((k) => !this._commandKeyIsPressed(k))
if (prevNotPressed === undefined) { return true }
return false
}
}
/**
* Calculates a rough complexity metric for the user needing to release the command keys
* @return a timeout that can be waited for before bringing up up
*/
_commandKeyReleaseComplexityTime () {
if (this[privDirection] === DIRECTIONS.NEXT) {
return 200 + ((this[privNextCommandKeys].length - 1) * 50)
} else if (this[privDirection] === DIRECTIONS.PREV) {
return 200 + ((this[privPrevCommandKeys].length - 1) * 50)
} else {
return 200
}
}
/* ****************************************************************************/
// Event Utils
/* ****************************************************************************/
/**
* Emits the present options event if it's not been emitted already
* @return true if it was emitted, false if not
*/
_emitPresentOptionsEvent () {
if (this[privSentPresentOptions]) { return false }
this[privSentPresentOptions] = true
if (this[privDirection] === DIRECTIONS.NEXT) {
this.emit('present-options-next', {})
} else if (this[privDirection] === DIRECTIONS.PREV) {
this.emit('present-options-prev', {})
}
return true
}
/**
* Emits the change option event depending on the current direction
*/
_emitChangeOptionEvent () {
if (this[privDirection] === DIRECTIONS.NEXT) {
this.emit('next-option', {})
} else if (this[privDirection] === DIRECTIONS.PREV) {
this.emit('prev-option', {})
}
}
/**
* Emits the fast switch event
*/
_emitFastSwitchEvent () {
if (this[privDirection] === DIRECTIONS.NEXT) {
this.emit('fast-switch-next', {})
} else if (this[privDirection] === DIRECTIONS.PREV) {
this.emit('fast-switch-prev', {})
}
}
/* ****************************************************************************/
// Firing
/* ****************************************************************************/
/**
* Indicates the next accelerator fired
*/
nextAcceleratorFired = () => {
this[privDirection] = DIRECTIONS.NEXT
this._acceleratorFired()
}
/**
* Indicates the prev accelerator fired
*/
prevAcceleratorFired = () => {
this[privDirection] = DIRECTIONS.PREV
this._acceleratorFired()
}
/**
* Indicates the accelerator fired
* @param next: true if the direction is next
*/
_acceleratorFired () {
clearTimeout(this[privPresentOptionsTO])
if (this[privInLongPress] && this._commandKeysArePressed()) {
this._emitPresentOptionsEvent()
this._emitChangeOptionEvent()
} else {
if (this._commandKeysArePressed()) {
this[privInLongPress] = true
this[privSentPresentOptions] = false
WaveboxAppCommandKeyTracker.on('changed', this._handleCommandKeysChanged)
this[privPresentOptionsTO] = setTimeout(() => {
this._emitPresentOptionsEvent()
}, this._commandKeyReleaseComplexityTime())
} else {
this._emitFastSwitchEvent()
}
}
}
}
export default QuickSwitchAcceleratorHandler