Skip to content

Commit

Permalink
Merge pull request #157516 from microsoft/benibenj/disabledTreeItem
Browse files Browse the repository at this point in the history
Show TreeItem as disabled when command enablement is false
  • Loading branch information
benibenj authored and joyceerhl committed Aug 10, 2022
2 parents 9e26868 + c230b46 commit 2bf89d5
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 15 deletions.
17 changes: 12 additions & 5 deletions src/vs/base/browser/ui/iconLabel/iconLabel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface IIconLabelValueOptions {
matches?: IMatch[];
labelEscapeNewLines?: boolean;
descriptionMatches?: IMatch[];
disabledCommand?: boolean;
readonly separator?: string;
readonly domId?: string;
}
Expand Down Expand Up @@ -124,22 +125,28 @@ export class IconLabel extends Disposable {
}

setLabel(label: string | string[], description?: string, options?: IIconLabelValueOptions): void {
const classes = ['monaco-icon-label'];
const labelClasses = ['monaco-icon-label'];
const containerClasses = ['monaco-icon-label-container'];
if (options) {
if (options.extraClasses) {
classes.push(...options.extraClasses);
labelClasses.push(...options.extraClasses);
}

if (options.italic) {
classes.push('italic');
labelClasses.push('italic');
}

if (options.strikethrough) {
classes.push('strikethrough');
labelClasses.push('strikethrough');
}

if (options.disabledCommand) {
containerClasses.push('disabled');
}
}

this.domNode.className = classes.join(' ');
this.domNode.className = labelClasses.join(' ');
this.labelContainer.className = containerClasses.join(' ');
this.setupHover(options?.descriptionTitle ? this.labelContainer : this.element, options?.title);

this.nameNode.setLabel(label, options);
Expand Down
3 changes: 3 additions & 0 deletions src/vs/base/browser/ui/iconLabel/iconlabel.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
flex-shrink: 0; /* fix for https://github.com/microsoft/vscode/issues/13787 */
}

.monaco-icon-label-container.disabled {
color: var(--vscode-disabledForeground);
}
.monaco-icon-label > .monaco-icon-label-container {
min-width: 0;
overflow: hidden;
Expand Down
3 changes: 3 additions & 0 deletions src/vs/base/browser/ui/tree/media/tree.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
position: relative;
}

