106 lines
4.4 KiB
JavaScript
106 lines
4.4 KiB
JavaScript
/*---------------------------------------------------------------------------------------------
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
|
*--------------------------------------------------------------------------------------------*/
|
|
import { illegalArgument } from './errors.js';
|
|
export function createKeybinding(keybinding, OS) {
|
|
if (keybinding === 0) {
|
|
return null;
|
|
}
|
|
const firstPart = (keybinding & 0x0000FFFF) >>> 0;
|
|
const chordPart = (keybinding & 0xFFFF0000) >>> 16;
|
|
if (chordPart !== 0) {
|
|
return new ChordKeybinding([
|
|
createSimpleKeybinding(firstPart, OS),
|
|
createSimpleKeybinding(chordPart, OS)
|
|
]);
|
|
}
|
|
return new ChordKeybinding([createSimpleKeybinding(firstPart, OS)]);
|
|
}
|
|
export function createSimpleKeybinding(keybinding, OS) {
|
|
const ctrlCmd = (keybinding & 2048 /* BinaryKeybindingsMask.CtrlCmd */ ? true : false);
|
|
const winCtrl = (keybinding & 256 /* BinaryKeybindingsMask.WinCtrl */ ? true : false);
|
|
const ctrlKey = (OS === 2 /* OperatingSystem.Macintosh */ ? winCtrl : ctrlCmd);
|
|
const shiftKey = (keybinding & 1024 /* BinaryKeybindingsMask.Shift */ ? true : false);
|
|
const altKey = (keybinding & 512 /* BinaryKeybindingsMask.Alt */ ? true : false);
|
|
const metaKey = (OS === 2 /* OperatingSystem.Macintosh */ ? ctrlCmd : winCtrl);
|
|
const keyCode = (keybinding & 255 /* BinaryKeybindingsMask.KeyCode */);
|
|
return new SimpleKeybinding(ctrlKey, shiftKey, altKey, metaKey, keyCode);
|
|
}
|
|
export class SimpleKeybinding {
|
|
constructor(ctrlKey, shiftKey, altKey, metaKey, keyCode) {
|
|
this.ctrlKey = ctrlKey;
|
|
this.shiftKey = shiftKey;
|
|
this.altKey = altKey;
|
|
this.metaKey = metaKey;
|
|
this.keyCode = keyCode;
|
|
}
|
|
equals(other) {
|
|
return (this.ctrlKey === other.ctrlKey
|
|
&& this.shiftKey === other.shiftKey
|
|
&& this.altKey === other.altKey
|
|
&& this.metaKey === other.metaKey
|
|
&& this.keyCode === other.keyCode);
|
|
}
|
|
isModifierKey() {
|
|
return (this.keyCode === 0 /* KeyCode.Unknown */
|
|
|| this.keyCode === 5 /* KeyCode.Ctrl */
|
|
|| this.keyCode === 57 /* KeyCode.Meta */
|
|
|| this.keyCode === 6 /* KeyCode.Alt */
|
|
|| this.keyCode === 4 /* KeyCode.Shift */);
|
|
}
|
|
toChord() {
|
|
return new ChordKeybinding([this]);
|
|
}
|
|
/**
|
|
* Does this keybinding refer to the key code of a modifier and it also has the modifier flag?
|
|
*/
|
|
isDuplicateModifierCase() {
|
|
return ((this.ctrlKey && this.keyCode === 5 /* KeyCode.Ctrl */)
|
|
|| (this.shiftKey && this.keyCode === 4 /* KeyCode.Shift */)
|
|
|| (this.altKey && this.keyCode === 6 /* KeyCode.Alt */)
|
|
|| (this.metaKey && this.keyCode === 57 /* KeyCode.Meta */));
|
|
}
|
|
}
|
|
export class ChordKeybinding {
|
|
constructor(parts) {
|
|
if (parts.length === 0) {
|
|
throw illegalArgument(`parts`);
|
|
}
|
|
this.parts = parts;
|
|
}
|
|
}
|
|
export class ScanCodeBinding {
|
|
constructor(ctrlKey, shiftKey, altKey, metaKey, scanCode) {
|
|
this.ctrlKey = ctrlKey;
|
|
this.shiftKey = shiftKey;
|
|
this.altKey = altKey;
|
|
this.metaKey = metaKey;
|
|
this.scanCode = scanCode;
|
|
}
|
|
/**
|
|
* Does this keybinding refer to the key code of a modifier and it also has the modifier flag?
|
|
*/
|
|
isDuplicateModifierCase() {
|
|
return ((this.ctrlKey && (this.scanCode === 157 /* ScanCode.ControlLeft */ || this.scanCode === 161 /* ScanCode.ControlRight */))
|
|
|| (this.shiftKey && (this.scanCode === 158 /* ScanCode.ShiftLeft */ || this.scanCode === 162 /* ScanCode.ShiftRight */))
|
|
|| (this.altKey && (this.scanCode === 159 /* ScanCode.AltLeft */ || this.scanCode === 163 /* ScanCode.AltRight */))
|
|
|| (this.metaKey && (this.scanCode === 160 /* ScanCode.MetaLeft */ || this.scanCode === 164 /* ScanCode.MetaRight */)));
|
|
}
|
|
}
|
|
export class ResolvedKeybindingPart {
|
|
constructor(ctrlKey, shiftKey, altKey, metaKey, kbLabel, kbAriaLabel) {
|
|
this.ctrlKey = ctrlKey;
|
|
this.shiftKey = shiftKey;
|
|
this.altKey = altKey;
|
|
this.metaKey = metaKey;
|
|
this.keyLabel = kbLabel;
|
|
this.keyAriaLabel = kbAriaLabel;
|
|
}
|
|
}
|
|
/**
|
|
* A resolved keybinding. Can be a simple keybinding or a chord keybinding.
|
|
*/
|
|
export class ResolvedKeybinding {
|
|
}
|