From ed1ecfd53447ae2e41acfc7f077ac0cc2444c768 Mon Sep 17 00:00:00 2001 From: rebornix Date: Thu, 16 Jul 2020 16:25:06 -0700 Subject: [PATCH] kernel picker status bar item. --- .../browser/contrib/status/editorStatus.ts | 73 ++++++++++++++++--- .../notebook/browser/notebookBrowser.ts | 5 +- .../notebook/browser/notebookEditorWidget.ts | 9 ++- .../notebook/browser/notebookServiceImpl.ts | 2 +- .../notebook/test/testNotebookEditor.ts | 2 + 5 files changed, 74 insertions(+), 17 deletions(-) diff --git a/src/vs/workbench/contrib/notebook/browser/contrib/status/editorStatus.ts b/src/vs/workbench/contrib/notebook/browser/contrib/status/editorStatus.ts index 6e3e8447fd7..4e072f77fb1 100644 --- a/src/vs/workbench/contrib/notebook/browser/contrib/status/editorStatus.ts +++ b/src/vs/workbench/contrib/notebook/browser/contrib/status/editorStatus.ts @@ -4,15 +4,20 @@ *--------------------------------------------------------------------------------------------*/ import * as nls from 'vs/nls'; -import { Action2, MenuId, registerAction2 } from 'vs/platform/actions/common/actions'; +import { Registry } from 'vs/platform/registry/common/platform'; +import { Action2, registerAction2 } from 'vs/platform/actions/common/actions'; import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; import { IQuickInputService, IQuickPickItem, QuickPickInput } from 'vs/platform/quickinput/common/quickInput'; -import { INotebookCellActionContext, NOTEBOOK_ACTIONS_CATEGORY } from 'vs/workbench/contrib/notebook/browser/contrib/coreActions'; -import { INotebookEditor, NOTEBOOK_HAS_MULTIPLE_KERNELS, NOTEBOOK_IS_ACTIVE_EDITOR } from 'vs/workbench/contrib/notebook/browser/notebookBrowser'; +import { INotebookCellActionContext, NOTEBOOK_ACTIONS_CATEGORY, getActiveNotebookEditor } from 'vs/workbench/contrib/notebook/browser/contrib/coreActions'; +import { INotebookEditor, NOTEBOOK_IS_ACTIVE_EDITOR } from 'vs/workbench/contrib/notebook/browser/notebookBrowser'; import { INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService'; import { IEditorService } from 'vs/workbench/services/editor/common/editorService'; import { CancellationTokenSource } from 'vs/base/common/cancellation'; -import { INotebookKernelInfoDto2, INotebookKernelInfo2 } from 'vs/workbench/contrib/notebook/common/notebookCommon'; +import { INotebookKernelInfo2, INotebookKernelInfo } from 'vs/workbench/contrib/notebook/common/notebookCommon'; +import { Extensions as WorkbenchExtensions, IWorkbenchContributionsRegistry, IWorkbenchContribution } from 'vs/workbench/common/contributions'; +import { LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle'; +import { Disposable, DisposableStore, MutableDisposable } from 'vs/base/common/lifecycle'; +import { IStatusbarEntryAccessor, IStatusbarService, StatusbarAlignment } from 'vs/workbench/services/statusbar/common/statusbar'; registerAction2(class extends Action2 { @@ -23,12 +28,6 @@ registerAction2(class extends Action2 { title: { value: nls.localize('notebookActions.selectKernel', "Select Notebook Kernel"), original: 'Select Notebook Kernel' }, precondition: NOTEBOOK_IS_ACTIVE_EDITOR, icon: { id: 'codicon/server-environment' }, - menu: { - id: MenuId.EditorTitle, - when: NOTEBOOK_HAS_MULTIPLE_KERNELS, - group: 'navigation', - order: -2, - }, f1: true }); } @@ -54,8 +53,8 @@ registerAction2(class extends Action2 { label: a.label, picked: a.id === activeKernel?.id, description: - (a as INotebookKernelInfoDto2).description - ? (a as INotebookKernelInfoDto2).description + (a as INotebookKernelInfo2).description + ? (a as INotebookKernelInfo2).description : a.extension.value + (a.id === activeKernel?.id ? nls.localize('currentActiveKernel', " (Currently Active)") : ''), @@ -89,3 +88,53 @@ registerAction2(class extends Action2 { } }); + +export class KernelStatus extends Disposable implements IWorkbenchContribution { + private _editorDisposable = new DisposableStore(); + private readonly kernelInfoElement = this._register(new MutableDisposable()); + constructor( + @IEditorService private readonly _editorService: IEditorService, + @INotebookService private readonly _notebookService: INotebookService, + @IStatusbarService private readonly _statusbarService: IStatusbarService, + ) { + super(); + this.registerListeners(); + } + + registerListeners() { + this._register(this._editorService.onDidActiveEditorChange(() => this.updateStatusbar())); + this._register(this._notebookService.onDidChangeActiveEditor(() => this.updateStatusbar())); + this._register(this._notebookService.onDidChangeKernels(() => this.updateStatusbar())); + } + + updateStatusbar() { + this._editorDisposable.clear(); + + const activeEditor = getActiveNotebookEditor(this._editorService); + + if (activeEditor && activeEditor.multipleKernelsAvailable) { + this.showKernelStatus(activeEditor.activeKernel); + this._editorDisposable.add(activeEditor.onDidChangeKernel(() => { + if (activeEditor.multipleKernelsAvailable) { + this.showKernelStatus(activeEditor.activeKernel); + } else { + this.kernelInfoElement.clear(); + } + })); + } else { + this.kernelInfoElement.clear(); + } + } + + showKernelStatus(kernel: INotebookKernelInfo | INotebookKernelInfo2 | undefined) { + this.kernelInfoElement.value = this._statusbarService.addEntry({ + text: kernel ? kernel.label : 'Choose Kernel', + ariaLabel: kernel ? kernel.label : 'Choose Kernel', + tooltip: nls.localize('chooseActiveKernel', "Choose kernel for current notebook"), + command: 'notebook.selectKernel', + }, 'notebook.selectKernel', nls.localize('notebook.selectKernel', "Choose kernel for current notebook"), StatusbarAlignment.RIGHT, 100); + } +} + +Registry.as(WorkbenchExtensions.Workbench).registerWorkbenchContribution(KernelStatus, LifecyclePhase.Ready); + diff --git a/src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts b/src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts index d0e74e04e40..231e056f7c6 100644 --- a/src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts +++ b/src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts @@ -23,7 +23,7 @@ import { OutputRenderer } from 'vs/workbench/contrib/notebook/browser/view/outpu import { CellLanguageStatusBarItem, TimerRenderer } from 'vs/workbench/contrib/notebook/browser/view/renderers/cellRenderer'; import { CellViewModel, IModelDecorationsChangeAccessor, NotebookViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel'; import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel'; -import { CellKind, IProcessedOutput, IRenderOutput, NotebookCellMetadata, NotebookDocumentMetadata, INotebookKernelInfo, IEditor, INotebookKernelInfoDto2 } from 'vs/workbench/contrib/notebook/common/notebookCommon'; +import { CellKind, IProcessedOutput, IRenderOutput, NotebookCellMetadata, NotebookDocumentMetadata, INotebookKernelInfo, IEditor, INotebookKernelInfoDto2, INotebookKernelInfo2 } from 'vs/workbench/contrib/notebook/common/notebookCommon'; import { Webview } from 'vs/workbench/contrib/webview/browser/webview'; import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel'; @@ -171,7 +171,8 @@ export interface INotebookEditor extends IEditor { readonly onDidChangeModel: Event; readonly onDidFocusEditorWidget: Event; isNotebookEditor: boolean; - activeKernel: INotebookKernelInfo | INotebookKernelInfoDto2 | undefined; + activeKernel: INotebookKernelInfo | INotebookKernelInfo2 | undefined; + multipleKernelsAvailable: boolean; readonly onDidChangeKernel: Event; isDisposed: boolean; diff --git a/src/vs/workbench/contrib/notebook/browser/notebookEditorWidget.ts b/src/vs/workbench/contrib/notebook/browser/notebookEditorWidget.ts index e4c528dc905..749495f2a87 100644 --- a/src/vs/workbench/contrib/notebook/browser/notebookEditorWidget.ts +++ b/src/vs/workbench/contrib/notebook/browser/notebookEditorWidget.ts @@ -132,7 +132,7 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditor return this._notebookViewModel?.notebookDocument; } - private _activeKernel: INotebookKernelInfo | INotebookKernelInfoDto2 | undefined = undefined; + private _activeKernel: INotebookKernelInfo | INotebookKernelInfo2 | undefined = undefined; private readonly _onDidChangeKernel = this._register(new Emitter()); readonly onDidChangeKernel: Event = this._onDidChangeKernel.event; @@ -140,7 +140,7 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditor return this._activeKernel; } - set activeKernel(kernel: INotebookKernelInfo | INotebookKernelInfoDto2 | undefined) { + set activeKernel(kernel: INotebookKernelInfo | INotebookKernelInfo2 | undefined) { if (this._isDisposed) { return; } @@ -149,6 +149,8 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditor this._onDidChangeKernel.fire(); } + multipleKernelsAvailable: boolean = false; + private readonly _onDidChangeActiveEditor = this._register(new Emitter()); readonly onDidChangeActiveEditor: Event = this._onDidChangeActiveEditor.event; @@ -560,10 +562,13 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditor if (provider.kernel && (availableKernels.length + availableKernels2.length) > 0) { this._notebookHasMultipleKernels!.set(true); + this.multipleKernelsAvailable = true; } else if ((availableKernels.length + availableKernels2.length) > 1) { this._notebookHasMultipleKernels!.set(true); + this.multipleKernelsAvailable = true; } else { this._notebookHasMultipleKernels!.set(false); + this.multipleKernelsAvailable = false; } if (provider && provider.kernel) { diff --git a/src/vs/workbench/contrib/notebook/browser/notebookServiceImpl.ts b/src/vs/workbench/contrib/notebook/browser/notebookServiceImpl.ts index 20ec35b2b6e..6fce933fdb9 100644 --- a/src/vs/workbench/contrib/notebook/browser/notebookServiceImpl.ts +++ b/src/vs/workbench/contrib/notebook/browser/notebookServiceImpl.ts @@ -329,7 +329,7 @@ export class NotebookService extends Disposable implements INotebookService, ICu const tokenSource = new CancellationTokenSource(); return provider.resolveKernel(editorId, uri, dto.id, tokenSource.token); }, - executeNotebook: async (uri: URI, handle: number | undefined, token: CancellationToken) => { + executeNotebookCell: async (uri: URI, handle: number | undefined, token: CancellationToken) => { return provider.executeNotebook(uri, dto.id, handle, token); } }; diff --git a/src/vs/workbench/contrib/notebook/test/testNotebookEditor.ts b/src/vs/workbench/contrib/notebook/test/testNotebookEditor.ts index 61169d09d99..63020e5d178 100644 --- a/src/vs/workbench/contrib/notebook/test/testNotebookEditor.ts +++ b/src/vs/workbench/contrib/notebook/test/testNotebookEditor.ts @@ -62,6 +62,8 @@ export class TestNotebookEditor implements INotebookEditor { constructor( ) { } + multipleKernelsAvailable: boolean = false; + uri?: URI | undefined; textModel?: NotebookTextModel | undefined; -- GitLab