From bcb4fd87ec9b3a8902aecf8e4bc5e4f93e76c98c Mon Sep 17 00:00:00 2001 From: BABA <38986298+BABA983@users.noreply.github.com> Date: Mon, 12 Aug 2024 22:30:49 +0800 Subject: [PATCH] Add registerWindowTitleVariable command --- .../browser/parts/titlebar/titlebarPart.ts | 6 ++- .../browser/windowTitle.contribution.ts | 11 +++++ .../browser/windowTitleVariableService.ts | 47 +++++++++++++++++++ src/vs/workbench/workbench.common.main.ts | 3 ++ 4 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 src/vs/workbench/contrib/windowTitle/browser/windowTitle.contribution.ts create mode 100644 src/vs/workbench/contrib/windowTitle/browser/windowTitleVariableService.ts diff --git a/src/vs/workbench/browser/parts/titlebar/titlebarPart.ts b/src/vs/workbench/browser/parts/titlebar/titlebarPart.ts index f610512e84f..9c3e679f7ee 100644 --- a/src/vs/workbench/browser/parts/titlebar/titlebarPart.ts +++ b/src/vs/workbench/browser/parts/titlebar/titlebarPart.ts @@ -182,10 +182,12 @@ export class BrowserTitleService extends MultiWindowParts i private variables: ITitleVariable[] = []; registerVariables(variables: ITitleVariable[]): void { - this.variables.push(...variables); + const unregisteredVariables = variables.filter(v => !this.variables.some(v2 => v2.contextKey === v.contextKey)); + + this.variables.push(...unregisteredVariables); for (const part of this.parts) { - part.registerVariables(variables); + part.registerVariables(unregisteredVariables); } } diff --git a/src/vs/workbench/contrib/windowTitle/browser/windowTitle.contribution.ts b/src/vs/workbench/contrib/windowTitle/browser/windowTitle.contribution.ts new file mode 100644 index 00000000000..9071c74897f --- /dev/null +++ b/src/vs/workbench/contrib/windowTitle/browser/windowTitle.contribution.ts @@ -0,0 +1,11 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ +import { CommandsRegistry } from 'vs/platform/commands/common/commands'; +import { InstantiationType, registerSingleton } from 'vs/platform/instantiation/common/extensions'; +import { IWindowTitleVariableService, registerWindowTitleVariable, WindowTitleVariableService } from 'vs/workbench/contrib/windowTitle/browser/windowTitleVariableService'; + +registerSingleton(IWindowTitleVariableService, WindowTitleVariableService, InstantiationType.Delayed); + +CommandsRegistry.registerCommand('registerWindowTitleVariable', registerWindowTitleVariable); diff --git a/src/vs/workbench/contrib/windowTitle/browser/windowTitleVariableService.ts b/src/vs/workbench/contrib/windowTitle/browser/windowTitleVariableService.ts new file mode 100644 index 00000000000..5481fc7fe81 --- /dev/null +++ b/src/vs/workbench/contrib/windowTitle/browser/windowTitleVariableService.ts @@ -0,0 +1,47 @@ +/*--------------------------------------------------------------------------------------------- + * 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 { createDecorator, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; +import { ITitleService } from 'vs/workbench/services/title/browser/titleService'; +import { Disposable } from 'vs/base/common/lifecycle'; + +export interface IWindowTitleVariableService { + readonly _serviceBrand: undefined; + setVariable(key: string, value: string): void; +} + +export const IWindowTitleVariableService = createDecorator('windowTitleVariableService'); + +export class WindowTitleVariableService extends Disposable implements IWindowTitleVariableService { + declare readonly _serviceBrand: undefined; + private readonly _contextKeyMap = new Map>(); + + constructor( + @IContextKeyService private readonly contextKeyService: IContextKeyService, + @ITitleService private readonly titleService: ITitleService + ) { + super(); + } + + setVariable(key: string, value: string): void { + const prefixedKey = `windowTitleVariable.${key}`; + + let contextKey = this._contextKeyMap.get(prefixedKey); + if (!contextKey) { + contextKey = this.contextKeyService.createKey(prefixedKey, value); + this._contextKeyMap.set(prefixedKey, contextKey); + this.titleService.registerVariables([ + { name: prefixedKey, contextKey: prefixedKey } + ]); + } else { + contextKey.set(value); + } + } +} + +export function registerWindowTitleVariable(accessor: ServicesAccessor, key: string, value: string) { + const windowTitleVariableService = accessor.get(IWindowTitleVariableService); + windowTitleVariableService.setVariable(key, value); +} diff --git a/src/vs/workbench/workbench.common.main.ts b/src/vs/workbench/workbench.common.main.ts index 9e2c97b4a68..b1d2a86101a 100644 --- a/src/vs/workbench/workbench.common.main.ts +++ b/src/vs/workbench/workbench.common.main.ts @@ -397,4 +397,7 @@ import 'vs/workbench/contrib/accountEntitlements/browser/accountsEntitlements.co // Synchronized Scrolling import 'vs/workbench/contrib/scrollLocking/browser/scrollLocking.contribution'; + +// Window Title +import 'vs/workbench/contrib/windowTitle/browser/windowTitle.contribution'; //#endregion