Skip to content

Commit

Permalink
Use IconSelectBox to change terminal icon, remove dupes
Browse files Browse the repository at this point in the history
Fixes #199964
  • Loading branch information
Tyriar committed Mar 29, 2024
1 parent cea6ec5 commit f7fb22d
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 16 deletions.
77 changes: 77 additions & 0 deletions src/vs/workbench/contrib/terminal/browser/terminalIconPicker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { Dimension, getActiveDocument } from 'vs/base/browser/dom';
import { HoverPosition } from 'vs/base/browser/ui/hover/hoverWidget';
import { Lazy } from 'vs/base/common/lazy';
import { Disposable } from 'vs/base/common/lifecycle';
import type { ThemeIcon } from 'vs/base/common/themables';
import { IHoverService } from 'vs/platform/hover/browser/hover';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { defaultInputBoxStyles } from 'vs/platform/theme/browser/defaultStyles';
import { getIconRegistry, IconContribution } from 'vs/platform/theme/common/iconRegistry';
import { WorkbenchIconSelectBox } from 'vs/workbench/services/userDataProfile/browser/iconSelectBox';

const icons = new Lazy<IconContribution[]>(() => {
const iconDefinitions = getIconRegistry().getIcons();
const includedChars = new Set<string>();
const dedupedIcons = iconDefinitions.filter(e => {
if (!('fontCharacter' in e.defaults)) {
return false;
}
if (includedChars.has(e.defaults.fontCharacter)) {
return false;
}
includedChars.add(e.defaults.fontCharacter);
return true;
});
return dedupedIcons;
});

export class TerminalIconPicker extends Disposable {
private readonly _iconSelectBox: WorkbenchIconSelectBox;

constructor(
@IInstantiationService instantiationService: IInstantiationService,
@IHoverService private readonly _hoverService: IHoverService
) {
super();

this._iconSelectBox = instantiationService.createInstance(WorkbenchIconSelectBox, {
icons: icons.value,
inputBoxStyles: defaultInputBoxStyles,
showIconInfo: true
});
}

async pickIcons(): Promise<ThemeIcon | undefined> {
const dimension = new Dimension(486, 260);
return new Promise<ThemeIcon | undefined>(resolve => {
this._register(this._iconSelectBox.onDidSelect(e => {
resolve(e);
this._iconSelectBox.dispose();
}));
this._iconSelectBox.clearInput();
const hoverWidget = this._hoverService.showHover({
content: this._iconSelectBox.domNode,
target: getActiveDocument().body,
position: {
hoverPosition: HoverPosition.BELOW,
},
persistence: {
sticky: true,
},
appearance: {
showPointer: true
}
}, true);
if (hoverWidget) {
this._register(hoverWidget);
}
this._iconSelectBox.layout(dimension);
this._iconSelectBox.focus();
});
}
}
27 changes: 11 additions & 16 deletions src/vs/workbench/contrib/terminal/browser/terminalInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { Orientation } from 'vs/base/browser/ui/sash/sash';
import { DomScrollableElement } from 'vs/base/browser/ui/scrollbar/scrollableElement';
import { AutoOpenBarrier, Promises, disposableTimeout, timeout } from 'vs/base/common/async';
import { Codicon, getAllCodicons } from 'vs/base/common/codicons';
import { Codicon } from 'vs/base/common/codicons';
import { debounce } from 'vs/base/common/decorators';
import { ErrorNoTelemetry, onUnexpectedError } from 'vs/base/common/errors';
import { Emitter, Event } from 'vs/base/common/event';
Expand Down Expand Up @@ -88,6 +88,7 @@ import type { IMarker, Terminal as XTermTerminal } from '@xterm/xterm';
import { AccessibilityCommandId } from 'vs/workbench/contrib/accessibility/common/accessibilityCommands';
import { terminalStrings } from 'vs/workbench/contrib/terminal/common/terminalStrings';
import { shouldPasteTerminalText } from 'vs/workbench/contrib/terminal/common/terminalClipboard';
import { TerminalIconPicker } from 'vs/workbench/contrib/terminal/browser/terminalIconPicker';

const enum Constants {
/**
Expand Down Expand Up @@ -365,7 +366,7 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
@IOpenerService private readonly _openerService: IOpenerService,
@ICommandService private readonly _commandService: ICommandService,
@IAccessibilitySignalService private readonly _accessibilitySignalService: IAccessibilitySignalService,
@IViewDescriptorService private readonly _viewDescriptorService: IViewDescriptorService
@IViewDescriptorService private readonly _viewDescriptorService: IViewDescriptorService,
) {
super();

Expand Down Expand Up @@ -2171,21 +2172,15 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
this._onIconChanged.fire({ instance: this, userInitiated: true });
return icon;
}
type Item = IQuickPickItem & { icon: TerminalIcon };
const items: Item[] = [];
for (const icon of getAllCodicons()) {
items.push({ label: `$(${icon.id})`, description: `${icon.id}`, icon });
}
const result = await this._quickInputService.pick(items, {
matchOnDescription: true,
placeHolder: nls.localize('changeIcon', 'Select an icon for the terminal')
});
if (result) {
this._icon = result.icon;
this._onIconChanged.fire({ instance: this, userInitiated: true });
return this._icon;
const iconPicker = this._scopedInstantiationService.createInstance(TerminalIconPicker);
const pickedIcon = await iconPicker.pickIcons();
iconPicker.dispose();
if (!pickedIcon) {
return undefined;
}
return;
this._icon = pickedIcon;
this._onIconChanged.fire({ instance: this, userInitiated: true });
return pickedIcon;
}

async changeColor(color?: string, skipQuickPick?: boolean): Promise<string | undefined> {
Expand Down

0 comments on commit f7fb22d

Please sign in to comment.