Skip to content

Commit

Permalink
Retire KeybindingContext Part 1
Browse files Browse the repository at this point in the history
Retiring the use of `context` in keybindings

This is still in draft and review.
`KeybindingContext` classes still exist in Theia, but no longer used.

Needs some review to streamline
  • Loading branch information
FernandoAscencio committed Jul 19, 2023
1 parent 3deecfe commit 7742633
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 32 deletions.
10 changes: 5 additions & 5 deletions packages/console/src/browser/console-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { injectable, inject } from '@theia/core/shared/inversify';
import { Command, CommandContribution, CommandRegistry, MenuContribution, MenuModelRegistry, CommandHandler } from '@theia/core';
import { FrontendApplicationContribution, KeybindingContribution, KeybindingRegistry, CommonCommands } from '@theia/core/lib/browser';
import { ConsoleManager } from './console-manager';
import { ConsoleKeybindingContexts } from './console-keybinding-contexts';
// import { ConsoleKeybindingContexts } from './console-keybinding-contexts';
import { ConsoleWidget } from './console-widget';
import { ConsoleContentWidget } from './console-content-widget';
import { nls } from '@theia/core/lib/common/nls';
Expand Down Expand Up @@ -70,22 +70,22 @@ export class ConsoleContribution implements FrontendApplicationContribution, Com
keybindings.registerKeybinding({
command: ConsoleCommands.SELECT_ALL.id,
keybinding: 'ctrlcmd+a',
context: ConsoleKeybindingContexts.consoleContentFocus
when: 'consoleContentFocus'
});
keybindings.registerKeybinding({
command: ConsoleCommands.EXECUTE.id,
keybinding: 'enter',
context: ConsoleKeybindingContexts.consoleInputFocus
when: 'consoleInputFocus'
});
keybindings.registerKeybinding({
command: ConsoleCommands.NAVIGATE_BACK.id,
keybinding: 'up',
context: ConsoleKeybindingContexts.consoleNavigationBackEnabled
when: 'consoleInputFocus'
});
keybindings.registerKeybinding({
command: ConsoleCommands.NAVIGATE_FORWARD.id,
keybinding: 'down',
context: ConsoleKeybindingContexts.consoleNavigationForwardEnabled
when: 'consoleInputFocus'
});
}

