Skip to content

Commit

Permalink
context key - a bit of bufferChangeEvents when changing many context …
Browse files Browse the repository at this point in the history
…keys at once
  • Loading branch information
bpasero committed Jul 6, 2020
1 parent 9e17e6d commit db472f9
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 36 deletions.
1 change: 0 additions & 1 deletion src/vs/platform/contextkey/common/contextkey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,6 @@ export interface IContextKeyService {
onDidChangeContext: Event<IContextKeyChangeEvent>;
bufferChangeEvents(callback: Function): void;


createKey<T>(key: string, defaultValue: T | undefined): IContextKey<T>;
contextMatchesRules(rules: ContextKeyExpression | undefined): boolean;
getContextKeyValue<T>(key: string): T | undefined;
Expand Down
16 changes: 10 additions & 6 deletions src/vs/platform/list/browser/listService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,11 @@ export class WorkbenchList<T> extends List<T> {
const selection = this.getSelection();
const focus = this.getFocus();

this.listHasSelectionOrFocus.set(selection.length > 0 || focus.length > 0);
this.listMultiSelection.set(selection.length > 1);
this.listDoubleSelection.set(selection.length === 2);
this.contextKeyService.bufferChangeEvents(() => {
this.listHasSelectionOrFocus.set(selection.length > 0 || focus.length > 0);
this.listMultiSelection.set(selection.length > 1);
this.listDoubleSelection.set(selection.length === 2);
});
}));
this.disposables.add(this.onDidChangeFocus(() => {
const selection = this.getSelection();
Expand Down Expand Up @@ -906,9 +908,11 @@ class WorkbenchTreeInternals<TInput, T, TFilterData> {
const selection = tree.getSelection();
const focus = tree.getFocus();

this.hasSelectionOrFocus.set(selection.length > 0 || focus.length > 0);
this.hasMultiSelection.set(selection.length > 1);
this.hasDoubleSelection.set(selection.length === 2);
this.contextKeyService.bufferChangeEvents(() => {
this.hasSelectionOrFocus.set(selection.length > 0 || focus.length > 0);
this.hasMultiSelection.set(selection.length > 1);
this.hasDoubleSelection.set(selection.length === 2);
});
}),
tree.onDidChangeFocus(() => {
const selection = tree.getSelection();
Expand Down
8 changes: 5 additions & 3 deletions src/vs/workbench/browser/parts/editor/titleControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,11 @@ export abstract class TitleControl extends Themable {
this.editorToolBarMenuDisposables.clear();

// Update contexts
this.resourceContext.set(this.group.activeEditor ? withUndefinedAsNull(toResource(this.group.activeEditor, { supportSideBySide: SideBySideEditor.PRIMARY })) : null);
this.editorPinnedContext.set(this.group.activeEditor ? this.group.isPinned(this.group.activeEditor) : false);
this.editorStickyContext.set(this.group.activeEditor ? this.group.isSticky(this.group.activeEditor) : false);
this.contextKeyService.bufferChangeEvents(() => {
this.resourceContext.set(this.group.activeEditor ? withUndefinedAsNull(toResource(this.group.activeEditor, { supportSideBySide: SideBySideEditor.PRIMARY })) : null);
this.editorPinnedContext.set(this.group.activeEditor ? this.group.isPinned(this.group.activeEditor) : false);
this.editorStickyContext.set(this.group.activeEditor ? this.group.isSticky(this.group.activeEditor) : false);
});

// Editor actions require the editor control to be there, so we retrieve it via service
const activeEditorPane = this.group.activeEditorPane;
Expand Down
48 changes: 26 additions & 22 deletions src/vs/workbench/common/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ export class ResourceContextKey extends Disposable implements IContextKey<URI> {
private readonly _isFileSystemResource: IContextKey<boolean>;

constructor(
@IContextKeyService contextKeyService: IContextKeyService,
@IContextKeyService private readonly _contextKeyService: IContextKeyService,
@IFileService private readonly _fileService: IFileService,
@IModeService private readonly _modeService: IModeService
) {
super();

this._schemeKey = ResourceContextKey.Scheme.bindTo(contextKeyService);
this._filenameKey = ResourceContextKey.Filename.bindTo(contextKeyService);
this._langIdKey = ResourceContextKey.LangId.bindTo(contextKeyService);
this._resourceKey = ResourceContextKey.Resource.bindTo(contextKeyService);
this._extensionKey = ResourceContextKey.Extension.bindTo(contextKeyService);
this._hasResource = ResourceContextKey.HasResource.bindTo(contextKeyService);
this._isFileSystemResource = ResourceContextKey.IsFileSystemResource.bindTo(contextKeyService);
this._schemeKey = ResourceContextKey.Scheme.bindTo(this._contextKeyService);
this._filenameKey = ResourceContextKey.Filename.bindTo(this._contextKeyService);
this._langIdKey = ResourceContextKey.LangId.bindTo(this._contextKeyService);
this._resourceKey = ResourceContextKey.Resource.bindTo(this._contextKeyService);
this._extensionKey = ResourceContextKey.Extension.bindTo(this._contextKeyService);
this._hasResource = ResourceContextKey.HasResource.bindTo(this._contextKeyService);
this._isFileSystemResource = ResourceContextKey.IsFileSystemResource.bindTo(this._contextKeyService);

this._register(_fileService.onDidChangeFileSystemProviderRegistrations(() => {
const resource = this._resourceKey.get();
Expand All @@ -66,24 +66,28 @@ export class ResourceContextKey extends Disposable implements IContextKey<URI> {

set(value: URI | null) {
if (!ResourceContextKey._uriEquals(this._resourceKey.get(), value)) {
this._resourceKey.set(value);
this._schemeKey.set(value ? value.scheme : null);
this._filenameKey.set(value ? basename(value) : null);
this._langIdKey.set(value ? this._modeService.getModeIdByFilepathOrFirstLine(value) : null);
this._extensionKey.set(value ? extname(value) : null);
this._hasResource.set(!!value);
this._isFileSystemResource.set(value ? this._fileService.canHandleResource(value) : false);
this._contextKeyService.bufferChangeEvents(() => {
this._resourceKey.set(value);
this._schemeKey.set(value ? value.scheme : null);
this._filenameKey.set(value ? basename(value) : null);
this._langIdKey.set(value ? this._modeService.getModeIdByFilepathOrFirstLine(value) : null);
this._extensionKey.set(value ? extname(value) : null);
this._hasResource.set(!!value);
this._isFileSystemResource.set(value ? this._fileService.canHandleResource(value) : false);
});
}
}

reset(): void {
this._schemeKey.reset();
this._langIdKey.reset();
this._resourceKey.reset();
this._langIdKey.reset();
this._extensionKey.reset();
this._hasResource.reset();
this._isFileSystemResource.reset();
this._contextKeyService.bufferChangeEvents(() => {
this._schemeKey.reset();
this._langIdKey.reset();
this._resourceKey.reset();
this._langIdKey.reset();
this._extensionKey.reset();
this._hasResource.reset();
this._isFileSystemResource.reset();
});
}

get(): URI | undefined {
Expand Down
10 changes: 6 additions & 4 deletions src/vs/workbench/services/history/browser/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -755,10 +755,12 @@ export class HistoryService extends Disposable implements IHistoryService {
private readonly canReopenClosedEditorContextKey = (new RawContextKey<boolean>('canReopenClosedEditor', false)).bindTo(this.contextKeyService);

private updateContextKeys(): void {
this.canNavigateBackContextKey.set(this.navigationStack.length > 0 && this.navigationStackIndex > 0);
this.canNavigateForwardContextKey.set(this.navigationStack.length > 0 && this.navigationStackIndex < this.navigationStack.length - 1);
this.canNavigateToLastEditLocationContextKey.set(!!this.lastEditLocation);
this.canReopenClosedEditorContextKey.set(this.recentlyClosedEditors.length > 0);
this.contextKeyService.bufferChangeEvents(() => {
this.canNavigateBackContextKey.set(this.navigationStack.length > 0 && this.navigationStackIndex > 0);
this.canNavigateForwardContextKey.set(this.navigationStack.length > 0 && this.navigationStackIndex < this.navigationStack.length - 1);
this.canNavigateToLastEditLocationContextKey.set(!!this.lastEditLocation);
this.canReopenClosedEditorContextKey.set(this.recentlyClosedEditors.length > 0);
});
}

//#endregion
Expand Down

0 comments on commit db472f9

Please sign in to comment.