Path: blob/main/src/vs/platform/keyboardLayout/electron-main/keyboardLayoutMainService.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import type * as nativeKeymap from 'native-keymap';6import * as platform from '../../../base/common/platform.js';7import { Emitter } from '../../../base/common/event.js';8import { Disposable } from '../../../base/common/lifecycle.js';9import { createDecorator } from '../../instantiation/common/instantiation.js';10import { IKeyboardLayoutData, INativeKeyboardLayoutService } from '../common/keyboardLayoutService.js';11import { ILifecycleMainService, LifecycleMainPhase } from '../../lifecycle/electron-main/lifecycleMainService.js';1213export const IKeyboardLayoutMainService = createDecorator<IKeyboardLayoutMainService>('keyboardLayoutMainService');1415export interface IKeyboardLayoutMainService extends INativeKeyboardLayoutService { }1617export class KeyboardLayoutMainService extends Disposable implements INativeKeyboardLayoutService {1819declare readonly _serviceBrand: undefined;2021private readonly _onDidChangeKeyboardLayout = this._register(new Emitter<IKeyboardLayoutData>());22readonly onDidChangeKeyboardLayout = this._onDidChangeKeyboardLayout.event;2324private _initPromise: Promise<void> | null;25private _keyboardLayoutData: IKeyboardLayoutData | null;2627constructor(28@ILifecycleMainService lifecycleMainService: ILifecycleMainService29) {30super();31this._initPromise = null;32this._keyboardLayoutData = null;3334// perf: automatically trigger initialize after windows35// have opened so that we can do this work in parallel36// to the window load.37lifecycleMainService.when(LifecycleMainPhase.AfterWindowOpen).then(() => this._initialize());38}3940private _initialize(): Promise<void> {41if (!this._initPromise) {42this._initPromise = this._doInitialize();43}44return this._initPromise;45}4647private async _doInitialize(): Promise<void> {48const nativeKeymapMod = await import('native-keymap');4950this._keyboardLayoutData = readKeyboardLayoutData(nativeKeymapMod);51if (!platform.isCI) {52// See https://github.com/microsoft/vscode/issues/15284053// Do not register the keyboard layout change listener in CI because it doesn't work54// on the build machines and it just adds noise to the build logs.55nativeKeymapMod.onDidChangeKeyboardLayout(() => {56this._keyboardLayoutData = readKeyboardLayoutData(nativeKeymapMod);57this._onDidChangeKeyboardLayout.fire(this._keyboardLayoutData);58});59}60}6162public async getKeyboardLayoutData(): Promise<IKeyboardLayoutData> {63await this._initialize();64return this._keyboardLayoutData!;65}66}6768function readKeyboardLayoutData(nativeKeymapMod: typeof nativeKeymap): IKeyboardLayoutData {69const keyboardMapping = nativeKeymapMod.getKeyMap();70const keyboardLayoutInfo = nativeKeymapMod.getCurrentKeyboardLayout();71return { keyboardMapping, keyboardLayoutInfo };72}737475