提交 95f4f8d0 编写于 作者: A Alex Dima

Allow custom keypresses to NormalizedKeybindingItem

上级 6d295709
......@@ -23,7 +23,7 @@ import { NormalizedKeybindingItem } from 'vs/platform/keybinding/common/normaliz
import { OS, OperatingSystem } from 'vs/base/common/platform';
/**
* Do not instantiate. Use KeybindingService to get a ResolvedKeybinding.
* Do not instantiate. Use KeybindingService to get a ResolvedKeybinding seeded with information about the current kb layout.
*/
export class USLayoutResolvedKeybinding extends ResolvedKeybinding {
......@@ -57,17 +57,17 @@ export class USLayoutResolvedKeybinding extends ResolvedKeybinding {
}
public getLabel(): string {
const [firstPart, chordPart] = PrintableKeypress.fromKeybinding2(this._actual, USLayoutResolvedKeybinding._usKeyCodeToUILabel, this._os);
const [firstPart, chordPart] = PrintableKeypress.fromKeybinding(this._actual, USLayoutResolvedKeybinding._usKeyCodeToUILabel, this._os);
return UILabelProvider.toLabel2(firstPart, chordPart, this._os);
}
public getAriaLabel(): string {
const [firstPart, chordPart] = PrintableKeypress.fromKeybinding2(this._actual, USLayoutResolvedKeybinding._usKeyCodeToAriaLabel, this._os);
const [firstPart, chordPart] = PrintableKeypress.fromKeybinding(this._actual, USLayoutResolvedKeybinding._usKeyCodeToAriaLabel, this._os);
return AriaLabelProvider.toLabel2(firstPart, chordPart, this._os);
}
public getHTMLLabel(): IHTMLContentElement[] {
const [firstPart, chordPart] = PrintableKeypress.fromKeybinding2(this._actual, USLayoutResolvedKeybinding._usKeyCodeToUILabel, this._os);
const [firstPart, chordPart] = PrintableKeypress.fromKeybinding(this._actual, USLayoutResolvedKeybinding._usKeyCodeToUILabel, this._os);
return UILabelProvider.toHTMLLabel2(firstPart, chordPart, this._os);
}
......@@ -98,7 +98,7 @@ export class USLayoutResolvedKeybinding extends ResolvedKeybinding {
return null;
}
const [firstPart, chordPart] = PrintableKeypress.fromKeybinding2(this._actual, USLayoutResolvedKeybinding._usKeyCodeToElectronAccelerator, this._os);
const [firstPart, chordPart] = PrintableKeypress.fromKeybinding(this._actual, USLayoutResolvedKeybinding._usKeyCodeToElectronAccelerator, this._os);
return ElectronAcceleratorLabelProvider.toLabel2(firstPart, chordPart, this._os);
}
......
......@@ -86,7 +86,7 @@ export class KeybindingIO {
}
public static writeKeybinding(keybinding: Keybinding, OS: OperatingSystem): string {
const [firstPart, chordPart] = PrintableKeypress.fromKeybinding2(keybinding, this._keyCodeToStr, OS);
const [firstPart, chordPart] = PrintableKeypress.fromKeybinding(keybinding, this._keyCodeToStr, OS);
let result = UserSettingsLabelProvider.toLabel2(firstPart, chordPart, OS);
return result.toLowerCase();
......
......@@ -10,13 +10,13 @@ import { OperatingSystem } from 'vs/base/common/platform';
import { IHTMLContentElement } from 'vs/base/common/htmlContent';
import { Keybinding, SimpleKeybinding, KeyCode } from 'vs/base/common/keyCodes';
export interface IKeyCodeLabelProvider2 {
export interface IKeyCodeLabelProvider {
(keyCode: KeyCode, OS: OperatingSystem): string;
}
export class PrintableKeypress {
public static fromSimpleKeybinding2(keybinding: SimpleKeybinding, labelProvider: IKeyCodeLabelProvider2, OS: OperatingSystem): PrintableKeypress {
public static fromSimpleKeybinding(keybinding: SimpleKeybinding, labelProvider: IKeyCodeLabelProvider, OS: OperatingSystem): PrintableKeypress {
const ctrlCmd = keybinding.hasCtrlCmd();
const winCtrl = keybinding.hasWinCtrl();
......@@ -31,13 +31,13 @@ export class PrintableKeypress {
return new PrintableKeypress(ctrlKey, shiftKey, altKey, metaKey, keyLabel);
}
public static fromKeybinding2(keybinding: Keybinding, labelProvider: IKeyCodeLabelProvider2, OS: OperatingSystem): [PrintableKeypress, PrintableKeypress] {
public static fromKeybinding(keybinding: Keybinding, labelProvider: IKeyCodeLabelProvider, OS: OperatingSystem): [PrintableKeypress, PrintableKeypress] {
if (keybinding.isChord()) {
const firstPart = PrintableKeypress.fromSimpleKeybinding2(keybinding.extractFirstPart(), labelProvider, OS);
const chordPart = PrintableKeypress.fromSimpleKeybinding2(keybinding.extractChordPart(), labelProvider, OS);
const firstPart = PrintableKeypress.fromSimpleKeybinding(keybinding.extractFirstPart(), labelProvider, OS);
const chordPart = PrintableKeypress.fromSimpleKeybinding(keybinding.extractChordPart(), labelProvider, OS);
return [firstPart, chordPart];
} else {
const printableKeypress = PrintableKeypress.fromSimpleKeybinding2(keybinding, labelProvider, OS);
const printableKeypress = PrintableKeypress.fromSimpleKeybinding(keybinding, labelProvider, OS);
return [printableKeypress, null];
}
}
......
......@@ -30,23 +30,26 @@ export class NormalizedKeybindingItem {
if (source.keybinding !== 0) {
keybinding = createKeybinding(source.keybinding);
}
return new NormalizedKeybindingItem(keybinding, source.command, source.commandArgs, when, isDefault);
}
constructor(keybinding: Keybinding, command: string, commandArgs: any, when: ContextKeyExpr, isDefault: boolean) {
this.keybinding = keybinding;
let keypressFirstPart: string;
let keypressChordPart: string;
if (keybinding === null) {
this.keypressFirstPart = null;
this.keypressChordPart = null;
keypressFirstPart = null;
keypressChordPart = null;
} else if (keybinding.isChord()) {
this.keypressFirstPart = keybinding.extractFirstPart().value.toString();
this.keypressChordPart = keybinding.extractChordPart().value.toString();
keypressFirstPart = keybinding.extractFirstPart().value.toString();
keypressChordPart = keybinding.extractChordPart().value.toString();
} else {
this.keypressFirstPart = keybinding.value.toString();
this.keypressChordPart = null;
keypressFirstPart = keybinding.value.toString();
keypressChordPart = null;
}
return new NormalizedKeybindingItem(keybinding, keypressFirstPart, keypressChordPart, source.command, source.commandArgs, when, isDefault);
}
constructor(keybinding: Keybinding, keypressFirstPart: string, keypressChordPart: string, command: string, commandArgs: any, when: ContextKeyExpr, isDefault: boolean) {
this.keybinding = keybinding;
this.keypressFirstPart = keypressFirstPart;
this.keypressChordPart = keypressChordPart;
this.bubble = (command ? command.charCodeAt(0) === CharCode.Caret : false);
this.command = this.bubble ? command.substr(1) : command;
this.commandArgs = commandArgs;
......
......@@ -129,8 +129,14 @@ suite('AbstractKeybindingService', () => {
});
function kbItem(keybinding: number, command: string, when: ContextKeyExpr = null): NormalizedKeybindingItem {
let kb = (keybinding !== 0 ? createKeybinding(keybinding) : null);
return new NormalizedKeybindingItem(kb, command, null, when, true);
return NormalizedKeybindingItem.fromKeybindingItem({
keybinding: keybinding,
command: command,
commandArgs: null,
when: when,
weight1: 0,
weight2: 0
}, true);
}
function toUsLabel(keybinding: number): string {
......
......@@ -13,8 +13,14 @@ import { NormalizedKeybindingItem } from 'vs/platform/keybinding/common/normaliz
suite('KeybindingResolver', () => {
function kbItem(keybinding: number, command: string, commandArgs: any, when: ContextKeyExpr, isDefault: boolean): NormalizedKeybindingItem {
let kb = (keybinding !== 0 ? createKeybinding(keybinding) : null);
return new NormalizedKeybindingItem(kb, command, commandArgs, when, isDefault);
return NormalizedKeybindingItem.fromKeybindingItem({
keybinding: keybinding,
command: command,
commandArgs: commandArgs,
when: when,
weight1: 0,
weight2: 0
}, isDefault);
}
test('resolve key', function () {
......@@ -49,8 +55,8 @@ suite('KeybindingResolver', () => {
];
let actual = KeybindingResolver.combine(defaults, overrides);
assert.deepEqual(actual, [
new NormalizedKeybindingItem(createKeybinding(KeyCode.KEY_A), 'yes1', null, ContextKeyExpr.equals('1', 'a'), true),
new NormalizedKeybindingItem(createKeybinding(KeyCode.KEY_B), 'yes2', null, ContextKeyExpr.equals('2', 'b'), false),
kbItem(KeyCode.KEY_A, 'yes1', null, ContextKeyExpr.equals('1', 'a'), true),
kbItem(KeyCode.KEY_B, 'yes2', null, ContextKeyExpr.equals('2', 'b'), false),
]);
});
......@@ -64,9 +70,9 @@ suite('KeybindingResolver', () => {
];
let actual = KeybindingResolver.combine(defaults, overrides);
assert.deepEqual(actual, [
new NormalizedKeybindingItem(createKeybinding(KeyCode.KEY_A), 'yes1', null, ContextKeyExpr.equals('1', 'a'), true),
new NormalizedKeybindingItem(createKeybinding(KeyCode.KEY_B), 'yes2', null, ContextKeyExpr.equals('2', 'b'), true),
new NormalizedKeybindingItem(createKeybinding(KeyCode.KEY_C), 'yes3', null, ContextKeyExpr.equals('3', 'c'), false),
kbItem(KeyCode.KEY_A, 'yes1', null, ContextKeyExpr.equals('1', 'a'), true),
kbItem(KeyCode.KEY_B, 'yes2', null, ContextKeyExpr.equals('2', 'b'), true),
kbItem(KeyCode.KEY_C, 'yes3', null, ContextKeyExpr.equals('3', 'c'), false),
]);
});
......@@ -80,8 +86,8 @@ suite('KeybindingResolver', () => {
];
let actual = KeybindingResolver.combine(defaults, overrides);
assert.deepEqual(actual, [
new NormalizedKeybindingItem(createKeybinding(KeyCode.KEY_A), 'yes1', null, ContextKeyExpr.equals('1', 'a'), true),
new NormalizedKeybindingItem(createKeybinding(KeyCode.KEY_B), 'yes2', null, ContextKeyExpr.equals('2', 'b'), true)
kbItem(KeyCode.KEY_A, 'yes1', null, ContextKeyExpr.equals('1', 'a'), true),
kbItem(KeyCode.KEY_B, 'yes2', null, ContextKeyExpr.equals('2', 'b'), true)
]);
});
......@@ -95,8 +101,8 @@ suite('KeybindingResolver', () => {
];
let actual = KeybindingResolver.combine(defaults, overrides);
assert.deepEqual(actual, [
new NormalizedKeybindingItem(createKeybinding(KeyCode.KEY_A), 'yes1', null, ContextKeyExpr.equals('1', 'a'), true),
new NormalizedKeybindingItem(createKeybinding(KeyCode.KEY_B), 'yes2', null, ContextKeyExpr.equals('2', 'b'), true)
kbItem(KeyCode.KEY_A, 'yes1', null, ContextKeyExpr.equals('1', 'a'), true),
kbItem(KeyCode.KEY_B, 'yes2', null, ContextKeyExpr.equals('2', 'b'), true)
]);
});
......@@ -110,7 +116,7 @@ suite('KeybindingResolver', () => {
];
let actual = KeybindingResolver.combine(defaults, overrides);
assert.deepEqual(actual, [
new NormalizedKeybindingItem(createKeybinding(KeyCode.KEY_B), 'yes2', null, ContextKeyExpr.equals('2', 'b'), true)
kbItem(KeyCode.KEY_B, 'yes2', null, ContextKeyExpr.equals('2', 'b'), true)
]);
});
......@@ -124,7 +130,7 @@ suite('KeybindingResolver', () => {
];
let actual = KeybindingResolver.combine(defaults, overrides);
assert.deepEqual(actual, [
new NormalizedKeybindingItem(createKeybinding(KeyCode.KEY_B), 'yes2', null, ContextKeyExpr.equals('2', 'b'), true)
kbItem(KeyCode.KEY_B, 'yes2', null, ContextKeyExpr.equals('2', 'b'), true)
]);
});
......@@ -138,7 +144,7 @@ suite('KeybindingResolver', () => {
];
let actual = KeybindingResolver.combine(defaults, overrides);
assert.deepEqual(actual, [
new NormalizedKeybindingItem(createKeybinding(KeyCode.KEY_B), 'yes2', null, ContextKeyExpr.equals('2', 'b'), true)
kbItem(KeyCode.KEY_B, 'yes2', null, ContextKeyExpr.equals('2', 'b'), true)
]);
});
......@@ -152,7 +158,7 @@ suite('KeybindingResolver', () => {
];
let actual = KeybindingResolver.combine(defaults, overrides);
assert.deepEqual(actual, [
new NormalizedKeybindingItem(createKeybinding(KeyCode.KEY_B), 'yes2', null, ContextKeyExpr.equals('2', 'b'), true)
kbItem(KeyCode.KEY_B, 'yes2', null, ContextKeyExpr.equals('2', 'b'), true)
]);
});
......@@ -166,7 +172,7 @@ suite('KeybindingResolver', () => {
];
let actual = KeybindingResolver.combine(defaults, overrides);
assert.deepEqual(actual, [
new NormalizedKeybindingItem(createKeybinding(KeyCode.KEY_B), 'yes2', null, ContextKeyExpr.equals('2', 'b'), true)
kbItem(KeyCode.KEY_B, 'yes2', null, ContextKeyExpr.equals('2', 'b'), true)
]);
});
......
......@@ -130,21 +130,21 @@ export class FancyResolvedKeybinding extends ResolvedKeybinding {
public getLabel(): string {
const keyCodeLabelProvider = getNativeUIKeyCodeLabelProvider();
const [firstPart, chordPart] = PrintableKeypress.fromKeybinding2(this._actual, keyCodeLabelProvider, OS);
const [firstPart, chordPart] = PrintableKeypress.fromKeybinding(this._actual, keyCodeLabelProvider, OS);
return UILabelProvider.toLabel2(firstPart, chordPart, OS);
}
public getAriaLabel(): string {
const keyCodeLabelProvider = getNativeAriaKeyCodeLabelProvider();
const [firstPart, chordPart] = PrintableKeypress.fromKeybinding2(this._actual, keyCodeLabelProvider, OS);
const [firstPart, chordPart] = PrintableKeypress.fromKeybinding(this._actual, keyCodeLabelProvider, OS);
return AriaLabelProvider.toLabel2(firstPart, chordPart, OS);
}
public getHTMLLabel(): IHTMLContentElement[] {
const keyCodeLabelProvider = getNativeUIKeyCodeLabelProvider();
const [firstPart, chordPart] = PrintableKeypress.fromKeybinding2(this._actual, keyCodeLabelProvider, OS);
const [firstPart, chordPart] = PrintableKeypress.fromKeybinding(this._actual, keyCodeLabelProvider, OS);
return UILabelProvider.toHTMLLabel2(firstPart, chordPart, OS);
}
......
......@@ -7,7 +7,7 @@
import * as nativeKeymap from 'native-keymap';
import { KeyCode, KeyCodeUtils } from 'vs/base/common/keyCodes';
import { CharCode } from 'vs/base/common/charCode';
import { IKeyCodeLabelProvider2 } from 'vs/platform/keybinding/common/keybindingLabels';
import { IKeyCodeLabelProvider } from 'vs/platform/keybinding/common/keybindingLabels';
import { lookupKeyCode, setExtractKeyCode } from 'vs/base/browser/keyboardEvent';
import Platform = require('vs/base/common/platform');
......@@ -332,8 +332,8 @@ setExtractKeyCode((e: KeyboardEvent) => {
return lookupKeyCode(e);
});
let nativeUIKeyCodeLabelProvider: IKeyCodeLabelProvider2 = null;
export function getNativeUIKeyCodeLabelProvider(): IKeyCodeLabelProvider2 {
let nativeUIKeyCodeLabelProvider: IKeyCodeLabelProvider = null;
export function getNativeUIKeyCodeLabelProvider(): IKeyCodeLabelProvider {
if (!nativeUIKeyCodeLabelProvider) {
let remaps = getNativeLabelProviderRemaps();
nativeUIKeyCodeLabelProvider = (keyCode: KeyCode, OS: Platform.OperatingSystem): string => {
......@@ -360,8 +360,8 @@ export function getNativeUIKeyCodeLabelProvider(): IKeyCodeLabelProvider2 {
return nativeUIKeyCodeLabelProvider;
}
let nativeAriaKeyCodeLabelProvider: IKeyCodeLabelProvider2 = null;
export function getNativeAriaKeyCodeLabelProvider(): IKeyCodeLabelProvider2 {
let nativeAriaKeyCodeLabelProvider: IKeyCodeLabelProvider = null;
export function getNativeAriaKeyCodeLabelProvider(): IKeyCodeLabelProvider {
if (!nativeAriaKeyCodeLabelProvider) {
let remaps = getNativeLabelProviderRemaps();
nativeAriaKeyCodeLabelProvider = (keyCode: KeyCode, OS: Platform.OperatingSystem): string => {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册