Expand Down
30 changes: 24 additions & 6 deletions packages/console/src/browser/console-widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { ElementExt } from '@theia/core/shared/@phosphor/domutils';
import { injectable, inject, postConstruct, interfaces, Container } from '@theia/core/shared/inversify';
import { TreeSourceNode } from '@theia/core/lib/browser/source-tree';
import { ContextKey } from '@theia/core/lib/browser/context-key-service';
import { ContextKeyService, ContextKey } from '@theia/core/lib/browser/context-key-service';
import { BaseWidget, PanelLayout, Widget, Message, MessageLoop, StatefulWidget, CompositeTreeNode } from '@theia/core/lib/browser';
import { MonacoEditor } from '@theia/monaco/lib/browser/monaco-editor';
import URI from '@theia/core/lib/common/uri';
Expand Down Expand Up @@ -75,7 +75,11 @@ export class ConsoleWidget extends BaseWidget implements StatefulWidget {
@inject(MonacoEditorProvider)
protected readonly editorProvider: MonacoEditorProvider;

@inject(ContextKeyService)
protected readonly contextKeyService: ContextKeyService;

protected _input: MonacoEditor;
protected consoleContentFocus: ContextKey<boolean>;

constructor() {
super();
Expand Down Expand Up @@ -130,6 +134,13 @@ export class ConsoleWidget extends BaseWidget implements StatefulWidget {
if (inputFocusContextKey) {
this.toDispose.push(input.onFocusChanged(() => inputFocusContextKey.set(this.hasInputFocus())));
}
// this.consoleContentFocus = this.contextKeyService.createKey('consoleContentFocus', true);
const inputContext = this.contextKeyService.createScoped(inputWidget.node);
inputContext.setContext('consoleInputFocus', true);
const contentContext = this.contextKeyService.createScoped(this.content.node);
contentContext.setContext('consoleContentFocus', true);
this.toDispose.push(inputContext);
this.toDispose.push(contentContext);
}

protected createInput(node: HTMLElement): Promise<MonacoEditor> {
Expand Down Expand Up @@ -198,7 +209,9 @@ export class ConsoleWidget extends BaseWidget implements StatefulWidget {
return;
}
const editor = this.input.getControl();
editor.setValue(value);
if (editor.getPosition()!.equals({ lineNumber: 1, column: 1 })) {
editor.setValue(value);
}
editor.setPosition({
lineNumber: 1,
column: 1
Expand All @@ -208,10 +221,15 @@ export class ConsoleWidget extends BaseWidget implements StatefulWidget {
navigateForward(): void {
const value = this.history.next || '';
const editor = this.input.getControl();
editor.setValue(value);
const lineNumber = editor.getModel()!.getLineCount();
const column = editor.getModel()!.getLineMaxColumn(lineNumber);
editor.setPosition({ lineNumber, column });
const getFinalPos = () => {
const lineNumber = editor.getModel()!.getLineCount();
const column = editor.getModel()!.getLineMaxColumn(lineNumber);
return {lineNumber, column};
};
if (editor.getPosition()!.equals(getFinalPos())) {
editor.setValue(value);
}
editor.setPosition(getFinalPos());
}

protected revealLastOutput(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1020,60 +1020,60 @@ export class DebugFrontendApplicationContribution extends AbstractViewContributi
keybindings.registerKeybinding({
command: DebugCommands.STOP.id,
keybinding: 'shift+f5',
context: DebugKeybindingContexts.inDebugMode
when: DebugKeybindingContexts.inDebugMode
});

keybindings.registerKeybinding({
command: DebugCommands.RESTART.id,
keybinding: 'shift+ctrlcmd+f5',
context: DebugKeybindingContexts.inDebugMode
when: DebugKeybindingContexts.inDebugMode
});
keybindings.registerKeybinding({
command: DebugCommands.STEP_OVER.id,
keybinding: 'f10',
context: DebugKeybindingContexts.inDebugMode
when: DebugKeybindingContexts.inDebugMode
});
keybindings.registerKeybinding({
command: DebugCommands.STEP_INTO.id,
keybinding: 'f11',
context: DebugKeybindingContexts.inDebugMode
when: DebugKeybindingContexts.inDebugMode
});
keybindings.registerKeybinding({
command: DebugCommands.STEP_OUT.id,
keybinding: 'shift+f11',
context: DebugKeybindingContexts.inDebugMode
when: DebugKeybindingContexts.inDebugMode
});
keybindings.registerKeybinding({
command: DebugCommands.CONTINUE.id,
keybinding: 'f5',
context: DebugKeybindingContexts.inDebugMode
when: DebugKeybindingContexts.inDebugMode
});
keybindings.registerKeybinding({
command: DebugCommands.PAUSE.id,
keybinding: 'f6',
context: DebugKeybindingContexts.inDebugMode
when: DebugKeybindingContexts.inDebugMode
});

keybindings.registerKeybinding({
command: DebugCommands.TOGGLE_BREAKPOINT.id,
keybinding: 'f9',
context: EditorKeybindingContexts.editorTextFocus
when: EditorKeybindingContexts.editorTextFocus
});
keybindings.registerKeybinding({
command: DebugCommands.INLINE_BREAKPOINT.id,
keybinding: 'shift+f9',
context: EditorKeybindingContexts.editorTextFocus
when: EditorKeybindingContexts.editorTextFocus
});

keybindings.registerKeybinding({
command: DebugBreakpointWidgetCommands.ACCEPT.id,
keybinding: 'enter',
context: DebugKeybindingContexts.breakpointWidgetInputFocus
when: 'isBreakpointWidgetVisible && breakpointWidgetFocus'
});
keybindings.registerKeybinding({
command: DebugBreakpointWidgetCommands.CLOSE.id,
keybinding: 'esc',
context: DebugKeybindingContexts.breakpointWidgetInputStrictFocus
when: 'isBreakpointWidgetVisible'
});
}

Expand Down
13 changes: 13 additions & 0 deletions packages/debug/src/browser/editor/debug-breakpoint-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { ILanguageFeaturesService } from '@theia/monaco-editor-core/esm/vs/edito
import { StandaloneServices } from '@theia/monaco-editor-core/esm/vs/editor/standalone/browser/standaloneServices';
import { TextModel } from '@theia/monaco-editor-core/esm/vs/editor/common/model/textModel';
import { SelectComponent, SelectOption } from '@theia/core/lib/browser/widgets/select-component';
import { ContextKeyService, ContextKey } from '@theia/core/lib/browser/context-key-service';

export type ShowDebugBreakpointOptions = DebugSourceBreakpoint | {
position: monaco.Position,
Expand All @@ -54,6 +55,9 @@ export class DebugBreakpointWidget implements Disposable {
@inject(MonacoEditorProvider)
protected readonly editorProvider: MonacoEditorProvider;

@inject(ContextKeyService)
protected readonly contextKeyService: ContextKeyService;

protected selectNode: HTMLDivElement;
protected selectNodeRoot: Root;

Expand Down Expand Up @@ -92,6 +96,8 @@ export class DebugBreakpointWidget implements Disposable {
}
}

protected breakpointWidgetVisible: ContextKey<boolean>;

@postConstruct()
protected init(): void {
this.doInit();
Expand Down Expand Up @@ -155,6 +161,11 @@ export class DebugBreakpointWidget implements Disposable {
this.zone.layout(heightInLines);
this.updatePlaceholder();
}));

this.breakpointWidgetVisible = this.contextKeyService.createKey('isBreakpointWidgetVisible', false);
const localContext = this.contextKeyService.createScoped(this.selectNode);
localContext.setContext('breakpointWidgetFocus', true);
this.toDispose.push(localContext);
}

dispose(): void {
Expand Down Expand Up @@ -195,11 +206,13 @@ export class DebugBreakpointWidget implements Disposable {
const heightInLines = editor.getModel()!.getLineCount() + 1;
this.zone.show({ afterLineNumber, afterColumn, heightInLines, frameWidth: 1 });
editor.setPosition(editor.getModel()!.getPositionAt(editor.getModel()!.getValueLength()));
this.breakpointWidgetVisible.set(true);
this._input.focus();
}

hide(): void {
this.zone.hide();
this.breakpointWidgetVisible.set(false);
this.editor.focus();
}

Expand Down
19 changes: 16 additions & 3 deletions packages/git/src/browser/blame/blame-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************

import { inject, injectable } from '@theia/core/shared/inversify';
import { inject, injectable, postConstruct } from '@theia/core/shared/inversify';
import { KeybindingContribution, KeybindingRegistry } from '@theia/core/lib/browser';
import { CommandContribution, CommandRegistry, Command, MenuContribution, MenuModelRegistry, DisposableCollection } from '@theia/core/lib/common';
import { BlameDecorator } from './blame-decorator';
import { EditorManager, EditorKeybindingContexts, EditorWidget, EditorTextFocusContext, StrictEditorTextFocusContext } from '@theia/editor/lib/browser';
import { BlameManager } from './blame-manager';
import URI from '@theia/core/lib/common/uri';
import { EDITOR_CONTEXT_MENU_SCM } from '@theia/scm-extra/lib/browser/scm-extra-contribution';
import { ContextKey, ContextKeyService } from '@theia/core/lib/browser/context-key-service';

import debounce = require('@theia/core/shared/lodash.debounce');

Expand All @@ -48,6 +49,16 @@ export class BlameContribution implements CommandContribution, KeybindingContrib
@inject(BlameManager)
protected readonly blameManager: BlameManager;

@inject(ContextKeyService)
protected readonly contextKeyService: ContextKeyService;

protected visibleBlameAnnotations: ContextKey<boolean>;

@postConstruct()
protected init(): void {
this.visibleBlameAnnotations = this.contextKeyService.createKey<boolean>('showsBlameAnnotations', false);
}

registerCommands(commands: CommandRegistry): void {
commands.registerCommand(BlameCommands.TOGGLE_GIT_ANNOTATIONS, {
execute: () => {
Expand Down Expand Up @@ -123,6 +134,7 @@ export class BlameContribution implements CommandContribution, KeybindingContrib
}
}, 50)));
editorWidget.disposed.connect(() => this.clearBlame(uri));
this.visibleBlameAnnotations.set(true);
}
} finally {
if (toDispose.disposed) {
Expand All @@ -136,6 +148,7 @@ export class BlameContribution implements CommandContribution, KeybindingContrib
if (decorations) {
this.appliedDecorations.delete(uri.toString());
decorations.dispose();
this.visibleBlameAnnotations.reset();
}
}

Expand All @@ -148,12 +161,12 @@ export class BlameContribution implements CommandContribution, KeybindingContrib
registerKeybindings(keybindings: KeybindingRegistry): void {
keybindings.registerKeybinding({
command: BlameCommands.TOGGLE_GIT_ANNOTATIONS.id,
context: EditorKeybindingContexts.editorTextFocus,
when: EditorKeybindingContexts.editorTextFocus,
keybinding: 'alt+b'
});
keybindings.registerKeybinding({
command: BlameCommands.CLEAR_GIT_ANNOTATIONS.id,
context: BlameAnnotationsKeybindingContext.showsBlameAnnotations,
when: BlameAnnotationsKeybindingContext.showsBlameAnnotations,
keybinding: 'esc'
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class NotificationsContribution implements FrontendApplicationContributio
registerKeybindings(keybindings: KeybindingRegistry): void {
keybindings.registerKeybinding({
command: NotificationsCommands.HIDE.id,
context: NotificationsKeybindingContext.notificationsVisible,
when: NotificationsKeybindingContext.notificationsVisible,
keybinding: 'esc'
});
}
Expand Down
3 changes: 3 additions & 0 deletions packages/messages/src/browser/notifications-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export class NotificationManager extends MessageClient {

protected notificationToastsVisibleKey: ContextKey<boolean>;
protected notificationCenterVisibleKey: ContextKey<boolean>;
protected notificationsVisible: ContextKey<boolean>;

@postConstruct()
protected init(): void {
Expand All @@ -91,11 +92,13 @@ export class NotificationManager extends MessageClient {
protected async doInit(): Promise<void> {
this.notificationToastsVisibleKey = this.contextKeyService.createKey<boolean>('notificationToastsVisible', false);
this.notificationCenterVisibleKey = this.contextKeyService.createKey<boolean>('notificationCenterVisible', false);
this.notificationsVisible = this.contextKeyService.createKey<boolean>('notificationsVisible', false);
}

protected updateContextKeys(): void {
this.notificationToastsVisibleKey.set(this.toastsVisible);
this.notificationCenterVisibleKey.set(this.centerVisible);
this.notificationsVisible.set(this.toastsVisible || this.centerVisible);
}

get toastsVisible(): boolean {
Expand Down
7 changes: 3 additions & 4 deletions packages/navigator/src/browser/navigator-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import {
import { EXPLORER_VIEW_CONTAINER_ID, EXPLORER_VIEW_CONTAINER_TITLE_OPTIONS } from './navigator-widget-factory';
import { FILE_NAVIGATOR_ID, FileNavigatorWidget } from './navigator-widget';
import { FileNavigatorPreferences } from './navigator-preferences';
import { NavigatorKeybindingContexts } from './navigator-keybinding-context';
import { FileNavigatorFilter } from './navigator-filter';
import { WorkspaceNode } from './navigator-tree';
import { NavigatorContextKeyService } from './navigator-context-key-service';
Expand Down Expand Up @@ -507,19 +506,19 @@ export class FileNavigatorContribution extends AbstractViewContribution<FileNavi
registry.registerKeybinding({
command: WorkspaceCommands.FILE_DELETE.id,
keybinding: isOSX ? 'cmd+backspace' : 'del',
context: NavigatorKeybindingContexts.navigatorActive
when: 'filesExplorerFocus'
});

registry.registerKeybinding({
command: WorkspaceCommands.FILE_RENAME.id,
keybinding: 'f2',
context: NavigatorKeybindingContexts.navigatorActive
when: 'filesExplorerFocus'
});

registry.registerKeybinding({
command: FileNavigatorCommands.TOGGLE_HIDDEN_FILES.id,
keybinding: 'ctrlcmd+i',
context: NavigatorKeybindingContexts.navigatorActive
when: 'filesExplorerFocus'
});
}

Expand Down
15 changes: 13 additions & 2 deletions packages/terminal/src/browser/terminal-frontend-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,18 @@ export class TerminalFrontendContribution implements FrontendApplicationContribu
});

const terminalFocusKey = this.contextKeyService.createKey<boolean>('terminalFocus', false);
const updateFocusKey = () => terminalFocusKey.set(this.shell.activeWidget instanceof TerminalWidget);
const terminalSearchToggle = this.contextKeyService.createKey<boolean>('hideSearch', false);
const updateSearchToggle = () => {
if (!(this.shell.activeWidget instanceof TerminalWidget)) {
return false;
}
const searchWidget = this.shell.activeWidget.getSearchBox();
return searchWidget.isVisible;
};
const updateFocusKey = () => {
terminalFocusKey.set(this.shell.activeWidget instanceof TerminalWidget);
terminalSearchToggle.set(updateSearchToggle());
};
updateFocusKey();
this.shell.onDidChangeActiveWidget(updateFocusKey);

Expand Down Expand Up @@ -844,7 +855,7 @@ export class TerminalFrontendContribution implements FrontendApplicationContribu
keybindings.registerKeybinding({
command: TerminalCommands.TERMINAL_FIND_TEXT_CANCEL.id,
keybinding: 'esc',
context: TerminalKeybindingContexts.terminalHideSearch
when: TerminalKeybindingContexts.terminalHideSearch
});
keybindings.registerKeybinding({
command: TerminalCommands.SCROLL_LINE_UP.id,
Expand Down

0 comments on commit 7742633

Please sign in to comment.