Skip to content

Commit

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

Signed-Off-By: Fernando Ascencio <fernando.ascencio.cama@ericsson.com>
  • Loading branch information
FernandoAscencio committed Aug 30, 2023
1 parent bdc34ba commit 50b1e04
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 31 deletions.
9 changes: 4 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,6 @@ 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 { ConsoleWidget } from './console-widget';
import { ConsoleContentWidget } from './console-content-widget';
import { nls } from '@theia/core/lib/common/nls';
Expand Down Expand Up @@ -70,22 +69,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 && consoleNavigationBackEnabled'
});
keybindings.registerKeybinding({
command: ConsoleCommands.NAVIGATE_FORWARD.id,
keybinding: 'down',
context: ConsoleKeybindingContexts.consoleNavigationForwardEnabled
when: 'consoleInputFocus && consoleNavigationForwardEnabled'
});
}

Expand Down
24 changes: 23 additions & 1 deletion 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 _inputFocusContextKey: ContextKey<boolean>;

constructor() {
super();
Expand Down Expand Up @@ -129,7 +133,13 @@ export class ConsoleWidget extends BaseWidget implements StatefulWidget {
this.updateFont();
if (inputFocusContextKey) {
this.toDispose.push(input.onFocusChanged(() => inputFocusContextKey.set(this.hasInputFocus())));
this.toDispose.push(input.onCursorPositionChanged(() => input.getControl().createContextKey('consoleNavigationBackEnabled', this.consoleNavigationBackEnabled)));
this.toDispose.push(input.onCursorPositionChanged(() => input.getControl().createContextKey('consoleNavigationForwardEnabled', this.consoleNavigationForwardEnabled)));
}
input.getControl().createContextKey('consoleInputFocus', true);
const contentContext = this.contextKeyService.createScoped(this.content.node);
contentContext.setContext('consoleContentFocus', true);
this.toDispose.push(contentContext);
}

protected createInput(node: HTMLElement): Promise<MonacoEditor> {
Expand Down Expand Up @@ -159,6 +169,18 @@ export class ConsoleWidget extends BaseWidget implements StatefulWidget {
return this._input;
}

get consoleNavigationBackEnabled(): boolean {
const editor = this.input.getControl();
return !!editor.getPosition()!.equals({ lineNumber: 1, column: 1 });
}

get consoleNavigationForwardEnabled(): boolean {
const editor = this.input.getControl();
const lineNumber = editor.getModel()!.getLineCount();
const column = editor.getModel()!.getLineMaxColumn(lineNumber);
return !!editor.getPosition()!.equals({ lineNumber, column });
}

selectAll(): void {
const selection = document.getSelection();
if (selection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
import { injectable, inject } from '@theia/core/shared/inversify';
import * as monaco from '@theia/monaco-editor-core';
import { MenuModelRegistry, CommandRegistry, MAIN_MENU_BAR, Command, Emitter, Mutable, CompoundMenuNodeRole } from '@theia/core/lib/common';
import { EDITOR_LINENUMBER_CONTEXT_MENU, EditorKeybindingContexts, EditorManager } from '@theia/editor/lib/browser';
import { EDITOR_LINENUMBER_CONTEXT_MENU, EditorManager } from '@theia/editor/lib/browser';
import { DebugSessionManager } from './debug-session-manager';
import { DebugWidget } from './view/debug-widget';
import { FunctionBreakpoint } from './breakpoint/breakpoint-marker';
Expand All @@ -36,7 +36,6 @@ import { DebugStackFrame } from './model/debug-stack-frame';
import { DebugVariablesWidget } from './view/debug-variables-widget';
import { DebugVariable } from './console/debug-console-items';
import { DebugSessionWidget } from './view/debug-session-widget';
import { DebugKeybindingContexts } from './debug-keybinding-contexts';
import { DebugEditorModel } from './editor/debug-editor-model';
import { DebugEditorService } from './editor/debug-editor-service';
import { DebugConsoleContribution } from './console/debug-console-contribution';
Expand Down Expand Up @@ -1021,60 +1020,60 @@ export class DebugFrontendApplicationContribution extends AbstractViewContributi
keybindings.registerKeybinding({
command: DebugCommands.STOP.id,
keybinding: 'shift+f5',
context: DebugKeybindingContexts.inDebugMode
when: 'inDebugMode'
});

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

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

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

Expand Down
3 changes: 3 additions & 0 deletions packages/debug/src/browser/editor/debug-breakpoint-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export class DebugBreakpointWidget implements Disposable {
this.zone.layout(heightInLines);
this.updatePlaceholder();
}));
this._input.getControl().createContextKey<boolean>('breakpointWidgetFocus', true);
}

dispose(): void {
Expand Down Expand Up @@ -198,10 +199,12 @@ export class DebugBreakpointWidget implements Disposable {
this.zone.show({ afterLineNumber, afterColumn, heightInLines, frameWidth: 1 });
editor.setPosition(editor.getModel()!.getPositionAt(editor.getModel()!.getValueLength()));
this._input.focus();
this.editor.getControl().createContextKey<boolean>('isBreakpointWidgetVisible', true);
}

hide(): void {
this.zone.hide();
this.editor.getControl().createContextKey<boolean>('isBreakpointWidgetVisible', false);
this.editor.focus();
}

Expand Down
34 changes: 30 additions & 4 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 { EditorManager, 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,21 @@ 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', this.visibleBlameAnnotations());
this.editorManager.onActiveEditorChanged(() => this.updateContext());
}

protected updateContext(): void {
this._visibleBlameAnnotations.set(this.visibleBlameAnnotations());
}

registerCommands(commands: CommandRegistry): void {
commands.registerCommand(BlameCommands.TOGGLE_GIT_ANNOTATIONS, {
execute: () => {
Expand Down Expand Up @@ -100,6 +116,14 @@ export class BlameContribution implements CommandContribution, KeybindingContrib
return this.blameManager.isBlameable(uri.toString());
}

protected visibleBlameAnnotations(): boolean {
const widget = this.editorManager.activeEditor;
if (widget && widget.editor.isFocused() && this.showsBlameAnnotations(widget.editor.uri)) {
return true;
}
return false;
}

protected appliedDecorations = new Map<string, DisposableCollection>();

protected async showBlame(editorWidget: EditorWidget): Promise<void> {
Expand Down Expand Up @@ -128,6 +152,7 @@ export class BlameContribution implements CommandContribution, KeybindingContrib
if (toDispose.disposed) {
this.appliedDecorations.delete(uri);
};
this.updateContext();
}
}

Expand All @@ -136,6 +161,7 @@ export class BlameContribution implements CommandContribution, KeybindingContrib
if (decorations) {
this.appliedDecorations.delete(uri.toString());
decorations.dispose();
this.updateContext();
}
}

Expand All @@ -148,12 +174,12 @@ export class BlameContribution implements CommandContribution, KeybindingContrib
registerKeybindings(keybindings: KeybindingRegistry): void {
keybindings.registerKeybinding({
command: BlameCommands.TOGGLE_GIT_ANNOTATIONS.id,
context: EditorKeybindingContexts.editorTextFocus,
when: 'editorTextFocus',
keybinding: 'alt+b'
});
keybindings.registerKeybinding({
command: BlameCommands.CLEAR_GIT_ANNOTATIONS.id,
context: BlameAnnotationsKeybindingContext.showsBlameAnnotations,
when: '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: '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
17 changes: 14 additions & 3 deletions packages/terminal/src/browser/terminal-frontend-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import {
} from '@theia/core/lib/browser';
import { TabBarToolbarContribution, TabBarToolbarRegistry } from '@theia/core/lib/browser/shell/tab-bar-toolbar';
import { TERMINAL_WIDGET_FACTORY_ID, TerminalWidgetFactoryOptions, TerminalWidgetImpl } from './terminal-widget-impl';
import { TerminalKeybindingContexts } from './terminal-keybinding-contexts';
import { TerminalService } from './base/terminal-service';
import { TerminalWidgetOptions, TerminalWidget, TerminalLocation } from './base/terminal-widget';
import { ContributedTerminalProfileStore, NULL_PROFILE, TerminalProfile, TerminalProfileService, TerminalProfileStore, UserTerminalProfileStore } from './terminal-profile-service';
Expand Down Expand Up @@ -239,7 +238,11 @@ 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>('terminalHideSearch', false);
const updateFocusKey = () => {
terminalFocusKey.set(this.shell.activeWidget instanceof TerminalWidget);
terminalSearchToggle.set(this.terminalHideSearch);
};
updateFocusKey();
this.shell.onDidChangeActiveWidget(updateFocusKey);

Expand All @@ -256,6 +259,14 @@ export class TerminalFrontendContribution implements FrontendApplicationContribu
});
}

get terminalHideSearch(): boolean {
if (!(this.shell.activeWidget instanceof TerminalWidget)) {
return false;
}
const searchWidget = this.shell.activeWidget.getSearchBox();
return searchWidget.isVisible;
}

async onStart(app: FrontendApplication): Promise<void> {
await this.contributeDefaultProfiles();

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: 'terminalHideSearch'
});
keybindings.registerKeybinding({
command: TerminalCommands.SCROLL_LINE_UP.id,
Expand Down

0 comments on commit 50b1e04

Please sign in to comment.