Path: blob/main/src/vs/platform/keybinding/common/baseResolvedKeybinding.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { illegalArgument } from '../../../base/common/errors.js';6import { AriaLabelProvider, ElectronAcceleratorLabelProvider, UILabelProvider, UserSettingsLabelProvider } from '../../../base/common/keybindingLabels.js';7import { Chord, SingleModifierChord, ResolvedKeybinding, ResolvedChord } from '../../../base/common/keybindings.js';8import { OperatingSystem } from '../../../base/common/platform.js';910export abstract class BaseResolvedKeybinding<T extends Chord> extends ResolvedKeybinding {1112protected readonly _os: OperatingSystem;13protected readonly _chords: readonly T[];1415constructor(os: OperatingSystem, chords: readonly T[]) {16super();17if (chords.length === 0) {18throw illegalArgument(`chords`);19}20this._os = os;21this._chords = chords;22}2324public getLabel(): string | null {25return UILabelProvider.toLabel(this._os, this._chords, (keybinding) => this._getLabel(keybinding));26}2728public getAriaLabel(): string | null {29return AriaLabelProvider.toLabel(this._os, this._chords, (keybinding) => this._getAriaLabel(keybinding));30}3132public getElectronAccelerator(): string | null {33if (this._chords.length > 1) {34// [Electron Accelerators] Electron cannot handle chords35return null;36}37if (this._chords[0].isDuplicateModifierCase()) {38// [Electron Accelerators] Electron cannot handle modifier only keybindings39// e.g. "shift shift"40return null;41}42return ElectronAcceleratorLabelProvider.toLabel(this._os, this._chords, (keybinding) => this._getElectronAccelerator(keybinding));43}4445public getUserSettingsLabel(): string | null {46return UserSettingsLabelProvider.toLabel(this._os, this._chords, (keybinding) => this._getUserSettingsLabel(keybinding));47}4849public isWYSIWYG(): boolean {50return this._chords.every((keybinding) => this._isWYSIWYG(keybinding));51}5253public hasMultipleChords(): boolean {54return (this._chords.length > 1);55}5657public getChords(): ResolvedChord[] {58return this._chords.map((keybinding) => this._getChord(keybinding));59}6061private _getChord(keybinding: T): ResolvedChord {62return new ResolvedChord(63keybinding.ctrlKey,64keybinding.shiftKey,65keybinding.altKey,66keybinding.metaKey,67this._getLabel(keybinding),68this._getAriaLabel(keybinding)69);70}7172public getDispatchChords(): (string | null)[] {73return this._chords.map((keybinding) => this._getChordDispatch(keybinding));74}7576public getSingleModifierDispatchChords(): (SingleModifierChord | null)[] {77return this._chords.map((keybinding) => this._getSingleModifierChordDispatch(keybinding));78}7980protected abstract _getLabel(keybinding: T): string | null;81protected abstract _getAriaLabel(keybinding: T): string | null;82protected abstract _getElectronAccelerator(keybinding: T): string | null;83protected abstract _getUserSettingsLabel(keybinding: T): string | null;84protected abstract _isWYSIWYG(keybinding: T): boolean;85protected abstract _getChordDispatch(keybinding: T): string | null;86protected abstract _getSingleModifierChordDispatch(keybinding: T): SingleModifierChord | null;87}888990