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
Show file tree
Hide file tree
Changes from 5 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
73 changes: 70 additions & 3 deletions src/vs/workbench/browser/parts/titlebar/titlebarPart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { isMacintosh, isWindows, isLinux, isWeb, isNative, platformLocale } from
import { Color } from 'vs/base/common/color';
import { EventType, EventHelper, Dimension, append, $, addDisposableListener, prepend, reset, getWindow, getWindowId, isAncestor, getActiveDocument, isHTMLElement } from 'vs/base/browser/dom';
import { CustomMenubarControl } from 'vs/workbench/browser/parts/titlebar/menubarControl';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { Emitter, Event } from 'vs/base/common/event';
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
import { Parts, IWorkbenchLayoutService, ActivityBarPosition, LayoutSettings, EditorActionsLocation, EditorTabsMode } from 'vs/workbench/services/layout/browser/layoutService';
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 @@ -83,6 +84,11 @@ export interface ITitlebarPart extends IDisposable {
* Adds variables to be supported in the window title.
*/
registerVariables(variables: ITitleVariable[]): void;

/**
* Unregisters variables from being supported in the window title.
*/
unregisterVariables(variableNames: string[]): void;
}

export class BrowserTitleService extends MultiWindowParts<BrowserTitlebarPart> implements ITitleService {
Expand All @@ -101,6 +107,7 @@ export class BrowserTitleService extends MultiWindowParts<BrowserTitlebarPart> i
this._register(this.registerPart(this.mainPart));

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

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

private registerCommands(): void {
this._register(CommandsRegistry.registerCommand({
id: 'registerWindowTitleVariable',
handler: (accessor: ServicesAccessor, name: string, contextKey: string) => {
if (accessor.get(IContextKeyService).getContextKeyValue(contextKey) === undefined) {
console.error(`Unable to register window title variable: Context key '${contextKey}' is not defined.`);
return;
}
this.registerVariables([{ name, contextKey }]);
},
metadata: {
description: 'Registers a new title variable',
args: [
{ name: 'name', schema: { type: 'string' }, description: 'The name of the variable to register' },
{ name: 'contextKey', schema: { type: 'string' }, description: 'The context key to use for the value of the variable' }
]
}
}));

this._register(CommandsRegistry.registerCommand({
id: 'unregisterWindowTitleVariable',
handler: (_, name: string) => {
this.unregisterVariables([name]);
},
metadata: {
description: 'Unregister a title variable',
args: [
{ name: 'name', schema: { type: 'string' }, description: 'The name of the variable to unregister' },
]
}
}));
benibenj marked this conversation as resolved.
Show resolved Hide resolved
}

//#region Auxiliary Titlebar Parts

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

registerVariables(variables: ITitleVariable[]): void {
this.variables.push(...variables);
const unregisteredVariables: ITitleVariable[] = [];

// Filter out any variables that are already registered
const existingVariableNames = new Set(this.variables.map(v => v.name));
const existingVariableContexKeys = new Set(this.variables.map(v => v.contextKey));
for (const v of variables) {
benibenj marked this conversation as resolved.
Show resolved Hide resolved
if (existingVariableNames.has(v.name)) {
console.error(`A title variable with the name '${v.name}' is already registered.`);
} else if (existingVariableContexKeys.has(v.contextKey)) {
console.error(`A title variable with the context key '${v.contextKey}' is already registered.`);
} else {
unregisteredVariables.push(v);
}
}

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

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

unregisterVariables(variableNames: string[]): void {
this.variables = this.variables.filter(v => !variableNames.includes(v.name));
benibenj marked this conversation as resolved.
Show resolved Hide resolved

for (const part of this.parts) {
part.unregisterVariables(variableNames);
}
}

Expand Down Expand Up @@ -400,6 +463,10 @@ export class BrowserTitlebarPart extends Part implements ITitlebarPart {
this.windowTitle.registerVariables(variables);
}

unregisterVariables(variableNames: string[]): void {
this.windowTitle.unregisterVariables(variableNames);
}

protected override createContentArea(parent: HTMLElement): HTMLElement {
this.element = parent;
this.rootContainer = append(parent, $('.titlebar-container'));
Expand Down
16 changes: 16 additions & 0 deletions src/vs/workbench/browser/parts/titlebar/windowTitle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,22 @@ export class WindowTitle extends Disposable {
}
}

unregisterVariables(variableNames: string[]): void {
let changed = false;

for (const [contextKey, name] of this.variables) {
if (variableNames.includes(name)) {
this.variables.delete(contextKey);

changed = true;
}
}

if (changed) {
this.titleUpdater.schedule();
}
}

/**
* Possible template values:
*
Expand Down
8 changes: 5 additions & 3 deletions src/vs/workbench/contrib/scm/browser/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { localize } from 'vs/nls';
import { basename } from 'vs/base/common/resources';
import { Disposable, DisposableStore } from 'vs/base/common/lifecycle';
import { Disposable, DisposableStore, toDisposable } from 'vs/base/common/lifecycle';
import { Emitter, Event } from 'vs/base/common/event';
import { VIEW_PANE_ID, ISCMService, ISCMRepository, ISCMViewService } from 'vs/workbench/contrib/scm/common/scm';
import { IActivityService, NumberBadge } from 'vs/workbench/services/activity/common/activity';
Expand Down Expand Up @@ -116,10 +116,12 @@ export class SCMActiveRepositoryController extends Disposable implements IWorkbe
this._activeRepositoryNameContextKey = ActiveRepositoryContextKeys.ActiveRepositoryName.bindTo(this.contextKeyService);
this._activeRepositoryBranchNameContextKey = ActiveRepositoryContextKeys.ActiveRepositoryBranchName.bindTo(this.contextKeyService);

this.titleService.registerVariables([
const titleVariables = [
{ name: 'activeRepositoryName', contextKey: ActiveRepositoryContextKeys.ActiveRepositoryName.key },
{ name: 'activeRepositoryBranchName', contextKey: ActiveRepositoryContextKeys.ActiveRepositoryBranchName.key, }
]);
];
this.titleService.registerVariables(titleVariables);
benibenj marked this conversation as resolved.
Show resolved Hide resolved
this._register(toDisposable(() => this.titleService.unregisterVariables(titleVariables.map(v => v.name))));

this._register(autorunWithStore((reader, store) => {
this._updateActivityCountBadge(this._countBadge.read(reader), store);
Expand Down
Loading