Skip to content

Commit a37ecd5

Browse files
authored
Merge pull request microsoft#182235 from microsoft/aamunger/webviewInputContextKey
new context key for input elements focused within the notebook output webview
2 parents 6189b6f + 836770d commit a37ecd5

File tree

6 files changed

+41
-1
lines changed

6 files changed

+41
-1
lines changed

src/vs/workbench/contrib/notebook/browser/diff/notebookDiffEditor.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ export class NotebookTextDiffEditor extends EditorPane implements INotebookTextD
183183
// throw new Error('Method not implemented.');
184184
}
185185

186+
didFocusOutputInputChange(inputFocused: boolean): void {
187+
// noop
188+
}
189+
186190
getScrollTop() {
187191
return this._list?.scrollTop ?? 0;
188192
}

src/vs/workbench/contrib/notebook/browser/notebookEditorWidget.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ import { NotebookOverviewRuler } from 'vs/workbench/contrib/notebook/browser/vie
7171
import { ListTopCellToolbar } from 'vs/workbench/contrib/notebook/browser/viewParts/notebookTopCellToolbar';
7272
import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel';
7373
import { CellEditType, CellKind, INotebookSearchOptions, RENDERER_NOT_AVAILABLE, SelectionStateType } from 'vs/workbench/contrib/notebook/common/notebookCommon';
74-
import { NOTEBOOK_CURSOR_NAVIGATION_MODE, NOTEBOOK_EDITOR_EDITABLE, NOTEBOOK_EDITOR_FOCUSED, NOTEBOOK_OUTPUT_FOCUSED } from 'vs/workbench/contrib/notebook/common/notebookContextKeys';
74+
import { NOTEBOOK_CURSOR_NAVIGATION_MODE, NOTEBOOK_EDITOR_EDITABLE, NOTEBOOK_EDITOR_FOCUSED, NOTEBOOK_OUTPUT_FOCUSED, NOTEBOOK_OUPTUT_INPUT_FOCUSED } from 'vs/workbench/contrib/notebook/common/notebookContextKeys';
7575
import { INotebookExecutionService } from 'vs/workbench/contrib/notebook/common/notebookExecutionService';
7676
import { INotebookExecutionStateService } from 'vs/workbench/contrib/notebook/common/notebookExecutionStateService';
7777
import { INotebookKernelService } from 'vs/workbench/contrib/notebook/common/notebookKernelService';
@@ -198,6 +198,7 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditorD
198198
private readonly _outputFocus: IContextKey<boolean>;
199199
private readonly _editorEditable: IContextKey<boolean>;
200200
private readonly _cursorNavMode: IContextKey<boolean>;
201+
private readonly _outputInputFocus: IContextKey<boolean>;
201202
protected readonly _contributions = new Map<string, INotebookEditorContribution>();
202203
private _scrollBeyondLastLine: boolean;
203204
private readonly _insetModifyQueueByOutputId = new SequencerByKey<string>();
@@ -390,6 +391,7 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditorD
390391
this._isVisible = true;
391392
this._editorFocus = NOTEBOOK_EDITOR_FOCUSED.bindTo(this.scopedContextKeyService);
392393
this._outputFocus = NOTEBOOK_OUTPUT_FOCUSED.bindTo(this.scopedContextKeyService);
394+
this._outputInputFocus = NOTEBOOK_OUPTUT_INPUT_FOCUSED.bindTo(this.scopedContextKeyService);
393395
this._editorEditable = NOTEBOOK_EDITOR_EDITABLE.bindTo(this.scopedContextKeyService);
394396
this._cursorNavMode = NOTEBOOK_CURSOR_NAVIGATION_MODE.bindTo(this.scopedContextKeyService);
395397

@@ -1349,6 +1351,7 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditorD
13491351
didEndDragMarkupCell: that._didEndDragMarkupCell.bind(that),
13501352
didResizeOutput: that._didResizeOutput.bind(that),
13511353
updatePerformanceMetadata: that._updatePerformanceMetadata.bind(that),
1354+
didFocusOutputInputChange: that._didFocusOutputInputChange.bind(that),
13521355
}, id, viewType, resource, {
13531356
...this._notebookOptions.computeWebviewOptions(),
13541357
fontFamily: this._generateFontFamily()
@@ -1975,6 +1978,10 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditorD
19751978
return false;
19761979
}
19771980

1981+
_didFocusOutputInputChange(hasFocus: boolean) {
1982+
this._outputInputFocus.set(hasFocus);
1983+
}
1984+
19781985
//#endregion
19791986

