Skip to content

Commit

Permalink
Make CellContextKeys a CellPart #131808
Browse files Browse the repository at this point in the history
  • Loading branch information
roblourens committed Mar 17, 2022
1 parent 6723778 commit 450c0fe
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@ import { MarkupCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewM
import { NotebookCellExecutionState } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { INotebookExecutionStateService } from 'vs/workbench/contrib/notebook/common/notebookExecutionStateService';
import { CellViewModelStateChangeEvent } from 'vs/workbench/contrib/notebook/browser/notebookViewEvents';
import { CellPart } from 'vs/workbench/contrib/notebook/browser/view/cellParts/cellPart';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';

export class CellContextKeyPart extends CellPart {
private cellContextKeyManager: CellContextKeyManager;

constructor(
notebookEditor: INotebookEditorDelegate,
@IInstantiationService private readonly instantiationService: IInstantiationService,
) {
super();

this.cellContextKeyManager = this._register(this.instantiationService.createInstance(CellContextKeyManager, notebookEditor, undefined));
}

protected override didRenderCell(element: ICellViewModel): void {
this.cellContextKeyManager.updateForElement(element);
}
}

export class CellContextKeyManager extends Disposable {

Expand All @@ -32,7 +51,7 @@ export class CellContextKeyManager extends Disposable {

constructor(
private readonly notebookEditor: INotebookEditorDelegate,
private element: ICellViewModel,
private element: ICellViewModel | undefined,
@IContextKeyService private readonly _contextKeyService: IContextKeyService,
@INotebookExecutionStateService private readonly _notebookExecutionStateService: INotebookExecutionStateService
) {
Expand All @@ -51,18 +70,26 @@ export class CellContextKeyManager extends Disposable {
this.cellOutputCollapsed = NOTEBOOK_CELL_OUTPUT_COLLAPSED.bindTo(this._contextKeyService);
this.cellLineNumbers = NOTEBOOK_CELL_LINE_NUMBERS.bindTo(this._contextKeyService);

this.updateForElement(element);
if (element) {
this.updateForElement(element);
}
});

this._register(this._notebookExecutionStateService.onDidChangeCellExecution(e => {
if (e.affectsCell(this.element.uri)) {
if (this.element && e.affectsCell(this.element.uri)) {
this.updateForExecutionState();
}
}));
}

public updateForElement(element: ICellViewModel) {
public updateForElement(element: ICellViewModel | undefined) {
this.elementDisposables.clear();
this.element = element;

if (!element) {
return;
}

this.elementDisposables.add(element.onDidChangeState(e => this.onDidChangeState(e)));

if (element instanceof CodeCellViewModel) {
Expand All @@ -71,7 +98,6 @@ export class CellContextKeyManager extends Disposable {

this.elementDisposables.add(this.notebookEditor.onDidChangeActiveCell(() => this.updateForFocusState()));

this.element = element;
if (this.element instanceof MarkupCellViewModel) {
this.cellType.set('markup');
} else if (this.element instanceof CodeCellViewModel) {
Expand All @@ -85,7 +111,7 @@ export class CellContextKeyManager extends Disposable {
this.updateForCollapseState();
this.updateForOutputs();

this.cellLineNumbers.set(this.element.lineNumbers);
this.cellLineNumbers.set(this.element!.lineNumbers);
});
}

Expand All @@ -104,7 +130,7 @@ export class CellContextKeyManager extends Disposable {
}

if (e.cellLineNumberChanged) {
this.cellLineNumbers.set(this.element.lineNumbers);
this.cellLineNumbers.set(this.element!.lineNumbers);
}

if (e.inputCollapsedChanged || e.outputCollapsedChanged) {
Expand All @@ -114,6 +140,10 @@ export class CellContextKeyManager extends Disposable {
}

private updateForFocusState() {
if (!this.element) {
return;
}

const activeCell = this.notebookEditor.getActiveCell();
this.cellFocused.set(this.notebookEditor.getActiveCell() === this.element);

Expand All @@ -126,6 +156,10 @@ export class CellContextKeyManager extends Disposable {
}

private updateForExecutionState() {
if (!this.element) {
return;
}

const internalMetadata = this.element.internalMetadata;
this.cellEditable.set(!this.notebookEditor.isReadOnly);

Expand All @@ -152,6 +186,10 @@ export class CellContextKeyManager extends Disposable {
}

private updateForEditState() {
if (!this.element) {
return;
}

if (this.element instanceof MarkupCellViewModel) {
this.markdownEditMode.set(this.element.getEditState() === CellEditState.Editing);
} else {
Expand All @@ -160,6 +198,10 @@ export class CellContextKeyManager extends Disposable {
}

private updateForCollapseState() {
if (!this.element) {
return;
}

this.cellContentCollapsed.set(!!this.element.isInputCollapsed);
this.cellOutputCollapsed.set(!!this.element.isOutputCollapsed);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { ServiceCollection } from 'vs/platform/instantiation/common/serviceColle
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { INotificationService } from 'vs/platform/notification/common/notification';
import { ICellViewModel, INotebookEditorDelegate } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
import { CellContextKeyManager } from 'vs/workbench/contrib/notebook/browser/view/cellParts/cellContextKeys';
import { CellContextKeyPart } from 'vs/workbench/contrib/notebook/browser/view/cellParts/cellContextKeys';
import { CellDecorations } from 'vs/workbench/contrib/notebook/browser/view/cellParts/cellDecorations';
import { CellDragAndDropController, CellDragAndDropPart } from 'vs/workbench/contrib/notebook/browser/view/cellParts/cellDnd';
import { CodeCellDragImageRenderer } from 'vs/workbench/contrib/notebook/browser/view/cellParts/cellDragRenderer';
Expand Down Expand Up @@ -107,7 +107,6 @@ abstract class AbstractCellRenderer {

protected commonRenderElement(element: ICellViewModel, templateData: BaseCellRenderTemplate): void {
templateData.elementDisposables.add(new CellDecorations(templateData.rootContainer, templateData.decorationContainer, element));
templateData.elementDisposables.add(templateData.instantiationService.createInstance(CellContextKeyManager, this.notebookEditor, element));
}
}

Expand Down Expand Up @@ -179,7 +178,8 @@ export class MarkupCellRenderer extends AbstractCellRenderer implements IListRen
foldedCellHint,
templateDisposables.add(new CollapsedCellInput(this.notebookEditor, cellInputCollapsedContainer)),
templateDisposables.add(new CellFocusPart(container, undefined, this.notebookEditor)),
templateDisposables.add(new CellDragAndDropPart()),
templateDisposables.add(new CellDragAndDropPart(container)),
templateDisposables.add(this.instantiationService.createInstance(CellContextKeyPart, this.notebookEditor)),
];

const templateData: MarkdownCellRenderTemplate = {
Expand Down Expand Up @@ -330,7 +330,8 @@ export class CodeCellRenderer extends AbstractCellRenderer implements IListRende
templateDisposables.add(this.instantiationService.createInstance(CollapsedCellOutput, this.notebookEditor, cellOutputCollapsedContainer)),
templateDisposables.add(new CollapsedCellInput(this.notebookEditor, cellInputCollapsedContainer)),
templateDisposables.add(new CellFocusPart(container, focusSinkElement, this.notebookEditor)),
templateDisposables.add(new CellDragAndDropPart()),
templateDisposables.add(new CellDragAndDropPart(container)),
templateDisposables.add(this.instantiationService.createInstance(CellContextKeyPart, this.notebookEditor)),
];

const templateData: CodeCellRenderTemplate = {
Expand Down

0 comments on commit 450c0fe

Please sign in to comment.