|
4 | 4 | *--------------------------------------------------------------------------------------------*/
|
5 | 5 |
|
6 | 6 | import * as DOM from 'vs/base/browser/dom';
|
7 |
| -import { Disposable, DisposableStore } from 'vs/base/common/lifecycle'; |
| 7 | +import { Disposable, DisposableStore, MutableDisposable } from 'vs/base/common/lifecycle'; |
8 | 8 | import { HiddenItemStrategy, MenuWorkbenchToolBar } from 'vs/platform/actions/browser/toolbar';
|
9 | 9 | import { IMenuService, MenuItemAction } from 'vs/platform/actions/common/actions';
|
10 |
| -import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey'; |
11 | 10 | import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
|
12 | 11 | import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
|
13 | 12 | import { INotebookActionContext } from 'vs/workbench/contrib/notebook/browser/controller/coreActions';
|
14 | 13 | import { INotebookEditorDelegate } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
|
| 14 | +import { NotebookOptions } from 'vs/workbench/contrib/notebook/browser/notebookOptions'; |
15 | 15 | import { CodiconActionViewItem } from 'vs/workbench/contrib/notebook/browser/view/cellParts/cellActionView';
|
16 | 16 |
|
17 | 17 | export class ListTopCellToolbar extends Disposable {
|
| 18 | + private readonly topCellToolbarContainer: HTMLElement; |
18 | 19 | private topCellToolbar: HTMLElement;
|
19 |
| - private toolbar: MenuWorkbenchToolBar; |
| 20 | + private viewZone: MutableDisposable<DisposableStore> = this._register(new MutableDisposable()); |
20 | 21 | private readonly _modelDisposables = this._register(new DisposableStore());
|
21 | 22 | constructor(
|
22 | 23 | protected readonly notebookEditor: INotebookEditorDelegate,
|
23 |
| - |
24 |
| - contextKeyService: IContextKeyService, |
25 |
| - insertionIndicatorContainer: HTMLElement, |
| 24 | + private readonly notebookOptions: NotebookOptions, |
26 | 25 | @IInstantiationService protected readonly instantiationService: IInstantiationService,
|
27 | 26 | @IContextMenuService protected readonly contextMenuService: IContextMenuService,
|
28 | 27 | @IMenuService protected readonly menuService: IMenuService
|
29 | 28 | ) {
|
30 | 29 | super();
|
31 | 30 |
|
32 |
| - this.topCellToolbar = DOM.append(insertionIndicatorContainer, DOM.$('.cell-list-top-cell-toolbar-container')); |
33 |
| - |
34 |
| - this.toolbar = this._register(instantiationService.createInstance(MenuWorkbenchToolBar, this.topCellToolbar, this.notebookEditor.creationOptions.menuIds.cellTopInsertToolbar, { |
35 |
| - actionViewItemProvider: action => { |
36 |
| - if (action instanceof MenuItemAction) { |
37 |
| - const item = this.instantiationService.createInstance(CodiconActionViewItem, action, undefined); |
38 |
| - return item; |
39 |
| - } |
| 31 | + this.topCellToolbarContainer = DOM.$('div'); |
| 32 | + this.topCellToolbar = DOM.$('.cell-list-top-cell-toolbar-container'); |
| 33 | + this.topCellToolbarContainer.appendChild(this.topCellToolbar); |
40 | 34 |
|
41 |
| - return undefined; |
42 |
| - }, |
43 |
| - menuOptions: { |
44 |
| - shouldForwardArgs: true |
45 |
| - }, |
46 |
| - toolbarOptions: { |
47 |
| - primaryGroup: (g: string) => /^inline/.test(g), |
48 |
| - }, |
49 |
| - hiddenItemStrategy: HiddenItemStrategy.Ignore, |
| 35 | + this._register(this.notebookEditor.onDidAttachViewModel(() => { |
| 36 | + this.updateTopToolbar(); |
50 | 37 | }));
|
51 | 38 |
|
52 |
| - this.toolbar.context = <INotebookActionContext>{ |
53 |
| - notebookEditor |
54 |
| - }; |
| 39 | + this._register(this.notebookOptions.onDidChangeOptions(e => { |
| 40 | + if (e.insertToolbarAlignment || e.insertToolbarPosition || e.cellToolbarLocation) { |
| 41 | + this.updateTopToolbar(); |
| 42 | + } |
| 43 | + })); |
| 44 | + } |
55 | 45 |
|
56 |
| - // update toolbar container css based on cell list length |
57 |
| - this._register(this.notebookEditor.onDidChangeModel(() => { |
58 |
| - this._modelDisposables.clear(); |
| 46 | + private updateTopToolbar() { |
| 47 | + const layoutInfo = this.notebookOptions.getLayoutConfiguration(); |
| 48 | + this.viewZone.value = new DisposableStore(); |
59 | 49 |
|
60 |
| - if (this.notebookEditor.hasModel()) { |
61 |
| - this._modelDisposables.add(this.notebookEditor.onDidChangeViewCells(() => { |
62 |
| - this.updateClass(); |
63 |
| - })); |
| 50 | + if (layoutInfo.insertToolbarPosition === 'hidden' || layoutInfo.insertToolbarPosition === 'notebookToolbar') { |
| 51 | + const height = this.notebookOptions.computeTopInsertToolbarHeight(this.notebookEditor.textModel?.viewType); |
64 | 52 |
|
65 |
| - this.updateClass(); |
| 53 | + if (height !== 0) { |
| 54 | + // reserve whitespace to avoid overlap with cell toolbar |
| 55 | + this.notebookEditor.changeViewZones(accessor => { |
| 56 | + const id = accessor.addZone({ |
| 57 | + afterModelPosition: 0, |
| 58 | + heightInPx: height, |
| 59 | + domNode: DOM.$('div') |
| 60 | + }); |
| 61 | + accessor.layoutZone(id); |
| 62 | + this.viewZone.value?.add({ |
| 63 | + dispose: () => { |
| 64 | + if (!this.notebookEditor.isDisposed) { |
| 65 | + this.notebookEditor.changeViewZones(accessor => { |
| 66 | + accessor.removeZone(id); |
| 67 | + }); |
| 68 | + } |
| 69 | + } |
| 70 | + }); |
| 71 | + }); |
66 | 72 | }
|
67 |
| - })); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + this.notebookEditor.changeViewZones(accessor => { |
| 78 | + const height = this.notebookOptions.computeTopInsertToolbarHeight(this.notebookEditor.textModel?.viewType); |
| 79 | + const id = accessor.addZone({ |
| 80 | + afterModelPosition: 0, |
| 81 | + heightInPx: height, |
| 82 | + domNode: this.topCellToolbarContainer |
| 83 | + }); |
| 84 | + accessor.layoutZone(id); |
| 85 | + |
| 86 | + this.viewZone.value?.add({ |
| 87 | + dispose: () => { |
| 88 | + if (!this.notebookEditor.isDisposed) { |
| 89 | + this.notebookEditor.changeViewZones(accessor => { |
| 90 | + accessor.removeZone(id); |
| 91 | + }); |
| 92 | + } |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + DOM.clearNode(this.topCellToolbar); |
| 97 | + |
| 98 | + const toolbar = this.instantiationService.createInstance(MenuWorkbenchToolBar, this.topCellToolbar, this.notebookEditor.creationOptions.menuIds.cellTopInsertToolbar, { |
| 99 | + actionViewItemProvider: action => { |
| 100 | + if (action instanceof MenuItemAction) { |
| 101 | + const item = this.instantiationService.createInstance(CodiconActionViewItem, action, undefined); |
| 102 | + return item; |
| 103 | + } |
| 104 | + |
| 105 | + return undefined; |
| 106 | + }, |
| 107 | + menuOptions: { |
| 108 | + shouldForwardArgs: true |
| 109 | + }, |
| 110 | + toolbarOptions: { |
| 111 | + primaryGroup: (g: string) => /^inline/.test(g), |
| 112 | + }, |
| 113 | + hiddenItemStrategy: HiddenItemStrategy.Ignore, |
| 114 | + }); |
| 115 | + |
| 116 | + toolbar.context = <INotebookActionContext>{ |
| 117 | + notebookEditor: this.notebookEditor |
| 118 | + }; |
| 119 | + |
| 120 | + this.viewZone.value?.add(toolbar); |
| 121 | + |
| 122 | + // update toolbar container css based on cell list length |
| 123 | + this.viewZone.value?.add(this.notebookEditor.onDidChangeModel(() => { |
| 124 | + this._modelDisposables.clear(); |
| 125 | + |
| 126 | + if (this.notebookEditor.hasModel()) { |
| 127 | + this._modelDisposables.add(this.notebookEditor.onDidChangeViewCells(() => { |
| 128 | + this.updateClass(); |
| 129 | + })); |
| 130 | + |
| 131 | + this.updateClass(); |
| 132 | + } |
| 133 | + })); |
68 | 134 |
|
69 |
| - this.updateClass(); |
| 135 | + this.updateClass(); |
| 136 | + }); |
70 | 137 | }
|
71 | 138 |
|
72 | 139 | private updateClass() {
|
|
0 commit comments