Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New names around keybindings #169936

Merged
merged 2 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/vs/base/browser/keyboardEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import * as browser from 'vs/base/browser/browser';
import { EVENT_KEY_CODE_MAP, KeyCode, KeyCodeUtils, KeyMod } from 'vs/base/common/keyCodes';
import { SimpleKeybinding } from 'vs/base/common/keybindings';
import { KeyCodeChord } from 'vs/base/common/keybindings';
import * as platform from 'vs/base/common/platform';


Expand Down Expand Up @@ -65,7 +65,7 @@ export interface IKeyboardEvent {
/**
* @internal
*/
toKeybinding(): SimpleKeybinding;
toKeyCodeChord(): KeyCodeChord;
equals(keybinding: number): boolean;

preventDefault(): void;
Expand Down Expand Up @@ -127,7 +127,7 @@ export class StandardKeyboardEvent implements IKeyboardEvent {
public readonly code: string;

private _asKeybinding: number;
private _asRuntimeKeybinding: SimpleKeybinding;
private _asKeyCodeChord: KeyCodeChord;

constructor(source: KeyboardEvent) {
const e = source;
Expand All @@ -151,7 +151,7 @@ export class StandardKeyboardEvent implements IKeyboardEvent {
this.metaKey = this.metaKey || this.keyCode === KeyCode.Meta;

this._asKeybinding = this._computeKeybinding();
this._asRuntimeKeybinding = this._computeRuntimeKeybinding();
this._asKeyCodeChord = this._computeKeyCodeChord();

// console.log(`code: ${e.code}, keyCode: ${e.keyCode}, key: ${e.key}`);
}
Expand All @@ -168,8 +168,8 @@ export class StandardKeyboardEvent implements IKeyboardEvent {
}
}

public toKeybinding(): SimpleKeybinding {
return this._asRuntimeKeybinding;
public toKeyCodeChord(): KeyCodeChord {
return this._asKeyCodeChord;
}

public equals(other: number): boolean {
Expand Down Expand Up @@ -200,11 +200,11 @@ export class StandardKeyboardEvent implements IKeyboardEvent {
return result;
}

private _computeRuntimeKeybinding(): SimpleKeybinding {
private _computeKeyCodeChord(): KeyCodeChord {
let key = KeyCode.Unknown;
if (this.keyCode !== KeyCode.Ctrl && this.keyCode !== KeyCode.Shift && this.keyCode !== KeyCode.Alt && this.keyCode !== KeyCode.Meta) {
key = this.keyCode;
}
return new SimpleKeybinding(this.ctrlKey, this.shiftKey, this.altKey, this.metaKey, key);
return new KeyCodeChord(this.ctrlKey, this.shiftKey, this.altKey, this.metaKey, key);
}
}
30 changes: 15 additions & 15 deletions src/vs/base/browser/ui/keybindingLabel/keybindingLabel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

import * as dom from 'vs/base/browser/dom';
import { UILabelProvider } from 'vs/base/common/keybindingLabels';
import { ResolvedKeybinding, ResolvedKeybindingPart } from 'vs/base/common/keybindings';
import { ResolvedKeybinding, ResolvedChord } from 'vs/base/common/keybindings';
import { equals } from 'vs/base/common/objects';
import { OperatingSystem } from 'vs/base/common/platform';
import 'vs/css!./keybindingLabel';
import { localize } from 'vs/nls';

const $ = dom.$;

export interface PartMatches {
export interface ChordMatches {
ctrlKey?: boolean;
shiftKey?: boolean;
altKey?: boolean;
Expand All @@ -22,8 +22,8 @@ export interface PartMatches {
}

export interface Matches {
firstPart: PartMatches;
chordPart: PartMatches;
firstPart: ChordMatches;
chordPart: ChordMatches;
}

export interface KeybindingLabelOptions extends IKeybindingLabelStyles {
Expand Down Expand Up @@ -93,13 +93,13 @@ export class KeybindingLabel {
this.clear();

if (this.keybinding) {
const [firstPart, chordPart] = this.keybinding.getParts();
if (firstPart) {
this.renderPart(this.domNode, firstPart, this.matches ? this.matches.firstPart : null);
const [firstChord, secondChord] = this.keybinding.getChords();// TODO@chords
if (firstChord) {
this.renderChord(this.domNode, firstChord, this.matches ? this.matches.firstPart : null);
}
if (chordPart) {
if (secondChord) {
dom.append(this.domNode, $('span.monaco-keybinding-key-chord-separator', undefined, ' '));
this.renderPart(this.domNode, chordPart, this.matches ? this.matches.chordPart : null);
this.renderChord(this.domNode, secondChord, this.matches ? this.matches.chordPart : null);
}
this.domNode.title = this.keybinding.getAriaLabel() || '';
} else if (this.options && this.options.renderUnboundKeybindings) {
Expand All @@ -114,21 +114,21 @@ export class KeybindingLabel {
this.keyElements.clear();
}

private renderPart(parent: HTMLElement, part: ResolvedKeybindingPart, match: PartMatches | null) {
private renderChord(parent: HTMLElement, chord: ResolvedChord, match: ChordMatches | null) {
const modifierLabels = UILabelProvider.modifierLabels[this.os];
if (part.ctrlKey) {
if (chord.ctrlKey) {
this.renderKey(parent, modifierLabels.ctrlKey, Boolean(match?.ctrlKey), modifierLabels.separator);
}
if (part.shiftKey) {
if (chord.shiftKey) {
this.renderKey(parent, modifierLabels.shiftKey, Boolean(match?.shiftKey), modifierLabels.separator);
}
if (part.altKey) {
if (chord.altKey) {
this.renderKey(parent, modifierLabels.altKey, Boolean(match?.altKey), modifierLabels.separator);
}
if (part.metaKey) {
if (chord.metaKey) {
this.renderKey(parent, modifierLabels.metaKey, Boolean(match?.metaKey), modifierLabels.separator);
}
const keyLabel = part.keyLabel;
const keyLabel = chord.keyLabel;
if (keyLabel) {
this.renderKey(parent, keyLabel, Boolean(match?.keyCode), '');
}
Expand Down
12 changes: 6 additions & 6 deletions src/vs/base/common/keybindingLabels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ export class ModifierLabelProvider {
this.modifierLabels[OperatingSystem.Linux] = linux;
}

public toLabel<T extends Modifiers>(OS: OperatingSystem, parts: readonly T[], keyLabelProvider: KeyLabelProvider<T>): string | null {
if (parts.length === 0) {
public toLabel<T extends Modifiers>(OS: OperatingSystem, chords: readonly T[], keyLabelProvider: KeyLabelProvider<T>): string | null {
if (chords.length === 0) {
return null;
}

const result: string[] = [];
for (let i = 0, len = parts.length; i < len; i++) {
const part = parts[i];
const keyLabel = keyLabelProvider(part);
for (let i = 0, len = chords.length; i < len; i++) {
const chord = chords[i];
const keyLabel = keyLabelProvider(chord);
if (keyLabel === null) {
// this keybinding cannot be expressed...
return null;
}
result[i] = _simpleAsString(part, keyLabel, this.modifierLabels[OS]);
result[i] = _simpleAsString(chord, keyLabel, this.modifierLabels[OS]);
}
return result.join(' ');
}
Expand Down
42 changes: 10 additions & 32 deletions src/vs/base/common/keybindingParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import { KeyCodeUtils, ScanCodeUtils } from 'vs/base/common/keyCodes';
import { ChordKeybinding, Keybinding, SimpleKeybinding, ScanCodeBinding, UserKeybinding } from 'vs/base/common/keybindings';
import { OperatingSystem } from 'vs/base/common/platform';
import { KeyCodeChord, ScanCodeChord, Keybinding, Chord } from 'vs/base/common/keybindings';

export class KeybindingParser {

Expand Down Expand Up @@ -74,51 +73,30 @@ export class KeybindingParser {
};
}

private static parseSimpleKeybinding(input: string): [SimpleKeybinding, string] {
const mods = this._readModifiers(input);
const keyCode = KeyCodeUtils.fromUserSettings(mods.key);
return [new SimpleKeybinding(mods.ctrl, mods.shift, mods.alt, mods.meta, keyCode), mods.remains];
}

public static parseKeybinding(input: string, OS: OperatingSystem): Keybinding | null {
if (!input) {
return null;
}

const parts: SimpleKeybinding[] = [];
let part: SimpleKeybinding;

do {
[part, input] = this.parseSimpleKeybinding(input);
parts.push(part);
} while (input.length > 0);
return new ChordKeybinding(parts);
}

private static parseSimpleUserBinding(input: string): [SimpleKeybinding | ScanCodeBinding, string] {
private static parseChord(input: string): [Chord, string] {
const mods = this._readModifiers(input);
const scanCodeMatch = mods.key.match(/^\[([^\]]+)\]$/);
if (scanCodeMatch) {
const strScanCode = scanCodeMatch[1];
const scanCode = ScanCodeUtils.lowerCaseToEnum(strScanCode);
return [new ScanCodeBinding(mods.ctrl, mods.shift, mods.alt, mods.meta, scanCode), mods.remains];
return [new ScanCodeChord(mods.ctrl, mods.shift, mods.alt, mods.meta, scanCode), mods.remains];
}
const keyCode = KeyCodeUtils.fromUserSettings(mods.key);
return [new SimpleKeybinding(mods.ctrl, mods.shift, mods.alt, mods.meta, keyCode), mods.remains];
return [new KeyCodeChord(mods.ctrl, mods.shift, mods.alt, mods.meta, keyCode), mods.remains];
}

static parseUserBinding(input: string): UserKeybinding | null {
static parseKeybinding(input: string): Keybinding | null {
if (!input) {
return null;
}

const parts: (SimpleKeybinding | ScanCodeBinding)[] = [];
let part: SimpleKeybinding | ScanCodeBinding;
const chords: Chord[] = [];
let chord: Chord;

while (input.length > 0) {
[part, input] = this.parseSimpleUserBinding(input);
parts.push(part);
[chord, input] = this.parseChord(input);
chords.push(chord);
}
return (parts.length > 0 ? new UserKeybinding(parts) : null);
return (chords.length > 0 ? new Keybinding(chords) : null);
}
}