| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 /** | 4 /** |
| 5 * @unrestricted | 5 * @unrestricted |
| 6 */ | 6 */ |
| 7 WebInspector.CommandMenu = class { | 7 UI.CommandMenu = class { |
| 8 constructor() { | 8 constructor() { |
| 9 this._commands = []; | 9 this._commands = []; |
| 10 this._loadCommands(); | 10 this._loadCommands(); |
| 11 } | 11 } |
| 12 | 12 |
| 13 /** | 13 /** |
| 14 * @param {string} category | 14 * @param {string} category |
| 15 * @param {string} keys | 15 * @param {string} keys |
| 16 * @param {string} title | 16 * @param {string} title |
| 17 * @param {string} shortcut | 17 * @param {string} shortcut |
| 18 * @param {function()} executeHandler | 18 * @param {function()} executeHandler |
| 19 * @param {function()=} availableHandler | 19 * @param {function()=} availableHandler |
| 20 * @return {!WebInspector.CommandMenu.Command} | 20 * @return {!UI.CommandMenu.Command} |
| 21 */ | 21 */ |
| 22 static createCommand(category, keys, title, shortcut, executeHandler, availabl
eHandler) { | 22 static createCommand(category, keys, title, shortcut, executeHandler, availabl
eHandler) { |
| 23 // Separate keys by null character, to prevent fuzzy matching from matching
across them. | 23 // Separate keys by null character, to prevent fuzzy matching from matching
across them. |
| 24 var key = keys.replace(/,/g, '\0'); | 24 var key = keys.replace(/,/g, '\0'); |
| 25 return new WebInspector.CommandMenu.Command(category, title, key, shortcut,
executeHandler, availableHandler); | 25 return new UI.CommandMenu.Command(category, title, key, shortcut, executeHan
dler, availableHandler); |
| 26 } | 26 } |
| 27 | 27 |
| 28 /** | 28 /** |
| 29 * @param {!Runtime.Extension} extension | 29 * @param {!Runtime.Extension} extension |
| 30 * @param {string} title | 30 * @param {string} title |
| 31 * @param {V} value | 31 * @param {V} value |
| 32 * @return {!WebInspector.CommandMenu.Command} | 32 * @return {!UI.CommandMenu.Command} |
| 33 * @template V | 33 * @template V |
| 34 */ | 34 */ |
| 35 static createSettingCommand(extension, title, value) { | 35 static createSettingCommand(extension, title, value) { |
| 36 var category = extension.descriptor()['category'] || ''; | 36 var category = extension.descriptor()['category'] || ''; |
| 37 var tags = extension.descriptor()['tags'] || ''; | 37 var tags = extension.descriptor()['tags'] || ''; |
| 38 var setting = WebInspector.settings.moduleSetting(extension.descriptor()['se
ttingName']); | 38 var setting = Common.settings.moduleSetting(extension.descriptor()['settingN
ame']); |
| 39 return WebInspector.CommandMenu.createCommand( | 39 return UI.CommandMenu.createCommand( |
| 40 category, tags, title, '', setting.set.bind(setting, value), availableHa
ndler); | 40 category, tags, title, '', setting.set.bind(setting, value), availableHa
ndler); |
| 41 | 41 |
| 42 /** | 42 /** |
| 43 * @return {boolean} | 43 * @return {boolean} |
| 44 */ | 44 */ |
| 45 function availableHandler() { | 45 function availableHandler() { |
| 46 return setting.get() !== value; | 46 return setting.get() !== value; |
| 47 } | 47 } |
| 48 } | 48 } |
| 49 | 49 |
| 50 /** | 50 /** |
| 51 * @param {!WebInspector.Action} action | 51 * @param {!UI.Action} action |
| 52 * @return {!WebInspector.CommandMenu.Command} | 52 * @return {!UI.CommandMenu.Command} |
| 53 */ | 53 */ |
| 54 static createActionCommand(action) { | 54 static createActionCommand(action) { |
| 55 var shortcut = WebInspector.shortcutRegistry.shortcutTitleForAction(action.i
d()) || ''; | 55 var shortcut = UI.shortcutRegistry.shortcutTitleForAction(action.id()) || ''
; |
| 56 return WebInspector.CommandMenu.createCommand( | 56 return UI.CommandMenu.createCommand( |
| 57 action.category(), action.tags(), action.title(), shortcut, action.execu
te.bind(action)); | 57 action.category(), action.tags(), action.title(), shortcut, action.execu
te.bind(action)); |
| 58 } | 58 } |
| 59 | 59 |
| 60 /** | 60 /** |
| 61 * @param {!Runtime.Extension} extension | 61 * @param {!Runtime.Extension} extension |
| 62 * @return {!WebInspector.CommandMenu.Command} | 62 * @return {!UI.CommandMenu.Command} |
| 63 */ | 63 */ |
| 64 static createRevealPanelCommand(extension) { | 64 static createRevealPanelCommand(extension) { |
| 65 var panelName = extension.descriptor()['name']; | 65 var panelName = extension.descriptor()['name']; |
| 66 var tags = extension.descriptor()['tags'] || ''; | 66 var tags = extension.descriptor()['tags'] || ''; |
| 67 return WebInspector.CommandMenu.createCommand( | 67 return UI.CommandMenu.createCommand( |
| 68 WebInspector.UIString('Panel'), tags, WebInspector.UIString('Show %s', e
xtension.title()), '', executeHandler, | 68 Common.UIString('Panel'), tags, Common.UIString('Show %s', extension.tit
le()), '', executeHandler, |
| 69 availableHandler); | 69 availableHandler); |
| 70 | 70 |
| 71 /** | 71 /** |
| 72 * @return {boolean} | 72 * @return {boolean} |
| 73 */ | 73 */ |
| 74 function availableHandler() { | 74 function availableHandler() { |
| 75 return true; | 75 return true; |
| 76 } | 76 } |
| 77 | 77 |
| 78 function executeHandler() { | 78 function executeHandler() { |
| 79 WebInspector.viewManager.showView(panelName); | 79 UI.viewManager.showView(panelName); |
| 80 } | 80 } |
| 81 } | 81 } |
| 82 | 82 |
| 83 /** | 83 /** |
| 84 * @param {!Runtime.Extension} extension | 84 * @param {!Runtime.Extension} extension |
| 85 * @return {!WebInspector.CommandMenu.Command} | 85 * @return {!UI.CommandMenu.Command} |
| 86 */ | 86 */ |
| 87 static createRevealDrawerCommand(extension) { | 87 static createRevealDrawerCommand(extension) { |
| 88 var drawerId = extension.descriptor()['id']; | 88 var drawerId = extension.descriptor()['id']; |
| 89 var executeHandler = WebInspector.viewManager.showView.bind(WebInspector.vie
wManager, drawerId); | 89 var executeHandler = UI.viewManager.showView.bind(UI.viewManager, drawerId); |
| 90 var tags = extension.descriptor()['tags'] || ''; | 90 var tags = extension.descriptor()['tags'] || ''; |
| 91 return WebInspector.CommandMenu.createCommand( | 91 return UI.CommandMenu.createCommand( |
| 92 WebInspector.UIString('Drawer'), tags, WebInspector.UIString('Show %s',
extension.title()), '', executeHandler); | 92 Common.UIString('Drawer'), tags, Common.UIString('Show %s', extension.ti
tle()), '', executeHandler); |
| 93 } | 93 } |
| 94 | 94 |
| 95 _loadCommands() { | 95 _loadCommands() { |
| 96 // Populate panels. | 96 // Populate panels. |
| 97 var panelExtensions = self.runtime.extensions(WebInspector.Panel); | 97 var panelExtensions = self.runtime.extensions(UI.Panel); |
| 98 for (var extension of panelExtensions) | 98 for (var extension of panelExtensions) |
| 99 this._commands.push(WebInspector.CommandMenu.createRevealPanelCommand(exte
nsion)); | 99 this._commands.push(UI.CommandMenu.createRevealPanelCommand(extension)); |
| 100 | 100 |
| 101 // Populate drawers. | 101 // Populate drawers. |
| 102 var drawerExtensions = self.runtime.extensions('view'); | 102 var drawerExtensions = self.runtime.extensions('view'); |
| 103 for (var extension of drawerExtensions) { | 103 for (var extension of drawerExtensions) { |
| 104 if (extension.descriptor()['location'] !== 'drawer-view') | 104 if (extension.descriptor()['location'] !== 'drawer-view') |
| 105 continue; | 105 continue; |
| 106 this._commands.push(WebInspector.CommandMenu.createRevealDrawerCommand(ext
ension)); | 106 this._commands.push(UI.CommandMenu.createRevealDrawerCommand(extension)); |
| 107 } | 107 } |
| 108 | 108 |
| 109 // Populate whitelisted settings. | 109 // Populate whitelisted settings. |
| 110 var settingExtensions = self.runtime.extensions('setting'); | 110 var settingExtensions = self.runtime.extensions('setting'); |
| 111 for (var extension of settingExtensions) { | 111 for (var extension of settingExtensions) { |
| 112 var options = extension.descriptor()['options']; | 112 var options = extension.descriptor()['options']; |
| 113 if (!options || !extension.descriptor()['category']) | 113 if (!options || !extension.descriptor()['category']) |
| 114 continue; | 114 continue; |
| 115 for (var pair of options) | 115 for (var pair of options) |
| 116 this._commands.push(WebInspector.CommandMenu.createSettingCommand(extens
ion, pair['title'], pair['value'])); | 116 this._commands.push(UI.CommandMenu.createSettingCommand(extension, pair[
'title'], pair['value'])); |
| 117 } | 117 } |
| 118 } | 118 } |
| 119 | 119 |
| 120 /** | 120 /** |
| 121 * @return {!Array.<!WebInspector.CommandMenu.Command>} | 121 * @return {!Array.<!UI.CommandMenu.Command>} |
| 122 */ | 122 */ |
| 123 commands() { | 123 commands() { |
| 124 return this._commands; | 124 return this._commands; |
| 125 } | 125 } |
| 126 }; | 126 }; |
| 127 | 127 |
| 128 /** | 128 /** |
| 129 * @unrestricted | 129 * @unrestricted |
| 130 */ | 130 */ |
| 131 WebInspector.CommandMenuDelegate = class extends WebInspector.FilteredListWidget
.Delegate { | 131 UI.CommandMenuDelegate = class extends UI.FilteredListWidget.Delegate { |
| 132 constructor() { | 132 constructor() { |
| 133 super([]); | 133 super([]); |
| 134 this._commands = []; | 134 this._commands = []; |
| 135 this._appendAvailableCommands(); | 135 this._appendAvailableCommands(); |
| 136 } | 136 } |
| 137 | 137 |
| 138 _appendAvailableCommands() { | 138 _appendAvailableCommands() { |
| 139 var allCommands = WebInspector.commandMenu.commands(); | 139 var allCommands = UI.commandMenu.commands(); |
| 140 | 140 |
| 141 // Populate whitelisted actions. | 141 // Populate whitelisted actions. |
| 142 var actions = WebInspector.actionRegistry.availableActions(); | 142 var actions = UI.actionRegistry.availableActions(); |
| 143 for (var action of actions) { | 143 for (var action of actions) { |
| 144 if (action.category()) | 144 if (action.category()) |
| 145 this._commands.push(WebInspector.CommandMenu.createActionCommand(action)
); | 145 this._commands.push(UI.CommandMenu.createActionCommand(action)); |
| 146 } | 146 } |
| 147 | 147 |
| 148 for (var command of allCommands) { | 148 for (var command of allCommands) { |
| 149 if (command.available()) | 149 if (command.available()) |
| 150 this._commands.push(command); | 150 this._commands.push(command); |
| 151 } | 151 } |
| 152 | 152 |
| 153 this._commands = this._commands.sort(commandComparator); | 153 this._commands = this._commands.sort(commandComparator); |
| 154 | 154 |
| 155 /** | 155 /** |
| 156 * @param {!WebInspector.CommandMenu.Command} left | 156 * @param {!UI.CommandMenu.Command} left |
| 157 * @param {!WebInspector.CommandMenu.Command} right | 157 * @param {!UI.CommandMenu.Command} right |
| 158 * @return {number} | 158 * @return {number} |
| 159 */ | 159 */ |
| 160 function commandComparator(left, right) { | 160 function commandComparator(left, right) { |
| 161 var cats = left.category().compareTo(right.category()); | 161 var cats = left.category().compareTo(right.category()); |
| 162 return cats ? cats : left.title().compareTo(right.title()); | 162 return cats ? cats : left.title().compareTo(right.title()); |
| 163 } | 163 } |
| 164 } | 164 } |
| 165 | 165 |
| 166 /** | 166 /** |
| 167 * @override | 167 * @override |
| (...skipping 13 matching lines...) Expand all Loading... |
| 181 } | 181 } |
| 182 | 182 |
| 183 /** | 183 /** |
| 184 * @override | 184 * @override |
| 185 * @param {number} itemIndex | 185 * @param {number} itemIndex |
| 186 * @param {string} query | 186 * @param {string} query |
| 187 * @return {number} | 187 * @return {number} |
| 188 */ | 188 */ |
| 189 itemScoreAt(itemIndex, query) { | 189 itemScoreAt(itemIndex, query) { |
| 190 var command = this._commands[itemIndex]; | 190 var command = this._commands[itemIndex]; |
| 191 var opcodes = WebInspector.Diff.charDiff(query.toLowerCase(), command.title(
).toLowerCase()); | 191 var opcodes = Diff.Diff.charDiff(query.toLowerCase(), command.title().toLowe
rCase()); |
| 192 var score = 0; | 192 var score = 0; |
| 193 // Score longer sequences higher. | 193 // Score longer sequences higher. |
| 194 for (var i = 0; i < opcodes.length; ++i) { | 194 for (var i = 0; i < opcodes.length; ++i) { |
| 195 if (opcodes[i][0] === WebInspector.Diff.Operation.Equal) | 195 if (opcodes[i][0] === Diff.Diff.Operation.Equal) |
| 196 score += opcodes[i][1].length * opcodes[i][1].length; | 196 score += opcodes[i][1].length * opcodes[i][1].length; |
| 197 } | 197 } |
| 198 | 198 |
| 199 // Score panel/drawer reveals above regular actions. | 199 // Score panel/drawer reveals above regular actions. |
| 200 if (command.category().startsWith('Panel')) | 200 if (command.category().startsWith('Panel')) |
| 201 score += 2; | 201 score += 2; |
| 202 else if (command.category().startsWith('Drawer')) | 202 else if (command.category().startsWith('Drawer')) |
| 203 score += 1; | 203 score += 1; |
| 204 | 204 |
| 205 return score; | 205 return score; |
| 206 } | 206 } |
| 207 | 207 |
| 208 /** | 208 /** |
| 209 * @override | 209 * @override |
| 210 * @param {number} itemIndex | 210 * @param {number} itemIndex |
| 211 * @param {string} query | 211 * @param {string} query |
| 212 * @param {!Element} titleElement | 212 * @param {!Element} titleElement |
| 213 * @param {!Element} subtitleElement | 213 * @param {!Element} subtitleElement |
| 214 */ | 214 */ |
| 215 renderItem(itemIndex, query, titleElement, subtitleElement) { | 215 renderItem(itemIndex, query, titleElement, subtitleElement) { |
| 216 var command = this._commands[itemIndex]; | 216 var command = this._commands[itemIndex]; |
| 217 titleElement.removeChildren(); | 217 titleElement.removeChildren(); |
| 218 var tagElement = titleElement.createChild('span', 'tag'); | 218 var tagElement = titleElement.createChild('span', 'tag'); |
| 219 var index = String.hashCode(command.category()) % WebInspector.CommandMenuDe
legate.MaterialPaletteColors.length; | 219 var index = String.hashCode(command.category()) % UI.CommandMenuDelegate.Mat
erialPaletteColors.length; |
| 220 tagElement.style.backgroundColor = WebInspector.CommandMenuDelegate.Material
PaletteColors[index]; | 220 tagElement.style.backgroundColor = UI.CommandMenuDelegate.MaterialPaletteCol
ors[index]; |
| 221 tagElement.textContent = command.category(); | 221 tagElement.textContent = command.category(); |
| 222 titleElement.createTextChild(command.title()); | 222 titleElement.createTextChild(command.title()); |
| 223 this.highlightRanges(titleElement, query); | 223 this.highlightRanges(titleElement, query); |
| 224 subtitleElement.textContent = command.shortcut(); | 224 subtitleElement.textContent = command.shortcut(); |
| 225 } | 225 } |
| 226 | 226 |
| 227 /** | 227 /** |
| 228 * @override | 228 * @override |
| 229 * @param {?number} itemIndex | 229 * @param {?number} itemIndex |
| 230 * @param {string} promptValue | 230 * @param {string} promptValue |
| (...skipping 12 matching lines...) Expand all Loading... |
| 243 | 243 |
| 244 /** | 244 /** |
| 245 * @override | 245 * @override |
| 246 * @return {boolean} | 246 * @return {boolean} |
| 247 */ | 247 */ |
| 248 renderMonospace() { | 248 renderMonospace() { |
| 249 return false; | 249 return false; |
| 250 } | 250 } |
| 251 }; | 251 }; |
| 252 | 252 |
| 253 WebInspector.CommandMenuDelegate.MaterialPaletteColors = [ | 253 UI.CommandMenuDelegate.MaterialPaletteColors = [ |
| 254 '#F44336', '#E91E63', '#9C27B0', '#673AB7', '#3F51B5', '#03A9F4', '#00BCD4', '
#009688', '#4CAF50', '#8BC34A', | 254 '#F44336', '#E91E63', '#9C27B0', '#673AB7', '#3F51B5', '#03A9F4', '#00BCD4', '
#009688', '#4CAF50', '#8BC34A', |
| 255 '#CDDC39', '#FFC107', '#FF9800', '#FF5722', '#795548', '#9E9E9E', '#607D8B' | 255 '#CDDC39', '#FFC107', '#FF9800', '#FF5722', '#795548', '#9E9E9E', '#607D8B' |
| 256 ]; | 256 ]; |
| 257 | 257 |
| 258 /** | 258 /** |
| 259 * @unrestricted | 259 * @unrestricted |
| 260 */ | 260 */ |
| 261 WebInspector.CommandMenu.Command = class { | 261 UI.CommandMenu.Command = class { |
| 262 /** | 262 /** |
| 263 * @param {string} category | 263 * @param {string} category |
| 264 * @param {string} title | 264 * @param {string} title |
| 265 * @param {string} key | 265 * @param {string} key |
| 266 * @param {string} shortcut | 266 * @param {string} shortcut |
| 267 * @param {function()} executeHandler | 267 * @param {function()} executeHandler |
| 268 * @param {function()=} availableHandler | 268 * @param {function()=} availableHandler |
| 269 */ | 269 */ |
| 270 constructor(category, title, key, shortcut, executeHandler, availableHandler)
{ | 270 constructor(category, title, key, shortcut, executeHandler, availableHandler)
{ |
| 271 this._category = category; | 271 this._category = category; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 available() { | 310 available() { |
| 311 return this._availableHandler ? this._availableHandler() : true; | 311 return this._availableHandler ? this._availableHandler() : true; |
| 312 } | 312 } |
| 313 | 313 |
| 314 execute() { | 314 execute() { |
| 315 this._executeHandler(); | 315 this._executeHandler(); |
| 316 } | 316 } |
| 317 }; | 317 }; |
| 318 | 318 |
| 319 | 319 |
| 320 /** @type {!WebInspector.CommandMenu} */ | 320 /** @type {!UI.CommandMenu} */ |
| 321 WebInspector.commandMenu = new WebInspector.CommandMenu(); | 321 UI.commandMenu = new UI.CommandMenu(); |
| 322 | 322 |
| 323 /** | 323 /** |
| 324 * @implements {WebInspector.ActionDelegate} | 324 * @implements {UI.ActionDelegate} |
| 325 * @unrestricted | 325 * @unrestricted |
| 326 */ | 326 */ |
| 327 WebInspector.CommandMenu.ShowActionDelegate = class { | 327 UI.CommandMenu.ShowActionDelegate = class { |
| 328 /** | 328 /** |
| 329 * @override | 329 * @override |
| 330 * @param {!WebInspector.Context} context | 330 * @param {!UI.Context} context |
| 331 * @param {string} actionId | 331 * @param {string} actionId |
| 332 * @return {boolean} | 332 * @return {boolean} |
| 333 */ | 333 */ |
| 334 handleAction(context, actionId) { | 334 handleAction(context, actionId) { |
| 335 new WebInspector.FilteredListWidget(new WebInspector.CommandMenuDelegate()).
showAsDialog(); | 335 new UI.FilteredListWidget(new UI.CommandMenuDelegate()).showAsDialog(); |
| 336 InspectorFrontendHost.bringToFront(); | 336 InspectorFrontendHost.bringToFront(); |
| 337 return true; | 337 return true; |
| 338 } | 338 } |
| 339 }; | 339 }; |
| OLD | NEW |