UNPKG

3.99 kBTypeScriptView Raw
1import { Emitter, Event } from '../../common/event';
2import { ILogger } from '../../common/logger';
3import { Deferred } from '../../common/promise-util';
4import { NativeKeyboardLayout, KeyboardLayoutProvider, KeyboardLayoutChangeNotifier, KeyValidator, KeyValidationInput } from '../../common/keyboard/keyboard-layout-provider';
5import { LocalStorageService } from '../storage-service';
6export declare type KeyboardLayoutSource = 'navigator.keyboard' | 'user-choice' | 'pressed-keys';
7export declare class BrowserKeyboardLayoutProvider implements KeyboardLayoutProvider, KeyboardLayoutChangeNotifier, KeyValidator {
8 protected readonly logger: ILogger;
9 protected readonly storageService: LocalStorageService;
10 protected readonly initialized: Deferred<void>;
11 protected readonly nativeLayoutChanged: Emitter<NativeKeyboardLayout>;
12 get onDidChangeNativeLayout(): Event<NativeKeyboardLayout>;
13 protected readonly tester: KeyboardTester;
14 protected source: KeyboardLayoutSource;
15 protected currentLayout: KeyboardLayoutData;
16 get allLayoutData(): KeyboardLayoutData[];
17 get currentLayoutData(): KeyboardLayoutData;
18 get currentLayoutSource(): KeyboardLayoutSource;
19 protected initialize(): Promise<void>;
20 getNativeLayout(): Promise<NativeKeyboardLayout>;
21 /**
22 * Set user-chosen keyboard layout data.
23 */
24 setLayoutData(layout: KeyboardLayoutData | 'autodetect'): Promise<KeyboardLayoutData>;
25 /**
26 * Test all known keyboard layouts with the given combination of pressed key and
27 * produced character. Matching layouts have their score increased (see class
28 * KeyboardTester). If this leads to a change of the top-scoring layout, a layout
29 * change event is fired.
30 */
31 validateKey(keyCode: KeyValidationInput): void;
32 protected setCurrent(layout: KeyboardLayoutData, source: KeyboardLayoutSource): void;
33 protected autodetect(): Promise<[KeyboardLayoutData, KeyboardLayoutSource]>;
34 /**
35 * @param layoutMap a keyboard layout map according to https://wicg.github.io/keyboard-map/
36 */
37 protected testLayoutMap(layoutMap: KeyboardLayoutMap): void;
38 /**
39 * Select a layout based on the current tester state and the operating system
40 * and language detected from the browser.
41 */
42 protected selectLayout(): KeyboardLayoutData;
43 protected saveState(): Promise<void>;
44 protected loadState(): Promise<void>;
45}
46export interface KeyboardLayoutData {
47 name: string;
48 hardware: 'pc' | 'mac';
49 language: string;
50 raw: NativeKeyboardLayout;
51}
52/**
53 * This is the fallback keyboard layout selected when nothing else matches.
54 * It has an empty mapping, so user inputs are handled like with a standard US keyboard.
55 */
56export declare const DEFAULT_LAYOUT_DATA: KeyboardLayoutData;
57export interface LayoutProviderState {
58 tester?: KeyboardTesterState;
59 source?: KeyboardLayoutSource;
60 currentLayout?: string;
61}
62export interface KeyboardTesterState {
63 scores?: {
64 [id: string]: number;
65 };
66 topScore?: number;
67 testedInputs?: {
68 [key: string]: string;
69 };
70}
71/**
72 * Holds score values for all known keyboard layouts. Scores are updated
73 * by comparing key codes with the corresponding character produced by
74 * the user's keyboard.
75 */
76export declare class KeyboardTester {
77 readonly candidates: KeyboardLayoutData[];
78 readonly scores: number[];
79 topScore: number;
80 private readonly testedInputs;
81 get inputCount(): number;
82 constructor(candidates: KeyboardLayoutData[]);
83 reset(): void;
84 updateScores(input: KeyValidationInput): boolean;
85 protected testCandidate(candidate: KeyboardLayoutData, input: KeyValidationInput, property: 'value' | 'withShift' | 'withAltGr' | 'withShiftAltGr'): number;
86 getState(): KeyboardTesterState;
87 setState(state: KeyboardTesterState): void;
88}
89declare type KeyboardLayoutMap = Map<string, string>;
90export {};
91//# sourceMappingURL=browser-keyboard-layout-provider.d.ts.map
\No newline at end of file