Skip to content

Commit

Permalink
Fixes #21780: Add keyboard.dispatch setting
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdima committed Mar 30, 2017
1 parent 02c2388 commit 822ab91
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class MacLinuxFallbackKeyboardMapper implements IKeyboardMapper {
*/
private readonly _OS: OperatingSystem;

constructor(rawMappings: IMacLinuxKeyboardMapping, OS: OperatingSystem) {
constructor(OS: OperatingSystem) {
this._OS = OS;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import { IStorageService, StorageScope } from 'vs/platform/storage/common/storag
import { Action } from 'vs/base/common/actions';
import { TPromise } from 'vs/base/common/winjs.base';
import Severity from 'vs/base/common/severity';
import { Extensions as ConfigExtensions, IConfigurationRegistry, IConfigurationNode } from 'vs/platform/configuration/common/configurationRegistry';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';

export class KeyboardMapperFactory {
public static INSTANCE = new KeyboardMapperFactory();
Expand All @@ -63,10 +65,14 @@ export class KeyboardMapperFactory {
}
}

public getKeyboardMapper(): IKeyboardMapper {
public getKeyboardMapper(dispatchConfig: DispatchConfig): IKeyboardMapper {
if (!this._initialized) {
this._setKeyboardData(nativeKeymap.getCurrentKeyboardLayout(), nativeKeymap.getKeyMap());
}
if (dispatchConfig === DispatchConfig.KeyCode) {
// Forcefully set to use keyCode
return new MacLinuxFallbackKeyboardMapper(OS);
}
return this._keyboardMapper;
}

Expand Down Expand Up @@ -127,7 +133,7 @@ export class KeyboardMapperFactory {

if (Object.keys(rawMapping).length === 0) {
// Looks like reading the mappings failed (most likely Mac + Japanese/Chinese keyboard layouts)
return new MacLinuxFallbackKeyboardMapper(<IMacLinuxKeyboardMapping>rawMapping, OS);
return new MacLinuxFallbackKeyboardMapper(OS);
}

return new MacLinuxKeyboardMapper(<IMacLinuxKeyboardMapping>rawMapping, OS);
Expand Down Expand Up @@ -266,6 +272,17 @@ class KeybindingsMigrationsStorage {
}
}

export const enum DispatchConfig {
Code,
KeyCode
}

function getDispatchConfig(configurationService: IConfigurationService): DispatchConfig {
const keyboard = configurationService.getConfiguration('keyboard');
const r = (keyboard ? (<any>keyboard).dispatch : null);
return (r === 'keyCode' ? DispatchConfig.KeyCode : DispatchConfig.Code);
}

export class WorkbenchKeybindingService extends AbstractKeybindingService {

private _keyboardMapper: IKeyboardMapper;
Expand All @@ -281,15 +298,29 @@ export class WorkbenchKeybindingService extends AbstractKeybindingService {
@IMessageService private messageService: IMessageService,
@IEnvironmentService environmentService: IEnvironmentService,
@IStorageService private storageService: IStorageService,
@IStatusbarService statusBarService: IStatusbarService
@IStatusbarService statusBarService: IStatusbarService,
@IConfigurationService private configurationService: IConfigurationService
) {
super(contextKeyService, commandService, messageService, statusBarService);

this._keyboardMapper = KeyboardMapperFactory.INSTANCE.getKeyboardMapper();
let dispatchConfig = getDispatchConfig(configurationService);
configurationService.onDidUpdateConfiguration((e) => {
let newDispatchConfig = getDispatchConfig(configurationService);
if (dispatchConfig === newDispatchConfig) {
return;
}

dispatchConfig = newDispatchConfig;
this._keyboardMapper = KeyboardMapperFactory.INSTANCE.getKeyboardMapper(dispatchConfig);
this.updateResolver({ source: KeybindingSource.Default });
});

this._keyboardMapper = KeyboardMapperFactory.INSTANCE.getKeyboardMapper(dispatchConfig);
KeyboardMapperFactory.INSTANCE.onDidChangeKeyboardMapper(() => {
this._keyboardMapper = KeyboardMapperFactory.INSTANCE.getKeyboardMapper();
this._keyboardMapper = KeyboardMapperFactory.INSTANCE.getKeyboardMapper(dispatchConfig);
this.updateResolver({ source: KeybindingSource.Default });
});

this._cachedResolver = null;
this._firstTimeComputingResolver = true;

Expand Down Expand Up @@ -597,3 +628,26 @@ let schema: IJSONSchema = {

let schemaRegistry = <IJSONContributionRegistry>Registry.as(Extensions.JSONContribution);
schemaRegistry.registerSchema(schemaId, schema);

if (OS === OperatingSystem.Macintosh || OS === OperatingSystem.Linux) {

const configurationRegistry = <IConfigurationRegistry>Registry.as(ConfigExtensions.Configuration);
const keyboardConfiguration: IConfigurationNode = {
'id': 'keyboard',
'order': 15,
'type': 'object',
'title': nls.localize('keyboardConfigurationTitle', "Keyboard"),
'overridable': true,
'properties': {
'keyboard.dispatch': {
'type': 'string',
'enum': ['code', 'keyCode'],
'default': 'code',
'description': nls.localize('dispatch', "Controls the dispatching logic for key presses to use either `keydown.code` (recommended) or `keydown.keyCode`.")
}
}
};

configurationRegistry.registerConfiguration(keyboardConfiguration);

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { ScanCodeBinding, ScanCode } from 'vs/workbench/services/keybinding/comm

suite('keyboardMapper - MAC fallback', () => {

let mapper = new MacLinuxFallbackKeyboardMapper({}, OperatingSystem.Macintosh);
let mapper = new MacLinuxFallbackKeyboardMapper(OperatingSystem.Macintosh);

function _assertResolveKeybinding(k: number, expected: IResolvedKeybinding[]): void {
assertResolveKeybinding(mapper, createKeybinding(k, OperatingSystem.Macintosh), expected);
Expand Down Expand Up @@ -149,7 +149,7 @@ suite('keyboardMapper - MAC fallback', () => {

suite('keyboardMapper - LINUX fallback', () => {

let mapper = new MacLinuxFallbackKeyboardMapper({}, OperatingSystem.Linux);
let mapper = new MacLinuxFallbackKeyboardMapper(OperatingSystem.Linux);

function _assertResolveKeybinding(k: number, expected: IResolvedKeybinding[]): void {
assertResolveKeybinding(mapper, createKeybinding(k, OperatingSystem.Linux), expected);
Expand Down

0 comments on commit 822ab91

Please sign in to comment.