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

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/ui/KeyboardShortcut.js

Issue 2493373002: DevTools: rename WebInspector into modules. (Closed)
Patch Set: for bots Created 4 years, 1 month 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 /* 1 /*
2 * Copyright (C) 2009 Apple Inc. All rights reserved. 2 * Copyright (C) 2009 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Google Inc. All rights reserved. 3 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
(...skipping 12 matching lines...) Expand all
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */ 28 */
29 29
30 /** 30 /**
31 * @unrestricted 31 * @unrestricted
32 */ 32 */
33 WebInspector.KeyboardShortcut = class { 33 UI.KeyboardShortcut = class {
34 /** 34 /**
35 * Creates a number encoding keyCode in the lower 8 bits and modifiers mask in the higher 8 bits. 35 * Creates a number encoding keyCode in the lower 8 bits and modifiers mask in the higher 8 bits.
36 * It is useful for matching pressed keys. 36 * It is useful for matching pressed keys.
37 * 37 *
38 * @param {number|string} keyCode The code of the key, or a character "a-z" wh ich is converted to a keyCode value. 38 * @param {number|string} keyCode The code of the key, or a character "a-z" wh ich is converted to a keyCode value.
39 * @param {number=} modifiers Optional list of modifiers passed as additional parameters. 39 * @param {number=} modifiers Optional list of modifiers passed as additional parameters.
40 * @return {number} 40 * @return {number}
41 */ 41 */
42 static makeKey(keyCode, modifiers) { 42 static makeKey(keyCode, modifiers) {
43 if (typeof keyCode === 'string') 43 if (typeof keyCode === 'string')
44 keyCode = keyCode.charCodeAt(0) - (/^[a-z]/.test(keyCode) ? 32 : 0); 44 keyCode = keyCode.charCodeAt(0) - (/^[a-z]/.test(keyCode) ? 32 : 0);
45 modifiers = modifiers || WebInspector.KeyboardShortcut.Modifiers.None; 45 modifiers = modifiers || UI.KeyboardShortcut.Modifiers.None;
46 return WebInspector.KeyboardShortcut._makeKeyFromCodeAndModifiers(keyCode, m odifiers); 46 return UI.KeyboardShortcut._makeKeyFromCodeAndModifiers(keyCode, modifiers);
47 } 47 }
48 48
49 /** 49 /**
50 * @param {?KeyboardEvent} keyboardEvent 50 * @param {?KeyboardEvent} keyboardEvent
51 * @return {number} 51 * @return {number}
52 */ 52 */
53 static makeKeyFromEvent(keyboardEvent) { 53 static makeKeyFromEvent(keyboardEvent) {
54 var modifiers = WebInspector.KeyboardShortcut.Modifiers.None; 54 var modifiers = UI.KeyboardShortcut.Modifiers.None;
55 if (keyboardEvent.shiftKey) 55 if (keyboardEvent.shiftKey)
56 modifiers |= WebInspector.KeyboardShortcut.Modifiers.Shift; 56 modifiers |= UI.KeyboardShortcut.Modifiers.Shift;
57 if (keyboardEvent.ctrlKey) 57 if (keyboardEvent.ctrlKey)
58 modifiers |= WebInspector.KeyboardShortcut.Modifiers.Ctrl; 58 modifiers |= UI.KeyboardShortcut.Modifiers.Ctrl;
59 if (keyboardEvent.altKey) 59 if (keyboardEvent.altKey)
60 modifiers |= WebInspector.KeyboardShortcut.Modifiers.Alt; 60 modifiers |= UI.KeyboardShortcut.Modifiers.Alt;
61 if (keyboardEvent.metaKey) 61 if (keyboardEvent.metaKey)
62 modifiers |= WebInspector.KeyboardShortcut.Modifiers.Meta; 62 modifiers |= UI.KeyboardShortcut.Modifiers.Meta;
63 63
64 // Use either a real or a synthetic keyCode (for events originating from ext ensions). 64 // Use either a real or a synthetic keyCode (for events originating from ext ensions).
65 var keyCode = keyboardEvent.keyCode || keyboardEvent['__keyCode']; 65 var keyCode = keyboardEvent.keyCode || keyboardEvent['__keyCode'];
66 return WebInspector.KeyboardShortcut._makeKeyFromCodeAndModifiers(keyCode, m odifiers); 66 return UI.KeyboardShortcut._makeKeyFromCodeAndModifiers(keyCode, modifiers);
67 } 67 }
68 68
69 /** 69 /**
70 * @param {?KeyboardEvent} keyboardEvent 70 * @param {?KeyboardEvent} keyboardEvent
71 * @return {number} 71 * @return {number}
72 */ 72 */
73 static makeKeyFromEventIgnoringModifiers(keyboardEvent) { 73 static makeKeyFromEventIgnoringModifiers(keyboardEvent) {
74 var keyCode = keyboardEvent.keyCode || keyboardEvent['__keyCode']; 74 var keyCode = keyboardEvent.keyCode || keyboardEvent['__keyCode'];
75 return WebInspector.KeyboardShortcut._makeKeyFromCodeAndModifiers( 75 return UI.KeyboardShortcut._makeKeyFromCodeAndModifiers(
76 keyCode, WebInspector.KeyboardShortcut.Modifiers.None); 76 keyCode, UI.KeyboardShortcut.Modifiers.None);
77 } 77 }
78 78
79 /** 79 /**
80 * @param {(?KeyboardEvent|?MouseEvent)} event 80 * @param {(?KeyboardEvent|?MouseEvent)} event
81 * @return {boolean} 81 * @return {boolean}
82 */ 82 */
83 static eventHasCtrlOrMeta(event) { 83 static eventHasCtrlOrMeta(event) {
84 return WebInspector.isMac() ? event.metaKey && !event.ctrlKey : event.ctrlKe y && !event.metaKey; 84 return Host.isMac() ? event.metaKey && !event.ctrlKey : event.ctrlKey && !ev ent.metaKey;
85 } 85 }
86 86
87 /** 87 /**
88 * @param {!Event} event 88 * @param {!Event} event
89 * @return {boolean} 89 * @return {boolean}
90 */ 90 */
91 static hasNoModifiers(event) { 91 static hasNoModifiers(event) {
92 return !event.ctrlKey && !event.shiftKey && !event.altKey && !event.metaKey; 92 return !event.ctrlKey && !event.shiftKey && !event.altKey && !event.metaKey;
93 } 93 }
94 94
95 /** 95 /**
96 * @param {string|!WebInspector.KeyboardShortcut.Key} key 96 * @param {string|!UI.KeyboardShortcut.Key} key
97 * @param {number=} modifiers 97 * @param {number=} modifiers
98 * @return {!WebInspector.KeyboardShortcut.Descriptor} 98 * @return {!UI.KeyboardShortcut.Descriptor}
99 */ 99 */
100 static makeDescriptor(key, modifiers) { 100 static makeDescriptor(key, modifiers) {
101 return { 101 return {
102 key: WebInspector.KeyboardShortcut.makeKey(typeof key === 'string' ? key : key.code, modifiers), 102 key: UI.KeyboardShortcut.makeKey(typeof key === 'string' ? key : key.code, modifiers),
103 name: WebInspector.KeyboardShortcut.shortcutToString(key, modifiers) 103 name: UI.KeyboardShortcut.shortcutToString(key, modifiers)
104 }; 104 };
105 } 105 }
106 106
107 /** 107 /**
108 * @param {string} shortcut 108 * @param {string} shortcut
109 * @return {?WebInspector.KeyboardShortcut.Descriptor} 109 * @return {?UI.KeyboardShortcut.Descriptor}
110 */ 110 */
111 static makeDescriptorFromBindingShortcut(shortcut) { 111 static makeDescriptorFromBindingShortcut(shortcut) {
112 var parts = shortcut.split(/\+(?!$)/); 112 var parts = shortcut.split(/\+(?!$)/);
113 var modifiers = 0; 113 var modifiers = 0;
114 var keyString; 114 var keyString;
115 for (var i = 0; i < parts.length; ++i) { 115 for (var i = 0; i < parts.length; ++i) {
116 if (typeof WebInspector.KeyboardShortcut.Modifiers[parts[i]] !== 'undefine d') { 116 if (typeof UI.KeyboardShortcut.Modifiers[parts[i]] !== 'undefined') {
117 modifiers |= WebInspector.KeyboardShortcut.Modifiers[parts[i]]; 117 modifiers |= UI.KeyboardShortcut.Modifiers[parts[i]];
118 continue; 118 continue;
119 } 119 }
120 console.assert( 120 console.assert(
121 i === parts.length - 1, 'Only one key other than modifier is allowed i n shortcut <' + shortcut + '>'); 121 i === parts.length - 1, 'Only one key other than modifier is allowed i n shortcut <' + shortcut + '>');
122 keyString = parts[i]; 122 keyString = parts[i];
123 break; 123 break;
124 } 124 }
125 console.assert(keyString, 'Modifiers-only shortcuts are not allowed (encount ered <' + shortcut + '>)'); 125 console.assert(keyString, 'Modifiers-only shortcuts are not allowed (encount ered <' + shortcut + '>)');
126 if (!keyString) 126 if (!keyString)
127 return null; 127 return null;
128 128
129 var key = WebInspector.KeyboardShortcut.Keys[keyString] || WebInspector.Keyb oardShortcut.KeyBindings[keyString]; 129 var key = UI.KeyboardShortcut.Keys[keyString] || UI.KeyboardShortcut.KeyBind ings[keyString];
130 if (key && key.shiftKey) 130 if (key && key.shiftKey)
131 modifiers |= WebInspector.KeyboardShortcut.Modifiers.Shift; 131 modifiers |= UI.KeyboardShortcut.Modifiers.Shift;
132 return WebInspector.KeyboardShortcut.makeDescriptor(key ? key : keyString, m odifiers); 132 return UI.KeyboardShortcut.makeDescriptor(key ? key : keyString, modifiers);
133 } 133 }
134 134
135 /** 135 /**
136 * @param {string|!WebInspector.KeyboardShortcut.Key} key 136 * @param {string|!UI.KeyboardShortcut.Key} key
137 * @param {number=} modifiers 137 * @param {number=} modifiers
138 * @return {string} 138 * @return {string}
139 */ 139 */
140 static shortcutToString(key, modifiers) { 140 static shortcutToString(key, modifiers) {
141 return WebInspector.KeyboardShortcut._modifiersToString(modifiers) + WebInsp ector.KeyboardShortcut._keyName(key); 141 return UI.KeyboardShortcut._modifiersToString(modifiers) + UI.KeyboardShortc ut._keyName(key);
142 } 142 }
143 143
144 /** 144 /**
145 * @param {string|!WebInspector.KeyboardShortcut.Key} key 145 * @param {string|!UI.KeyboardShortcut.Key} key
146 * @return {string} 146 * @return {string}
147 */ 147 */
148 static _keyName(key) { 148 static _keyName(key) {
149 if (typeof key === 'string') 149 if (typeof key === 'string')
150 return key.toUpperCase(); 150 return key.toUpperCase();
151 if (typeof key.name === 'string') 151 if (typeof key.name === 'string')
152 return key.name; 152 return key.name;
153 return key.name[WebInspector.platform()] || key.name.other || ''; 153 return key.name[Host.platform()] || key.name.other || '';
154 } 154 }
155 155
156 /** 156 /**
157 * @param {number} keyCode 157 * @param {number} keyCode
158 * @param {?number} modifiers 158 * @param {?number} modifiers
159 * @return {number} 159 * @return {number}
160 */ 160 */
161 static _makeKeyFromCodeAndModifiers(keyCode, modifiers) { 161 static _makeKeyFromCodeAndModifiers(keyCode, modifiers) {
162 return (keyCode & 255) | (modifiers << 8); 162 return (keyCode & 255) | (modifiers << 8);
163 } 163 }
164 164
165 /** 165 /**
166 * @param {number} key 166 * @param {number} key
167 * @return {!{keyCode: number, modifiers: number}} 167 * @return {!{keyCode: number, modifiers: number}}
168 */ 168 */
169 static keyCodeAndModifiersFromKey(key) { 169 static keyCodeAndModifiersFromKey(key) {
170 return {keyCode: key & 255, modifiers: key >> 8}; 170 return {keyCode: key & 255, modifiers: key >> 8};
171 } 171 }
172 172
173 /** 173 /**
174 * @param {number|undefined} modifiers 174 * @param {number|undefined} modifiers
175 * @return {string} 175 * @return {string}
176 */ 176 */
177 static _modifiersToString(modifiers) { 177 static _modifiersToString(modifiers) {
178 var isMac = WebInspector.isMac(); 178 var isMac = Host.isMac();
179 var m = WebInspector.KeyboardShortcut.Modifiers; 179 var m = UI.KeyboardShortcut.Modifiers;
180 var modifierNames = new Map([ 180 var modifierNames = new Map([
181 [m.Ctrl, isMac ? 'Ctrl\u2004' : 'Ctrl\u200A+\u200A'], [m.Alt, isMac ? '\u2 325\u2004' : 'Alt\u200A+\u200A'], 181 [m.Ctrl, isMac ? 'Ctrl\u2004' : 'Ctrl\u200A+\u200A'], [m.Alt, isMac ? '\u2 325\u2004' : 'Alt\u200A+\u200A'],
182 [m.Shift, isMac ? '\u21e7\u2004' : 'Shift\u200A+\u200A'], [m.Meta, isMac ? '\u2318\u2004' : 'Win\u200A+\u200A'] 182 [m.Shift, isMac ? '\u21e7\u2004' : 'Shift\u200A+\u200A'], [m.Meta, isMac ? '\u2318\u2004' : 'Win\u200A+\u200A']
183 ]); 183 ]);
184 return [m.Meta, m.Ctrl, m.Alt, m.Shift].map(mapModifiers).join(''); 184 return [m.Meta, m.Ctrl, m.Alt, m.Shift].map(mapModifiers).join('');
185 185
186 /** 186 /**
187 * @param {number} m 187 * @param {number} m
188 * @return {string} 188 * @return {string}
189 */ 189 */
190 function mapModifiers(m) { 190 function mapModifiers(m) {
191 return modifiers & m ? /** @type {string} */ (modifierNames.get(m)) : ''; 191 return modifiers & m ? /** @type {string} */ (modifierNames.get(m)) : '';
192 } 192 }
193 } 193 }
194 }; 194 };
195 195
196 /** 196 /**
197 * Constants for encoding modifier key set as a bit mask. 197 * Constants for encoding modifier key set as a bit mask.
198 * @see #_makeKeyFromCodeAndModifiers 198 * @see #_makeKeyFromCodeAndModifiers
199 */ 199 */
200 WebInspector.KeyboardShortcut.Modifiers = { 200 UI.KeyboardShortcut.Modifiers = {
201 None: 0, // Constant for empty modifiers set. 201 None: 0, // Constant for empty modifiers set.
202 Shift: 1, 202 Shift: 1,
203 Ctrl: 2, 203 Ctrl: 2,
204 Alt: 4, 204 Alt: 4,
205 Meta: 8, // Command key on Mac, Win key on other platforms. 205 Meta: 8, // Command key on Mac, Win key on other platforms.
206 get CtrlOrMeta() { 206 get CtrlOrMeta() {
207 // "default" command/ctrl key for platform, Command on Mac, Ctrl on other pl atforms 207 // "default" command/ctrl key for platform, Command on Mac, Ctrl on other pl atforms
208 return WebInspector.isMac() ? this.Meta : this.Ctrl; 208 return Host.isMac() ? this.Meta : this.Ctrl;
209 }, 209 },
210 get ShiftOrOption() { 210 get ShiftOrOption() {
211 // Option on Mac, Shift on other platforms 211 // Option on Mac, Shift on other platforms
212 return WebInspector.isMac() ? this.Alt : this.Shift; 212 return Host.isMac() ? this.Alt : this.Shift;
213 } 213 }
214 }; 214 };
215 215
216 /** @typedef {!{code: number, name: (string|!Object.<string, string>)}} */ 216 /** @typedef {!{code: number, name: (string|!Object.<string, string>)}} */
217 WebInspector.KeyboardShortcut.Key; 217 UI.KeyboardShortcut.Key;
218 218
219 /** @type {!Object.<string, !WebInspector.KeyboardShortcut.Key>} */ 219 /** @type {!Object.<string, !UI.KeyboardShortcut.Key>} */
220 WebInspector.KeyboardShortcut.Keys = { 220 UI.KeyboardShortcut.Keys = {
221 Backspace: {code: 8, name: '\u21a4'}, 221 Backspace: {code: 8, name: '\u21a4'},
222 Tab: {code: 9, name: {mac: '\u21e5', other: 'Tab'}}, 222 Tab: {code: 9, name: {mac: '\u21e5', other: 'Tab'}},
223 Enter: {code: 13, name: {mac: '\u21a9', other: 'Enter'}}, 223 Enter: {code: 13, name: {mac: '\u21a9', other: 'Enter'}},
224 Shift: {code: 16, name: {mac: '\u21e7', other: 'Shift'}}, 224 Shift: {code: 16, name: {mac: '\u21e7', other: 'Shift'}},
225 Ctrl: {code: 17, name: 'Ctrl'}, 225 Ctrl: {code: 17, name: 'Ctrl'},
226 Esc: {code: 27, name: 'Esc'}, 226 Esc: {code: 27, name: 'Esc'},
227 Space: {code: 32, name: 'Space'}, 227 Space: {code: 32, name: 'Space'},
228 PageUp: {code: 33, name: {mac: '\u21de', other: 'PageUp'}}, // also NUM_N ORTH_EAST 228 PageUp: {code: 33, name: {mac: '\u21de', other: 'PageUp'}}, // also NUM_N ORTH_EAST
229 PageDown: {code: 34, name: {mac: '\u21df', other: 'PageDown'}}, // also NUM_S OUTH_EAST 229 PageDown: {code: 34, name: {mac: '\u21df', other: 'PageDown'}}, // also NUM_S OUTH_EAST
230 End: {code: 35, name: {mac: '\u2197', other: 'End'}}, // also NUM_S OUTH_WEST 230 End: {code: 35, name: {mac: '\u2197', other: 'End'}}, // also NUM_S OUTH_WEST
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 Slash: {code: 191, name: '/'}, 262 Slash: {code: 191, name: '/'},
263 QuestionMark: {code: 191, name: '?'}, 263 QuestionMark: {code: 191, name: '?'},
264 Apostrophe: {code: 192, name: '`'}, 264 Apostrophe: {code: 192, name: '`'},
265 Tilde: {code: 192, name: 'Tilde'}, 265 Tilde: {code: 192, name: 'Tilde'},
266 LeftSquareBracket: {code: 219, name: '['}, 266 LeftSquareBracket: {code: 219, name: '['},
267 RightSquareBracket: {code: 221, name: ']'}, 267 RightSquareBracket: {code: 221, name: ']'},
268 Backslash: {code: 220, name: '\\'}, 268 Backslash: {code: 220, name: '\\'},
269 SingleQuote: {code: 222, name: '\''}, 269 SingleQuote: {code: 222, name: '\''},
270 get CtrlOrMeta() { 270 get CtrlOrMeta() {
271 // "default" command/ctrl key for platform, Command on Mac, Ctrl on other pl atforms 271 // "default" command/ctrl key for platform, Command on Mac, Ctrl on other pl atforms
272 return WebInspector.isMac() ? this.Meta : this.Ctrl; 272 return Host.isMac() ? this.Meta : this.Ctrl;
273 }, 273 },
274 }; 274 };
275 275
276 WebInspector.KeyboardShortcut.KeyBindings = {}; 276 UI.KeyboardShortcut.KeyBindings = {};
277 277
278 (function() { 278 (function() {
279 for (var key in WebInspector.KeyboardShortcut.Keys) { 279 for (var key in UI.KeyboardShortcut.Keys) {
280 var descriptor = WebInspector.KeyboardShortcut.Keys[key]; 280 var descriptor = UI.KeyboardShortcut.Keys[key];
281 if (typeof descriptor === 'object' && descriptor['code']) { 281 if (typeof descriptor === 'object' && descriptor['code']) {
282 var name = typeof descriptor['name'] === 'string' ? descriptor['name'] : k ey; 282 var name = typeof descriptor['name'] === 'string' ? descriptor['name'] : k ey;
283 WebInspector.KeyboardShortcut.KeyBindings[name] = descriptor; 283 UI.KeyboardShortcut.KeyBindings[name] = descriptor;
284 } 284 }
285 } 285 }
286 })(); 286 })();
287 287
288 288
289 /** @typedef {!{key: number, name: string}} */ 289 /** @typedef {!{key: number, name: string}} */
290 WebInspector.KeyboardShortcut.Descriptor; 290 UI.KeyboardShortcut.Descriptor;
291 291
292 292
293 WebInspector.KeyboardShortcut.SelectAll = 293 UI.KeyboardShortcut.SelectAll =
294 WebInspector.KeyboardShortcut.makeKey('a', WebInspector.KeyboardShortcut.Mod ifiers.CtrlOrMeta); 294 UI.KeyboardShortcut.makeKey('a', UI.KeyboardShortcut.Modifiers.CtrlOrMeta);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698