keyboardMapper.ts 1.8 KB
Newer Older
A
Alex Dima 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import { Keybinding, ResolvedKeybinding, SimpleKeybinding } from 'vs/base/common/keyCodes';
J
Joao Moreno 已提交
7
import { ScanCodeBinding } from 'vs/base/common/scanCode';
A
Alex Dima 已提交
8
import { IKeyboardEvent } from 'vs/platform/keybinding/common/keybinding';
A
Alex Dima 已提交
9

10 11 12
export interface IKeyboardMapper {
	dumpDebugInfo(): string;
	resolveKeybinding(keybinding: Keybinding): ResolvedKeybinding[];
13
	resolveKeyboardEvent(keyboardEvent: IKeyboardEvent): ResolvedKeybinding;
A
Alex Dima 已提交
14
	resolveUserBinding(firstPart: (SimpleKeybinding | ScanCodeBinding)[]): ResolvedKeybinding[];
A
Alex Dima 已提交
15
}
16 17 18 19 20 21

export class CachedKeyboardMapper implements IKeyboardMapper {

	private _actual: IKeyboardMapper;
	private _cache: Map<string, ResolvedKeybinding[]>;

A
Alex Dima 已提交
22
	constructor(actual: IKeyboardMapper) {
23 24 25 26 27 28 29 30 31
		this._actual = actual;
		this._cache = new Map<string, ResolvedKeybinding[]>();
	}

	public dumpDebugInfo(): string {
		return this._actual.dumpDebugInfo();
	}

	public resolveKeybinding(keybinding: Keybinding): ResolvedKeybinding[] {
32 33 34 35
		const hashCode = keybinding.getHashCode();
		const resolved = this._cache.get(hashCode);
		if (!resolved) {
			const r = this._actual.resolveKeybinding(keybinding);
36 37 38
			this._cache.set(hashCode, r);
			return r;
		}
39
		return resolved;
40 41 42 43 44 45
	}

	public resolveKeyboardEvent(keyboardEvent: IKeyboardEvent): ResolvedKeybinding {
		return this._actual.resolveKeyboardEvent(keyboardEvent);
	}

A
Alex Dima 已提交
46
	public resolveUserBinding(parts: (SimpleKeybinding | ScanCodeBinding)[]): ResolvedKeybinding[] {
47
		return this._actual.resolveUserBinding(parts);
48 49
	}
}