Debian Codegraph @

node/@xterm/xterm/src/vs/base/common/keybindings.ts

raw
1 /*---------------------------------------------------------------------------------------------
2 * Copyright (c) Microsoft Corporation. All rights reserved.
3 * Licensed under the MIT License. See License.txt in the project root for license information.
4 *--------------------------------------------------------------------------------------------*/
5
6 import { illegalArgument } from 'vs/base/common/errors';
7 import { KeyCode, ScanCode } from 'vs/base/common/keyCodes';
8 import { OperatingSystem } from 'vs/base/common/platform';
9
10 /**
11 * Binary encoding strategy:
12 * ```
13 * 1111 11
14 * 5432 1098 7654 3210
15 * ---- CSAW KKKK KKKK
16 * C = bit 11 = ctrlCmd flag
17 * S = bit 10 = shift flag
18 * A = bit 9 = alt flag
19 * W = bit 8 = winCtrl flag
20 * K = bits 0-7 = key code
21 * ```
22 */
23 const enum BinaryKeybindingsMask {
24 CtrlCmd = (1 << 11) >>> 0,
25 Shift = (1 << 10) >>> 0,
26 Alt = (1 << 9) >>> 0,
27 WinCtrl = (1 << 8) >>> 0,
28 KeyCode = 0x000000FF
29 }
30
31 export function decodeKeybinding(keybinding: number | number[], OS: OperatingSystem): Keybinding | null {
32 if (typeof keybinding === 'number') {
33 if (keybinding === 0) {
34 return null;
35 }
36 const firstChord = (keybinding & 0x0000FFFF) >>> 0;
37 const secondChord = (keybinding & 0xFFFF0000) >>> 16;
38 if (secondChord !== 0) {
39 return new Keybinding([
40 createSimpleKeybinding(firstChord, OS),
41 createSimpleKeybinding(secondChord, OS)
42 ]);
43 }
44 return new Keybinding([createSimpleKeybinding(firstChord, OS)]);
45 } else {
46 const chords = [];
47 for (let i = 0; i < keybinding.length; i++) {
48 chords.push(createSimpleKeybinding(keybinding[i], OS));
49 }
50 return new Keybinding(chords);
51 }
52 }
53
54 export function createSimpleKeybinding(keybinding: number, OS: OperatingSystem): KeyCodeChord {
55
56 const ctrlCmd = (keybinding & BinaryKeybindingsMask.CtrlCmd ? true : false);
57 const winCtrl = (keybinding & BinaryKeybindingsMask.WinCtrl ? true : false);
58
59 const ctrlKey = (OS === OperatingSystem.Macintosh ? winCtrl : ctrlCmd);
60 const shiftKey = (keybinding & BinaryKeybindingsMask.Shift ? true : false);
61 const altKey = (keybinding & BinaryKeybindingsMask.Alt ? true : false);
62 const metaKey = (OS === OperatingSystem.Macintosh ? ctrlCmd : winCtrl);
63 const keyCode = (keybinding & BinaryKeybindingsMask.KeyCode);
64
65 return new KeyCodeChord(ctrlKey, shiftKey, altKey, metaKey, keyCode);
66 }
67
68 export interface Modifiers {
69 readonly ctrlKey: boolean;
70 readonly shiftKey: boolean;
71 readonly altKey: boolean;
72 readonly metaKey: boolean;
73 }
74
75 /**
76 * Represents a chord which uses the `keyCode` field of keyboard events.
77 * A chord is a combination of keys pressed simultaneously.
78 */
79 export class KeyCodeChord implements Modifiers {
80
81 constructor(
82 public readonly ctrlKey: boolean,
83 public readonly shiftKey: boolean,
84 public readonly altKey: boolean,
85 public readonly metaKey: boolean,
86 public readonly keyCode: KeyCode
87 ) { }
88
89 public equals(other: Chord): boolean {
90 return (
91 other instanceof KeyCodeChord
92 && this.ctrlKey === other.ctrlKey
93 && this.shiftKey === other.shiftKey
94 && this.altKey === other.altKey
95 && this.metaKey === other.metaKey
96 && this.keyCode === other.keyCode
97 );
98 }
99
100 public getHashCode(): string {
101 const ctrl = this.ctrlKey ? '1' : '0';
102 const shift = this.shiftKey ? '1' : '0';
103 const alt = this.altKey ? '1' : '0';
104 const meta = this.metaKey ? '1' : '0';
105 return `K${ctrl}${shift}${alt}${meta}${this.keyCode}`;
106 }
107
108 public isModifierKey(): boolean {
109 return (
110 this.keyCode === KeyCode.Unknown
111 || this.keyCode === KeyCode.Ctrl
112 || this.keyCode === KeyCode.Meta
113 || this.keyCode === KeyCode.Alt
114 || this.keyCode === KeyCode.Shift
115 );
116 }
117
118 public toKeybinding(): Keybinding {
119 return new Keybinding([this]);
120 }
121
122 /**
123 * Does this keybinding refer to the key code of a modifier and it also has the modifier flag?
124 */
125 public isDuplicateModifierCase(): boolean {
126 return (
127 (this.ctrlKey && this.keyCode === KeyCode.Ctrl)
128 || (this.shiftKey && this.keyCode === KeyCode.Shift)
129 || (this.altKey && this.keyCode === KeyCode.Alt)
130 || (this.metaKey && this.keyCode === KeyCode.Meta)
131 );
132 }
133 }
134
135 /**
136 * Represents a chord which uses the `code` field of keyboard events.
137 * A chord is a combination of keys pressed simultaneously.
138 */
139 export class ScanCodeChord implements Modifiers {
140
141 constructor(
142 public readonly ctrlKey: boolean,
143 public readonly shiftKey: boolean,
144 public readonly altKey: boolean,
145 public readonly metaKey: boolean,
146 public readonly scanCode: ScanCode
147 ) { }
148
149 public equals(other: Chord): boolean {
150 return (
151 other instanceof ScanCodeChord
152 && this.ctrlKey === other.ctrlKey
153 && this.shiftKey === other.shiftKey
154 && this.altKey === other.altKey
155 && this.metaKey === other.metaKey
156 && this.scanCode === other.scanCode
157 );
158 }
159
160 public getHashCode(): string {
161 const ctrl = this.ctrlKey ? '1' : '0';
162 const shift = this.shiftKey ? '1' : '0';
163 const alt = this.altKey ? '1' : '0';
164 const meta = this.metaKey ? '1' : '0';
165 return `S${ctrl}${shift}${alt}${meta}${this.scanCode}`;
166 }
167
168 /**
169 * Does this keybinding refer to the key code of a modifier and it also has the modifier flag?
170 */
171 public isDuplicateModifierCase(): boolean {
172 return (
173 (this.ctrlKey && (this.scanCode === ScanCode.ControlLeft || this.scanCode === ScanCode.ControlRight))
174 || (this.shiftKey && (this.scanCode === ScanCode.ShiftLeft || this.scanCode === ScanCode.ShiftRight))
175 || (this.altKey && (this.scanCode === ScanCode.AltLeft || this.scanCode === ScanCode.AltRight))
176 || (this.metaKey && (this.scanCode === ScanCode.MetaLeft || this.scanCode === ScanCode.MetaRight))
177 );
178 }
179 }
180
181 export type Chord = KeyCodeChord | ScanCodeChord;
182
183 /**
184 * A keybinding is a sequence of chords.
185 */
186 export class Keybinding {
187
188 public readonly chords: Chord[];
189
190 constructor(chords: Chord[]) {
191 if (chords.length === 0) {
192 throw illegalArgument(`chords`);
193 }
194 this.chords = chords;
195 }
196
197 public getHashCode(): string {
198 let result = '';
199 for (let i = 0, len = this.chords.length; i < len; i++) {
200 if (i !== 0) {
201 result += ';';
202 }
203 result += this.chords[i].getHashCode();
204 }
205 return result;
206 }
207
208 public equals(other: Keybinding | null): boolean {
209 if (other === null) {
210 return false;
211 }
212 if (this.chords.length !== other.chords.length) {
213 return false;
214 }
215 for (let i = 0; i < this.chords.length; i++) {
216 if (!this.chords[i].equals(other.chords[i])) {
217 return false;
218 }
219 }
220 return true;
221 }
222 }
223
224 export class ResolvedChord {
225 constructor(
226 public readonly ctrlKey: boolean,
227 public readonly shiftKey: boolean,
228 public readonly altKey: boolean,
229 public readonly metaKey: boolean,
230 public readonly keyLabel: string | null,
231 public readonly keyAriaLabel: string | null
232 ) { }
233 }
234
235 export type SingleModifierChord = 'ctrl' | 'shift' | 'alt' | 'meta';
236
237 /**
238 * A resolved keybinding. Consists of one or multiple chords.
239 */
240 export abstract class ResolvedKeybinding {
241 /**
242 * This prints the binding in a format suitable for displaying in the UI.
243 */
244 public abstract getLabel(): string | null;
245 /**
246 * This prints the binding in a format suitable for ARIA.
247 */
248 public abstract getAriaLabel(): string | null;
249 /**
250 * This prints the binding in a format suitable for electron's accelerators.
251 * See https://github.com/electron/electron/blob/master/docs/api/accelerator.md
252 */
253 public abstract getElectronAccelerator(): string | null;
254 /**
255 * This prints the binding in a format suitable for user settings.
256 */
257 public abstract getUserSettingsLabel(): string | null;
258 /**
259 * Is the user settings label reflecting the label?
260 */
261 public abstract isWYSIWYG(): boolean;
262 /**
263 * Does the keybinding consist of more than one chord?
264 */
265 public abstract hasMultipleChords(): boolean;
266 /**
267 * Returns the chords that comprise of the keybinding.
268 */
269 public abstract getChords(): ResolvedChord[];
270 /**
271 * Returns the chords as strings useful for dispatching.
272 * Returns null for modifier only chords.
273 * @example keybinding "Shift" -> null
274 * @example keybinding ("D" with shift == true) -> "shift+D"
275 */
276 public abstract getDispatchChords(): (string | null)[];
277 /**
278 * Returns the modifier only chords as strings useful for dispatching.
279 * Returns null for chords that contain more than one modifier or a regular key.
280 * @example keybinding "Shift" -> "shift"
281 * @example keybinding ("D" with shift == true") -> null
282 */
283 public abstract getSingleModifierDispatchChords(): (SingleModifierChord | null)[];
284 }