Skip to content

Commit

Permalink
Have ICommandAction use URIs for the icon location
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdima committed Jun 13, 2018
1 parent 5946a4f commit 8810c8e
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/vs/code/electron-main/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -966,8 +966,8 @@ export class CodeWindow implements ICodeWindow {
private createTouchBarGroupSegments(items: ICommandAction[] = []): ITouchBarSegment[] {
const segments: ITouchBarSegment[] = items.map(item => {
let icon: Electron.NativeImage;
if (item.iconPath) {
icon = nativeImage.createFromPath(item.iconPath.dark);
if (item.iconLocation && item.iconLocation.dark.scheme === 'file') {
icon = nativeImage.createFromPath(item.iconLocation.dark.fsPath);
if (icon.isEmpty()) {
icon = void 0;
}
Expand Down
17 changes: 9 additions & 8 deletions src/vs/platform/actions/browser/menuItemActionItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { Emitter } from 'vs/base/common/event';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { IdGenerator } from 'vs/base/common/idGenerator';
import { createCSSRule } from 'vs/base/browser/dom';
import URI from 'vs/base/common/uri';
import { INotificationService } from 'vs/platform/notification/common/notification';
import { isWindows, isLinux } from 'vs/base/common/platform';

Expand Down Expand Up @@ -161,7 +160,7 @@ export class MenuItemActionItem extends ActionItem {
@INotificationService protected _notificationService: INotificationService,
@IContextMenuService private readonly _contextMenuService: IContextMenuService
) {
super(undefined, _action, { icon: !!(_action.class || _action.item.iconPath), label: !_action.class && !_action.item.iconPath });
super(undefined, _action, { icon: !!(_action.class || _action.item.iconLocation), label: !_action.class && !_action.item.iconLocation });
}

protected get _commandAction(): IAction {
Expand Down Expand Up @@ -234,16 +233,18 @@ export class MenuItemActionItem extends ActionItem {
dispose(this._itemClassDispose);
this._itemClassDispose = undefined;

if (item.iconPath) {
if (item.iconLocation) {
let iconClass: string;

if (MenuItemActionItem.ICON_PATH_TO_CSS_RULES.has(item.iconPath.dark)) {
iconClass = MenuItemActionItem.ICON_PATH_TO_CSS_RULES.get(item.iconPath.dark);
const iconPathMapKey = item.iconLocation.dark.toString();

if (MenuItemActionItem.ICON_PATH_TO_CSS_RULES.has(iconPathMapKey)) {
iconClass = MenuItemActionItem.ICON_PATH_TO_CSS_RULES.get(iconPathMapKey);
} else {
iconClass = ids.nextId();
createCSSRule(`.icon.${iconClass}`, `background-image: url("${URI.file(item.iconPath.light || item.iconPath.dark).toString()}")`);
createCSSRule(`.vs-dark .icon.${iconClass}, .hc-black .icon.${iconClass}`, `background-image: url("${URI.file(item.iconPath.dark).toString()}")`);
MenuItemActionItem.ICON_PATH_TO_CSS_RULES.set(item.iconPath.dark, iconClass);
createCSSRule(`.icon.${iconClass}`, `background-image: url("${(item.iconLocation.light || item.iconLocation.dark).toString()}")`);
createCSSRule(`.vs-dark .icon.${iconClass}, .hc-black .icon.${iconClass}`, `background-image: url("${item.iconLocation.dark.toString()}")`);
MenuItemActionItem.ICON_PATH_TO_CSS_RULES.set(iconPathMapKey, iconClass);
}

this.$e.getHTMLElement().classList.add('icon', iconClass);
Expand Down
3 changes: 2 additions & 1 deletion src/vs/platform/actions/common/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ContextKeyExpr, IContextKeyService } from 'vs/platform/contextkey/commo
import { ICommandService } from 'vs/platform/commands/common/commands';
import { IDisposable } from 'vs/base/common/lifecycle';
import { Event } from 'vs/base/common/event';
import URI from 'vs/base/common/uri';

export interface ILocalizedString {
value: string;
Expand All @@ -23,7 +24,7 @@ export interface ICommandAction {
id: string;
title: string | ILocalizedString;
category?: string | ILocalizedString;
iconPath?: { dark: string; light?: string; };
iconLocation?: { dark: URI; light?: URI; };
precondition?: ContextKeyExpr;
}

Expand Down
16 changes: 8 additions & 8 deletions src/vs/workbench/browser/parts/editor/editor.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,12 +405,12 @@ editorCommands.setup();
// Touch Bar
if (isMacintosh) {
MenuRegistry.appendMenuItem(MenuId.TouchBarContext, {
command: { id: NavigateBackwardsAction.ID, title: NavigateBackwardsAction.LABEL, iconPath: { dark: URI.parse(require.toUrl('vs/workbench/browser/parts/editor/media/back-tb.png')).fsPath } },
command: { id: NavigateBackwardsAction.ID, title: NavigateBackwardsAction.LABEL, iconLocation: { dark: URI.parse(require.toUrl('vs/workbench/browser/parts/editor/media/back-tb.png')) } },
group: 'navigation'
});

MenuRegistry.appendMenuItem(MenuId.TouchBarContext, {
command: { id: NavigateForwardAction.ID, title: NavigateForwardAction.LABEL, iconPath: { dark: URI.parse(require.toUrl('vs/workbench/browser/parts/editor/media/forward-tb.png')).fsPath } },
command: { id: NavigateForwardAction.ID, title: NavigateForwardAction.LABEL, iconLocation: { dark: URI.parse(require.toUrl('vs/workbench/browser/parts/editor/media/forward-tb.png')) } },
group: 'navigation'
});
}
Expand Down Expand Up @@ -447,17 +447,17 @@ function appendEditorToolItem(primary: IEditorToolItem, alternative: IEditorTool
command: {
id: primary.id,
title: primary.title,
iconPath: {
dark: URI.parse(require.toUrl(`vs/workbench/browser/parts/editor/media/${primary.iconDark}`)).fsPath,
light: URI.parse(require.toUrl(`vs/workbench/browser/parts/editor/media/${primary.iconLight}`)).fsPath
iconLocation: {
dark: URI.parse(require.toUrl(`vs/workbench/browser/parts/editor/media/${primary.iconDark}`)),
light: URI.parse(require.toUrl(`vs/workbench/browser/parts/editor/media/${primary.iconLight}`))
}
},
alt: {
id: alternative.id,
title: alternative.title,
iconPath: {
dark: URI.parse(require.toUrl(`vs/workbench/browser/parts/editor/media/${alternative.iconDark}`)).fsPath,
light: URI.parse(require.toUrl(`vs/workbench/browser/parts/editor/media/${alternative.iconLight}`)).fsPath
iconLocation: {
dark: URI.parse(require.toUrl(`vs/workbench/browser/parts/editor/media/${alternative.iconDark}`)),
light: URI.parse(require.toUrl(`vs/workbench/browser/parts/editor/media/${alternative.iconLight}`))
}
},
group: 'navigation',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ MenuRegistry.appendMenuItem(MenuId.EditorTitle, {
command: {
id: 'editor.action.toggleWordWrap',
title: nls.localize('unwrapMinified', "Disable wrapping for this file"),
iconPath: { dark: URI.parse(require.toUrl('vs/workbench/parts/codeEditor/electron-browser/media/WordWrap_16x.svg')).fsPath }
iconLocation: { dark: URI.parse(require.toUrl('vs/workbench/parts/codeEditor/electron-browser/media/WordWrap_16x.svg')) }
},
group: 'navigation',
order: 1,
Expand All @@ -271,7 +271,7 @@ MenuRegistry.appendMenuItem(MenuId.EditorTitle, {
command: {
id: 'editor.action.toggleWordWrap',
title: nls.localize('wrapMinified', "Enable wrapping for this file"),
iconPath: { dark: URI.parse(require.toUrl('vs/workbench/parts/codeEditor/electron-browser/media/WordWrap_16x.svg')).fsPath }
iconLocation: { dark: URI.parse(require.toUrl('vs/workbench/parts/codeEditor/electron-browser/media/WordWrap_16x.svg')) }
},
group: 'navigation',
order: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ if (isMacintosh) {
const registerTouchBarEntry = (id: string, title: string, order, when: ContextKeyExpr, icon: string) => {
MenuRegistry.appendMenuItem(MenuId.TouchBarContext, {
command: {
id, title, iconPath: { dark: URI.parse(require.toUrl(`vs/workbench/parts/debug/electron-browser/media/${icon}`)).fsPath }
id, title, iconLocation: { dark: URI.parse(require.toUrl(`vs/workbench/parts/debug/electron-browser/media/${icon}`)) }
},
when,
group: '9_debug',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,22 @@ function appendEditorTitleContextMenuItem(id: string, title: string, when: Conte

// Editor Title Menu for Conflict Resolution
appendSaveConflictEditorTitleAction('workbench.files.action.acceptLocalChanges', nls.localize('acceptLocalChanges', "Use your changes and overwrite disk contents"), {
light: URI.parse(require.toUrl(`vs/workbench/parts/files/electron-browser/media/check.svg`)).fsPath,
dark: URI.parse(require.toUrl(`vs/workbench/parts/files/electron-browser/media/check-inverse.svg`)).fsPath
light: URI.parse(require.toUrl(`vs/workbench/parts/files/electron-browser/media/check.svg`)),
dark: URI.parse(require.toUrl(`vs/workbench/parts/files/electron-browser/media/check-inverse.svg`))
}, -10, acceptLocalChangesCommand);
appendSaveConflictEditorTitleAction('workbench.files.action.revertLocalChanges', nls.localize('revertLocalChanges', "Discard your changes and revert to content on disk"), {
light: URI.parse(require.toUrl(`vs/workbench/parts/files/electron-browser/media/undo.svg`)).fsPath,
dark: URI.parse(require.toUrl(`vs/workbench/parts/files/electron-browser/media/undo-inverse.svg`)).fsPath
light: URI.parse(require.toUrl(`vs/workbench/parts/files/electron-browser/media/undo.svg`)),
dark: URI.parse(require.toUrl(`vs/workbench/parts/files/electron-browser/media/undo-inverse.svg`))
}, -9, revertLocalChangesCommand);

function appendSaveConflictEditorTitleAction(id: string, title: string, iconPath: { dark: string; light?: string; }, order: number, command: ICommandHandler): void {
function appendSaveConflictEditorTitleAction(id: string, title: string, iconLocation: { dark: URI; light?: URI; }, order: number, command: ICommandHandler): void {

// Command
CommandsRegistry.registerCommand(id, command);

// Action
MenuRegistry.appendMenuItem(MenuId.EditorTitle, {
command: { id, title, iconPath },
command: { id, title, iconLocation },
when: ContextKeyExpr.equals(CONFLICT_RESOLUTION_CONTEXT, true),
group: 'navigation',
order
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

import { localize } from 'vs/nls';
import { isFalsyOrWhitespace } from 'vs/base/common/strings';
import { join } from 'path';
import * as resources from 'vs/base/common/resources';
import { IJSONSchema } from 'vs/base/common/jsonSchema';
import { forEach } from 'vs/base/common/collections';
import { IExtensionPointUser, ExtensionMessageCollector, ExtensionsRegistry } from 'vs/workbench/services/extensions/common/extensionsRegistry';
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
import { MenuId, MenuRegistry, ILocalizedString } from 'vs/platform/actions/common/actions';
import URI from 'vs/base/common/uri';

namespace schema {

Expand Down Expand Up @@ -286,20 +287,19 @@ ExtensionsRegistry.registerExtensionPoint<schema.IUserFriendlyCommand | schema.I

const { icon, category, title, command } = userFriendlyCommand;

let absoluteIcon: { dark: string; light?: string; };
let absoluteIcon: { dark: URI; light?: URI; };
if (icon) {
//TODO@extensionLocation
if (typeof icon === 'string') {
absoluteIcon = { dark: join(extension.description.extensionLocation.fsPath, icon) };
absoluteIcon = { dark: resources.joinPath(extension.description.extensionLocation, icon) };
} else {
absoluteIcon = {
dark: join(extension.description.extensionLocation.fsPath, icon.dark),
light: join(extension.description.extensionLocation.fsPath, icon.light)
dark: resources.joinPath(extension.description.extensionLocation, icon.dark),
light: resources.joinPath(extension.description.extensionLocation, icon.light)
};
}
}

if (MenuRegistry.addCommand({ id: command, title, category, iconPath: absoluteIcon })) {
if (MenuRegistry.addCommand({ id: command, title, category, iconLocation: absoluteIcon })) {
extension.collector.info(localize('dup', "Command `{0}` appears multiple times in the `commands` section.", userFriendlyCommand.command));
}
}
Expand Down

0 comments on commit 8810c8e

Please sign in to comment.