Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(770)

Side by Side Diff: chrome/browser/resources/extensions/extension_command_list.js

Issue 23523019: Enable user assign media keys in chrome://extensions page. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Nit. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 cr.define('options', function() { 5 cr.define('options', function() {
6 'use strict'; 6 'use strict';
7 7
8 /** 8 /**
9 * Creates a new list of extension commands. 9 * Creates a new list of extension commands.
10 * @param {Object=} opt_propertyBag Optional properties. 10 * @param {Object=} opt_propertyBag Optional properties.
11 * @constructor 11 * @constructor
12 * @extends {cr.ui.div} 12 * @extends {cr.ui.div}
13 */ 13 */
14 var ExtensionCommandList = cr.ui.define('div'); 14 var ExtensionCommandList = cr.ui.define('div');
15 15
16 /** @const */ var keyComma = 188; 16 /** @const */ var keyComma = 188;
17 /** @const */ var keyDel = 46; 17 /** @const */ var keyDel = 46;
18 /** @const */ var keyDown = 40; 18 /** @const */ var keyDown = 40;
19 /** @const */ var keyEnd = 35; 19 /** @const */ var keyEnd = 35;
20 /** @const */ var keyHome = 36; 20 /** @const */ var keyHome = 36;
21 /** @const */ var keyIns = 45; 21 /** @const */ var keyIns = 45;
22 /** @const */ var keyLeft = 37; 22 /** @const */ var keyLeft = 37;
23 /** @const */ var keyMediaNextTrack = 176;
24 /** @const */ var keyMediaPlayPause = 179;
25 /** @const */ var keyMediaPrevTrack = 177;
26 /** @const */ var keyMediaStop = 178;
23 /** @const */ var keyPageDown = 34; 27 /** @const */ var keyPageDown = 34;
24 /** @const */ var keyPageUp = 33; 28 /** @const */ var keyPageUp = 33;
25 /** @const */ var keyPeriod = 190; 29 /** @const */ var keyPeriod = 190;
26 /** @const */ var keyRight = 39; 30 /** @const */ var keyRight = 39;
27 /** @const */ var keyTab = 9; 31 /** @const */ var keyTab = 9;
28 /** @const */ var keyUp = 38; 32 /** @const */ var keyUp = 38;
29 33
30 /** 34 /**
35 * Enum for whether we require modifiers of a keycode.
36 * @enum {number}
37 */
38 var Modifiers = {
39 ARE_NOT_ALLOWED: 0,
40 ARE_REQUIRED: 1
41 };
42
43 /**
31 * Returns whether the passed in |keyCode| is a valid extension command 44 * Returns whether the passed in |keyCode| is a valid extension command
32 * char or not. This is restricted to A-Z and 0-9 (ignoring modifiers) at 45 * char or not. This is restricted to A-Z and 0-9 (ignoring modifiers) at
33 * the moment. 46 * the moment.
34 * @param {int} keyCode The keycode to consider. 47 * @param {int} keyCode The keycode to consider.
35 * @return {boolean} Returns whether the char is valid. 48 * @return {boolean} Returns whether the char is valid.
36 */ 49 */
37 function validChar(keyCode) { 50 function validChar(keyCode) {
38 return keyCode == keyComma || 51 return keyCode == keyComma ||
39 keyCode == keyDel || 52 keyCode == keyDel ||
40 keyCode == keyDown || 53 keyCode == keyDown ||
41 keyCode == keyEnd || 54 keyCode == keyEnd ||
42 keyCode == keyHome || 55 keyCode == keyHome ||
43 keyCode == keyIns || 56 keyCode == keyIns ||
44 keyCode == keyLeft || 57 keyCode == keyLeft ||
58 keyCode == keyMediaNextTrack ||
59 keyCode == keyMediaPlayPause ||
60 keyCode == keyMediaPrevTrack ||
61 keyCode == keyMediaStop ||
45 keyCode == keyPageDown || 62 keyCode == keyPageDown ||
46 keyCode == keyPageUp || 63 keyCode == keyPageUp ||
47 keyCode == keyPeriod || 64 keyCode == keyPeriod ||
48 keyCode == keyRight || 65 keyCode == keyRight ||
49 keyCode == keyTab || 66 keyCode == keyTab ||
50 keyCode == keyUp || 67 keyCode == keyUp ||
51 (keyCode >= 'A'.charCodeAt(0) && keyCode <= 'Z'.charCodeAt(0)) || 68 (keyCode >= 'A'.charCodeAt(0) && keyCode <= 'Z'.charCodeAt(0)) ||
52 (keyCode >= '0'.charCodeAt(0) && keyCode <= '9'.charCodeAt(0)); 69 (keyCode >= '0'.charCodeAt(0) && keyCode <= '9'.charCodeAt(0));
53 } 70 }
54 71
(...skipping 28 matching lines...) Expand all
83 case keyDown: 100 case keyDown:
84 output += 'Down'; break; 101 output += 'Down'; break;
85 case keyEnd: 102 case keyEnd:
86 output += 'End'; break; 103 output += 'End'; break;
87 case keyHome: 104 case keyHome:
88 output += 'Home'; break; 105 output += 'Home'; break;
89 case keyIns: 106 case keyIns:
90 output += 'Insert'; break; 107 output += 'Insert'; break;
91 case keyLeft: 108 case keyLeft:
92 output += 'Left'; break; 109 output += 'Left'; break;
110 case keyMediaNextTrack:
111 output += 'MediaNextTrack'; break;
112 case keyMediaPlayPause:
113 output += 'MediaPlayPause'; break;
114 case keyMediaPrevTrack:
115 output += 'MediaPrevTrack'; break;
116 case keyMediaStop:
117 output += 'MediaStop'; break;
93 case keyPageDown: 118 case keyPageDown:
94 output += 'PageDown'; break; 119 output += 'PageDown'; break;
95 case keyPageUp: 120 case keyPageUp:
96 output += 'PageUp'; break; 121 output += 'PageUp'; break;
97 case keyPeriod: 122 case keyPeriod:
98 output += 'Period'; break; 123 output += 'Period'; break;
99 case keyRight: 124 case keyRight:
100 output += 'Right'; break; 125 output += 'Right'; break;
101 case keyTab: 126 case keyTab:
102 output += 'Tab'; break; 127 output += 'Tab'; break;
103 case keyUp: 128 case keyUp:
104 output += 'Up'; break; 129 output += 'Up'; break;
105 } 130 }
106 } 131 }
107 } 132 }
108 133
109 return output; 134 return output;
110 } 135 }
111 136
137 /**
138 * Returns whether the passed in |keyCode| require modifiers. Currently only
139 * "MediaNextTrack", "MediaPrevTrack", "MediaStop", "MediaPlayPause" are
140 * required to be used without any modifier.
141 * @param {int} keyCode The keycode to consider.
142 * @return {Modifiers} Returns whether the keycode require modifiers.
143 */
144 function modifiers(keyCode) {
145 switch (keyCode) {
146 case keyMediaNextTrack:
147 case keyMediaPlayPause:
148 case keyMediaPrevTrack:
149 case keyMediaStop:
150 return Modifiers.ARE_NOT_ALLOWED;
151 default:
152 return Modifiers.ARE_REQUIRED;
153 }
154 }
155
156 /**
157 * Return true if the specified keyboard event has any one of following
158 * modifiers: "Ctrl", "Alt", "Cmd" on Mac, and "Shift" when the
159 * countShiftAsModifier is true.
160 * @param {Event} event The keyboard event to consider.
161 * @param {boolean} countShiftAsModifier Whether the 'ShiftKey' should be
162 * counted as modifier.
163 */
164 function hasModifier(event, countShiftAsModifier) {
165 return event.ctrlKey || event.altKey || (cr.isMac && event.metaKey) ||
166 (countShiftAsModifier && event.shiftKey);
167 }
168
112 ExtensionCommandList.prototype = { 169 ExtensionCommandList.prototype = {
113 __proto__: HTMLDivElement.prototype, 170 __proto__: HTMLDivElement.prototype,
114 171
115 /** 172 /**
116 * While capturing, this records the current (last) keyboard event generated 173 * While capturing, this records the current (last) keyboard event generated
117 * by the user. Will be |null| after capture and during capture when no 174 * by the user. Will be |null| after capture and during capture when no
118 * keyboard event has been generated. 175 * keyboard event has been generated.
119 * @type: {keyboard event}. 176 * @type: {keyboard event}.
120 * @private 177 * @private
121 */ 178 */
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 * @param {Event} event The keyboard event to consider. 373 * @param {Event} event The keyboard event to consider.
317 * @private 374 * @private
318 */ 375 */
319 handleKey_: function(event) { 376 handleKey_: function(event) {
320 // While capturing, we prevent all events from bubbling, to prevent 377 // While capturing, we prevent all events from bubbling, to prevent
321 // shortcuts lacking the right modifier (F3 for example) from activating 378 // shortcuts lacking the right modifier (F3 for example) from activating
322 // and ending capture prematurely. 379 // and ending capture prematurely.
323 event.preventDefault(); 380 event.preventDefault();
324 event.stopPropagation(); 381 event.stopPropagation();
325 382
326 if (!event.ctrlKey && !event.altKey && (!cr.isMac || !event.metaKey)) 383 if (modifiers(event.keyCode) == Modifiers.ARE_REQUIRED &&
327 return; // Ctrl or Alt is a must (or Cmd on Mac). 384 !hasModifier(event, false)) {
385 // Ctrl or Alt (or Cmd on Mac) is a must for most shortcuts.
386 return;
387 }
388
389 if (modifiers(event.keyCode) == Modifiers.ARE_NOT_ALLOWED &&
390 hasModifier(event, true)) {
391 return;
392 }
328 393
329 var shortcutNode = this.capturingElement_; 394 var shortcutNode = this.capturingElement_;
330 var keystroke = keystrokeToString(event); 395 var keystroke = keystrokeToString(event);
331 shortcutNode.textContent = keystroke; 396 shortcutNode.textContent = keystroke;
332 event.target.classList.add('contains-chars'); 397 event.target.classList.add('contains-chars');
333 398
334 if (validChar(event.keyCode)) { 399 if (validChar(event.keyCode)) {
335 var node = event.target; 400 var node = event.target;
336 while (node && !node.id) 401 while (node && !node.id)
337 node = node.parentElement; 402 node = node.parentElement;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 namespace.length + 1 + kExtensionIdLength), 450 namespace.length + 1 + kExtensionIdLength),
386 commandName: id.substring(namespace.length + 1 + kExtensionIdLength + 1) 451 commandName: id.substring(namespace.length + 1 + kExtensionIdLength + 1)
387 }; 452 };
388 }, 453 },
389 }; 454 };
390 455
391 return { 456 return {
392 ExtensionCommandList: ExtensionCommandList 457 ExtensionCommandList: ExtensionCommandList
393 }; 458 };
394 }); 459 });
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698