Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[monaco] only focus an editor if it's revealed #7903

Merged
merged 1 commit into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/editor/src/browser/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ export interface TextEditor extends Disposable, TextEditorSelection, Navigatable
selection: Range;
readonly onSelectionChanged: Event<Range>;

/**
* The text editor should be revealed,
* otherwise it won't receive the focus.
*/
focus(): void;
blur(): void;
isFocused(): boolean;
Expand Down
4 changes: 4 additions & 0 deletions packages/monaco/src/browser/monaco-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ export class MonacoEditorCommandHandlers implements CommandContribution {
}
const handler: CommandHandler = {
execute: (...args) => {
/*
* We check monaco focused code editor first since they can contain inline like the debug console and embedded editors like in the peek reference.
* If there is not such then we check last focused editor tracked by us.
*/
const editor = codeEditorService.getFocusedCodeEditor() || codeEditorService.getActiveCodeEditor();
if (editorActions.has(id)) {
const action = editor && editor.getAction(id);
Expand Down
15 changes: 14 additions & 1 deletion packages/monaco/src/browser/monaco-editor-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,18 +377,31 @@ export class MonacoEditorProvider {
const control = editor.getControl();
const quickOpenController = control._contributions['editor.controller.quickOpenController'];
const originalRun = quickOpenController.run;
const toDispose = new DisposableCollection();
quickOpenController.dispose = () => toDispose.dispose();
quickOpenController.run = options => {
const toDisposeOnClose = toDispose.push(Disposable.create(() => this.quickOpenService.hide()));

const selection = control.getSelection();
this.quickOpenService.internalOpen({
...options,
onClose: canceled => {
toDisposeOnClose.dispose();

quickOpenController.clearDecorations();

// Restore selection if canceled
if (canceled && selection) {
control.setSelection(selection);
control.revealRangeInCenterIfOutsideViewport(selection);
}
editor.focus();

// Return focus to the editor if
// - focus is back on the <body> element because no other focusable element was clicked
// - a command was picked from the picker which indicates the editor should get focused
if (document.activeElement === document.body || !canceled) {
editor.focus();
}
}
});
};
Expand Down
12 changes: 11 additions & 1 deletion packages/monaco/src/browser/monaco-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,17 @@ export class MonacoEditor extends MonacoEditorServices implements TextEditor {
}

focus(): void {
this.editor.focus();
/**
* `this.editor.focus` forcefully changes the focus editor state,
* regardless whether the textarea actually received the focus.
* It could lead to issues like https://github.com/eclipse-theia/theia/issues/7902
* Instead we focus the underlying textarea.
*/
const node = this.editor.getDomNode();
if (node) {
const textarea = node.querySelector('textarea') as HTMLElement;
textarea.focus();
}
}

blur(): void {
Expand Down
4 changes: 2 additions & 2 deletions packages/monaco/src/typings/monaco/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1074,10 +1074,10 @@ declare module monaco.quickOpen {
// https://github.com/theia-ide/vscode/blob/standalone/0.19.x/src/vs/editor/standalone/browser/quickOpen/editorQuickOpen.ts#L25
export interface QuickOpenController extends IDisposable {
static readonly ID: string;
dispose(): void;
run(opts: IQuickOpenControllerOpts): void;

// https://github.com/theia-ide/vscode/blob/standalone/0.19.x/src/vs/editor/standalone/browser/quickOpen/editorQuickOpen.ts#L168-L169
decorateLine(range: Range, editor: monaco.editor.ICodeEditor): void;
// https://github.com/theia-ide/vscode/blob/standalone/0.19.x/src/vs/editor/standalone/browser/quickOpen/editorQuickOpen.ts#L169
clearDecorations(): void;
}
}
Expand Down