From 965b19a28f973195a985f3ec080be8de3908051c Mon Sep 17 00:00:00 2001 From: Rob Lourens Date: Wed, 6 May 2020 10:58:56 -0500 Subject: [PATCH] Handle focus in notebook going to webview Fix #93882 --- .../contrib/notebook/browser/notebookEditor.ts | 17 ++++++++++------- .../webview/browser/baseWebviewElement.ts | 5 +++++ .../contrib/webview/browser/webview.ts | 1 + 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/vs/workbench/contrib/notebook/browser/notebookEditor.ts b/src/vs/workbench/contrib/notebook/browser/notebookEditor.ts index 007ed398466..978435b1151 100644 --- a/src/vs/workbench/contrib/notebook/browser/notebookEditor.ts +++ b/src/vs/workbench/contrib/notebook/browser/notebookEditor.ts @@ -170,19 +170,20 @@ export class NotebookEditor extends BaseEditor implements INotebookEditor { return true; } + private updateEditorFocus() { + // Note - focus going to the webview will fire 'blur', but the webview element will be + // a descendent of the notebook editor root. + this.editorFocus?.set(DOM.isAncestor(document.activeElement, this.getDomNode())); + } + protected createEditor(parent: HTMLElement): void { this._rootElement = DOM.append(parent, $('.notebook-editor')); this.createBody(this._rootElement); this.generateFontInfo(); this.editorFocus = NOTEBOOK_EDITOR_FOCUSED.bindTo(this.contextKeyService); this.editorFocus.set(true); - this._register(this.onDidFocus(() => { - this.editorFocus?.set(true); - })); - - this._register(this.onDidBlur(() => { - this.editorFocus?.set(false); - })); + this._register(this.onDidFocus(() => this.updateEditorFocus())); + this._register(this.onDidBlur(() => this.updateEditorFocus())); this.editorEditable = NOTEBOOK_EDITOR_EDITABLE.bindTo(this.contextKeyService); this.editorEditable.set(true); @@ -307,6 +308,8 @@ export class NotebookEditor extends BaseEditor implements INotebookEditor { this.control = new NotebookCodeEditors(this.list, this.renderedEditors); this.webview = this.instantiationService.createInstance(BackLayerWebView, this); + this.webview.webview.onDidBlur(() => this.updateEditorFocus()); + this.webview.webview.onDidFocus(() => this.updateEditorFocus()); this._register(this.webview.onMessage(message => { if (this.viewModel) { this.notebookService.onDidReceiveMessage(this.viewModel.viewType, this.viewModel.uri, message); diff --git a/src/vs/workbench/contrib/webview/browser/baseWebviewElement.ts b/src/vs/workbench/contrib/webview/browser/baseWebviewElement.ts index 5da696c6f04..4d37537fa17 100644 --- a/src/vs/workbench/contrib/webview/browser/baseWebviewElement.ts +++ b/src/vs/workbench/contrib/webview/browser/baseWebviewElement.ts @@ -168,6 +168,9 @@ export abstract class BaseWebview extends Disposable { private readonly _onDidFocus = this._register(new Emitter()); public readonly onDidFocus = this._onDidFocus.event; + private readonly _onDidBlur = this._register(new Emitter()); + public readonly onDidBlur = this._onDidBlur.event; + public sendMessage(data: any): void { this._send('message', data); } @@ -267,6 +270,8 @@ export abstract class BaseWebview extends Disposable { this._focused = isFocused; if (isFocused) { this._onDidFocus.fire(); + } else { + this._onDidBlur.fire(); } } diff --git a/src/vs/workbench/contrib/webview/browser/webview.ts b/src/vs/workbench/contrib/webview/browser/webview.ts index dccddca8426..e592683896b 100644 --- a/src/vs/workbench/contrib/webview/browser/webview.ts +++ b/src/vs/workbench/contrib/webview/browser/webview.ts @@ -79,6 +79,7 @@ export interface Webview extends IDisposable { state: string | undefined; readonly onDidFocus: Event; + readonly onDidBlur: Event; readonly onDidClickLink: Event; readonly onDidScroll: Event<{ scrollYPercentage: number }>; readonly onDidWheel: Event;