Skip to content

Commit 99d69d2

Browse files
authored
Merge pull request #5635 from Tyriar/win32
Move win32 input mode into a class to avoid init cost
2 parents e2d8492 + d35481c commit 99d69d2

3 files changed

Lines changed: 255 additions & 253 deletions

File tree

src/browser/services/KeyboardService.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,33 @@
66
import { IKeyboardService } from 'browser/services/Services';
77
import { evaluateKeyboardEvent } from 'common/input/Keyboard';
88
import { evaluateKeyboardEventKitty, KittyKeyboardEventType, KittyKeyboardFlags, shouldUseKittyProtocol } from 'common/input/KittyKeyboard';
9-
import { evaluateKeyboardEventWin32 } from 'common/input/Win32InputMode';
9+
import { Win32InputMode } from 'common/input/Win32InputMode';
1010
import { isMac } from 'common/Platform';
1111
import { ICoreService, IOptionsService } from 'common/services/Services';
1212
import { IKeyboardResult } from 'common/Types';
1313

1414
export class KeyboardService implements IKeyboardService {
1515
public serviceBrand: undefined;
1616

17+
private _win32InputMode: Win32InputMode | undefined;
18+
1719
constructor(
1820
@ICoreService private readonly _coreService: ICoreService,
1921
@IOptionsService private readonly _optionsService: IOptionsService
2022
) {
2123
}
2224

25+
private _getWin32InputMode(): Win32InputMode {
26+
if (!this._win32InputMode) {
27+
this._win32InputMode = new Win32InputMode();
28+
}
29+
return this._win32InputMode;
30+
}
31+
2332
public evaluateKeyDown(event: KeyboardEvent): IKeyboardResult {
2433
// Win32 input mode takes priority (most raw)
2534
if (this.useWin32InputMode) {
26-
return evaluateKeyboardEventWin32(event, true);
35+
return this._getWin32InputMode().evaluateKeyboardEvent(event, true);
2736
}
2837
const kittyFlags = this._coreService.kittyKeyboard.flags;
2938
return this.useKitty
@@ -34,7 +43,7 @@ export class KeyboardService implements IKeyboardService {
3443
public evaluateKeyUp(event: KeyboardEvent): IKeyboardResult | undefined {
3544
// Win32 input mode sends key up events
3645
if (this.useWin32InputMode) {
37-
return evaluateKeyboardEventWin32(event, false);
46+
return this._getWin32InputMode().evaluateKeyboardEvent(event, false);
3847
}
3948
const kittyFlags = this._coreService.kittyKeyboard.flags;
4049
if (this.useKitty && (kittyFlags & KittyKeyboardFlags.REPORT_EVENT_TYPES)) {

src/common/input/Win32InputMode.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import { assert } from 'chai';
7-
import { evaluateKeyboardEventWin32, Win32ControlKeyState } from 'common/input/Win32InputMode';
7+
import { Win32InputMode, Win32ControlKeyState } from 'common/input/Win32InputMode';
88
import { IKeyboardEvent, KeyboardResultType } from 'common/Types';
99

1010
type EventOpts = Partial<IKeyboardEvent>;
@@ -18,18 +18,20 @@ const parse = (seq: string) => {
1818
return m ? { vk: +m[1], sc: +m[2], uc: +m[3], kd: +m[4], cs: +m[5], rc: +m[6] } : null;
1919
};
2020

21+
const win32 = new Win32InputMode();
22+
2123
const test = (opts: EventOpts, isDown: boolean, check: (p: ReturnType<typeof parse>) => void) => {
22-
const result = evaluateKeyboardEventWin32(ev(opts), isDown);
24+
const result = win32.evaluateKeyboardEvent(ev(opts), isDown);
2325
const parsed = parse(result.key!);
2426
assert.ok(parsed);
2527
check(parsed);
2628
};
2729

2830
describe('Win32InputMode', () => {
29-
describe('evaluateKeyboardEventWin32', () => {
31+
describe('evaluateKeyboardEvent', () => {
3032
describe('basic key encoding', () => {
3133
it('letter key press', () => {
32-
const result = evaluateKeyboardEventWin32(ev({ code: 'KeyA', key: 'a', keyCode: 65 }), true);
34+
const result = win32.evaluateKeyboardEvent(ev({ code: 'KeyA', key: 'a', keyCode: 65 }), true);
3335
assert.strictEqual(result.type, KeyboardResultType.SEND_KEY);
3436
assert.strictEqual(result.cancel, true);
3537
const p = parse(result.key!);
@@ -135,7 +137,7 @@ describe('Win32InputMode', () => {
135137

136138
describe('sequence format', () => {
137139
it('valid CSI format', () => {
138-
const result = evaluateKeyboardEventWin32(ev({ code: 'KeyA', key: 'a', keyCode: 65 }), true);
140+
const result = win32.evaluateKeyboardEvent(ev({ code: 'KeyA', key: 'a', keyCode: 65 }), true);
139141
assert.ok(result.key?.startsWith('\x1b[') && result.key.endsWith('_'));
140142
assert.strictEqual(result.key?.slice(2, -1).split(';').length, 6);
141143
});

0 commit comments

Comments
 (0)