提交 f4b6f17c 编写于 作者: P Peng Lyu

switch to user selected keyboard layout

上级 9e559490
......@@ -6,7 +6,7 @@
import * as nls from 'vs/nls';
import { StatusbarAlignment, IStatusbarService, IStatusbarEntryAccessor } from 'vs/platform/statusbar/common/statusbar';
import { Disposable, MutableDisposable } from 'vs/base/common/lifecycle';
import { IKeymapService, areKeyboardLayoutsEqual, parseKeyboardLayout } from 'vs/workbench/services/keybinding/common/keymapInfo';
import { IKeymapService, areKeyboardLayoutsEqual, parseKeyboardLayoutDescription, getKeyboardLayoutId, IKeyboardLayoutInfo } from 'vs/workbench/services/keybinding/common/keymapInfo';
import { LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle';
import { Registry } from 'vs/platform/registry/common/platform';
import { Extensions as WorkbenchExtensions, IWorkbenchContribution, IWorkbenchContributionsRegistry } from 'vs/workbench/common/contributions';
......@@ -34,7 +34,7 @@ export class KeyboardLayoutPickerContribution extends Disposable implements IWor
let layout = this.keymapService.getCurrentKeyboardLayout();
if (layout) {
let layoutInfo = parseKeyboardLayout(layout);
let layoutInfo = parseKeyboardLayoutDescription(layout);
this.pickerElement.value = this.statusbarService.addEntry(
{
text: `Layout: ${layoutInfo.label}`,
......@@ -49,7 +49,7 @@ export class KeyboardLayoutPickerContribution extends Disposable implements IWor
this._register(keymapService.onDidChangeKeyboardMapper(() => {
let layout = this.keymapService.getCurrentKeyboardLayout();
let layoutInfo = parseKeyboardLayout(layout);
let layoutInfo = parseKeyboardLayoutDescription(layout);
if (this.pickerElement.value) {
this.pickerElement.value.update({
......@@ -75,6 +75,9 @@ export class KeyboardLayoutPickerContribution extends Disposable implements IWor
const workbenchContributionsRegistry = Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench);
workbenchContributionsRegistry.registerWorkbenchContribution(KeyboardLayoutPickerContribution, LifecyclePhase.Starting);
interface LayoutQuickPickItem extends IQuickPickItem {
layout: IKeyboardLayoutInfo;
}
export class KeyboardLayoutPickerAction extends Action {
static readonly ID = KEYBOARD_LAYOUT_OPEN_PICKER;
......@@ -111,8 +114,9 @@ export class KeyboardLayoutPickerAction extends Action {
const picks: QuickPickInput[] = layouts.map(layout => {
const picked = !isAutoDetect && areKeyboardLayoutsEqual(currentLayout, layout);
const layoutInfo = parseKeyboardLayout(layout);
const layoutInfo = parseKeyboardLayoutDescription(layout);
return {
layout: layout,
label: [layoutInfo.label, (layout && layout.isUserKeyboardLayout) ? '(User configured layout)' : ''].join(' '),
id: (<any>layout).text || (<any>layout).lang || (<any>layout).layout,
description: layoutInfo.description + (picked ? ' (Current layout)' : ''),
......@@ -134,7 +138,7 @@ export class KeyboardLayoutPickerAction extends Action {
// Offer to "Auto Detect"
const autoDetectMode: IQuickPickItem = {
label: nls.localize('autoDetect', "Auto Detect"),
description: isAutoDetect ? `Current: ${parseKeyboardLayout(currentLayout).label}` : undefined,
description: isAutoDetect ? `Current: ${parseKeyboardLayoutDescription(currentLayout).label}` : undefined,
picked: isAutoDetect ? true : undefined
};
......@@ -171,7 +175,7 @@ export class KeyboardLayoutPickerAction extends Action {
return Promise.resolve();
}
this.configurationService.updateValue('keyboard.layout', pick.label);
this.configurationService.updateValue('keyboard.layout', getKeyboardLayoutId((<LayoutQuickPickItem>pick).layout));
}
}
......
......@@ -6,11 +6,11 @@
import * as nls from 'vs/nls';
import { Emitter, Event } from 'vs/base/common/event';
import { Disposable, toDisposable, IDisposable, MutableDisposable } from 'vs/base/common/lifecycle';
import { IKeymapService, IKeyboardLayoutInfo, IKeyboardMapping, IWindowsKeyboardMapping, IWindowsKeyboardLayoutInfo, IMacKeyboardLayoutInfo, ILinuxKeyboardLayoutInfo, KeymapInfo, IRawMixedKeyboardMapping } from 'vs/workbench/services/keybinding/common/keymapInfo';
import { IKeymapService, IKeyboardLayoutInfo, IKeyboardMapping, IWindowsKeyboardMapping, KeymapInfo, IRawMixedKeyboardMapping, getKeyboardLayoutId } from 'vs/workbench/services/keybinding/common/keymapInfo';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { DispatchConfig } from 'vs/workbench/services/keybinding/common/dispatchConfig';
import { IKeyboardMapper, CachedKeyboardMapper } from 'vs/workbench/services/keybinding/common/keyboardMapper';
import { OS, OperatingSystem, isMacintosh, isWindows, isLinux } from 'vs/base/common/platform';
import { OS, OperatingSystem, isMacintosh, isWindows } from 'vs/base/common/platform';
import { WindowsKeyboardMapper } from 'vs/workbench/services/keybinding/common/windowsKeyboardMapper';
import { MacLinuxFallbackKeyboardMapper } from 'vs/workbench/services/keybinding/common/macLinuxFallbackKeyboardMapper';
import { IKeyboardEvent } from 'vs/platform/keybinding/common/keybinding';
......@@ -41,6 +41,10 @@ export class BrowserKeyboardMapperFactory {
private _mru: KeymapInfo[];
private _activeKeymapInfo: KeymapInfo | null;
get activeKeymap(): KeymapInfo | null {
return this._activeKeymapInfo;
}
get keymapInfos(): KeymapInfo[] {
return this._keymapInfos;
}
......@@ -233,21 +237,7 @@ export class BrowserKeyboardMapperFactory {
}
public setKeyboardLayout(layoutName: string) {
let allKeyboardLayouts = this.keymapInfos;
let matchedLayouts: KeymapInfo[] = [];
if (isWindows) {
matchedLayouts = allKeyboardLayouts.filter(layout => (<IWindowsKeyboardLayoutInfo>layout.layout).name === layoutName);
}
if (isMacintosh) {
// todo, probably we should use layout.id?
matchedLayouts = allKeyboardLayouts.filter(layout => (<IMacKeyboardLayoutInfo>layout.layout).lang === layoutName);
}
if (isLinux) {
// todo, probably we should use layout.id?
matchedLayouts = allKeyboardLayouts.filter(layout => (<ILinuxKeyboardLayoutInfo>layout.layout).layout === layoutName);
}
let matchedLayouts: KeymapInfo[] = this.keymapInfos.filter(keymapInfo => getKeyboardLayoutId(keymapInfo.layout) === layoutName);
if (matchedLayouts.length > 0) {
this.setActiveKeymapInfo(matchedLayouts[0]);
......@@ -498,8 +488,8 @@ class BrowserKeymapService extends Disposable implements IKeymapService {
constructor(
@IEnvironmentService environmentService: IEnvironmentService,
@IConfigurationService configurationService: IConfigurationService,
@IFileService fileService: IFileService,
@IConfigurationService private configurationService: IConfigurationService,
) {
super();
const keyboardConfig = configurationService.getValue<{ layout: string }>('keyboard');
......@@ -530,6 +520,8 @@ class BrowserKeymapService extends Disposable implements IKeymapService {
this._userKeyboardLayout.initialize().then(() => {
if (this._userKeyboardLayout.keyboardLayout) {
BrowserKeyboardMapperFactory.INSTANCE.registerKeyboardLayout(this._userKeyboardLayout.keyboardLayout);
this.setUserKeyboardLayoutIfMatched();
}
});
......@@ -548,10 +540,24 @@ class BrowserKeymapService extends Disposable implements IKeymapService {
}
}
// TODO: trigger keymap update
this.setUserKeyboardLayoutIfMatched();
}));
}
setUserKeyboardLayoutIfMatched() {
const keyboardConfig = this.configurationService.getValue<{ layout: string }>('keyboard');
const layout = keyboardConfig.layout;
if (layout && this._userKeyboardLayout.keyboardLayout) {
if (getKeyboardLayoutId(this._userKeyboardLayout.keyboardLayout.layout) === layout && BrowserKeyboardMapperFactory.INSTANCE.activeKeymap) {
if (!this._userKeyboardLayout.keyboardLayout.equal(BrowserKeyboardMapperFactory.INSTANCE.activeKeymap)) {
BrowserKeyboardMapperFactory.INSTANCE.setActiveKeymapInfo(this._userKeyboardLayout.keyboardLayout);
}
}
}
}
registerKeyboardListener() {
this.layoutChangeListener.value = BrowserKeyboardMapperFactory.INSTANCE.onDidChangeKeyboardMapper(() => {
this._onDidChangeKeyboardMapper.fire();
......
......@@ -127,7 +127,7 @@ export function areKeyboardLayoutsEqual(a: IKeyboardLayoutInfo | null, b: IKeybo
return false;
}
export function parseKeyboardLayout(layout: IKeyboardLayoutInfo | null): { label: string, description: string } {
export function parseKeyboardLayoutDescription(layout: IKeyboardLayoutInfo | null): { label: string, description: string } {
if (!layout) {
return { label: '', description: '' };
}
......@@ -177,6 +177,18 @@ export function parseKeyboardLayout(layout: IKeyboardLayoutInfo | null): { label
};
}
export function getKeyboardLayoutId(layout: IKeyboardLayoutInfo): string {
if ((<IWindowsKeyboardLayoutInfo>layout).name) {
return (<IWindowsKeyboardLayoutInfo>layout).name;
}
if ((<IMacKeyboardLayoutInfo>layout).id) {
return (<IMacKeyboardLayoutInfo>layout).id;
}
return (<ILinuxKeyboardLayoutInfo>layout).layout;
}
function deserializeMapping(serializedMapping: ISerializedMapping) {
let mapping = serializedMapping;
......@@ -282,6 +294,18 @@ export class KeymapInfo {
return score;
}
equal(other: KeymapInfo): boolean {
if (this.isUserKeyboardLayout !== other.isUserKeyboardLayout) {
return false;
}
if (getKeyboardLayoutId(this.layout) !== getKeyboardLayoutId(other.layout)) {
return false;
}
return this.fuzzyEqual(other.mapping);
}
fuzzyEqual(other: IRawMixedKeyboardMapping): boolean {
for (let key in other) {
if (isWindows && (key === 'Backslash' || key === 'KeyQ')) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册