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

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

Issue 2939273002: DO NOT SUBMIT: what chrome/browser/resources/ could eventually look like with clang-format (Closed)
Patch Set: Created 3 years, 6 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
OLDNEW
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 cr.define('extensions', function() { 5 cr.define('extensions', function() {
6 'use strict'; 6 'use strict';
7 7
8 /** 8 /**
9 * @enum {number} 9 * @enum {number}
10 */ 10 */
(...skipping 16 matching lines...) Expand all
27 Right: 39, 27 Right: 39,
28 Space: 32, 28 Space: 32,
29 Tab: 9, 29 Tab: 9,
30 Up: 38, 30 Up: 38,
31 }; 31 };
32 32
33 /** 33 /**
34 * Enum for whether we require modifiers of a keycode. 34 * Enum for whether we require modifiers of a keycode.
35 * @enum {number} 35 * @enum {number}
36 */ 36 */
37 var ModifierPolicy = { 37 var ModifierPolicy = {NOT_ALLOWED: 0, REQUIRED: 1};
38 NOT_ALLOWED: 0,
39 REQUIRED: 1
40 };
41 38
42 /** 39 /**
43 * Gets the ModifierPolicy. Currently only "MediaNextTrack", "MediaPrevTrack", 40 * Gets the ModifierPolicy. Currently only "MediaNextTrack", "MediaPrevTrack",
44 * "MediaStop", "MediaPlayPause" are required to be used without any modifier. 41 * "MediaStop", "MediaPlayPause" are required to be used without any modifier.
45 * @param {number} keyCode 42 * @param {number} keyCode
46 * @return {ModifierPolicy} 43 * @return {ModifierPolicy}
47 */ 44 */
48 function getModifierPolicy(keyCode) { 45 function getModifierPolicy(keyCode) {
49 switch (keyCode) { 46 switch (keyCode) {
50 case Key.MediaNextTrack: 47 case Key.MediaNextTrack:
51 case Key.MediaPlayPause: 48 case Key.MediaPlayPause:
52 case Key.MediaPrevTrack: 49 case Key.MediaPrevTrack:
53 case Key.MediaStop: 50 case Key.MediaStop:
54 return ModifierPolicy.NOT_ALLOWED; 51 return ModifierPolicy.NOT_ALLOWED;
55 default: 52 default:
56 return ModifierPolicy.REQUIRED; 53 return ModifierPolicy.REQUIRED;
57 } 54 }
58 } 55 }
59 56
60 /** 57 /**
61 * Returns whether the keyboard event has a key modifier, which could affect 58 * Returns whether the keyboard event has a key modifier, which could affect
62 * how it's handled. 59 * how it's handled.
63 * @param {!KeyboardEvent} e 60 * @param {!KeyboardEvent} e
64 * @param {boolean} countShiftAsModifier Whether the 'Shift' key should be 61 * @param {boolean} countShiftAsModifier Whether the 'Shift' key should be
65 * counted as modifier. 62 * counted as modifier.
66 * @return {boolean} True if the event has any modifiers. 63 * @return {boolean} True if the event has any modifiers.
67 */ 64 */
68 function hasModifier(e, countShiftAsModifier) { 65 function hasModifier(e, countShiftAsModifier) {
69 return e.ctrlKey || e.altKey || 66 return e.ctrlKey || e.altKey ||
70 // Meta key is only relevant on Mac and CrOS, where we treat Command 67 // Meta key is only relevant on Mac and CrOS, where we treat Command
71 // and Search (respectively) as modifiers. 68 // and Search (respectively) as modifiers.
72 (cr.isMac && e.metaKey) || 69 (cr.isMac && e.metaKey) || (cr.isChromeOS && e.metaKey) ||
73 (cr.isChromeOS && e.metaKey) || 70 (countShiftAsModifier && e.shiftKey);
74 (countShiftAsModifier && e.shiftKey);
75 } 71 }
76 72
77 /** 73 /**
78 * Checks whether the passed in |keyCode| is a valid extension command key. 74 * Checks whether the passed in |keyCode| is a valid extension command key.
79 * @param {number} keyCode 75 * @param {number} keyCode
80 * @return {boolean} Whether the key is valid. 76 * @return {boolean} Whether the key is valid.
81 */ 77 */
82 function isValidKeyCode(keyCode) { 78 function isValidKeyCode(keyCode) {
83 if (keyCode == Key.Escape) 79 if (keyCode == Key.Escape)
84 return false; 80 return false;
85 for (var k in Key) { 81 for (var k in Key) {
86 if (Key[k] == keyCode) 82 if (Key[k] == keyCode)
87 return true; 83 return true;
88 } 84 }
89 return (keyCode >= 'A'.charCodeAt(0) && keyCode <= 'Z'.charCodeAt(0)) || 85 return (keyCode >= 'A'.charCodeAt(0) && keyCode <= 'Z'.charCodeAt(0)) ||
90 (keyCode >= '0'.charCodeAt(0) && keyCode <= '9'.charCodeAt(0)); 86 (keyCode >= '0'.charCodeAt(0) && keyCode <= '9'.charCodeAt(0));
91 } 87 }
92 88
93 /** 89 /**
94 * Converts a keystroke event to string form, ignoring invalid extension 90 * Converts a keystroke event to string form, ignoring invalid extension
95 * commands. 91 * commands.
96 * @param {!KeyboardEvent} e 92 * @param {!KeyboardEvent} e
97 * @return {string} The keystroke as a string. 93 * @return {string} The keystroke as a string.
98 */ 94 */
99 function keystrokeToString(e) { 95 function keystrokeToString(e) {
100 var output = []; 96 var output = [];
(...skipping 10 matching lines...) Expand all
111 output.push('Shift'); 107 output.push('Shift');
112 108
113 var keyCode = e.keyCode; 109 var keyCode = e.keyCode;
114 if (isValidKeyCode(keyCode)) { 110 if (isValidKeyCode(keyCode)) {
115 if ((keyCode >= 'A'.charCodeAt(0) && keyCode <= 'Z'.charCodeAt(0)) || 111 if ((keyCode >= 'A'.charCodeAt(0) && keyCode <= 'Z'.charCodeAt(0)) ||
116 (keyCode >= '0'.charCodeAt(0) && keyCode <= '9'.charCodeAt(0))) { 112 (keyCode >= '0'.charCodeAt(0) && keyCode <= '9'.charCodeAt(0))) {
117 output.push(String.fromCharCode(keyCode)); 113 output.push(String.fromCharCode(keyCode));
118 } else { 114 } else {
119 switch (keyCode) { 115 switch (keyCode) {
120 case Key.Comma: 116 case Key.Comma:
121 output.push('Comma'); break; 117 output.push('Comma');
118 break;
122 case Key.Del: 119 case Key.Del:
123 output.push('Delete'); break; 120 output.push('Delete');
121 break;
124 case Key.Down: 122 case Key.Down:
125 output.push('Down'); break; 123 output.push('Down');
124 break;
126 case Key.End: 125 case Key.End:
127 output.push('End'); break; 126 output.push('End');
127 break;
128 case Key.Home: 128 case Key.Home:
129 output.push('Home'); break; 129 output.push('Home');
130 break;
130 case Key.Ins: 131 case Key.Ins:
131 output.push('Insert'); break; 132 output.push('Insert');
133 break;
132 case Key.Left: 134 case Key.Left:
133 output.push('Left'); break; 135 output.push('Left');
136 break;
134 case Key.MediaNextTrack: 137 case Key.MediaNextTrack:
135 output.push('MediaNextTrack'); break; 138 output.push('MediaNextTrack');
139 break;
136 case Key.MediaPlayPause: 140 case Key.MediaPlayPause:
137 output.push('MediaPlayPause'); break; 141 output.push('MediaPlayPause');
142 break;
138 case Key.MediaPrevTrack: 143 case Key.MediaPrevTrack:
139 output.push('MediaPrevTrack'); break; 144 output.push('MediaPrevTrack');
145 break;
140 case Key.MediaStop: 146 case Key.MediaStop:
141 output.push('MediaStop'); break; 147 output.push('MediaStop');
148 break;
142 case Key.PageDown: 149 case Key.PageDown:
143 output.push('PageDown'); break; 150 output.push('PageDown');
151 break;
144 case Key.PageUp: 152 case Key.PageUp:
145 output.push('PageUp'); break; 153 output.push('PageUp');
154 break;
146 case Key.Period: 155 case Key.Period:
147 output.push('Period'); break; 156 output.push('Period');
157 break;
148 case Key.Right: 158 case Key.Right:
149 output.push('Right'); break; 159 output.push('Right');
160 break;
150 case Key.Space: 161 case Key.Space:
151 output.push('Space'); break; 162 output.push('Space');
163 break;
152 case Key.Tab: 164 case Key.Tab:
153 output.push('Tab'); break; 165 output.push('Tab');
166 break;
154 case Key.Up: 167 case Key.Up:
155 output.push('Up'); break; 168 output.push('Up');
169 break;
156 } 170 }
157 } 171 }
158 } 172 }
159 173
160 return output.join('+'); 174 return output.join('+');
161 } 175 }
162 176
163 /** 177 /**
164 * Returns true if the event has valid modifiers. 178 * Returns true if the event has valid modifiers.
165 * @param {!KeyboardEvent} e The keyboard event to consider. 179 * @param {!KeyboardEvent} e The keyboard event to consider.
166 * @return {boolean} True if the event is valid. 180 * @return {boolean} True if the event is valid.
167 */ 181 */
168 function hasValidModifiers(e) { 182 function hasValidModifiers(e) {
169 switch (getModifierPolicy(e.keyCode)) { 183 switch (getModifierPolicy(e.keyCode)) {
170 case ModifierPolicy.REQUIRED: 184 case ModifierPolicy.REQUIRED:
171 return hasModifier(e, false); 185 return hasModifier(e, false);
172 case ModifierPolicy.NOT_ALLOWED: 186 case ModifierPolicy.NOT_ALLOWED:
173 return !hasModifier(e, true); 187 return !hasModifier(e, true);
174 } 188 }
175 assertNotReached(); 189 assertNotReached();
176 } 190 }
177 191
178 return { 192 return {
179 isValidKeyCode: isValidKeyCode, 193 isValidKeyCode: isValidKeyCode,
180 keystrokeToString: keystrokeToString, 194 keystrokeToString: keystrokeToString,
181 hasValidModifiers: hasValidModifiers, 195 hasValidModifiers: hasValidModifiers,
182 Key: Key, 196 Key: Key,
183 }; 197 };
184 }); 198 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698