提交 8a948060 编写于 作者: A Alex Dima

Implement getElectronAccelerator(), getUserSettingsLabel() in the mac/linux...

Implement getElectronAccelerator(), getUserSettingsLabel() in the mac/linux NativeResolvedKeybinding
上级 51a5be77
......@@ -116,24 +116,24 @@ export const ElectronAcceleratorLabelProvider = new ModifierLabelProvider(
*/
export const UserSettingsLabelProvider = new ModifierLabelProvider(
{
ctrlKey: 'Ctrl',
shiftKey: 'Shift',
altKey: 'Alt',
metaKey: 'Cmd',
ctrlKey: 'ctrl',
shiftKey: 'shift',
altKey: 'alt',
metaKey: 'cmd',
separator: '+',
},
{
ctrlKey: 'Ctrl',
shiftKey: 'Shift',
altKey: 'Alt',
metaKey: 'Win',
ctrlKey: 'ctrl',
shiftKey: 'shift',
altKey: 'alt',
metaKey: 'win',
separator: '+',
},
{
ctrlKey: 'Ctrl',
shiftKey: 'Shift',
altKey: 'Alt',
metaKey: 'Meta',
ctrlKey: 'ctrl',
shiftKey: 'shift',
altKey: 'alt',
metaKey: 'meta',
separator: '+',
}
);
......
......@@ -6,11 +6,11 @@
'use strict';
import { OperatingSystem } from 'vs/base/common/platform';
import { KeyCode, ResolvedKeybinding, KeyCodeUtils, SimpleKeybinding, Keybinding, KeybindingType } from 'vs/base/common/keyCodes';
import { KeyCode, ResolvedKeybinding, KeyCodeUtils, SimpleKeybinding, Keybinding, KeybindingType, USER_SETTINGS } from 'vs/base/common/keyCodes';
import { KeyboardEventCode, KeyboardEventCodeUtils, IMMUTABLE_CODE_TO_KEY_CODE } from 'vs/workbench/services/keybinding/common/keyboardEventCode';
import { CharCode } from 'vs/base/common/charCode';
import { IHTMLContentElement } from 'vs/base/common/htmlContent';
import { UILabelProvider, AriaLabelProvider } from 'vs/platform/keybinding/common/keybindingLabels';
import { UILabelProvider, AriaLabelProvider, UserSettingsLabelProvider, ElectronAcceleratorLabelProvider } from 'vs/platform/keybinding/common/keybindingLabels';
import { IKeyboardMapper } from 'vs/workbench/services/keybinding/common/keyboardMapper';
export interface IKeyMapping {
......@@ -88,28 +88,19 @@ export class NativeResolvedKeybinding extends ResolvedKeybinding {
}
public getElectronAccelerator(): string {
throw new Error('TODO!');
// const usResolvedKeybinding = new USLayoutResolvedKeybinding(this._actual, OS);
// if (OS === OperatingSystem.Windows) {
// // electron menus always do the correct rendering on Windows
// return usResolvedKeybinding.getElectronAccelerator();
// }
// let usLabel = usResolvedKeybinding.getLabel();
// let label = this.getLabel();
// if (usLabel !== label) {
// // electron menus are incorrect in rendering (linux) and in rendering and interpreting (mac)
// // for non US standard keyboard layouts
// return null;
// }
if (this._chordPart !== null) {
// Electron cannot handle chords
return null;
}
// return usResolvedKeybinding.getElectronAccelerator();
let firstPart = this._firstPart ? this._mapper.getElectronLabelForHardwareCode(this._firstPart.code) : null;
return ElectronAcceleratorLabelProvider.toLabel(this._firstPart, firstPart, null, null, this._OS);
}
public getUserSettingsLabel(): string {
throw new Error('TODO!');
// return KeybindingIO.writeKeybinding(this._actual, OS);
let firstPart = this._firstPart ? this._mapper.getUserSettingsLabel(this._firstPart.code) : null;
let chordPart = this._chordPart ? this._mapper.getUserSettingsLabel(this._chordPart.code) : null;
return UserSettingsLabelProvider.toLabel(this._firstPart, firstPart, this._chordPart, chordPart, this._OS);
}
public isChord(): boolean {
......@@ -145,34 +136,10 @@ export class NativeResolvedKeybinding extends ResolvedKeybinding {
}
public getDispatchParts(): [string, string] {
throw new Error('TODO');
// let firstPart = this._firstPart ? this._getDispatchStr(this._firstPart) : null;
// let chordPart = this._chordPart ? this._getDispatchStr(this._chordPart) : null;
// return [firstPart, chordPart];
}
// private _getDispatchStr(keybinding: HardwareKeypress): string {
// if (keybinding.isModifierKey()) {
// return null;
// }
// 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;
// }
let firstPart = this._firstPart ? this._mapper.getDispatchStrForHardwareKeypress(this._firstPart) : null;
let chordPart = this._chordPart ? this._mapper.getDispatchStrForHardwareKeypress(this._chordPart) : null;
return [firstPart, chordPart];
}
}
interface IHardwareCodeMapping {
......@@ -549,6 +516,83 @@ export class MacLinuxKeyboardMapper implements IKeyboardMapper {
return result;
}
public getUserSettingsLabel(code: KeyboardEventCode): string {
const immutableKeyCode = IMMUTABLE_CODE_TO_KEY_CODE[code];
if (immutableKeyCode !== -1) {
return USER_SETTINGS.fromKeyCode(immutableKeyCode).toLowerCase();
}
// Check if this hw code always maps to the same kb code and back
let constantKeyCode: KeyCode = this._getStableKeyCodeForHWCode(code);
if (constantKeyCode !== -1) {
return USER_SETTINGS.fromKeyCode(constantKeyCode).toLowerCase();
}
return this._hwToDispatch[code];
}
private _getElectronLabelForKeyCode(keyCode: KeyCode): string {
if (keyCode >= KeyCode.NUMPAD_0 && keyCode <= KeyCode.NUMPAD_DIVIDE) {
// Electron cannot handle numpad keys
return null;
}
switch (keyCode) {
case KeyCode.UpArrow:
return 'Up';
case KeyCode.DownArrow:
return 'Down';
case KeyCode.LeftArrow:
return 'Left';
case KeyCode.RightArrow:
return 'Right';
}
// electron menus always do the correct rendering on Windows
return KeyCodeUtils.toString(keyCode);
}
public getElectronLabelForHardwareCode(code: KeyboardEventCode): string {
const immutableKeyCode = IMMUTABLE_CODE_TO_KEY_CODE[code];
if (immutableKeyCode !== -1) {
return this._getElectronLabelForKeyCode(immutableKeyCode);
}
// Check if this hw code always maps to the same kb code and back
let constantKeyCode: KeyCode = this._getStableKeyCodeForHWCode(code);
if (constantKeyCode !== -1) {
return this._getElectronLabelForKeyCode(constantKeyCode);
}
return null;
}
private _getStableKeyCodeForHWCode(code: KeyboardEventCode): KeyCode {
// Check if this hw code always maps to the same kb code and back
let constantKeyCode: KeyCode = -1;
for (let mod = 0; mod < 8; mod++) {
const hwEncoded = ((code << 3) + mod) >>> 0;
const kbEncoded = this._hwToKb[hwEncoded];
const keyCode = (kbEncoded >>> 3);
if (constantKeyCode === -1) {
constantKeyCode = keyCode;
} else if (constantKeyCode !== keyCode) {
// maps to different keyCode
return -1;
}
// Check that the inverse is true
const inverse = this._kbToHw[kbEncoded];
if (inverse.length !== 1) {
// multiple hw keypresses map to this kb
return -1;
}
}
return constantKeyCode;
}
public resolveKeybinding(keybinding: Keybinding): NativeResolvedKeybinding[] {
let result: NativeResolvedKeybinding[] = [], resultLen = 0;
......
......@@ -11,6 +11,7 @@ import { IKeyboardMapper } from 'vs/workbench/services/keybinding/common/keyboar
import { Keybinding } from 'vs/base/common/keyCodes';
import { TPromise } from 'vs/base/common/winjs.base';
import { readFile, writeFile } from 'vs/base/node/pfs';
import { OperatingSystem } from 'vs/base/common/platform';
export interface IResolvedKeybinding {
label: string;
......@@ -45,10 +46,10 @@ export function assertResolveKeybinding(mapper: IKeyboardMapper, keybinding: Key
assert.deepEqual(actual, expected);
}
function _htmlPieces(pieces: string[]): IHTMLContentElement[] {
function _htmlPieces(pieces: string[], OS: OperatingSystem): IHTMLContentElement[] {
let children: IHTMLContentElement[] = [];
for (let i = 0, len = pieces.length; i < len; i++) {
if (i !== 0) {
if (i !== 0 && OS !== OperatingSystem.Macintosh) {
children.push({ tagName: 'span', text: '+' });
}
children.push({ tagName: 'span', className: 'monaco-kbkey', text: pieces[i] });
......@@ -56,22 +57,22 @@ function _htmlPieces(pieces: string[]): IHTMLContentElement[] {
return children;
}
export function simpleHTMLLabel(pieces: string[]): IHTMLContentElement {
export function simpleHTMLLabel(pieces: string[], OS: OperatingSystem): IHTMLContentElement {
return {
tagName: 'span',
className: 'monaco-kb',
children: _htmlPieces(pieces)
children: _htmlPieces(pieces, OS)
};
}
export function chordHTMLLabel(firstPart: string[], chordPart: string[]): IHTMLContentElement {
export function chordHTMLLabel(firstPart: string[], chordPart: string[], OS: OperatingSystem): IHTMLContentElement {
return {
tagName: 'span',
className: 'monaco-kb',
children: [].concat(
_htmlPieces(firstPart),
_htmlPieces(firstPart, OS),
[{ tagName: 'span', text: ' ' }],
_htmlPieces(chordPart)
_htmlPieces(chordPart, OS)
)
};
}
......
......@@ -14,7 +14,7 @@ import { USLayoutResolvedKeybinding } from 'vs/platform/keybinding/common/usLayo
import { KeyboardEventCodeUtils } from 'vs/workbench/services/keybinding/common/keyboardEventCode';
import { IHTMLContentElement } from 'vs/base/common/htmlContent';
import { TPromise } from 'vs/base/common/winjs.base';
import { readRawMapping, assertMapping, IResolvedKeybinding, assertResolveKeybinding } from 'vs/workbench/services/keybinding/test/keyboardMapperTestUtils';
import { readRawMapping, assertMapping, IResolvedKeybinding, assertResolveKeybinding, simpleHTMLLabel } from 'vs/workbench/services/keybinding/test/keyboardMapperTestUtils';
function createKeyboardMapper(file: string, OS: OperatingSystem): TPromise<MacLinuxKeyboardMapper> {
return readRawMapping<IKeyboardMapping>(file).then((rawMappings) => {
......@@ -45,6 +45,14 @@ suite('keyboardMapper - MAC de_ch', () => {
assertResolveKeybinding(mapper, createKeybinding(k, OperatingSystem.Macintosh), expected);
}
function _simpleHTMLLabel(pieces: string[]): IHTMLContentElement {
return simpleHTMLLabel(pieces, OperatingSystem.Macintosh);
}
// function _chordHTMLLabel(firstPart: string[], chordPart: string[]): IHTMLContentElement {
// return chordHTMLLabel(firstPart, chordPart, OperatingSystem.Macintosh);
// }
test('kb => hw', () => {
// unchanged
assertKeybindingTranslation(KeyMod.CtrlCmd | KeyCode.KEY_1, 'Cmd+Digit1');
......@@ -104,24 +112,24 @@ suite('keyboardMapper - MAC de_ch', () => {
);
});
// test('resolveKeybinding Cmd+B', () => {
// _assertResolveKeybinding(
// KeyMod.CtrlCmd | KeyCode.KEY_B,
// [{
// label: '⌘A',
// ariaLabel: 'Control+A',
// HTMLLabel: [simpleHTMLLabel(['Ctrl', 'A'])],
// electronAccelerator: 'Ctrl+A',
// userSettingsLabel: 'ctrl+a',
// isChord: false,
// hasCtrlModifier: true,
// hasShiftModifier: false,
// hasAltModifier: false,
// hasMetaModifier: false,
// dispatchParts: ['ctrl+A', null],
// }]
// );
// });
test('resolveKeybinding Cmd+B', () => {
_assertResolveKeybinding(
KeyMod.CtrlCmd | KeyCode.KEY_B,
[{
label: '⌘B',
ariaLabel: 'Command+B',
HTMLLabel: [_simpleHTMLLabel(['', 'B'])],
electronAccelerator: 'Cmd+B',
userSettingsLabel: 'cmd+b',
isChord: false,
hasCtrlModifier: false,
hasShiftModifier: false,
hasAltModifier: false,
hasMetaModifier: true,
dispatchParts: ['meta+[KeyB]', null],
}]
);
});
});
......
......@@ -10,6 +10,7 @@ import { TPromise } from 'vs/base/common/winjs.base';
import { WindowsKeyboardMapper, IKeyboardMapping } from 'vs/workbench/services/keybinding/common/windowsKeyboardMapper';
import { createKeybinding, KeyMod, KeyCode, KeyChord } from 'vs/base/common/keyCodes';
import { IResolvedKeybinding, assertResolveKeybinding, readRawMapping, assertMapping, simpleHTMLLabel, chordHTMLLabel } from 'vs/workbench/services/keybinding/test/keyboardMapperTestUtils';
import { IHTMLContentElement } from 'vs/base/common/htmlContent';
function createKeyboardMapper(file: string): TPromise<WindowsKeyboardMapper> {
return readRawMapping<IKeyboardMapping>(file).then((rawMappings) => {
......@@ -21,6 +22,14 @@ function _assertResolveKeybinding(mapper: WindowsKeyboardMapper, k: number, expe
assertResolveKeybinding(mapper, createKeybinding(k, OperatingSystem.Windows), [expected]);
}
function _simpleHTMLLabel(pieces: string[]): IHTMLContentElement {
return simpleHTMLLabel(pieces, OperatingSystem.Windows);
}
function _chordHTMLLabel(firstPart: string[], chordPart: string[]): IHTMLContentElement {
return chordHTMLLabel(firstPart, chordPart, OperatingSystem.Windows);
}
suite('keyboardMapper - WINDOWS de_ch', () => {
let mapper: WindowsKeyboardMapper;
......@@ -43,7 +52,7 @@ suite('keyboardMapper - WINDOWS de_ch', () => {
{
label: 'Ctrl+A',
ariaLabel: 'Control+A',
HTMLLabel: [simpleHTMLLabel(['Ctrl', 'A'])],
HTMLLabel: [_simpleHTMLLabel(['Ctrl', 'A'])],
electronAccelerator: 'Ctrl+A',
userSettingsLabel: 'ctrl+a',
isChord: false,
......@@ -63,7 +72,7 @@ suite('keyboardMapper - WINDOWS de_ch', () => {
{
label: 'Ctrl+Z',
ariaLabel: 'Control+Z',
HTMLLabel: [simpleHTMLLabel(['Ctrl', 'Z'])],
HTMLLabel: [_simpleHTMLLabel(['Ctrl', 'Z'])],
electronAccelerator: 'Ctrl+Z',
userSettingsLabel: 'ctrl+z',
isChord: false,
......@@ -83,7 +92,7 @@ suite('keyboardMapper - WINDOWS de_ch', () => {
{
label: 'Ctrl+^',
ariaLabel: 'Control+^',
HTMLLabel: [simpleHTMLLabel(['Ctrl', '^'])],
HTMLLabel: [_simpleHTMLLabel(['Ctrl', '^'])],
electronAccelerator: 'Ctrl+]',
userSettingsLabel: 'ctrl+]',
isChord: false,
......@@ -103,7 +112,7 @@ suite('keyboardMapper - WINDOWS de_ch', () => {
{
label: 'Ctrl+§',
ariaLabel: 'Control+§',
HTMLLabel: [simpleHTMLLabel(['Ctrl', '§'])],
HTMLLabel: [_simpleHTMLLabel(['Ctrl', '§'])],
electronAccelerator: 'Ctrl+/',
userSettingsLabel: 'ctrl+/',
isChord: false,
......@@ -123,7 +132,7 @@ suite('keyboardMapper - WINDOWS de_ch', () => {
{
label: 'Ctrl+K Ctrl+ä',
ariaLabel: 'Control+K Control+ä',
HTMLLabel: [chordHTMLLabel(['Ctrl', 'K'], ['Ctrl', 'ä'])],
HTMLLabel: [_chordHTMLLabel(['Ctrl', 'K'], ['Ctrl', 'ä'])],
electronAccelerator: null,
userSettingsLabel: 'ctrl+k ctrl+\\',
isChord: true,
......@@ -159,7 +168,7 @@ suite('keyboardMapper - WINDOWS en_us', () => {
{
label: 'Ctrl+K Ctrl+\\',
ariaLabel: 'Control+K Control+\\',
HTMLLabel: [chordHTMLLabel(['Ctrl', 'K'], ['Ctrl', '\\'])],
HTMLLabel: [_chordHTMLLabel(['Ctrl', 'K'], ['Ctrl', '\\'])],
electronAccelerator: null,
userSettingsLabel: 'ctrl+k ctrl+\\',
isChord: true,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册