Add registerWindowTitleVariable command

This commit is contained in:
BABA
2024-08-12 22:30:49 +08:00
parent 48bf32d320
commit bcb4fd87ec
4 changed files with 65 additions and 2 deletions
@@ -182,10 +182,12 @@ export class BrowserTitleService extends MultiWindowParts<BrowserTitlebarPart> 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);
}
}
@@ -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);
@@ -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<IWindowTitleVariableService>('windowTitleVariableService');
export class WindowTitleVariableService extends Disposable implements IWindowTitleVariableService {
declare readonly _serviceBrand: undefined;
private readonly _contextKeyMap = new Map<string, IContextKey<string>>();
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);
}
@@ -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