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

Allow custom keypresses to NormalizedKeybindingItem

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