Commit 07714205 authored by Pavel Feldman's avatar Pavel Feldman Committed by Commit Bot

DevTools: wire add folder to workspace through the action system.

Change-Id: I12c8ed2052b171ab91658fd9f734117f18fc826b
Reviewed-on: https://chromium-review.googlesource.com/c/1354688Reviewed-by: default avatarDmitry Gozman <dgozman@chromium.org>
Commit-Queue: Pavel Feldman <pfeldman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#612547}
parent 56a79dae
...@@ -628,7 +628,7 @@ Elements.StylesSidebarPane = class extends Elements.ElementsSidebarPane { ...@@ -628,7 +628,7 @@ Elements.StylesSidebarPane = class extends Elements.ElementsSidebarPane {
filterContainerElement.appendChild(filterInput); filterContainerElement.appendChild(filterInput);
const toolbar = new UI.Toolbar('styles-pane-toolbar', hbox); const toolbar = new UI.Toolbar('styles-pane-toolbar', hbox);
toolbar.makeToggledGray(); toolbar.makeToggledGray();
toolbar.appendLocationItems('styles-sidebarpane-toolbar'); toolbar.appendItemsAtLocation('styles-sidebarpane-toolbar');
const toolbarPaneContainer = container.createChild('div', 'styles-sidebar-toolbar-pane-container'); const toolbarPaneContainer = container.createChild('div', 'styles-sidebar-toolbar-pane-container');
const toolbarPaneContent = toolbarPaneContainer.createChild('div', 'styles-sidebar-toolbar-pane'); const toolbarPaneContent = toolbarPaneContainer.createChild('div', 'styles-sidebar-toolbar-pane');
......
...@@ -113,18 +113,6 @@ Sources.NavigatorView = class extends UI.VBox { ...@@ -113,18 +113,6 @@ Sources.NavigatorView = class extends UI.VBox {
return order; return order;
} }
/**
* @param {!UI.ContextMenu} contextMenu
*/
static appendAddFolderItem(contextMenu) {
function addFolder() {
Persistence.isolatedFileSystemManager.addFileSystem();
}
const addFolderLabel = Common.UIString('Add folder to workspace');
contextMenu.defaultSection().appendItem(addFolderLabel, addFolder);
}
/** /**
* @param {!UI.ContextMenu} contextMenu * @param {!UI.ContextMenu} contextMenu
* @param {string=} path * @param {string=} path
...@@ -753,7 +741,7 @@ Sources.NavigatorView = class extends UI.VBox { ...@@ -753,7 +741,7 @@ Sources.NavigatorView = class extends UI.VBox {
} }
if (project.type() === Workspace.projectTypes.FileSystem) { if (project.type() === Workspace.projectTypes.FileSystem) {
Sources.NavigatorView.appendAddFolderItem(contextMenu); contextMenu.defaultSection().appendAction('sources.add-folder-to-workspace', undefined, true);
if (node instanceof Sources.NavigatorGroupTreeNode) if (node instanceof Sources.NavigatorGroupTreeNode)
contextMenu.defaultSection().appendItem(Common.UIString('Remove folder from workspace'), removeFolder); contextMenu.defaultSection().appendItem(Common.UIString('Remove folder from workspace'), removeFolder);
} }
......
...@@ -81,12 +81,10 @@ Sources.FilesNavigatorView = class extends Sources.NavigatorView { ...@@ -81,12 +81,10 @@ Sources.FilesNavigatorView = class extends Sources.NavigatorView {
constructor() { constructor() {
super(); super();
const toolbar = new UI.Toolbar('navigator-toolbar'); const toolbar = new UI.Toolbar('navigator-toolbar');
const title = Common.UIString('Add folder to workspace'); toolbar.appendItemsAtLocation('files-navigator-toolbar').then(() => {
const addButton = new UI.ToolbarButton(title, 'largeicon-add', title); if (!toolbar.empty())
addButton.addEventListener( this.contentElement.insertBefore(toolbar.element, this.contentElement.firstChild);
UI.ToolbarButton.Events.Click, () => Persistence.isolatedFileSystemManager.addFileSystem()); });
toolbar.appendToolbarItem(addButton);
this.contentElement.insertBefore(toolbar.element, this.contentElement.firstChild);
} }
/** /**
...@@ -106,7 +104,7 @@ Sources.FilesNavigatorView = class extends Sources.NavigatorView { ...@@ -106,7 +104,7 @@ Sources.FilesNavigatorView = class extends Sources.NavigatorView {
*/ */
handleContextMenu(event) { handleContextMenu(event) {
const contextMenu = new UI.ContextMenu(event); const contextMenu = new UI.ContextMenu(event);
Sources.NavigatorView.appendAddFolderItem(contextMenu); contextMenu.defaultSection().appendAction('sources.add-folder-to-workspace', undefined, true);
contextMenu.show(); contextMenu.show();
} }
}; };
...@@ -263,7 +261,7 @@ Sources.SnippetsNavigatorView = class extends Sources.NavigatorView { ...@@ -263,7 +261,7 @@ Sources.SnippetsNavigatorView = class extends Sources.NavigatorView {
/** /**
* @implements {UI.ActionDelegate} * @implements {UI.ActionDelegate}
*/ */
Sources.SnippetsNavigatorView.CreatingActionDelegate = class { Sources.ActionDelegate = class {
/** /**
* @override * @override
* @param {!UI.Context} context * @param {!UI.Context} context
......
...@@ -693,15 +693,24 @@ ...@@ -693,15 +693,24 @@
"type": "action", "type": "action",
"category": "Sources", "category": "Sources",
"actionId": "sources.create-snippet", "actionId": "sources.create-snippet",
"className": "Sources.SnippetsNavigatorView.CreatingActionDelegate", "className": "Sources.ActionDelegate",
"title": "Create new snippet" "title": "Create new snippet"
}, },
{ {
"type": "action", "type": "action",
"category": "Sources", "category": "Sources",
"actionId": "sources.add-folder-to-workspace", "actionId": "sources.add-folder-to-workspace",
"className": "Sources.SnippetsNavigatorView.CreatingActionDelegate", "className": "Sources.ActionDelegate",
"title": "Add folder to workspace" "iconClass": "largeicon-add",
"title": "Add folder to workspace",
"condition": "!sources.hide_add_folder"
},
{
"type": "@UI.ToolbarItem.Provider",
"actionId": "sources.add-folder-to-workspace",
"location": "files-navigator-toolbar",
"showLabel": true,
"condition": "!sources.hide_add_folder"
}, },
{ {
"type": "@UI.ViewLocationResolver", "type": "@UI.ViewLocationResolver",
......
...@@ -145,11 +145,13 @@ UI.ContextMenuSection = class { ...@@ -145,11 +145,13 @@ UI.ContextMenuSection = class {
/** /**
* @param {string} actionId * @param {string} actionId
* @param {string=} label * @param {string=} label
* @param {boolean=} optional
*/ */
appendAction(actionId, label) { appendAction(actionId, label, optional) {
const action = UI.actionRegistry.action(actionId); const action = UI.actionRegistry.action(actionId);
if (!action) { if (!action) {
console.error(`Action ${actionId} was not defined`); if (!optional)
console.error(`Action ${actionId} was not defined`);
return; return;
} }
if (!label) if (!label)
......
...@@ -138,8 +138,8 @@ UI.InspectorView = class extends UI.VBox { ...@@ -138,8 +138,8 @@ UI.InspectorView = class extends UI.VBox {
} }
createToolbars() { createToolbars() {
this._tabbedPane.leftToolbar().appendLocationItems('main-toolbar-left'); this._tabbedPane.leftToolbar().appendItemsAtLocation('main-toolbar-left');
this._tabbedPane.rightToolbar().appendLocationItems('main-toolbar-right'); this._tabbedPane.rightToolbar().appendItemsAtLocation('main-toolbar-right');
} }
/** /**
......
...@@ -52,10 +52,13 @@ UI.Toolbar = class { ...@@ -52,10 +52,13 @@ UI.Toolbar = class {
* @param {!UI.Action} action * @param {!UI.Action} action
* @param {!Array<!UI.ToolbarButton>=} toggledOptions * @param {!Array<!UI.ToolbarButton>=} toggledOptions
* @param {!Array<!UI.ToolbarButton>=} untoggledOptions * @param {!Array<!UI.ToolbarButton>=} untoggledOptions
* @param {boolean=} showLabel
* @return {!UI.ToolbarToggle} * @return {!UI.ToolbarToggle}
*/ */
static createActionButton(action, toggledOptions, untoggledOptions) { static createActionButton(action, toggledOptions, untoggledOptions, showLabel) {
const button = new UI.ToolbarToggle(action.title(), action.icon(), action.toggledIcon()); const button = new UI.ToolbarToggle(action.title(), action.icon(), action.toggledIcon());
if (showLabel)
button.setText(action.title());
button.setToggleWithRedColor(action.toggleWithRedColor()); button.setToggleWithRedColor(action.toggleWithRedColor());
button.addEventListener(UI.ToolbarButton.Events.Click, action.execute, action); button.addEventListener(UI.ToolbarButton.Events.Click, action.execute, action);
action.addEventListener(UI.Action.Events.Enabled, enabledChanged); action.addEventListener(UI.Action.Events.Enabled, enabledChanged);
...@@ -185,11 +188,13 @@ UI.Toolbar = class { ...@@ -185,11 +188,13 @@ UI.Toolbar = class {
/** /**
* @param {string} actionId * @param {string} actionId
* @param {boolean=} showLabel
* @return {!UI.ToolbarToggle} * @return {!UI.ToolbarToggle}
*/ */
static createActionButtonForId(actionId) { static createActionButtonForId(actionId, showLabel) {
const action = UI.actionRegistry.action(actionId); const action = UI.actionRegistry.action(actionId);
return UI.Toolbar.createActionButton(/** @type {!UI.Action} */ (action)); return UI.Toolbar.createActionButton(
/** @type {!UI.Action} */ (action), undefined, undefined, showLabel);
} }
/** /**
...@@ -224,6 +229,13 @@ UI.Toolbar = class { ...@@ -224,6 +229,13 @@ UI.Toolbar = class {
this._contentElement.classList.add('toolbar-render-as-links'); this._contentElement.classList.add('toolbar-render-as-links');
} }
/**
* @return {boolean}
*/
empty() {
return !this._items.length;
}
/** /**
* @param {boolean} enabled * @param {boolean} enabled
*/ */
...@@ -315,50 +327,20 @@ UI.Toolbar = class { ...@@ -315,50 +327,20 @@ UI.Toolbar = class {
/** /**
* @param {string} location * @param {string} location
* @return {!Promise}
*/ */
appendLocationItems(location) { async appendItemsAtLocation(location) {
const extensions = self.runtime.extensions(UI.ToolbarItem.Provider); const extensions = self.runtime.extensions(UI.ToolbarItem.Provider);
const promises = []; const filtered = extensions.filter(e => e.descriptor()['location'] === location);
for (let i = 0; i < extensions.length; ++i) { const items = await Promise.all(filtered.map(extension => {
if (extensions[i].descriptor()['location'] === location)
promises.push(resolveItem(extensions[i]));
}
Promise.all(promises).then(appendItemsInOrder.bind(this));
/**
* @param {!Runtime.Extension} extension
* @return {!Promise<?UI.ToolbarItem>}
*/
function resolveItem(extension) {
const descriptor = extension.descriptor(); const descriptor = extension.descriptor();
if (descriptor['separator']) if (descriptor['separator'])
return Promise.resolve(/** @type {?UI.ToolbarItem} */ (new UI.ToolbarSeparator())); return new UI.ToolbarSeparator();
if (descriptor['actionId']) { if (descriptor['actionId'])
return Promise.resolve( return UI.Toolbar.createActionButtonForId(descriptor['actionId'], descriptor['showLabel']);
/** @type {?UI.ToolbarItem} */ (UI.Toolbar.createActionButtonForId(descriptor['actionId']))); return extension.instance().then(p => p.item());
} }));
return extension.instance().then(fetchItemFromProvider); items.filter(item => item).forEach(item => this.appendToolbarItem(item));
/**
* @param {!Object} provider
* @return {?UI.ToolbarItem}
*/
function fetchItemFromProvider(provider) {
return /** @type {!UI.ToolbarItem.Provider} */ (provider).item();
}
}
/**
* @param {!Array.<?UI.ToolbarItem>} items
* @this {UI.Toolbar}
*/
function appendItemsInOrder(items) {
for (let i = 0; i < items.length; ++i) {
const item = items[i];
if (item)
this.appendToolbarItem(item);
}
}
} }
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment