UNPKG

12.7 kBTypeScriptView Raw
1import { Emitter, Event } from '../common/event';
2import { CommandRegistry, Command } from '../common/command';
3import { Disposable } from '../common/disposable';
4import { KeyCode, KeySequence, Key } from './keyboard/keys';
5import { KeyboardLayoutService } from './keyboard/keyboard-layout-service';
6import { ContributionProvider } from '../common/contribution-provider';
7import { ILogger } from '../common/logger';
8import { StatusBar } from './status-bar/status-bar';
9import { ContextKeyService } from './context-key-service';
10import { CorePreferences } from './core-preferences';
11import * as common from '../common/keybinding';
12export declare enum KeybindingScope {
13 DEFAULT = 0,
14 USER = 1,
15 WORKSPACE = 2,
16 END = 3
17}
18export declare namespace KeybindingScope {
19 const length: number;
20}
21/**
22 * @deprecated import from `@theia/core/lib/common/keybinding` instead
23 */
24export declare type Keybinding = common.Keybinding;
25export declare const Keybinding: typeof common.Keybinding;
26export interface ResolvedKeybinding extends common.Keybinding {
27 /**
28 * The KeyboardLayoutService may transform the `keybinding` depending on the
29 * user's keyboard layout. This property holds the transformed keybinding that
30 * should be used in the UI. The value is undefined if the KeyboardLayoutService
31 * has not been called yet to resolve the keybinding.
32 */
33 resolved?: KeyCode[];
34}
35export interface ScopedKeybinding extends common.Keybinding {
36 /** Current keybinding scope */
37 scope: KeybindingScope;
38}
39export declare const KeybindingContribution: unique symbol;
40/**
41 * Allows extensions to contribute {@link common.Keybinding}s
42 */
43export interface KeybindingContribution {
44 /**
45 * Registers keybindings.
46 * @param keybindings the keybinding registry.
47 */
48 registerKeybindings(keybindings: KeybindingRegistry): void;
49}
50export declare const KeybindingContext: unique symbol;
51export interface KeybindingContext {
52 /**
53 * The unique ID of the current context.
54 */
55 readonly id: string;
56 isEnabled(arg: common.Keybinding): boolean;
57}
58export declare namespace KeybindingContexts {
59 const NOOP_CONTEXT: KeybindingContext;
60 const DEFAULT_CONTEXT: KeybindingContext;
61}
62export declare class KeybindingRegistry {
63 static readonly PASSTHROUGH_PSEUDO_COMMAND = "passthrough";
64 protected keySequence: KeySequence;
65 protected readonly contexts: {
66 [id: string]: KeybindingContext;
67 };
68 protected readonly keymaps: ScopedKeybinding[][];
69 protected readonly corePreferences: CorePreferences;
70 protected readonly keyboardLayoutService: KeyboardLayoutService;
71 protected readonly contextProvider: ContributionProvider<KeybindingContext>;
72 protected readonly commandRegistry: CommandRegistry;
73 protected readonly contributions: ContributionProvider<KeybindingContribution>;
74 protected readonly statusBar: StatusBar;
75 protected readonly logger: ILogger;
76 protected readonly whenContextService: ContextKeyService;
77 onStart(): Promise<void>;
78 protected keybindingsChanged: Emitter<void>;
79 /**
80 * Event that is fired when the resolved keybindings change due to a different keyboard layout
81 * or when a new keymap is being set
82 */
83 get onKeybindingsChanged(): Event<void>;
84 /**
85 * Registers the keybinding context arguments into the application. Fails when an already registered
86 * context is being registered.
87 *
88 * @param contexts the keybinding contexts to register into the application.
89 */
90 protected registerContext(...contexts: KeybindingContext[]): void;
91 /**
92 * Register a default keybinding to the registry.
93 *
94 * Keybindings registered later have higher priority during evaluation.
95 *
96 * @param binding the keybinding to be registered
97 */
98 registerKeybinding(binding: common.Keybinding): Disposable;
99 /**
100 * Register multiple default keybindings to the registry
101 *
102 * @param bindings An array of keybinding to be registered
103 */
104 registerKeybindings(...bindings: common.Keybinding[]): Disposable;
105 /**
106 * Unregister all keybindings from the registry that are bound to the key of the given keybinding
107 *
108 * @param binding a keybinding specifying the key to be unregistered
109 */
110 unregisterKeybinding(binding: common.Keybinding): void;
111 /**
112 * Unregister all keybindings with the given key from the registry
113 *
114 * @param key a key to be unregistered
115 */
116 unregisterKeybinding(key: string): void;
117 /**
118 * Unregister all existing keybindings for the given command
119 * @param command the command to unregister all keybindings for
120 */
121 unregisterKeybinding(command: Command): void;
122 protected doRegisterKeybindings(bindings: common.Keybinding[], scope?: KeybindingScope): Disposable;
123 protected doRegisterKeybinding(binding: common.Keybinding, scope?: KeybindingScope): Disposable;
124 /**
125 * Ensures that keybindings are inserted in order of increasing length of binding to ensure that if a
126 * user triggers a short keybinding (e.g. ctrl+k), the UI won't wait for a longer one (e.g. ctrl+k enter)
127 */
128 protected insertBindingIntoScope(item: common.Keybinding & {
129 scope: KeybindingScope;
130 }, scope: KeybindingScope): void;
131 /**
132 * Ensure that the `resolved` property of the given binding is set by calling the KeyboardLayoutService.
133 */
134 resolveKeybinding(binding: ResolvedKeybinding): KeyCode[];
135 /**
136 * Clear all `resolved` properties of registered keybindings so the KeyboardLayoutService is called
137 * again to resolve them. This is necessary when the user's keyboard layout has changed.
138 */
139 protected clearResolvedKeybindings(): void;
140 /**
141 * Checks whether a colliding {@link common.Keybinding} exists in a specific scope.
142 * @param binding the keybinding to check
143 * @param scope the keybinding scope to check
144 * @returns true if there is a colliding keybinding
145 */
146 containsKeybindingInScope(binding: common.Keybinding, scope?: KeybindingScope): boolean;
147 /**
148 * Get a user visible representation of a {@link common.Keybinding}.
149 * @returns an array of strings representing all elements of the {@link KeySequence} defined by the {@link common.Keybinding}
150 * @param keybinding the keybinding
151 * @param separator the separator to be used to stringify {@link KeyCode}s that are part of the {@link KeySequence}
152 */
153 acceleratorFor(keybinding: common.Keybinding, separator?: string, asciiOnly?: boolean): string[];
154 /**
155 * Get a user visible representation of a {@link KeySequence}.
156 * @returns an array of strings representing all elements of the {@link KeySequence}
157 * @param keySequence the keysequence
158 * @param separator the separator to be used to stringify {@link KeyCode}s that are part of the {@link KeySequence}
159 */
160 acceleratorForSequence(keySequence: KeySequence, separator?: string, asciiOnly?: boolean): string[];
161 /**
162 * Get a user visible representation of a key code (a key with modifiers).
163 * @returns a string representing the {@link KeyCode}
164 * @param keyCode the keycode
165 * @param separator the separator used to separate keys (key and modifiers) in the returning string
166 * @param asciiOnly if `true`, no special characters will be substituted into the string returned. Ensures correct keyboard shortcuts in Electron menus.
167 */
168 acceleratorForKeyCode(keyCode: KeyCode, separator?: string, asciiOnly?: boolean): string;
169 componentsForKeyCode(keyCode: KeyCode, asciiOnly?: boolean): string[];
170 /**
171 * @param asciiOnly if `true`, no special characters will be substituted into the string returned. Ensures correct keyboard shortcuts in Electron menus.
172 *
173 * Return a user visible representation of a single key.
174 */
175 acceleratorForKey(key: Key, asciiOnly?: boolean): string;
176 /**
177 * Finds collisions for a key sequence inside a list of bindings (error-free)
178 *
179 * @param bindings the reference bindings
180 * @param candidate the sequence to match
181 */
182 protected getKeySequenceCollisions(bindings: ScopedKeybinding[], candidate: KeySequence): KeybindingRegistry.KeybindingsResult;
183 /**
184 * Get all keybindings associated to a commandId.
185 *
186 * @param commandId The ID of the command for which we are looking for keybindings.
187 * @returns an array of {@link ScopedKeybinding}
188 */
189 getKeybindingsForCommand(commandId: string): ScopedKeybinding[];
190 protected isActive(binding: common.Keybinding): boolean;
191 /**
192 * Tries to execute a keybinding.
193 *
194 * @param binding to execute
195 * @param event keyboard event.
196 */
197 protected executeKeyBinding(binding: common.Keybinding, event: KeyboardEvent): void;
198 /**
199 * Only execute if it has no context (global context) or if we're in that context.
200 */
201 protected isEnabled(binding: common.Keybinding, event: KeyboardEvent): boolean;
202 dispatchCommand(id: string, target?: EventTarget): void;
203 dispatchKeyDown(input: KeyboardEventInit | KeyCode | string, target?: EventTarget): void;
204 protected asKeyboardEventInit(input: KeyboardEventInit | KeyCode | string): KeyboardEventInit & Partial<{
205 keyCode: number;
206 }>;
207 registerEventListeners(win: Window): Disposable;
208 /**
209 * Run the command matching to the given keyboard event.
210 */
211 run(event: KeyboardEvent): void;
212 /**
213 * Match first binding in the current context.
214 * Keybindings ordered by a scope and by a registration order within the scope.
215 *
216 * FIXME:
217 * This method should run very fast since it happens on each keystroke. We should reconsider how keybindings are stored.
218 * It should be possible to look up full and partial keybinding for given key sequence for constant time using some kind of tree.
219 * Such tree should not contain disabled keybindings and be invalidated whenever the registry is changed.
220 */
221 matchKeybinding(keySequence: KeySequence, event?: KeyboardEvent): KeybindingRegistry.Match;
222 /**
223 * Returns true if the binding is usable
224 * @param binding Binding to be checked
225 */
226 protected isUsable(binding: common.Keybinding): boolean;
227 /**
228 * Return a new filtered array containing only the usable bindings among the input bindings
229 * @param bindings Bindings to filter
230 */
231 protected getUsableBindings<T extends common.Keybinding>(bindings: T[]): T[];
232 /**
233 * Return true of string a pseudo-command id, in other words a command id
234 * that has a special meaning and that we won't find in the command
235 * registry.
236 *
237 * @param commandId commandId to test
238 */
239 isPseudoCommand(commandId: string): boolean;
240 /**
241 * Sets a new keymap replacing all existing {@link common.Keybinding}s in the given scope.
242 * @param scope the keybinding scope
243 * @param bindings an array containing the new {@link common.Keybinding}s
244 */
245 setKeymap(scope: KeybindingScope, bindings: common.Keybinding[]): void;
246 protected readonly toResetKeymap: Map<KeybindingScope, Disposable>;
247 /**
248 * Reset keybindings for a specific scope
249 * @param scope scope to reset the keybindings for
250 */
251 resetKeybindingsForScope(scope: KeybindingScope): void;
252 /**
253 * Reset keybindings for all scopes(only leaves the default keybindings mapped)
254 */
255 resetKeybindings(): void;
256 /**
257 * Get all {@link common.Keybinding}s for a {@link KeybindingScope}.
258 * @returns an array of {@link common.ScopedKeybinding}
259 * @param scope the keybinding scope to retrieve the {@link common.Keybinding}s for.
260 */
261 getKeybindingsByScope(scope: KeybindingScope): ScopedKeybinding[];
262}
263export declare namespace KeybindingRegistry {
264 type Match = {
265 kind: 'full' | 'partial';
266 binding: ScopedKeybinding;
267 } | undefined;
268 class KeybindingsResult {
269 full: ScopedKeybinding[];
270 partial: ScopedKeybinding[];
271 shadow: ScopedKeybinding[];
272 /**
273 * Merge two results together inside `this`
274 *
275 * @param other the other KeybindingsResult to merge with
276 * @return this
277 */
278 merge(other: KeybindingsResult): KeybindingsResult;
279 /**
280 * Returns a new filtered KeybindingsResult
281 *
282 * @param fn callback filter on the results
283 * @return filtered new result
284 */
285 filter(fn: (binding: common.Keybinding) => boolean): KeybindingsResult;
286 }
287}
288//# sourceMappingURL=keybinding.d.ts.map
\No newline at end of file