diff --git a/src/vs/workbench/contrib/notebook/browser/contrib/coreActions.ts b/src/vs/workbench/contrib/notebook/browser/contrib/coreActions.ts index cb63a7b06b92b..58e4cd2da3d1d 100644 --- a/src/vs/workbench/contrib/notebook/browser/contrib/coreActions.ts +++ b/src/vs/workbench/contrib/notebook/browser/contrib/coreActions.ts @@ -687,8 +687,6 @@ registerAction2(class ExecuteCellSelectBelow extends NotebookCellAction { } return; } else { - const executionP = runCell(accessor, context); - // Try to select below, fall back on inserting const nextCell = context.notebookEditor.viewModel.cellAt(idx + 1); if (nextCell) { @@ -700,7 +698,7 @@ registerAction2(class ExecuteCellSelectBelow extends NotebookCellAction { } } - return executionP; + return runCell(accessor, context); } } }); diff --git a/src/vs/workbench/contrib/notebook/browser/notebookEditor.ts b/src/vs/workbench/contrib/notebook/browser/notebookEditor.ts index ef5a4fa441c0e..6468518da843e 100644 --- a/src/vs/workbench/contrib/notebook/browser/notebookEditor.ts +++ b/src/vs/workbench/contrib/notebook/browser/notebookEditor.ts @@ -49,9 +49,11 @@ export class NotebookEditor extends EditorPane { private readonly inputListener = this._register(new MutableDisposable()); - // todo@rebornix is there a reason that `super.fireOnDidFocus` isn't used? + // override onDidFocus and onDidBlur to be based on the NotebookEditorWidget element private readonly _onDidFocusWidget = this._register(new Emitter()); override get onDidFocus(): Event { return this._onDidFocusWidget.event; } + private readonly _onDidBlurWidget = this._register(new Emitter()); + override get onDidBlur(): Event { return this._onDidBlurWidget.event; } private readonly _onDidChangeModel = this._register(new Emitter()); readonly onDidChangeModel: Event = this._onDidChangeModel.event; @@ -226,6 +228,7 @@ export class NotebookEditor extends EditorPane { const isReadOnly = input.hasCapability(EditorInputCapabilities.Readonly); await this._widget.value!.setOptions({ ...options, isReadOnly }); this._widgetDisposableStore.add(this._widget.value!.onDidFocus(() => this._onDidFocusWidget.fire())); + this._widgetDisposableStore.add(this._widget.value!.onDidBlur(() => this._onDidBlurWidget.fire())); this._widgetDisposableStore.add(this._editorDropService.createEditorDropTarget(this._widget.value!.getDomNode(), { containsGroup: (group) => this.group?.id === group.id diff --git a/src/vs/workbench/contrib/notebook/browser/notebookEditorWidget.ts b/src/vs/workbench/contrib/notebook/browser/notebookEditorWidget.ts index 540da8eb753fa..aad2d558c8e52 100644 --- a/src/vs/workbench/contrib/notebook/browser/notebookEditorWidget.ts +++ b/src/vs/workbench/contrib/notebook/browser/notebookEditorWidget.ts @@ -1621,12 +1621,15 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditor updateEditorFocus() { // Note - focus going to the webview will fire 'blur', but the webview element will be // a descendent of the notebook editor root. - const focused = DOM.isAncestor(document.activeElement, this._overlayContainer); + const focused = this._overlayContainer.contains(document.activeElement); this._editorFocus.set(focused); this.viewModel?.setEditorFocus(focused); } hasEditorFocus() { + // _editorFocus is driven by the FocusTracker, which is only guaranteed to _eventually_ fire blur. + // If we need to know whether we have focus at this instant, we need to check the DOM manually. + this.updateEditorFocus(); return this._editorFocus.get() || false; } diff --git a/src/vs/workbench/contrib/notebook/browser/view/renderers/codeCell.ts b/src/vs/workbench/contrib/notebook/browser/view/renderers/codeCell.ts index a507556d08715..02ecec5c8f1bc 100644 --- a/src/vs/workbench/contrib/notebook/browser/view/renderers/codeCell.ts +++ b/src/vs/workbench/contrib/notebook/browser/view/renderers/codeCell.ts @@ -56,18 +56,19 @@ export class CodeCell extends Disposable { if (model && templateData.editor) { templateData.editor.setModel(model); viewCell.attachTextEditor(templateData.editor); - if (notebookEditor.getActiveCell() === viewCell && viewCell.focusMode === CellFocusMode.Editor && this.notebookEditor.hasEditorFocus()) { - templateData.editor?.focus(); - } + const focusEditorIfNeeded = () => { + if (notebookEditor.getActiveCell() === viewCell && viewCell.focusMode === CellFocusMode.Editor && this.notebookEditor.hasEditorFocus()) { + templateData.editor?.focus(); + } + }; + focusEditorIfNeeded(); const realContentHeight = templateData.editor?.getContentHeight(); if (realContentHeight !== undefined && realContentHeight !== editorHeight) { this.onCellHeightChange(realContentHeight); } - if (this.notebookEditor.getActiveCell() === this.viewCell && viewCell.focusMode === CellFocusMode.Editor && this.notebookEditor.hasEditorFocus()) { - templateData.editor?.focus(); - } + focusEditorIfNeeded(); } });