.monaco-tl-row.disabled {
cursor: default;
}
.monaco-tl-indent {
height: 100%;
position: absolute;
Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/api/common/extHostTreeViews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -696,8 +696,8 @@ class ExtHostTreeView<T> extends Disposable {
return tooltip;
}

private getCommand(disposable: DisposableStore, command?: vscode.Command): Command | undefined {
return command ? this.commands.toInternal(command, disposable) : undefined;
private getCommand(disposable: DisposableStore, command?: vscode.Command): Command & { originalId: string } | undefined {
return command ? { ...this.commands.toInternal(command, disposable), originalId: command.command } : undefined;
}

private validateTreeItem(extensionTreeItem: vscode.TreeItem) {
Expand Down
3 changes: 2 additions & 1 deletion src/vs/workbench/browser/labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,8 @@ class ResourceLabelWidget extends IconLabel {
descriptionMatches: this.options?.descriptionMatches,
extraClasses: [],
separator: this.options?.separator,
domId: this.options?.domId
domId: this.options?.domId,
disabledCommand: this.options?.disabledCommand,
};

const resource = toResource(this.label);
Expand Down
3 changes: 3 additions & 0 deletions src/vs/workbench/browser/parts/views/media/views.css
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@
-moz-osx-font-smoothing: grayscale;
}

.customview-tree .monaco-list .monaco-list-row .custom-view-tree-node-item>.custom-view-tree-node-item-icon.disabled {
opacity: 60%;
}
/* makes spinning icons square */
.customview-tree .monaco-list .monaco-list-row .custom-view-tree-node-item > .custom-view-tree-node-item-icon.codicon.codicon-modifier-spin {
padding-left: 6px;
Expand Down
32 changes: 27 additions & 5 deletions src/vs/workbench/browser/parts/views/treeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import { VSDataTransfer } from 'vs/base/common/dataTransfer';
import { Command } from 'vs/editor/common/languages';
import { localize } from 'vs/nls';
import { createActionViewItem, createAndFillInContextMenuActions } from 'vs/platform/actions/browser/menuEntryActionViewItem';
import { Action2, IMenu, IMenuService, MenuId, registerAction2 } from 'vs/platform/actions/common/actions';
import { ICommandService } from 'vs/platform/commands/common/commands';
import { Action2, IMenu, IMenuService, MenuId, MenuRegistry, registerAction2 } from 'vs/platform/actions/common/actions';
import { CommandsRegistry, ICommandService } from 'vs/platform/commands/common/commands';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { ContextKeyExpr, IContextKey, IContextKeyService, RawContextKey } from 'vs/platform/contextkey/common/contextkey';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
Expand Down Expand Up @@ -941,7 +941,8 @@ class TreeRenderer extends Disposable implements ITreeRenderer<ITreeItem, FuzzyS
@IConfigurationService private readonly configurationService: IConfigurationService,
@ILabelService private readonly labelService: ILabelService,
@IHoverService private readonly hoverService: IHoverService,
@ITreeViewsService private readonly treeViewsService: ITreeViewsService
@ITreeViewsService private readonly treeViewsService: ITreeViewsService,
@IContextKeyService private readonly contextKeyService: IContextKeyService,
) {
super();
this._hoverDelegate = {
Expand Down Expand Up @@ -1029,6 +1030,18 @@ class TreeRenderer extends Disposable implements ITreeRenderer<ITreeItem, FuzzyS
templateData.actionBar.clear();
templateData.icon.style.color = '';

let commandEnabled = true;
if (node.command) {
const command = CommandsRegistry.getCommand(node.command.originalId ? node.command.originalId : node.command.id);
if (command) {
const commandAction = MenuRegistry.getCommand(command.id);
const precondition = commandAction && commandAction.precondition;
if (precondition) {
commandEnabled = this.contextKeyService.contextMatchesRules(precondition);
}
}
}

if (resource) {
const fileDecorations = this.configurationService.getValue<{ colors: boolean; badges: boolean }>('explorer.decorations');
const labelResource = resource ? resource : URI.parse('missing:_icon_resource');
Expand All @@ -1039,15 +1052,17 @@ class TreeRenderer extends Disposable implements ITreeRenderer<ITreeItem, FuzzyS
fileDecorations,
extraClasses: ['custom-view-tree-node-item-resourceLabel'],
matches: matches ? matches : createMatches(element.filterData),
strikethrough: treeItemLabel?.strikethrough
strikethrough: treeItemLabel?.strikethrough,
disabledCommand: !commandEnabled
});
} else {
templateData.resourceLabel.setResource({ name: label, description }, {
title,
hideIcon: true,
extraClasses: ['custom-view-tree-node-item-resourceLabel'],
matches: matches ? matches : createMatches(element.filterData),
strikethrough: treeItemLabel?.strikethrough
strikethrough: treeItemLabel?.strikethrough,
disabledCommand: !commandEnabled
});
}

Expand All @@ -1066,6 +1081,13 @@ class TreeRenderer extends Disposable implements ITreeRenderer<ITreeItem, FuzzyS
templateData.icon.style.backgroundImage = '';
}

if (!commandEnabled) {
templateData.icon.className = templateData.icon.className + ' disabled';
if (templateData.container.parentElement) {
templateData.container.parentElement.className = templateData.container.parentElement.className + ' disabled';
}
}

templateData.actionBar.context = <TreeViewItemHandleArg>{ $treeViewId: this.treeViewId, $treeItemHandle: node.handle };

const disposableStore = new DisposableStore();
Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/common/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ export interface ITreeItem {

contextValue?: string;

command?: Command;
command?: Command & { originalId?: string };

children?: ITreeItem[];

Expand All @@ -774,7 +774,7 @@ export class ResolvableTreeItem implements ITreeItem {
resourceUri?: UriComponents;
tooltip?: string | IMarkdownString;
contextValue?: string;
command?: Command;
command?: Command & { originalId?: string };
children?: ITreeItem[];
accessibilityInformation?: IAccessibilityInformation;
resolve: (token: CancellationToken) => Promise<void>;
Expand Down

0 comments on commit 2bf89d5

Please sign in to comment.