From 537b1f0b9e9a04ffe694e6c56bdffcb232efcd57 Mon Sep 17 00:00:00 2001 From: meganrogge Date: Tue, 15 Jun 2021 07:00:12 -0700 Subject: [PATCH] add icon manager --- .../contrib/terminal/browser/terminal.ts | 2 + .../terminal/browser/terminalEditor.ts | 5 -- .../browser/terminalEditorIconManager.ts | 85 +++++++++++++++++++ .../terminal/browser/terminalService.ts | 10 +-- 4 files changed, 92 insertions(+), 10 deletions(-) create mode 100644 src/vs/workbench/contrib/terminal/browser/terminalEditorIconManager.ts diff --git a/src/vs/workbench/contrib/terminal/browser/terminal.ts b/src/vs/workbench/contrib/terminal/browser/terminal.ts index 1aaecfa534d03..4500ed1677071 100644 --- a/src/vs/workbench/contrib/terminal/browser/terminal.ts +++ b/src/vs/workbench/contrib/terminal/browser/terminal.ts @@ -234,6 +234,8 @@ export interface ITerminalService { setEditable(instance: ITerminalInstance, data: IEditableData | null): Promise; instanceIsSplit(instance: ITerminalInstance): boolean; safeDisposeTerminal(instance: ITerminalInstance): Promise; + + getPlatformKey(): Promise; } /** diff --git a/src/vs/workbench/contrib/terminal/browser/terminalEditor.ts b/src/vs/workbench/contrib/terminal/browser/terminalEditor.ts index 4d56e93594f7f..c821b43e4c0ed 100644 --- a/src/vs/workbench/contrib/terminal/browser/terminalEditor.ts +++ b/src/vs/workbench/contrib/terminal/browser/terminalEditor.ts @@ -47,11 +47,6 @@ export class TerminalEditor extends EditorPane { if (!this._editorInput?.terminalInstance) { return; } - - if (!this._isAttached) { - this._editorInput.terminalInstance.attachToElement(this._parentElement!); - this._isAttached = true; - } this._editorInput.terminalInstance.setVisible(visible); } diff --git a/src/vs/workbench/contrib/terminal/browser/terminalEditorIconManager.ts b/src/vs/workbench/contrib/terminal/browser/terminalEditorIconManager.ts new file mode 100644 index 0000000000000..00995774ff9a4 --- /dev/null +++ b/src/vs/workbench/contrib/terminal/browser/terminalEditorIconManager.ts @@ -0,0 +1,85 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import * as dom from 'vs/base/browser/dom'; +import { IDisposable } from 'vs/base/common/lifecycle'; +import { URI } from 'vs/base/common/uri'; +import { IConfigurationService } from 'vs/platform/configuration/common/configuration'; +import { TerminalIcon, TerminalSettingPrefix } from 'vs/platform/terminal/common/terminal'; +import { ITerminalService } from 'vs/workbench/contrib/terminal/browser/terminal'; +import { ILifecycleService, LifecyclePhase } from 'vs/workbench/services/lifecycle/common/lifecycle'; + +export class TerminalEditorIconManager implements IDisposable { + + private readonly _icons = new Map(); + + private _styleElement: HTMLStyleElement | undefined; + + constructor( + @ILifecycleService private readonly _lifecycleService: ILifecycleService, + @IConfigurationService private readonly _configurationService: IConfigurationService, + @ITerminalService terminalService: ITerminalService + ) { + _configurationService.onDidChangeConfiguration(async e => { + if (e.affectsConfiguration(TerminalSettingPrefix.DefaultProfile + terminalService.getPlatformKey()) || + e.affectsConfiguration(TerminalSettingPrefix.Profiles + terminalService.getPlatformKey())) { + this._updateStyleSheet(); + } + }); + } + + dispose() { + this._styleElement?.remove(); + this._styleElement = undefined; + } + + // eslint-disable-next-line @typescript-eslint/naming-convention + private get styleElement(): HTMLStyleElement { + if (!this._styleElement) { + this._styleElement = dom.createStyleSheet(); + this._styleElement.className = 'terminal-icons'; + } + return this._styleElement; + } + + public setIcons( + webviewId: string, + iconPath?: TerminalIcon + ) { + if (iconPath) { + this._icons.set(webviewId, iconPath); + } else { + this._icons.delete(webviewId); + } + + this._updateStyleSheet(); + } + + private async _updateStyleSheet() { + await this._lifecycleService.when(LifecyclePhase.Starting); + + const cssRules: string[] = []; + if (this._configurationService.getValue('workbench.iconTheme') !== null) { + for (const [key, value] of this._icons) { + const webviewSelector = `.show-file-icons .terminal-${key}-name-file-icon::before`; + try { + if (typeof value === 'object' && 'light' in value && 'dark' in value) { + cssRules.push( + `.monaco-workbench.vs ${webviewSelector} { content: ""; background-image: ${dom.asCSSUrl(value.light)}; }`, + `.monaco-workbench.vs-dark ${webviewSelector}, .monaco-workbench.hc-black ${webviewSelector} { content: ""; background-image: ${dom.asCSSUrl(value.dark)}; }` + ); + } else if (URI.isUri(value)) { + + } else { + + } + } catch { + // noop + } + } + } + this.styleElement.textContent = cssRules.join('\n'); + } +} diff --git a/src/vs/workbench/contrib/terminal/browser/terminalService.ts b/src/vs/workbench/contrib/terminal/browser/terminalService.ts index 296084fcd9be9..e97ad1d893eaf 100644 --- a/src/vs/workbench/contrib/terminal/browser/terminalService.ts +++ b/src/vs/workbench/contrib/terminal/browser/terminalService.ts @@ -181,8 +181,8 @@ export class TerminalService implements ITerminalService { lifecycleService.onWillShutdown(e => this._onWillShutdown(e)); this._configurationService.onDidChangeConfiguration(async e => { - if (e.affectsConfiguration(TerminalSettingPrefix.DefaultProfile + this._getPlatformKey()) || - e.affectsConfiguration(TerminalSettingPrefix.Profiles + this._getPlatformKey()) || + if (e.affectsConfiguration(TerminalSettingPrefix.DefaultProfile + this.getPlatformKey()) || + e.affectsConfiguration(TerminalSettingPrefix.Profiles + this.getPlatformKey()) || e.affectsConfiguration(TerminalSettingId.UseWslProfiles)) { this._refreshAvailableProfiles(); } @@ -380,7 +380,7 @@ export class TerminalService implements ITerminalService { if (!this._primaryOffProcessTerminalService) { return this._availableProfiles || []; } - const platform = await this._getPlatformKey(); + const platform = await this.getPlatformKey(); return this._primaryOffProcessTerminalService?.getProfiles(this._configurationService.getValue(`${TerminalSettingPrefix.Profiles}${platform}`), this._configurationService.getValue(`${TerminalSettingPrefix.DefaultProfile}${platform}`), includeDetectedProfiles); } @@ -911,7 +911,7 @@ export class TerminalService implements ITerminalService { - private async _getPlatformKey(): Promise { + async getPlatformKey(): Promise { const env = await this._remoteAgentService.getEnvironment(); if (env) { return env.os === OperatingSystem.Windows ? 'windows' : (env.os === OperatingSystem.Macintosh ? 'osx' : 'linux'); @@ -922,7 +922,7 @@ export class TerminalService implements ITerminalService { async showProfileQuickPick(type: 'setDefault' | 'createInstance', cwd?: string | URI): Promise { let keyMods: IKeyMods | undefined; const profiles = await this._detectProfiles(true); - const platformKey = await this._getPlatformKey(); + const platformKey = await this.getPlatformKey(); const options: IPickOptions = { placeHolder: type === 'createInstance' ? nls.localize('terminal.integrated.selectProfileToCreate', "Select the terminal profile to create") : nls.localize('terminal.integrated.chooseDefaultProfile', "Select your default terminal profile"),