Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/keyboardLayout/electron-main/keyboardLayoutMainService.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import type * as nativeKeymap from 'native-keymap';
7
import * as platform from '../../../base/common/platform.js';
8
import { Emitter } from '../../../base/common/event.js';
9
import { Disposable } from '../../../base/common/lifecycle.js';
10
import { createDecorator } from '../../instantiation/common/instantiation.js';
11
import { IKeyboardLayoutData, INativeKeyboardLayoutService } from '../common/keyboardLayoutService.js';
12
import { ILifecycleMainService, LifecycleMainPhase } from '../../lifecycle/electron-main/lifecycleMainService.js';
13
14
export const IKeyboardLayoutMainService = createDecorator<IKeyboardLayoutMainService>('keyboardLayoutMainService');
15
16
export interface IKeyboardLayoutMainService extends INativeKeyboardLayoutService { }
17
18
export class KeyboardLayoutMainService extends Disposable implements INativeKeyboardLayoutService {
19
20
declare readonly _serviceBrand: undefined;
21
22
private readonly _onDidChangeKeyboardLayout = this._register(new Emitter<IKeyboardLayoutData>());
23
readonly onDidChangeKeyboardLayout = this._onDidChangeKeyboardLayout.event;
24
25
private _initPromise: Promise<void> | null;
26
private _keyboardLayoutData: IKeyboardLayoutData | null;
27
28
constructor(
29
@ILifecycleMainService lifecycleMainService: ILifecycleMainService
30
) {
31
super();
32
this._initPromise = null;
33
this._keyboardLayoutData = null;
34
35
// perf: automatically trigger initialize after windows
36
// have opened so that we can do this work in parallel
37
// to the window load.
38
lifecycleMainService.when(LifecycleMainPhase.AfterWindowOpen).then(() => this._initialize());
39
}
40
41
private _initialize(): Promise<void> {
42
if (!this._initPromise) {
43
this._initPromise = this._doInitialize();
44
}
45
return this._initPromise;
46
}
47
48
private async _doInitialize(): Promise<void> {
49
const nativeKeymapMod = await import('native-keymap');
50
51
this._keyboardLayoutData = readKeyboardLayoutData(nativeKeymapMod);
52
if (!platform.isCI) {
53
// See https://github.com/microsoft/vscode/issues/152840
54
// Do not register the keyboard layout change listener in CI because it doesn't work
55
// on the build machines and it just adds noise to the build logs.
56
nativeKeymapMod.onDidChangeKeyboardLayout(() => {
57
this._keyboardLayoutData = readKeyboardLayoutData(nativeKeymapMod);
58
this._onDidChangeKeyboardLayout.fire(this._keyboardLayoutData);
59
});
60
}
61
}
62
63
public async getKeyboardLayoutData(): Promise<IKeyboardLayoutData> {
64
await this._initialize();
65
return this._keyboardLayoutData!;
66
}
67
}
68
69
function readKeyboardLayoutData(nativeKeymapMod: typeof nativeKeymap): IKeyboardLayoutData {
70
const keyboardMapping = nativeKeymapMod.getKeyMap();
71
const keyboardLayoutInfo = nativeKeymapMod.getCurrentKeyboardLayout();
72
return { keyboardMapping, keyboardLayoutInfo };
73
}
74
75