19801987
//#region Editor Features

src/vs/workbench/contrib/notebook/browser/view/renderers/backLayerWebView.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export interface INotebookDelegateForWebview {
9494
setScrollTop(scrollTop: number): void;
9595
triggerScroll(event: IMouseWheelEvent): void;
9696
updatePerformanceMetadata(cellId: string, executionId: string, duration: number, rendererId: string): void;
97+
didFocusOutputInputChange(inputFocused: boolean): void;
9798
}
9899

99100
interface BacklayerWebviewOptions {
@@ -877,6 +878,9 @@ export class BackLayerWebView<T extends ICommonCellInfo> extends Themable {
877878
this.notebookEditor.updatePerformanceMetadata(data.cellId, data.executionId, data.duration, data.rendererId);
878879
break;
879880
}
881+
case 'outputInputFocus': {
882+
this.notebookEditor.didFocusOutputInputChange(data.inputFocused);
883+
}
880884
}
881885
}));
882886

src/vs/workbench/contrib/notebook/browser/view/renderers/webviewMessages.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ export interface IOutputBlurMessage extends BaseToWebviewMessage {
4747
readonly id: string;
4848
}
4949

50+
export interface IOutputInputFocusMessage extends BaseToWebviewMessage {
51+
readonly type: 'outputInputFocus';
52+
readonly inputFocused: boolean;
53+
}
54+
5055
export interface IScrollToRevealMessage extends BaseToWebviewMessage {
5156
readonly type: 'scroll-to-reveal';
5257
readonly scrollTop: number;
@@ -475,6 +480,7 @@ export type FromWebviewMessage = WebviewInitialized |
475480
IMouseLeaveMessage |
476481
IOutputFocusMessage |
477482
IOutputBlurMessage |
483+
IOutputInputFocusMessage |
478484
IScrollToRevealMessage |
479485
IWheelMessage |
480486
IScrollAckMessage |

src/vs/workbench/contrib/notebook/browser/view/renderers/webviewPreloads.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,23 @@ async function webviewPreloads(ctx: PreloadContext) {
137137
};
138138
};
139139

140+
// check if an input element is focused within the output element
141+
const checkOutputInputFocus = () => {
142+
143+
const activeElement = document.activeElement;
144+
if (!activeElement) {
145+
return;
146+
}
147+
148+
if (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA') {
149+
postNotebookMessage<webviewMessages.IOutputInputFocusMessage>('outputInputFocus', { inputFocused: true });
150+
151+
activeElement.addEventListener('blur', () => {
152+
postNotebookMessage<webviewMessages.IOutputInputFocusMessage>('outputInputFocus', { inputFocused: false });
153+
}, { once: true });
154+
}
155+
};
156+
140157
const handleInnerClick = (event: MouseEvent) => {
141158
if (!event || !event.view || !event.view.document) {
142159
return;
@@ -222,6 +239,7 @@ async function webviewPreloads(ctx: PreloadContext) {
222239
};
223240

224241
document.body.addEventListener('click', handleInnerClick);
242+
document.body.addEventListener('focusin', checkOutputInputFocus);
225243

226244
interface RendererContext extends rendererApi.RendererContext<unknown> {
227245
readonly onDidChangeSettings: Event<RenderOptions>;

src/vs/workbench/contrib/notebook/common/notebookContextKeys.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const INTERACTIVE_WINDOW_IS_ACTIVE_EDITOR = ContextKeyExpr.equals('active
2020
export const NOTEBOOK_EDITOR_FOCUSED = new RawContextKey<boolean>('notebookEditorFocused', false);
2121
export const NOTEBOOK_CELL_LIST_FOCUSED = new RawContextKey<boolean>('notebookCellListFocused', false);
2222
export const NOTEBOOK_OUTPUT_FOCUSED = new RawContextKey<boolean>('notebookOutputFocused', false);
23+
export const NOTEBOOK_OUPTUT_INPUT_FOCUSED = new RawContextKey<boolean>('notebookOutputInputFocused', false);
2324
export const NOTEBOOK_EDITOR_EDITABLE = new RawContextKey<boolean>('notebookEditable', true);
2425
export const NOTEBOOK_HAS_RUNNING_CELL = new RawContextKey<boolean>('notebookHasRunningCell', false);
2526
export const NOTEBOOK_HAS_SOMETHING_RUNNING = new RawContextKey<boolean>('notebookHasSomethingRunning', false);

0 commit comments

Comments
 (0)