Skip to content

Commit

Permalink
extract nb top cell toolbar.
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornix committed Nov 16, 2021
1 parent aa1a68f commit 57f6be8
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import { OutputRenderer } from 'vs/workbench/contrib/notebook/browser/view/outpu
import { BackLayerWebView, INotebookWebviewMessage } from 'vs/workbench/contrib/notebook/browser/view/renderers/backLayerWebView';
import { CellContextKeyManager } from 'vs/workbench/contrib/notebook/browser/view/viewParts/cellContextKeys';
import { CellDragAndDropController } from 'vs/workbench/contrib/notebook/browser/view/viewParts/cellDnd';
import { CodeCellRenderer, ListTopCellToolbar, MarkupCellRenderer, NotebookCellListDelegate } from 'vs/workbench/contrib/notebook/browser/view/renderers/cellRenderer';
import { CodeCellRenderer, MarkupCellRenderer, NotebookCellListDelegate } from 'vs/workbench/contrib/notebook/browser/view/renderers/cellRenderer';
import { CodeCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/codeCellViewModel';
import { NotebookEventDispatcher } from 'vs/workbench/contrib/notebook/browser/viewModel/eventDispatcher';
import { MarkupCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/markupCellViewModel';
Expand All @@ -70,6 +70,7 @@ import { SuggestController } from 'vs/editor/contrib/suggest/suggestController';
import { registerZIndex, ZIndex } from 'vs/platform/layout/browser/zIndexRegistry';
import { INotebookCellList } from 'vs/workbench/contrib/notebook/browser/view/notebookRenderingCommon';
import { notebookDebug } from 'vs/workbench/contrib/notebook/browser/notebookLogger';
import { ListTopCellToolbar } from 'vs/workbench/contrib/notebook/browser/view/viewParts/topCellToolbar';

const $ = DOM.$;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ import { IInstantiationService } from 'vs/platform/instantiation/common/instanti
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { INotificationService } from 'vs/platform/notification/common/notification';
import { INotebookActionContext, INotebookCellActionContext, INotebookCellToolbarActionContext } from 'vs/workbench/contrib/notebook/browser/controller/coreActions';
import { INotebookCellActionContext, INotebookCellToolbarActionContext } from 'vs/workbench/contrib/notebook/browser/controller/coreActions';
import { CodeCellLayoutInfo, EXPAND_CELL_OUTPUT_COMMAND_ID, ICellViewModel, INotebookEditorDelegate, NOTEBOOK_CELL_EXECUTION_STATE, NOTEBOOK_CELL_LIST_FOCUSED, NOTEBOOK_CELL_TYPE, NOTEBOOK_EDITOR_FOCUSED } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
import { BaseCellRenderTemplate, CodeCellRenderTemplate, MarkdownCellRenderTemplate } from 'vs/workbench/contrib/notebook/browser/view/notebookRenderingCommon';
import { CodiconActionViewItem } from 'vs/workbench/contrib/notebook/browser/view/viewParts/cellActionView';
import { CellContextKeyManager } from 'vs/workbench/contrib/notebook/browser/view/viewParts/cellContextKeys';
import { CellDragAndDropController, DRAGGING_CLASS } from 'vs/workbench/contrib/notebook/browser/view/viewParts/cellDnd';
import { CellEditorOptions } from 'vs/workbench/contrib/notebook/browser/view/viewParts/cellEditorOptions';
Expand Down Expand Up @@ -862,81 +861,3 @@ export function getCodeCellExecutionContextKeyService(contextKeyService: IContex

return executionContextKeyService;
}

export class ListTopCellToolbar extends Disposable {
private topCellToolbar: HTMLElement;
private menu: IMenu;
private toolbar: ToolBar;
private readonly _modelDisposables = this._register(new DisposableStore());
constructor(
protected readonly notebookEditor: INotebookEditorDelegate,

contextKeyService: IContextKeyService,
insertionIndicatorContainer: HTMLElement,
@IInstantiationService protected readonly instantiationService: IInstantiationService,
@IContextMenuService protected readonly contextMenuService: IContextMenuService,
@IMenuService protected readonly menuService: IMenuService
) {
super();

this.topCellToolbar = DOM.append(insertionIndicatorContainer, $('.cell-list-top-cell-toolbar-container'));

this.toolbar = this._register(new ToolBar(this.topCellToolbar, this.contextMenuService, {
actionViewItemProvider: action => {
if (action instanceof MenuItemAction) {
const item = this.instantiationService.createInstance(CodiconActionViewItem, action);
return item;
}

return undefined;
}
}));
this.toolbar.context = <INotebookActionContext>{
notebookEditor
};

this.menu = this._register(this.menuService.createMenu(this.notebookEditor.creationOptions.menuIds.cellTopInsertToolbar, contextKeyService));
this._register(this.menu.onDidChange(() => {
this.updateActions();
}));
this.updateActions();

// update toolbar container css based on cell list length
this._register(this.notebookEditor.onDidChangeModel(() => {
this._modelDisposables.clear();

if (this.notebookEditor.hasModel()) {
this._modelDisposables.add(this.notebookEditor.onDidChangeViewCells(() => {
this.updateClass();
}));

this.updateClass();
}
}));

this.updateClass();
}

private updateActions() {
const actions = this.getCellToolbarActions(this.menu, false);
this.toolbar.setActions(actions.primary, actions.secondary);
}

private updateClass() {
if (this.notebookEditor.hasModel() && this.notebookEditor.getLength() === 0) {
this.topCellToolbar.classList.add('emptyNotebook');
} else {
this.topCellToolbar.classList.remove('emptyNotebook');
}
}

private getCellToolbarActions(menu: IMenu, alwaysFillSecondaryActions: boolean): { primary: IAction[], secondary: IAction[]; } {
const primary: IAction[] = [];
const secondary: IAction[] = [];
const result = { primary, secondary };

createAndFillInActionBarActions(menu, { shouldForwardArgs: true }, result, g => /^inline/.test(g));

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import * as DOM from 'vs/base/browser/dom';
import { ToolBar } from 'vs/base/browser/ui/toolbar/toolbar';
import { IAction } from 'vs/base/common/actions';
import { Disposable, DisposableStore } from 'vs/base/common/lifecycle';
import { createAndFillInActionBarActions } from 'vs/platform/actions/browser/menuEntryActionViewItem';
import { IMenu, IMenuService, MenuItemAction } from 'vs/platform/actions/common/actions';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { INotebookActionContext } from 'vs/workbench/contrib/notebook/browser/controller/coreActions';
import { INotebookEditorDelegate } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
import { CodiconActionViewItem } from 'vs/workbench/contrib/notebook/browser/view/viewParts/cellActionView';

export class ListTopCellToolbar extends Disposable {
private topCellToolbar: HTMLElement;
private menu: IMenu;
private toolbar: ToolBar;
private readonly _modelDisposables = this._register(new DisposableStore());
constructor(
protected readonly notebookEditor: INotebookEditorDelegate,

contextKeyService: IContextKeyService,
insertionIndicatorContainer: HTMLElement,
@IInstantiationService protected readonly instantiationService: IInstantiationService,
@IContextMenuService protected readonly contextMenuService: IContextMenuService,
@IMenuService protected readonly menuService: IMenuService
) {
super();

this.topCellToolbar = DOM.append(insertionIndicatorContainer, DOM.$('.cell-list-top-cell-toolbar-container'));

this.toolbar = this._register(new ToolBar(this.topCellToolbar, this.contextMenuService, {
actionViewItemProvider: action => {
if (action instanceof MenuItemAction) {
const item = this.instantiationService.createInstance(CodiconActionViewItem, action);
return item;
}

return undefined;
}
}));
this.toolbar.context = <INotebookActionContext>{
notebookEditor
};

this.menu = this._register(this.menuService.createMenu(this.notebookEditor.creationOptions.menuIds.cellTopInsertToolbar, contextKeyService));
this._register(this.menu.onDidChange(() => {
this.updateActions();
}));
this.updateActions();

// update toolbar container css based on cell list length
this._register(this.notebookEditor.onDidChangeModel(() => {
this._modelDisposables.clear();

if (this.notebookEditor.hasModel()) {
this._modelDisposables.add(this.notebookEditor.onDidChangeViewCells(() => {
this.updateClass();
}));

this.updateClass();
}
}));

this.updateClass();
}

private updateActions() {
const actions = this.getCellToolbarActions(this.menu, false);
this.toolbar.setActions(actions.primary, actions.secondary);
}

private updateClass() {
if (this.notebookEditor.hasModel() && this.notebookEditor.getLength() === 0) {
this.topCellToolbar.classList.add('emptyNotebook');
} else {
this.topCellToolbar.classList.remove('emptyNotebook');
}
}

private getCellToolbarActions(menu: IMenu, alwaysFillSecondaryActions: boolean): { primary: IAction[], secondary: IAction[]; } {
type NewType = IAction;

const primary: NewType[] = [];
const secondary: IAction[] = [];
const result = { primary, secondary };

createAndFillInActionBarActions(menu, { shouldForwardArgs: true }, result, g => /^inline/.test(g));

return result;
}
}

0 comments on commit 57f6be8

Please sign in to comment.