From eb10cea1014e5d4b581ac369e46a640dd292edbb Mon Sep 17 00:00:00 2001 From: Daniel Imms <2193314+Tyriar@users.noreply.github.com> Date: Mon, 1 Apr 2024 05:37:04 -0700 Subject: [PATCH] Introduce ITerminalConfigurationService Part of #181900 --- .../contrib/terminal/browser/terminal.ts | 13 ++++ .../browser/terminalConfigurationService.ts | 60 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 src/vs/workbench/contrib/terminal/browser/terminalConfigurationService.ts diff --git a/src/vs/workbench/contrib/terminal/browser/terminal.ts b/src/vs/workbench/contrib/terminal/browser/terminal.ts index 2e32099c497..1b1d10745b7 100644 --- a/src/vs/workbench/contrib/terminal/browser/terminal.ts +++ b/src/vs/workbench/contrib/terminal/browser/terminal.ts @@ -31,6 +31,7 @@ import { ACTIVE_GROUP_TYPE, AUX_WINDOW_GROUP_TYPE, SIDE_GROUP_TYPE } from 'vs/wo import type { ICurrentPartialCommand } from 'vs/platform/terminal/common/capabilities/commandDetection/terminalCommand'; export const ITerminalService = createDecorator('terminalService'); +export const ITerminalConfigurationService = createDecorator('terminalConfigurationService'); export const ITerminalEditorService = createDecorator('terminalEditorService'); export const ITerminalGroupService = createDecorator('terminalGroupService'); export const ITerminalInstanceService = createDecorator('terminalInstanceService'); @@ -87,6 +88,9 @@ export interface ITerminalInstanceService { } export interface ITerminalConfigHelper { + /** + * @deprecated Use `ITerminalConfigurationService.config` instead. + */ config: ITerminalConfiguration; panelContainer: HTMLElement | undefined; @@ -352,6 +356,15 @@ export interface ITerminalService extends ITerminalInstanceHost { */ createOnInstanceCapabilityEvent(capabilityId: T, getEvent: (capability: ITerminalCapabilityImplMap[T]) => Event): IDynamicListEventMultiplexer<{ instance: ITerminalInstance; data: K }>; } + +export interface ITerminalConfigurationService { + readonly _serviceBrand: undefined; + + readonly config: ITerminalConfiguration; + + readonly onConfigChanged: Event; +} + export class TerminalLinkQuickPickEvent extends MouseEvent { } diff --git a/src/vs/workbench/contrib/terminal/browser/terminalConfigurationService.ts b/src/vs/workbench/contrib/terminal/browser/terminalConfigurationService.ts new file mode 100644 index 00000000000..602376b8d2f --- /dev/null +++ b/src/vs/workbench/contrib/terminal/browser/terminalConfigurationService.ts @@ -0,0 +1,60 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import { Emitter, Event } from 'vs/base/common/event'; +import { Disposable } from 'vs/base/common/lifecycle'; +import { IConfigurationService } from 'vs/platform/configuration/common/configuration'; +import { ITerminalConfigurationService } from 'vs/workbench/contrib/terminal/browser/terminal'; +import { DEFAULT_BOLD_FONT_WEIGHT, DEFAULT_FONT_WEIGHT, FontWeight, ITerminalConfiguration, MAXIMUM_FONT_WEIGHT, MINIMUM_FONT_WEIGHT, TERMINAL_CONFIG_SECTION } from 'vs/workbench/contrib/terminal/common/terminal'; + +export class TerminalConfigurationService extends Disposable implements ITerminalConfigurationService { + declare _serviceBrand: undefined; + + config!: ITerminalConfiguration; + + private readonly _onConfigChanged = new Emitter(); + get onConfigChanged(): Event { return this._onConfigChanged.event; } + + constructor( + @IConfigurationService private readonly _configurationService: IConfigurationService, + ) { + super(); + this._register(Event.runAndSubscribe(this._configurationService.onDidChangeConfiguration, e => { + if (!e || e.affectsConfiguration(TERMINAL_CONFIG_SECTION)) { + this._updateConfig(); + } + })); + } + + private _updateConfig(): void { + const configValues = { ...this._configurationService.getValue(TERMINAL_CONFIG_SECTION) }; + configValues.fontWeight = this._normalizeFontWeight(configValues.fontWeight, DEFAULT_FONT_WEIGHT); + configValues.fontWeightBold = this._normalizeFontWeight(configValues.fontWeightBold, DEFAULT_BOLD_FONT_WEIGHT); + this.config = configValues; + this._onConfigChanged.fire(); + } + + private _normalizeFontWeight(input: any, defaultWeight: FontWeight): FontWeight { + if (input === 'normal' || input === 'bold') { + return input; + } + return clampInt(input, MINIMUM_FONT_WEIGHT, MAXIMUM_FONT_WEIGHT, defaultWeight); + } +} + + +function clampInt(source: any, minimum: number, maximum: number, fallback: T): number | T { + let r = parseInt(source, 10); + if (isNaN(r)) { + return fallback; + } + if (typeof minimum === 'number') { + r = Math.max(minimum, r); + } + if (typeof maximum === 'number') { + r = Math.min(maximum, r); + } + return r; +}