Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/keybinding/common/baseResolvedKeybinding.ts
3296 views
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 '../../../base/common/errors.js';
7
import { AriaLabelProvider, ElectronAcceleratorLabelProvider, UILabelProvider, UserSettingsLabelProvider } from '../../../base/common/keybindingLabels.js';
8
import { Chord, SingleModifierChord, ResolvedKeybinding, ResolvedChord } from '../../../base/common/keybindings.js';
9
import { OperatingSystem } from '../../../base/common/platform.js';
10
11
export abstract class BaseResolvedKeybinding<T extends Chord> extends ResolvedKeybinding {
12
13
protected readonly _os: OperatingSystem;
14
protected readonly _chords: readonly T[];
15
16
constructor(os: OperatingSystem, chords: readonly T[]) {
17
super();
18
if (chords.length === 0) {
19
throw illegalArgument(`chords`);
20
}
21
this._os = os;
22
this._chords = chords;
23
}
24
25
public getLabel(): string | null {
26
return UILabelProvider.toLabel(this._os, this._chords, (keybinding) => this._getLabel(keybinding));
27
}
28
29
public getAriaLabel(): string | null {
30
return AriaLabelProvider.toLabel(this._os, this._chords, (keybinding) => this._getAriaLabel(keybinding));
31
}
32
33
public getElectronAccelerator(): string | null {
34
if (this._chords.length > 1) {
35
// [Electron Accelerators] Electron cannot handle chords
36
return null;
37
}
38
if (this._chords[0].isDuplicateModifierCase()) {
39
// [Electron Accelerators] Electron cannot handle modifier only keybindings
40
// e.g. "shift shift"
41
return null;
42
}
43
return ElectronAcceleratorLabelProvider.toLabel(this._os, this._chords, (keybinding) => this._getElectronAccelerator(keybinding));
44
}
45
46
public getUserSettingsLabel(): string | null {
47
return UserSettingsLabelProvider.toLabel(this._os, this._chords, (keybinding) => this._getUserSettingsLabel(keybinding));
48
}
49
50
public isWYSIWYG(): boolean {
51
return this._chords.every((keybinding) => this._isWYSIWYG(keybinding));
52
}
53
54
public hasMultipleChords(): boolean {
55
return (this._chords.length > 1);
56
}
57
58
public getChords(): ResolvedChord[] {
59
return this._chords.map((keybinding) => this._getChord(keybinding));
60
}
61
62
private _getChord(keybinding: T): ResolvedChord {
63
return new ResolvedChord(
64
keybinding.ctrlKey,
65
keybinding.shiftKey,
66
keybinding.altKey,
67
keybinding.metaKey,
68
this._getLabel(keybinding),
69
this._getAriaLabel(keybinding)
70
);
71
}
72
73
public getDispatchChords(): (string | null)[] {
74
return this._chords.map((keybinding) => this._getChordDispatch(keybinding));
75
}
76
77
public getSingleModifierDispatchChords(): (SingleModifierChord | null)[] {
78
return this._chords.map((keybinding) => this._getSingleModifierChordDispatch(keybinding));
79
}
80
81
protected abstract _getLabel(keybinding: T): string | null;
82
protected abstract _getAriaLabel(keybinding: T): string | null;
83
protected abstract _getElectronAccelerator(keybinding: T): string | null;
84
protected abstract _getUserSettingsLabel(keybinding: T): string | null;
85
protected abstract _isWYSIWYG(keybinding: T): boolean;
86
protected abstract _getChordDispatch(keybinding: T): string | null;
87
protected abstract _getSingleModifierChordDispatch(keybinding: T): SingleModifierChord | null;
88
}
89
90