提交 93c52c48 编写于 作者: A Alex Dima

Introduce and adopt RuntimeKeybinding

上级 bb508d8e
......@@ -6,6 +6,7 @@
'use strict';
import { IHTMLContentElement } from 'vs/base/common/htmlContent';
import { OperatingSystem } from 'vs/base/common/platform';
/**
* Virtual Key Codes, the value does not hold any inherent meaning.
......@@ -567,6 +568,67 @@ export class ChordKeybinding {
export type Keybinding = SimpleKeybinding | ChordKeybinding;
export function createRuntimeKeybinding(keybinding: Keybinding, OS: OperatingSystem): RuntimeKeybinding {
if (keybinding.isChord()) {
return new ChordRuntimeKeybinding(
createSimpleRuntimeKeybinding(keybinding.extractFirstPart(), OS),
createSimpleRuntimeKeybinding(keybinding.extractChordPart(), OS),
);
}
return createSimpleRuntimeKeybinding(keybinding, OS);
}
export function createSimpleRuntimeKeybinding(keybinding: SimpleKeybinding, OS: OperatingSystem): SimpleRuntimeKeybinding {
const ctrlCmd = keybinding.hasCtrlCmd();
const winCtrl = keybinding.hasWinCtrl();
const ctrlKey = (OS === OperatingSystem.Macintosh ? winCtrl : ctrlCmd);
const metaKey = (OS === OperatingSystem.Macintosh ? ctrlCmd : winCtrl);
const shiftKey = keybinding.hasShift();
const altKey = keybinding.hasAlt();
const keyCode = keybinding.getKeyCode();
return new SimpleRuntimeKeybinding(ctrlKey, shiftKey, altKey, metaKey, keyCode);
}
export const enum RuntimeKeybindingType {
Simple = 1,
Chord = 2
}
export class SimpleRuntimeKeybinding {
public readonly type = RuntimeKeybindingType.Simple;
public readonly ctrlKey: boolean;
public readonly shiftKey: boolean;
public readonly altKey: boolean;
public readonly metaKey: boolean;
public readonly keyCode: KeyCode;
constructor(ctrlKey: boolean, shiftKey: boolean, altKey: boolean, metaKey: boolean, keyCode: KeyCode) {
this.ctrlKey = ctrlKey;
this.shiftKey = shiftKey;
this.altKey = altKey;
this.metaKey = metaKey;
this.keyCode = keyCode;
}
}
export class ChordRuntimeKeybinding {
public readonly type = RuntimeKeybindingType.Chord;
public readonly firstPart: SimpleRuntimeKeybinding;
public readonly chordPart: SimpleRuntimeKeybinding;
constructor(firstPart: SimpleRuntimeKeybinding, chordPart: SimpleRuntimeKeybinding) {
this.firstPart = firstPart;
this.chordPart = chordPart;
}
}
export type RuntimeKeybinding = SimpleRuntimeKeybinding | ChordRuntimeKeybinding;
/**
* A resolved keybinding.
*/
......
......@@ -32,7 +32,7 @@ import { KeybindingsRegistry } from 'vs/platform/keybinding/common/keybindingsRe
import { MenuId, IMenu, IMenuService } from 'vs/platform/actions/common/actions';
import { Menu } from 'vs/platform/actions/common/menu';
import { ITelemetryService, ITelemetryExperiments, ITelemetryInfo } from 'vs/platform/telemetry/common/telemetry';
import { ResolvedKeybinding, Keybinding, createKeybinding } from 'vs/base/common/keyCodes';
import { ResolvedKeybinding, Keybinding, createKeybinding, createRuntimeKeybinding } from 'vs/base/common/keyCodes';
import { ResolvedKeybindingItem } from 'vs/platform/keybinding/common/resolvedKeybindingItem';
import { OS } from 'vs/base/common/platform';
......@@ -396,7 +396,7 @@ export class StandaloneKeybindingService extends AbstractKeybindingService {
}
protected _createResolvedKeybinding(kb: Keybinding): ResolvedKeybinding {
return new USLayoutResolvedKeybinding(kb, OS);
return new USLayoutResolvedKeybinding(createRuntimeKeybinding(kb, OS), OS);
}
}
......
......@@ -103,8 +103,10 @@ export abstract class AbstractKeybindingService implements IKeybindingService {
const contextValue = this._contextKeyService.getContextValue(target);
const currentChord = this._currentChord ? this._currentChord.keypress : null;
const keypress = keybinding.value.toString();
return this._getResolver().resolve(contextValue, currentChord, keypress);
const resolvedKeybinding = this._createResolvedKeybinding(keybinding);
const [firstPart,] = resolvedKeybinding.getDispatchParts();
// We know for a fact the chordPart is null since we're using a single keypress
return this._getResolver().resolve(contextValue, currentChord, firstPart);
}
protected _dispatch(keybinding: SimpleKeybinding, target: IContextKeyServiceTarget): boolean {
......@@ -117,14 +119,15 @@ export abstract class AbstractKeybindingService implements IKeybindingService {
const contextValue = this._contextKeyService.getContextValue(target);
const currentChord = this._currentChord ? this._currentChord.keypress : null;
const keypress = keybinding.value.toString();
const resolvedKeybinding = this._createResolvedKeybinding(keybinding);
const [firstPart,] = resolvedKeybinding.getDispatchParts();
const keypressLabel = this._createResolvedKeybinding(keybinding).getLabel();
const resolveResult = this._getResolver().resolve(contextValue, currentChord, keypress);
const resolveResult = this._getResolver().resolve(contextValue, currentChord, firstPart);
if (resolveResult && resolveResult.enterChord) {
shouldPreventDefault = true;
this._currentChord = {
keypress: keypress,
keypress: firstPart,
label: keypressLabel
};
if (this._statusService) {
......
......@@ -8,7 +8,7 @@
import * as nls from 'vs/nls';
import { OperatingSystem } from 'vs/base/common/platform';
import { IHTMLContentElement } from 'vs/base/common/htmlContent';
import { Keybinding, SimpleKeybinding, KeyCode } from 'vs/base/common/keyCodes';
import { KeyCode, RuntimeKeybinding, RuntimeKeybindingType, SimpleRuntimeKeybinding } from 'vs/base/common/keyCodes';
export interface IKeyCodeLabelProvider {
(keyCode: KeyCode, OS: OperatingSystem): string;
......@@ -16,28 +16,18 @@ export interface IKeyCodeLabelProvider {
export class PrintableKeypress {
public static fromSimpleKeybinding(keybinding: SimpleKeybinding, labelProvider: IKeyCodeLabelProvider, OS: OperatingSystem): PrintableKeypress {
const ctrlCmd = keybinding.hasCtrlCmd();
const winCtrl = keybinding.hasWinCtrl();
const ctrlKey = (OS === OperatingSystem.Macintosh ? winCtrl : ctrlCmd);
const metaKey = (OS === OperatingSystem.Macintosh ? ctrlCmd : winCtrl);
const shiftKey = keybinding.hasShift();
const altKey = keybinding.hasAlt();
const keyCode = keybinding.getKeyCode();
const keyLabel = labelProvider(keyCode, OS) || '';
return new PrintableKeypress(ctrlKey, shiftKey, altKey, metaKey, keyLabel);
private static _fromSimpleKeybinding(k: SimpleRuntimeKeybinding, labelProvider: IKeyCodeLabelProvider, OS: OperatingSystem): PrintableKeypress {
const keyLabel = labelProvider(k.keyCode, OS) || '';
return new PrintableKeypress(k.ctrlKey, k.shiftKey, k.altKey, k.metaKey, keyLabel);
}
public static fromKeybinding(keybinding: Keybinding, labelProvider: IKeyCodeLabelProvider, OS: OperatingSystem): [PrintableKeypress, PrintableKeypress] {
if (keybinding.isChord()) {
const firstPart = PrintableKeypress.fromSimpleKeybinding(keybinding.extractFirstPart(), labelProvider, OS);
const chordPart = PrintableKeypress.fromSimpleKeybinding(keybinding.extractChordPart(), labelProvider, OS);
public static fromKeybinding(keybinding: RuntimeKeybinding, labelProvider: IKeyCodeLabelProvider, OS: OperatingSystem): [PrintableKeypress, PrintableKeypress] {
if (keybinding.type === RuntimeKeybindingType.Chord) {
const firstPart = PrintableKeypress._fromSimpleKeybinding(keybinding.firstPart, labelProvider, OS);
const chordPart = PrintableKeypress._fromSimpleKeybinding(keybinding.chordPart, labelProvider, OS);
return [firstPart, chordPart];
} else {
const printableKeypress = PrintableKeypress.fromSimpleKeybinding(keybinding, labelProvider, OS);
const printableKeypress = PrintableKeypress._fromSimpleKeybinding(keybinding, labelProvider, OS);
return [printableKeypress, null];
}
}
......
......@@ -5,7 +5,7 @@
'use strict';
import { IHTMLContentElement } from 'vs/base/common/htmlContent';
import { ResolvedKeybinding, Keybinding, KeyCode, KeyCodeUtils, USER_SETTINGS } from 'vs/base/common/keyCodes';
import { ResolvedKeybinding, KeyCode, KeyCodeUtils, USER_SETTINGS, RuntimeKeybinding, RuntimeKeybindingType, SimpleRuntimeKeybinding } from 'vs/base/common/keyCodes';
import { PrintableKeypress, UILabelProvider, AriaLabelProvider, ElectronAcceleratorLabelProvider, UserSettingsLabelProvider } from 'vs/platform/keybinding/common/keybindingLabels';
import { OperatingSystem } from 'vs/base/common/platform';
......@@ -14,13 +14,13 @@ import { OperatingSystem } from 'vs/base/common/platform';
*/
export class USLayoutResolvedKeybinding extends ResolvedKeybinding {
private readonly _actual: Keybinding;
private readonly _actual: RuntimeKeybinding;
private readonly _os: OperatingSystem;
constructor(actual: Keybinding, os: OperatingSystem) {
constructor(actual: RuntimeKeybinding, OS: OperatingSystem) {
super();
this._actual = actual;
this._os = os;
this._os = OS;
}
private static _usKeyCodeToUILabel(keyCode: KeyCode, OS: OperatingSystem): string {
......@@ -74,12 +74,12 @@ export class USLayoutResolvedKeybinding extends ResolvedKeybinding {
}
public getElectronAccelerator(): string {
if (this._actual.isChord()) {
if (this._actual.type === RuntimeKeybindingType.Chord) {
// Electron cannot handle chords
return null;
}
let keyCode = this._actual.getKeyCode();
let keyCode = this._actual.keyCode;
if (keyCode >= KeyCode.NUMPAD_0 && keyCode <= KeyCode.NUMPAD_DIVIDE) {
// Electron cannot handle numpad keys
return null;
......@@ -101,43 +101,35 @@ export class USLayoutResolvedKeybinding extends ResolvedKeybinding {
}
public isChord(): boolean {
return this._actual.isChord();
return (this._actual.type === RuntimeKeybindingType.Chord);
}
public hasCtrlModifier(): boolean {
if (this._actual.isChord()) {
if (this._actual.type === RuntimeKeybindingType.Chord) {
return false;
}
if (this._os === OperatingSystem.Macintosh) {
return this._actual.hasWinCtrl();
} else {
return this._actual.hasCtrlCmd();
}
return this._actual.ctrlKey;
}
public hasShiftModifier(): boolean {
if (this._actual.isChord()) {
if (this._actual.type === RuntimeKeybindingType.Chord) {
return false;
}
return this._actual.hasShift();
return this._actual.shiftKey;
}
public hasAltModifier(): boolean {
if (this._actual.isChord()) {
if (this._actual.type === RuntimeKeybindingType.Chord) {
return false;
}
return this._actual.hasAlt();
return this._actual.altKey;
}
public hasMetaModifier(): boolean {
if (this._actual.isChord()) {
if (this._actual.type === RuntimeKeybindingType.Chord) {
return false;
}
if (this._os === OperatingSystem.Macintosh) {
return this._actual.hasCtrlCmd();
} else {
return this._actual.hasWinCtrl();
}
return this._actual.metaKey;
}
public getDispatchParts(): [string, string] {
......@@ -146,13 +138,33 @@ export class USLayoutResolvedKeybinding extends ResolvedKeybinding {
if (this._actual === null) {
keypressFirstPart = null;
keypressChordPart = null;
} else if (this._actual.isChord()) {
keypressFirstPart = this._actual.extractFirstPart().value.toString();
keypressChordPart = this._actual.extractChordPart().value.toString();
} else if (this._actual.type === RuntimeKeybindingType.Chord) {
keypressFirstPart = USLayoutResolvedKeybinding.getDispatchStr(this._actual.firstPart);
keypressChordPart = USLayoutResolvedKeybinding.getDispatchStr(this._actual.chordPart);
} else {
keypressFirstPart = this._actual.value.toString();
keypressFirstPart = USLayoutResolvedKeybinding.getDispatchStr(this._actual);
keypressChordPart = null;
}
return [keypressFirstPart, keypressChordPart];
}
public static getDispatchStr(keybinding: SimpleRuntimeKeybinding): string {
let result = '';
if (keybinding.ctrlKey) {
result += 'ctrl+';
}
if (keybinding.shiftKey) {
result += 'shift+';
}
if (keybinding.altKey) {
result += 'alt+';
}
if (keybinding.metaKey) {
result += 'meta+';
}
result += KeyCodeUtils.toString(keybinding.keyCode);
return result;
}
}
......@@ -5,7 +5,7 @@
'use strict';
import * as assert from 'assert';
import { ResolvedKeybinding, Keybinding, SimpleKeybinding, createKeybinding, KeyCode, KeyMod, KeyChord } from 'vs/base/common/keyCodes';
import { ResolvedKeybinding, Keybinding, SimpleKeybinding, createKeybinding, KeyCode, KeyMod, KeyChord, createRuntimeKeybinding } from 'vs/base/common/keyCodes';
import { AbstractKeybindingService } from 'vs/platform/keybinding/common/abstractKeybindingService';
import { USLayoutResolvedKeybinding } from 'vs/platform/keybinding/common/usLayoutResolvedKeybinding';
import { IDisposable } from 'vs/base/common/lifecycle';
......@@ -40,7 +40,7 @@ suite('AbstractKeybindingService', () => {
}
protected _createResolvedKeybinding(kb: Keybinding): ResolvedKeybinding {
return new USLayoutResolvedKeybinding(kb, OS);
return new USLayoutResolvedKeybinding(createRuntimeKeybinding(kb, OS), OS);
}
public dispatch(keybinding: SimpleKeybinding): boolean {
......@@ -130,7 +130,7 @@ suite('AbstractKeybindingService', () => {
});
function kbItem(keybinding: number, command: string, when: ContextKeyExpr = null): ResolvedKeybindingItem {
const resolvedKeybinding = (keybinding !== 0 ? new USLayoutResolvedKeybinding(createKeybinding(keybinding), OS) : null);
const resolvedKeybinding = (keybinding !== 0 ? new USLayoutResolvedKeybinding(createRuntimeKeybinding(createKeybinding(keybinding), OS), OS) : null);
return new ResolvedKeybindingItem(
resolvedKeybinding,
command,
......@@ -141,7 +141,7 @@ suite('AbstractKeybindingService', () => {
}
function toUsLabel(keybinding: number): string {
const usResolvedKeybinding = new USLayoutResolvedKeybinding(createKeybinding(keybinding), OS);
const usResolvedKeybinding = new USLayoutResolvedKeybinding(createRuntimeKeybinding(createKeybinding(keybinding), OS), OS);
return usResolvedKeybinding.getLabel();
}
......
......@@ -5,7 +5,7 @@
'use strict';
import * as assert from 'assert';
import { createKeybinding, KeyCode, KeyMod, KeyChord } from 'vs/base/common/keyCodes';
import { createKeybinding, KeyCode, KeyMod, KeyChord, createRuntimeKeybinding } from 'vs/base/common/keyCodes';
import { IHTMLContentElement } from 'vs/base/common/htmlContent';
import { USLayoutResolvedKeybinding } from 'vs/platform/keybinding/common/usLayoutResolvedKeybinding';
import { OperatingSystem } from 'vs/base/common/platform';
......@@ -13,7 +13,7 @@ import { OperatingSystem } from 'vs/base/common/platform';
suite('KeybindingLabels', () => {
function assertUSLabel(OS: OperatingSystem, keybinding: number, expected: string): void {
const usResolvedKeybinding = new USLayoutResolvedKeybinding(createKeybinding(keybinding), OS);
const usResolvedKeybinding = new USLayoutResolvedKeybinding(createRuntimeKeybinding(createKeybinding(keybinding), OS), OS);
assert.equal(usResolvedKeybinding.getLabel(), expected);
}
......@@ -118,7 +118,7 @@ suite('KeybindingLabels', () => {
test('Aria label', () => {
function assertAriaLabel(OS: OperatingSystem, keybinding: number, expected: string): void {
const usResolvedKeybinding = new USLayoutResolvedKeybinding(createKeybinding(keybinding), OS);
const usResolvedKeybinding = new USLayoutResolvedKeybinding(createRuntimeKeybinding(createKeybinding(keybinding), OS), OS);
assert.equal(usResolvedKeybinding.getAriaLabel(), expected);
}
......@@ -129,7 +129,7 @@ suite('KeybindingLabels', () => {
test('Electron Accelerator label', () => {
function assertElectronAcceleratorLabel(OS: OperatingSystem, keybinding: number, expected: string): void {
const usResolvedKeybinding = new USLayoutResolvedKeybinding(createKeybinding(keybinding), OS);
const usResolvedKeybinding = new USLayoutResolvedKeybinding(createRuntimeKeybinding(createKeybinding(keybinding), OS), OS);
assert.equal(usResolvedKeybinding.getElectronAccelerator(), expected);
}
......@@ -156,7 +156,7 @@ suite('KeybindingLabels', () => {
test('User Settings label', () => {
function assertElectronAcceleratorLabel(OS: OperatingSystem, keybinding: number, expected: string): void {
const usResolvedKeybinding = new USLayoutResolvedKeybinding(createKeybinding(keybinding), OS);
const usResolvedKeybinding = new USLayoutResolvedKeybinding(createRuntimeKeybinding(createKeybinding(keybinding), OS), OS);
assert.equal(usResolvedKeybinding.getUserSettingsLabel(), expected);
}
......@@ -172,7 +172,7 @@ suite('KeybindingLabels', () => {
test('US HTML label', () => {
function assertHTMLLabel(OS: OperatingSystem, keybinding: number, expected: IHTMLContentElement[]): void {
const usResolvedKeybinding = new USLayoutResolvedKeybinding(createKeybinding(keybinding), OS);
const usResolvedKeybinding = new USLayoutResolvedKeybinding(createRuntimeKeybinding(createKeybinding(keybinding), OS), OS);
assert.deepEqual(usResolvedKeybinding.getHTMLLabel(), expected);
}
......
......@@ -5,7 +5,7 @@
'use strict';
import * as assert from 'assert';
import { createKeybinding, SimpleKeybinding, KeyCode, KeyMod, KeyChord } from 'vs/base/common/keyCodes';
import { createKeybinding, SimpleKeybinding, KeyCode, KeyMod, KeyChord, createRuntimeKeybinding, createSimpleRuntimeKeybinding } from 'vs/base/common/keyCodes';
import { KeybindingResolver } from 'vs/platform/keybinding/common/keybindingResolver';
import { ContextKeyAndExpr, ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
import { ResolvedKeybindingItem } from 'vs/platform/keybinding/common/resolvedKeybindingItem';
......@@ -15,7 +15,7 @@ import { OS } from 'vs/base/common/platform';
suite('KeybindingResolver', () => {
function kbItem(keybinding: number, command: string, commandArgs: any, when: ContextKeyExpr, isDefault: boolean): ResolvedKeybindingItem {
const resolvedKeybinding = (keybinding !== 0 ? new USLayoutResolvedKeybinding(createKeybinding(keybinding), OS) : null);
const resolvedKeybinding = (keybinding !== 0 ? new USLayoutResolvedKeybinding(createRuntimeKeybinding(createKeybinding(keybinding), OS), OS) : null);
return new ResolvedKeybindingItem(
resolvedKeybinding,
command,
......@@ -25,6 +25,11 @@ suite('KeybindingResolver', () => {
);
}
function getDispatchStr(kb: SimpleKeybinding): string {
let runtimeKb = createSimpleRuntimeKeybinding(kb, OS);
return USLayoutResolvedKeybinding.getDispatchStr(runtimeKb);
}
test('resolve key', function () {
let keybinding = KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_Z;
let contextRules = ContextKeyExpr.equals('bar', 'baz');
......@@ -34,8 +39,8 @@ suite('KeybindingResolver', () => {
assert.equal(KeybindingResolver.contextMatchesRules({ bar: 'bz' }, contextRules), false);
let resolver = new KeybindingResolver([keybindingItem], []);
assert.equal(resolver.resolve({ bar: 'baz' }, null, new SimpleKeybinding(keybinding).value.toString()).commandId, 'yes');
assert.equal(resolver.resolve({ bar: 'bz' }, null, new SimpleKeybinding(keybinding).value.toString()), null);
assert.equal(resolver.resolve({ bar: 'baz' }, null, getDispatchStr(new SimpleKeybinding(keybinding))).commandId, 'yes');
assert.equal(resolver.resolve({ bar: 'bz' }, null, getDispatchStr(new SimpleKeybinding(keybinding))), null);
});
test('resolve key with arguments', function () {
......@@ -45,7 +50,7 @@ suite('KeybindingResolver', () => {
let keybindingItem = kbItem(keybinding, 'yes', commandArgs, contextRules, true);
let resolver = new KeybindingResolver([keybindingItem], []);
assert.equal(resolver.resolve({ bar: 'baz' }, null, new SimpleKeybinding(keybinding).value.toString()).commandArgs, commandArgs);
assert.equal(resolver.resolve({ bar: 'baz' }, null, getDispatchStr(new SimpleKeybinding(keybinding))).commandArgs, commandArgs);
});
test('KeybindingResolver.combine simple 1', function () {
......@@ -321,7 +326,7 @@ suite('KeybindingResolver', () => {
let lookupResult = resolver.lookupKeybindings(commandId);
assert.equal(lookupResult.length, expectedKeys.length, 'Length mismatch @ commandId ' + commandId + '; GOT: ' + JSON.stringify(lookupResult, null, '\t'));
for (let i = 0, len = lookupResult.length; i < len; i++) {
const expected = new USLayoutResolvedKeybinding(createKeybinding(expectedKeys[i]), OS);
const expected = new USLayoutResolvedKeybinding(createRuntimeKeybinding(createKeybinding(expectedKeys[i]), OS), OS);
assert.equal(lookupResult[i].resolvedKeybinding.getUserSettingsLabel(), expected.getUserSettingsLabel(), 'value mismatch @ commandId ' + commandId);
}
......@@ -331,8 +336,8 @@ suite('KeybindingResolver', () => {
let expectedKey = createKeybinding(_expectedKey);
if (expectedKey.isChord()) {
let firstPart = expectedKey.extractFirstPart().value.toString();
let chordPart = expectedKey.extractChordPart().value.toString();
let firstPart = getDispatchStr(expectedKey.extractFirstPart());
let chordPart = getDispatchStr(expectedKey.extractChordPart());
let result = resolver.resolve(ctx, null, firstPart);
assert.ok(result !== null, 'Enters chord for ' + commandId);
......@@ -344,7 +349,7 @@ suite('KeybindingResolver', () => {
assert.equal(result.commandId, commandId, 'Finds chorded command ' + commandId);
assert.equal(result.enterChord, false, 'Finds chorded command ' + commandId);
} else {
let result = resolver.resolve(ctx, null, expectedKey.value.toString());
let result = resolver.resolve(ctx, null, getDispatchStr(expectedKey));
assert.ok(result !== null, 'Finds command ' + commandId);
assert.equal(result.commandId, commandId, 'Finds command ' + commandId);
assert.equal(result.enterChord, false, 'Finds command ' + commandId);
......
......@@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/
'use strict';
import { ResolvedKeybinding, Keybinding } from 'vs/base/common/keyCodes';
import { ResolvedKeybinding, Keybinding, createRuntimeKeybinding } from 'vs/base/common/keyCodes';
import Event from 'vs/base/common/event';
import { IKeybindingService, IKeybindingEvent, IKeybindingItem2 } from 'vs/platform/keybinding/common/keybinding';
import { IContextKey, IContextKeyService, IContextKeyServiceTarget, ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
......@@ -77,7 +77,7 @@ export class MockKeybindingService2 implements IKeybindingService {
}
public resolveKeybinding(keybinding: Keybinding): ResolvedKeybinding {
return new USLayoutResolvedKeybinding(keybinding, OS);
return new USLayoutResolvedKeybinding(createRuntimeKeybinding(keybinding, OS), OS);
}
public lookupKeybindings(commandId: string): ResolvedKeybinding[] {
......
......@@ -9,7 +9,7 @@ import * as nls from 'vs/nls';
import * as dom from 'vs/base/browser/dom';
import { RunOnceScheduler } from 'vs/base/common/async';
import { MarkedString } from 'vs/base/common/htmlContent';
import { createKeybinding, KeyCode, KeyMod, KeyChord } from 'vs/base/common/keyCodes';
import { createKeybinding, KeyCode, KeyMod, KeyChord, createRuntimeKeybinding } from 'vs/base/common/keyCodes';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { KeybindingIO } from 'vs/workbench/services/keybinding/common/keybindingIO';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
......@@ -170,7 +170,7 @@ export class DefineKeybindingController implements editorCommon.IEditorContribut
let keybinding = createKeybinding(numKeybinding);
let resolvedKeybinding = this._keybindingService.resolveKeybinding(keybinding);
const usResolvedKeybinding = new USLayoutResolvedKeybinding(keybinding, OS);
const usResolvedKeybinding = new USLayoutResolvedKeybinding(createRuntimeKeybinding(keybinding, OS), OS);
return {
strKeybinding: strKeybinding,
keybinding: keybinding,
......
......@@ -7,7 +7,7 @@
import * as nls from 'vs/nls';
import { IHTMLContentElement } from 'vs/base/common/htmlContent';
import { IJSONSchema } from 'vs/base/common/jsonSchema';
import { ResolvedKeybinding, Keybinding, createKeybinding, KeyCode, USER_SETTINGS } from 'vs/base/common/keyCodes';
import { ResolvedKeybinding, Keybinding, createKeybinding, KeyCode, USER_SETTINGS, RuntimeKeybinding, RuntimeKeybindingType, SimpleRuntimeKeybinding, KeyCodeUtils, createRuntimeKeybinding } from 'vs/base/common/keyCodes';
import { PrintableKeypress, UILabelProvider, AriaLabelProvider, UserSettingsLabelProvider } from 'vs/platform/keybinding/common/keybindingLabels';
import { OS, OperatingSystem } from 'vs/base/common/platform';
import { toDisposable } from 'vs/base/common/lifecycle';
......@@ -122,9 +122,9 @@ let keybindingsExtPoint = ExtensionsRegistry.registerExtensionPoint<ContributedK
export class FancyResolvedKeybinding extends ResolvedKeybinding {
private readonly _actual: Keybinding;
private readonly _actual: RuntimeKeybinding;
constructor(actual: Keybinding) {
constructor(actual: RuntimeKeybinding) {
super();
this._actual = actual;
}
......@@ -181,43 +181,35 @@ export class FancyResolvedKeybinding extends ResolvedKeybinding {
}
public isChord(): boolean {
return this._actual.isChord();
return (this._actual.type === RuntimeKeybindingType.Chord);
}
public hasCtrlModifier(): boolean {
if (this._actual.isChord()) {
if (this._actual.type === RuntimeKeybindingType.Chord) {
return false;
}
if (OS === OperatingSystem.Macintosh) {
return this._actual.hasWinCtrl();
} else {
return this._actual.hasCtrlCmd();
}
return this._actual.ctrlKey;
}
public hasShiftModifier(): boolean {
if (this._actual.isChord()) {
if (this._actual.type === RuntimeKeybindingType.Chord) {
return false;
}
return this._actual.hasShift();
return this._actual.shiftKey;
}
public hasAltModifier(): boolean {
if (this._actual.isChord()) {
if (this._actual.type === RuntimeKeybindingType.Chord) {
return false;
}
return this._actual.hasAlt();
return this._actual.altKey;
}
public hasMetaModifier(): boolean {
if (this._actual.isChord()) {
if (this._actual.type === RuntimeKeybindingType.Chord) {
return false;
}
if (OS === OperatingSystem.Macintosh) {
return this._actual.hasCtrlCmd();
} else {
return this._actual.hasWinCtrl();
}
return this._actual.metaKey;
}
public getDispatchParts(): [string, string] {
......@@ -226,15 +218,35 @@ export class FancyResolvedKeybinding extends ResolvedKeybinding {
if (this._actual === null) {
keypressFirstPart = null;
keypressChordPart = null;
} else if (this._actual.isChord()) {
keypressFirstPart = this._actual.extractFirstPart().value.toString();
keypressChordPart = this._actual.extractChordPart().value.toString();
} else if (this._actual.type === RuntimeKeybindingType.Chord) {
keypressFirstPart = this._getDispatchPart(this._actual.firstPart);
keypressChordPart = this._getDispatchPart(this._actual.chordPart);
} else {
keypressFirstPart = this._actual.value.toString();
keypressFirstPart = this._getDispatchPart(this._actual);
keypressChordPart = null;
}
return [keypressFirstPart, keypressChordPart];
}
private _getDispatchPart(keybinding: SimpleRuntimeKeybinding): string {
let result = '';
if (keybinding.ctrlKey) {
result += 'ctrl+';
}
if (keybinding.shiftKey) {
result += 'shift+';
}
if (keybinding.altKey) {
result += 'alt+';
}
if (keybinding.metaKey) {
result += 'meta+';
}
result += KeyCodeUtils.toString(keybinding.keyCode);
return result;
}
}
export class WorkbenchKeybindingService extends AbstractKeybindingService {
......@@ -349,7 +361,7 @@ export class WorkbenchKeybindingService extends AbstractKeybindingService {
}
protected _createResolvedKeybinding(kb: Keybinding): ResolvedKeybinding {
return new FancyResolvedKeybinding(kb);
return new FancyResolvedKeybinding(createRuntimeKeybinding(kb, OS));
}
private _handleKeybindingsExtensionPointUser(isBuiltin: boolean, keybindings: ContributedKeyBinding | ContributedKeyBinding[], collector: ExtensionMessageCollector): boolean {
......
......@@ -5,7 +5,7 @@
'use strict';
import * as assert from 'assert';
import { createKeybinding, KeyCode, KeyMod, KeyChord, KeyCodeUtils } from 'vs/base/common/keyCodes';
import { createKeybinding, KeyCode, KeyMod, KeyChord, KeyCodeUtils, createRuntimeKeybinding } from 'vs/base/common/keyCodes';
import { KeybindingIO } from 'vs/workbench/services/keybinding/common/keybindingIO';
import { OS, OperatingSystem } from 'vs/base/common/platform';
import { IUserFriendlyKeybinding } from 'vs/platform/keybinding/common/keybinding';
......@@ -31,7 +31,7 @@ suite('keybindingIO', () => {
if (ignore[keyCode]) {
continue;
}
let usLayoutResolvedKeybinding = new USLayoutResolvedKeybinding(createKeybinding(keyCode), OS);
let usLayoutResolvedKeybinding = new USLayoutResolvedKeybinding(createRuntimeKeybinding(createKeybinding(keyCode), OS), OS);
let userSettings = usLayoutResolvedKeybinding.getUserSettingsLabel();
testIsGood(userSettings, keyCode + ' - ' + KeyCodeUtils.toString(keyCode) + ' - ' + userSettings);
}
......@@ -57,7 +57,7 @@ suite('keybindingIO', () => {
test('serialize/deserialize', function () {
function testOneSerialization(keybinding: number, expected: string, msg: string, OS: OperatingSystem): void {
let usLayoutResolvedKeybinding = new USLayoutResolvedKeybinding(createKeybinding(keybinding), OS);
let usLayoutResolvedKeybinding = new USLayoutResolvedKeybinding(createRuntimeKeybinding(createKeybinding(keybinding), OS), OS);
let actualSerialized = usLayoutResolvedKeybinding.getUserSettingsLabel();
assert.equal(actualSerialized, expected, expected + ' - ' + msg);
}
......
......@@ -6,7 +6,7 @@
'use strict';
import * as assert from 'assert';
import { KeyMod, KeyCode, SimpleKeybinding, createKeybinding, Keybinding } from 'vs/base/common/keyCodes';
import { KeyMod, KeyCode, SimpleKeybinding, createKeybinding, Keybinding, createRuntimeKeybinding } from 'vs/base/common/keyCodes';
import { KeyboardMapper, IKeyboardMapping } from 'vs/workbench/services/keybinding/common/keyboardMapper';
import { OperatingSystem } from 'vs/base/common/platform';
import { UserSettingsLabelProvider, PrintableKeypress } from 'vs/platform/keybinding/common/keybindingLabels';
......@@ -179,7 +179,7 @@ function _assertKeybindingTranslation(mapper: KeyboardMapper, OS: OperatingSyste
} else {
expected = [];
}
let keybindingLabel = new USLayoutResolvedKeybinding(createKeybinding(kb), OS).getUserSettingsLabel();
let keybindingLabel = new USLayoutResolvedKeybinding(createRuntimeKeybinding(createKeybinding(kb), OS), OS).getUserSettingsLabel();
let actualHardwareKeypresses = mapper.simpleKeybindingToHardwareKeypress(new SimpleKeybinding(kb));
if (actualHardwareKeypresses.length === 0) {
......@@ -201,7 +201,7 @@ function _assertKeybindingTranslation(mapper: KeyboardMapper, OS: OperatingSyste
return;
}
const reversedLabel = new USLayoutResolvedKeybinding(reversed, OS).getUserSettingsLabel();
const reversedLabel = new USLayoutResolvedKeybinding(createRuntimeKeybinding(reversed, OS), OS).getUserSettingsLabel();
assert.equal(reversedLabel, keybindingLabel, `${keybindingLabel} -> ${hardwareKeypressLabel} -> ${reversedLabel}`);
});
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册