Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get rid of cell focus, refactor context keys, fix #99390
- Loading branch information
1 parent
315d372
commit 4615200
Showing
6 changed files
with
126 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
src/vs/workbench/contrib/notebook/browser/view/renderers/cellContextKeys.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey'; | ||
import { INotebookTextModel, NotebookCellRunState } from 'vs/workbench/contrib/notebook/common/notebookCommon'; | ||
import { BaseCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/baseCellViewModel'; | ||
import { NOTEBOOK_CELL_TYPE, NOTEBOOK_VIEW_TYPE, NOTEBOOK_CELL_EDITABLE, NOTEBOOK_CELL_RUNNABLE, NOTEBOOK_CELL_MARKDOWN_EDIT_MODE, NOTEBOOK_CELL_RUN_STATE, NOTEBOOK_CELL_HAS_OUTPUTS, CellViewModelStateChangeEvent, CellEditState } from 'vs/workbench/contrib/notebook/browser/notebookBrowser'; | ||
import { CodeCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/codeCellViewModel'; | ||
import { MarkdownCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/markdownCellViewModel'; | ||
import { Disposable } from 'vs/base/common/lifecycle'; | ||
|
||
export class CellContextKeyManager extends Disposable { | ||
|
||
private cellType: IContextKey<string>; | ||
private viewType: IContextKey<string>; | ||
private cellEditable: IContextKey<boolean>; | ||
private cellRunnable: IContextKey<boolean>; | ||
private cellRunState: IContextKey<string>; | ||
private cellHasOutputs: IContextKey<boolean>; | ||
|
||
private markdownEditMode: IContextKey<boolean>; | ||
|
||
constructor( | ||
private readonly contextKeyService: IContextKeyService, | ||
private readonly notebookTextModel: INotebookTextModel, | ||
private readonly element: BaseCellViewModel | ||
) { | ||
super(); | ||
|
||
this.cellType = NOTEBOOK_CELL_TYPE.bindTo(this.contextKeyService); | ||
this.viewType = NOTEBOOK_VIEW_TYPE.bindTo(this.contextKeyService); | ||
this.cellEditable = NOTEBOOK_CELL_EDITABLE.bindTo(this.contextKeyService); | ||
this.cellRunnable = NOTEBOOK_CELL_RUNNABLE.bindTo(this.contextKeyService); | ||
this.markdownEditMode = NOTEBOOK_CELL_MARKDOWN_EDIT_MODE.bindTo(this.contextKeyService); | ||
this.cellRunState = NOTEBOOK_CELL_RUN_STATE.bindTo(this.contextKeyService); | ||
this.cellHasOutputs = NOTEBOOK_CELL_HAS_OUTPUTS.bindTo(this.contextKeyService); | ||
|
||
this._register(element.onDidChangeState(e => this.onDidChangeState(e))); | ||
|
||
if (element instanceof CodeCellViewModel) { | ||
this._register(element.onDidChangeOutputs(() => this.updateForOutputs())); | ||
} | ||
|
||
this.initialize(); | ||
} | ||
|
||
private initialize() { | ||
if (this.element instanceof MarkdownCellViewModel) { | ||
this.cellType.set('markdown'); | ||
} else if (this.element instanceof CodeCellViewModel) { | ||
this.cellType.set('code'); | ||
} | ||
|
||
this.updateForMetadata(); | ||
this.updateForEditState(); | ||
this.updateForOutputs(); | ||
|
||
this.viewType.set(this.element.viewType); | ||
} | ||
|
||
private onDidChangeState(e: CellViewModelStateChangeEvent) { | ||
if (e.metadataChanged) { | ||
this.updateForMetadata(); | ||
} | ||
|
||
if (e.editStateChanged) { | ||
this.updateForEditState(); | ||
} | ||
} | ||
|
||
private updateForMetadata() { | ||
const metadata = this.element.getEvaluatedMetadata(this.notebookTextModel.metadata); | ||
this.cellEditable.set(!!metadata.editable); | ||
this.cellRunnable.set(!!metadata.runnable); | ||
|
||
const runState = metadata.runState ?? NotebookCellRunState.Idle; | ||
this.cellRunState.set(NotebookCellRunState[runState]); | ||
} | ||
|
||
private updateForEditState() { | ||
if (this.element instanceof MarkdownCellViewModel) { | ||
this.markdownEditMode.set(this.element.editState === CellEditState.Editing); | ||
} else { | ||
this.markdownEditMode.set(false); | ||
} | ||
} | ||
|
||
private updateForOutputs() { | ||
if (this.element instanceof CodeCellViewModel) { | ||
this.cellHasOutputs.set(this.element.outputs.length > 0); | ||
} else { | ||
this.cellHasOutputs.set(false); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters