Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add registerWindowTitleVariable command #225408

Merged
merged 6 commits into from
Aug 19, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions src/vs/workbench/browser/parts/titlebar/titlebarPart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { IStorageService, StorageScope } from 'vs/platform/storage/common/storag
import { Parts, IWorkbenchLayoutService, ActivityBarPosition, LayoutSettings, EditorActionsLocation, EditorTabsMode } from 'vs/workbench/services/layout/browser/layoutService';
import { createActionViewItem, createAndFillInActionBarActions } from 'vs/platform/actions/browser/menuEntryActionViewItem';
import { Action2, IMenu, IMenuService, MenuId, registerAction2 } from 'vs/platform/actions/common/actions';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IHostService } from 'vs/workbench/services/host/browser/host';
import { Codicon } from 'vs/base/common/codicons';
import { getIconRegistry } from 'vs/platform/theme/common/iconRegistry';
Expand Down Expand Up @@ -55,6 +55,7 @@ import { IView } from 'vs/base/browser/ui/grid/grid';
import { createInstantHoverDelegate } from 'vs/base/browser/ui/hover/hoverDelegateFactory';
import { IBaseActionViewItemOptions } from 'vs/base/browser/ui/actionbar/actionViewItems';
import { IHoverDelegate } from 'vs/base/browser/ui/hover/hoverDelegate';
import { CommandsRegistry } from 'vs/platform/commands/common/commands';

export interface ITitleVariable {
readonly name: string;
Expand Down Expand Up @@ -94,13 +95,15 @@ export class BrowserTitleService extends MultiWindowParts<BrowserTitlebarPart> i
constructor(
@IInstantiationService protected readonly instantiationService: IInstantiationService,
@IStorageService storageService: IStorageService,
@IThemeService themeService: IThemeService
@IThemeService themeService: IThemeService,
@IContextKeyService private readonly contextKeyService: IContextKeyService
) {
super('workbench.titleService', themeService, storageService);

this._register(this.registerPart(this.mainPart));

this.registerActions();
this.registerCommands();
}

protected createMainTitlebarPart(): BrowserTitlebarPart {
Expand Down Expand Up @@ -128,6 +131,29 @@ export class BrowserTitleService extends MultiWindowParts<BrowserTitlebarPart> i
}));
}

private registerCommands(): void {
this._register(CommandsRegistry.registerCommand('setWindowTitleVariable', (_, key: string, value: string) => {
benibenj marked this conversation as resolved.
Show resolved Hide resolved
this.setWindowTitleVariable(key, value);
}));
}

private readonly windowTitleVariableContextKeyMap = new Map<string, IContextKey<string>>();

private setWindowTitleVariable(key: string, value: string): void {
const prefixedKey = `windowTitleVariable.${key}`;

let contextKey = this.windowTitleVariableContextKeyMap.get(prefixedKey);
if (!contextKey) {
contextKey = this.contextKeyService.createKey(prefixedKey, value);
benibenj marked this conversation as resolved.
Show resolved Hide resolved
this.windowTitleVariableContextKeyMap.set(prefixedKey, contextKey);
this.registerVariables([
{ name: prefixedKey, contextKey: prefixedKey }
]);
} else {
contextKey.set(value);
}
}

//#region Auxiliary Titlebar Parts

createAuxiliaryTitlebarPart(container: HTMLElement, editorGroupsContainer: IEditorGroupsContainer): IAuxiliaryTitlebarPart {
Expand Down Expand Up @@ -182,10 +208,26 @@ export class BrowserTitleService extends MultiWindowParts<BrowserTitlebarPart> i
private variables: ITitleVariable[] = [];

registerVariables(variables: ITitleVariable[]): void {
this.variables.push(...variables);
const existingVariables = new Set(this.variables.map(v => v.contextKey));
const unregisteredVariables: ITitleVariable[] = [];
const duplicateVariables: ITitleVariable[] = [];
for (const v of variables) {
benibenj marked this conversation as resolved.
Show resolved Hide resolved
if (existingVariables.has(v.contextKey)) {
duplicateVariables.push(v);
} else {
unregisteredVariables.push(v);
}
}

if (duplicateVariables.length > 0) {
const duplicateKeys = duplicateVariables.map(v => v.contextKey).join(', ');
throw new Error(`Window title variables already registered: ${duplicateKeys}`);
}
benibenj marked this conversation as resolved.
Show resolved Hide resolved

this.variables.push(...unregisteredVariables);

for (const part of this.parts) {
part.registerVariables(variables);
part.registerVariables(unregisteredVariables);
}
}

Expand Down
Loading