Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/services/keybinding/electron-browser/nativeKeyboardLayoutService.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 { Disposable } from '../../../../base/common/lifecycle.js';
7
import { IKeyboardLayoutInfo, IKeyboardMapping, IMacLinuxKeyboardMapping, IWindowsKeyboardMapping, macLinuxKeyboardMappingEquals, windowsKeyboardMappingEquals } from '../../../../platform/keyboardLayout/common/keyboardLayout.js';
8
import { Emitter, Event } from '../../../../base/common/event.js';
9
import { OperatingSystem, OS } from '../../../../base/common/platform.js';
10
import { IMainProcessService } from '../../../../platform/ipc/common/mainProcessService.js';
11
import { INativeKeyboardLayoutService as IBaseNativeKeyboardLayoutService } from '../../../../platform/keyboardLayout/common/keyboardLayoutService.js';
12
import { ProxyChannel } from '../../../../base/parts/ipc/common/ipc.js';
13
import { createDecorator } from '../../../../platform/instantiation/common/instantiation.js';
14
15
export const INativeKeyboardLayoutService = createDecorator<INativeKeyboardLayoutService>('nativeKeyboardLayoutService');
16
17
export interface INativeKeyboardLayoutService {
18
readonly _serviceBrand: undefined;
19
readonly onDidChangeKeyboardLayout: Event<void>;
20
getRawKeyboardMapping(): IKeyboardMapping | null;
21
getCurrentKeyboardLayout(): IKeyboardLayoutInfo | null;
22
}
23
24
export class NativeKeyboardLayoutService extends Disposable implements INativeKeyboardLayoutService {
25
26
declare readonly _serviceBrand: undefined;
27
28
private readonly _onDidChangeKeyboardLayout = this._register(new Emitter<void>());
29
readonly onDidChangeKeyboardLayout = this._onDidChangeKeyboardLayout.event;
30
31
private readonly _keyboardLayoutService: IBaseNativeKeyboardLayoutService;
32
private _initPromise: Promise<void> | null;
33
private _keyboardMapping: IKeyboardMapping | null;
34
private _keyboardLayoutInfo: IKeyboardLayoutInfo | null;
35
36
constructor(
37
@IMainProcessService mainProcessService: IMainProcessService
38
) {
39
super();
40
this._keyboardLayoutService = ProxyChannel.toService<IBaseNativeKeyboardLayoutService>(mainProcessService.getChannel('keyboardLayout'));
41
this._initPromise = null;
42
this._keyboardMapping = null;
43
this._keyboardLayoutInfo = null;
44
45
this._register(this._keyboardLayoutService.onDidChangeKeyboardLayout(async ({ keyboardLayoutInfo, keyboardMapping }) => {
46
await this.initialize();
47
if (keyboardMappingEquals(this._keyboardMapping, keyboardMapping)) {
48
// the mappings are equal
49
return;
50
}
51
52
this._keyboardMapping = keyboardMapping;
53
this._keyboardLayoutInfo = keyboardLayoutInfo;
54
this._onDidChangeKeyboardLayout.fire();
55
}));
56
}
57
58
public initialize(): Promise<void> {
59
if (!this._initPromise) {
60
this._initPromise = this._doInitialize();
61
}
62
return this._initPromise;
63
}
64
65
private async _doInitialize(): Promise<void> {
66
const keyboardLayoutData = await this._keyboardLayoutService.getKeyboardLayoutData();
67
const { keyboardLayoutInfo, keyboardMapping } = keyboardLayoutData;
68
this._keyboardMapping = keyboardMapping;
69
this._keyboardLayoutInfo = keyboardLayoutInfo;
70
}
71
72
public getRawKeyboardMapping(): IKeyboardMapping | null {
73
return this._keyboardMapping;
74
}
75
76
public getCurrentKeyboardLayout(): IKeyboardLayoutInfo | null {
77
return this._keyboardLayoutInfo;
78
}
79
}
80
81
function keyboardMappingEquals(a: IKeyboardMapping | null, b: IKeyboardMapping | null): boolean {
82
if (OS === OperatingSystem.Windows) {
83
return windowsKeyboardMappingEquals(<IWindowsKeyboardMapping | null>a, <IWindowsKeyboardMapping | null>b);
84
}
85
86
return macLinuxKeyboardMappingEquals(<IMacLinuxKeyboardMapping | null>a, <IMacLinuxKeyboardMapping | null>b);
87
}
88
89