Skip to content

Commit cc83415

Browse files
committed
mru cache for keyboard layouts
1 parent e39072b commit cc83415

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

src/vs/workbench/services/keybinding/browser/keyboardLayoutProvider.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ export class KeyboardLayoutProvider {
223223
public static readonly INSTANCE: KeyboardLayoutProvider = new KeyboardLayoutProvider();
224224

225225
private _layoutInfos: KeyboardLayoutInfo[] = [];
226-
// private _mru: KeyboardLayoutInfo[] = [];
226+
private _mru: KeyboardLayoutInfo[] = [];
227227
private _active: KeyboardLayoutInfo | null;
228228

229229
private constructor() {
@@ -232,6 +232,7 @@ export class KeyboardLayoutProvider {
232232

233233
registerKeyboardLayout(layout: KeyboardLayoutInfo) {
234234
this._layoutInfos.push(layout);
235+
this._mru = this._layoutInfos;
235236
}
236237

237238
get activeKeyboardLayout() {
@@ -244,13 +245,25 @@ export class KeyboardLayoutProvider {
244245

245246
setActive(keymap: IKeyboardMapping) {
246247
this._active = this.getMatchedKeyboardLayout(keymap);
248+
249+
if (!this._active) {
250+
return;
251+
}
252+
const index = this._mru.indexOf(this._active);
253+
254+
if (index === 0) {
255+
return;
256+
}
257+
258+
this._mru.splice(index, 1);
259+
this._mru.unshift(this._active);
247260
}
248261

249262
getMatchedKeyboardLayout(keymap: IKeyboardMapping): KeyboardLayoutInfo | null {
250263
// TODO go through mru list instead of _layoutInfos
251-
for (let i = 0; i < this._layoutInfos.length; i++) {
252-
if (this._layoutInfos[i].fuzzyEqual(keymap)) {
253-
return this._layoutInfos[i];
264+
for (let i = 0; i < this._mru.length; i++) {
265+
if (this._mru[i].fuzzyEqual(keymap)) {
266+
return this._mru[i];
254267
}
255268
}
256269

src/vs/workbench/services/keybinding/browser/keyboardLayoutService.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ export class BrowserKeyboardMapperFactory {
6464
}
6565

6666
this.getBrowserKeyMap().then(keyMap => {
67+
// might be false positive
68+
if (KeyboardLayoutProvider.INSTANCE.isActive(keyMap)) {
69+
return;
70+
}
6771
KeyboardLayoutProvider.INSTANCE.setActive(keyMap);
6872
let currentKeyboardLayout = KeyboardLayoutProvider.INSTANCE.activeKeyboardLayout;
6973

@@ -152,6 +156,7 @@ export class BrowserKeyboardMapperFactory {
152156
return false;
153157
}
154158

159+
// TODO, this assumption is wrong as `browserEvent.key` doesn't necessarily equal expectedValue from real keymap
155160
if (!isDead && standardKeyboardEvent.browserEvent.key !== expectedValue) {
156161
return false;
157162
}

0 commit comments

Comments
 (0)