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

Side by Side Diff: Source/devtools/front_end/KeyboardShortcut.js

Issue 170273003: DevTools: Implement extensions-based shortcut bindings (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Rebased patch Created 6 years, 9 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 | Annotate | Revision Log
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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 // "default" command/ctrl key for platform, Command on Mac, Ctrl on othe r platforms 49 // "default" command/ctrl key for platform, Command on Mac, Ctrl on othe r platforms
50 return WebInspector.isMac() ? this.Meta : this.Ctrl; 50 return WebInspector.isMac() ? this.Meta : this.Ctrl;
51 } 51 }
52 }; 52 };
53 53
54 /** @typedef {!{code: number, name: (string|!Object.<string, string>)}} */ 54 /** @typedef {!{code: number, name: (string|!Object.<string, string>)}} */
55 WebInspector.KeyboardShortcut.Key; 55 WebInspector.KeyboardShortcut.Key;
56 56
57 /** @type {!Object.<string, !WebInspector.KeyboardShortcut.Key>} */ 57 /** @type {!Object.<string, !WebInspector.KeyboardShortcut.Key>} */
58 WebInspector.KeyboardShortcut.Keys = { 58 WebInspector.KeyboardShortcut.Keys = {
59 Backspace: { code: 8, name: "\u21a4" }, 59 Backspace: { code: 8, name: "\u21a4", shortcut: "backspace" },
pfeldman 2014/03/24 16:54:22 Just use the key for this.
apavlov 2014/03/25 10:03:45 Done.
60 Tab: { code: 9, name: { mac: "\u21e5", other: "Tab" } }, 60 Tab: { code: 9, name: { mac: "\u21e5", other: "Tab" }, shortcut: "tab" },
61 Enter: { code: 13, name: { mac: "\u21a9", other: "Enter" } }, 61 Enter: { code: 13, name: { mac: "\u21a9", other: "Enter" }, shortcut: "enter " },
62 Ctrl: { code: 17, name: "Ctrl" }, 62 Ctrl: { code: 17, name: "Ctrl", shortcut: "Control" },
63 Esc: { code: 27, name: { mac: "\u238b", other: "Esc" } }, 63 Esc: { code: 27, name: { mac: "\u238b", other: "Esc" }, shortcut: "Esc" },
64 Space: { code: 32, name: "Space" }, 64 Space: { code: 32, name: "Space", shortcut: " " },
65 PageUp: { code: 33, name: { mac: "\u21de", other: "PageUp" } }, // als o NUM_NORTH_EAST 65 PageUp: { code: 33, name: { mac: "\u21de", other: "PageUp" }, shortcut: "Pa geUp" }, // also NUM_NORTH_EAST
66 PageDown: { code: 34, name: { mac: "\u21df", other: "PageDown" } }, // als o NUM_SOUTH_EAST 66 PageDown: { code: 34, name: { mac: "\u21df", other: "PageDown" }, shortcut: "PageDown" }, // also NUM_SOUTH_EAST
67 End: { code: 35, name: { mac: "\u2197", other: "End" } }, // als o NUM_SOUTH_WEST 67 End: { code: 35, name: { mac: "\u2197", other: "End" }, shortcut: "End" }, // also NUM_SOUTH_WEST
68 Home: { code: 36, name: { mac: "\u2196", other: "Home" } }, // als o NUM_NORTH_WEST 68 Home: { code: 36, name: { mac: "\u2196", other: "Home" }, shortcut: "Home" } , // also NUM_NORTH_WEST
69 Left: { code: 37, name: "\u2190" }, // also NUM_WEST 69 Left: { code: 37, name: "\u2190", shortcut: "Left" }, // also NUM_ WEST
70 Up: { code: 38, name: "\u2191" }, // also NUM_NORTH 70 Up: { code: 38, name: "\u2191", shortcut: "Up" }, // also NUM_NO RTH
71 Right: { code: 39, name: "\u2192" }, // also NUM_EAST 71 Right: { code: 39, name: "\u2192", shortcut: "Right" }, // also NUM _EAST
72 Down: { code: 40, name: "\u2193" }, // also NUM_SOUTH 72 Down: { code: 40, name: "\u2193", shortcut: "Down" }, // also NUM_ SOUTH
73 Delete: { code: 46, name: "Del" }, 73 Delete: { code: 46, name: "Del", shortcut: "Delete" },
74 Zero: { code: 48, name: "0" }, 74 Zero: { code: 48, name: "0" },
75 H: { code: 72, name: "H" }, 75 H: { code: 72, name: "H" },
76 Meta: { code: 91, name: "Meta" }, 76 Meta: { code: 91, name: "Meta" },
77 F1: { code: 112, name: "F1" }, 77 F1: { code: 112, name: "F1" },
78 F2: { code: 113, name: "F2" }, 78 F2: { code: 113, name: "F2" },
79 F3: { code: 114, name: "F3" }, 79 F3: { code: 114, name: "F3" },
80 F4: { code: 115, name: "F4" }, 80 F4: { code: 115, name: "F4" },
81 F5: { code: 116, name: "F5" }, 81 F5: { code: 116, name: "F5" },
82 F6: { code: 117, name: "F6" }, 82 F6: { code: 117, name: "F6" },
83 F7: { code: 118, name: "F7" }, 83 F7: { code: 118, name: "F7" },
84 F8: { code: 119, name: "F8" }, 84 F8: { code: 119, name: "F8" },
85 F9: { code: 120, name: "F9" }, 85 F9: { code: 120, name: "F9" },
86 F10: { code: 121, name: "F10" }, 86 F10: { code: 121, name: "F10" },
87 F11: { code: 122, name: "F11" }, 87 F11: { code: 122, name: "F11" },
88 F12: { code: 123, name: "F12" }, 88 F12: { code: 123, name: "F12" },
89 Semicolon: { code: 186, name: ";" }, 89 Semicolon: { code: 186, name: ";" },
90 Plus: { code: 187, name: "+" }, 90 Plus: { code: 187, name: "+" },
91 Comma: { code: 188, name: "," }, 91 Comma: { code: 188, name: "," },
92 Minus: { code: 189, name: "-" }, 92 Minus: { code: 189, name: "-" },
93 Period: { code: 190, name: "." }, 93 Period: { code: 190, name: "." },
94 Slash: { code: 191, name: "/" }, 94 Slash: { code: 191, name: "/" },
95 QuestionMark: { code: 191, name: "?" }, 95 QuestionMark: { code: 191, name: "?" },
96 Apostrophe: { code: 192, name: "`" }, 96 Apostrophe: { code: 192, name: "`" },
97 Tilde: { code: 192, name: "Tilde" }, 97 Tilde: { code: 192, name: "Tilde", shortcut: "~" },
98 Backslash: { code: 220, name: "\\" }, 98 Backslash: { code: 220, name: "\\" },
99 SingleQuote: { code: 222, name: "\'" }, 99 SingleQuote: { code: 222, name: "\'" },
100 get CtrlOrMeta() 100 get CtrlOrMeta()
101 { 101 {
102 // "default" command/ctrl key for platform, Command on Mac, Ctrl on othe r platforms 102 // "default" command/ctrl key for platform, Command on Mac, Ctrl on othe r platforms
103 return WebInspector.isMac() ? this.Meta : this.Ctrl; 103 return WebInspector.isMac() ? this.Meta : this.Ctrl;
104 }, 104 },
105 }; 105 };
106 106
107 WebInspector.KeyboardShortcut.KeyBindings = {};
108
109 (function() {
110 for (var key in WebInspector.KeyboardShortcut.Keys) {
111 var descriptor = WebInspector.KeyboardShortcut.Keys[key];
112 if (typeof descriptor === "object" && descriptor["code"])
113 WebInspector.KeyboardShortcut.KeyBindings[descriptor["shortcut"] || descriptor["name"]] = { code: descriptor["code"] };
114 }
115 })();
116
107 /** 117 /**
108 * Creates a number encoding keyCode in the lower 8 bits and modifiers mask in t he higher 8 bits. 118 * Creates a number encoding keyCode in the lower 8 bits and modifiers mask in t he higher 8 bits.
109 * It is useful for matching pressed keys. 119 * It is useful for matching pressed keys.
110 * 120 *
111 * @param {number|string} keyCode The code of the key, or a character "a-z" whic h is converted to a keyCode value. 121 * @param {number|string} keyCode The code of the key, or a character "a-z" whic h is converted to a keyCode value.
112 * @param {number=} modifiers Optional list of modifiers passed as additional pa ramerters. 122 * @param {number=} modifiers Optional list of modifiers passed as additional pa rameters.
113 * @return {number} 123 * @return {number}
114 */ 124 */
115 WebInspector.KeyboardShortcut.makeKey = function(keyCode, modifiers) 125 WebInspector.KeyboardShortcut.makeKey = function(keyCode, modifiers)
116 { 126 {
117 if (typeof keyCode === "string") 127 if (typeof keyCode === "string")
118 keyCode = keyCode.charCodeAt(0) - 32; 128 keyCode = keyCode.charCodeAt(0) - (/^[a-z]/.test(keyCode) ? 32 : 0);
119 modifiers = modifiers || WebInspector.KeyboardShortcut.Modifiers.None; 129 modifiers = modifiers || WebInspector.KeyboardShortcut.Modifiers.None;
120 return WebInspector.KeyboardShortcut._makeKeyFromCodeAndModifiers(keyCode, m odifiers); 130 return WebInspector.KeyboardShortcut._makeKeyFromCodeAndModifiers(keyCode, m odifiers);
121 } 131 }
122 132
123 /** 133 /**
124 * @param {?KeyboardEvent} keyboardEvent 134 * @param {?KeyboardEvent} keyboardEvent
125 * @return {number} 135 * @return {number}
126 */ 136 */
127 WebInspector.KeyboardShortcut.makeKeyFromEvent = function(keyboardEvent) 137 WebInspector.KeyboardShortcut.makeKeyFromEvent = function(keyboardEvent)
128 { 138 {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 */ 176 */
167 WebInspector.KeyboardShortcut.makeDescriptor = function(key, modifiers) 177 WebInspector.KeyboardShortcut.makeDescriptor = function(key, modifiers)
168 { 178 {
169 return { 179 return {
170 key: WebInspector.KeyboardShortcut.makeKey(typeof key === "string" ? key : key.code, modifiers), 180 key: WebInspector.KeyboardShortcut.makeKey(typeof key === "string" ? key : key.code, modifiers),
171 name: WebInspector.KeyboardShortcut.shortcutToString(key, modifiers) 181 name: WebInspector.KeyboardShortcut.shortcutToString(key, modifiers)
172 }; 182 };
173 } 183 }
174 184
175 /** 185 /**
186 * @param {string} shortcut
187 * @return {number}
188 */
189 WebInspector.KeyboardShortcut.makeKeyFromBindingShortcut = function(shortcut)
190 {
191 var parts = shortcut.split(/\+(?!$)/);
192 var modifiers = 0;
193 for (var i = 0; i < parts.length; ++i) {
194 if (typeof WebInspector.KeyboardShortcut.Modifiers[parts[i]] !== "undefi ned") {
195 modifiers |= WebInspector.KeyboardShortcut.Modifiers[parts[i]];
196 continue;
197 }
198 console.assert(i === parts.length - 1, "Modifiers-only shortcuts are not allowed (encountered <" + shortcut + ">)");
199 var key = WebInspector.KeyboardShortcut.Keys[parts[i]] || WebInspector.K eyboardShortcut.KeyBindings[parts[i]];
200 if (key && key.shiftKey)
201 modifiers |= WebInspector.KeyboardShortcut.Modifiers.Shift;
202 return WebInspector.KeyboardShortcut.makeKey(key ? key.code : parts[i].t oLowerCase(), modifiers)
203 }
204 console.assert(false);
205 return 0;
206 }
207
208 /**
176 * @param {string|!WebInspector.KeyboardShortcut.Key} key 209 * @param {string|!WebInspector.KeyboardShortcut.Key} key
177 * @param {number=} modifiers 210 * @param {number=} modifiers
178 * @return {string} 211 * @return {string}
179 */ 212 */
180 WebInspector.KeyboardShortcut.shortcutToString = function(key, modifiers) 213 WebInspector.KeyboardShortcut.shortcutToString = function(key, modifiers)
181 { 214 {
182 return WebInspector.KeyboardShortcut._modifiersToString(modifiers) + WebInsp ector.KeyboardShortcut._keyName(key); 215 return WebInspector.KeyboardShortcut._modifiersToString(modifiers) + WebInsp ector.KeyboardShortcut._keyName(key);
183 } 216 }
184 217
185 /** 218 /**
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Alt) 256 if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Alt)
224 res += isMac ? optKey : "Alt + "; 257 res += isMac ? optKey : "Alt + ";
225 if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Shift) 258 if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Shift)
226 res += isMac ? shiftKey : "Shift + "; 259 res += isMac ? shiftKey : "Shift + ";
227 if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Meta) 260 if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Meta)
228 res += isMac ? cmdKey : "Win + "; 261 res += isMac ? cmdKey : "Win + ";
229 262
230 return res; 263 return res;
231 }; 264 };
232 265
266 /**
267 * @param {!KeyboardEvent} event
268 */
269 WebInspector.KeyboardShortcut.handleShortcut = function(event)
270 {
271 var key = WebInspector.KeyboardShortcut.makeKeyFromEvent(event);
272 var extensions = WebInspector.KeyboardShortcut._keysToActionExtensions[key];
273 if (!extensions)
274 return;
275
276 function handler(extension)
277 {
278 var result = extension.instance().handleAction(event);
279 if (result)
280 event.consume(true);
281 delete WebInspector.KeyboardShortcut._pendingActionTimer;
282 return result;
283 }
284
285 for (var i = 0; i < extensions.length; ++i) {
286 var ident = event.keyIdentifier;
287 if (/^F\d+|Control|Shift|Alt|Meta|Win|U\+001B$/.test(ident) || event.ctr lKey || event.altKey || event.metaKey) {
288 if (handler(extensions[i]))
289 return;
290 } else {
291 WebInspector.KeyboardShortcut._pendingActionTimer = setTimeout(handl er.bind(null, extensions[i]), 0);
292 break;
293 }
294 }
295 }
296
233 WebInspector.KeyboardShortcut.SelectAll = WebInspector.KeyboardShortcut.makeKey( "a", WebInspector.KeyboardShortcut.Modifiers.CtrlOrMeta); 297 WebInspector.KeyboardShortcut.SelectAll = WebInspector.KeyboardShortcut.makeKey( "a", WebInspector.KeyboardShortcut.Modifiers.CtrlOrMeta);
298
299 WebInspector.KeyboardShortcut._onkeypress = function(event)
pfeldman 2014/03/24 16:54:22 _onKeyPress
apavlov 2014/03/25 10:03:45 Done.
300 {
301 if (!WebInspector.KeyboardShortcut._pendingActionTimer)
302 return;
303
304 var target = event.target;
305 if (WebInspector.isBeingEdited(event.target)) {
306 clearTimeout(WebInspector.KeyboardShortcut._pendingActionTimer);
307 delete WebInspector.KeyboardShortcut._pendingActionTimer;
308 }
309 }
310
311 WebInspector.KeyboardShortcut.registerActions = function()
312 {
313 /**
314 * @param {string=} platformsString
315 * @return {boolean}
316 */
317 function platformMatches(platformsString)
318 {
319 if (!platformsString)
320 return true;
321 var platforms = platformsString.split(",");
322 var isMatch = false;
323 var currentPlatform = WebInspector.platform();
324 for (var i = 0; !isMatch && i < platforms.length; ++i)
325 isMatch |= (platforms[i] === currentPlatform);
pfeldman 2014/03/24 16:54:22 There is no logical |=
apavlov 2014/03/25 10:03:45 Done.
326 return isMatch;
327 }
328
329 /**
330 * @param {!WebInspector.ModuleManager.Extension} extension
331 */
332 function registerBindings(extension)
333 {
334 var bindings = extension.descriptor().bindings;
335 if (!bindings)
336 return;
337 for (var i = 0; i < bindings.length; ++i) {
pfeldman 2014/03/24 16:54:22 bindings &&
apavlov 2014/03/25 10:03:45 Done.
338 if (!platformMatches(bindings[i].platform))
339 continue;
340 var key = WebInspector.KeyboardShortcut.makeKeyFromBindingShortcut(b indings[i].shortcut);
341 if (!key)
342 continue;
343 if (WebInspector.KeyboardShortcut._keysToActionExtensions[key])
344 WebInspector.KeyboardShortcut._keysToActionExtensions[key].push( extension);
345 else
346 WebInspector.KeyboardShortcut._keysToActionExtensions[key] = [ex tension];
347 }
348 }
349
350 document.addEventListener("keypress", WebInspector.KeyboardShortcut._onkeypr ess, true);
pfeldman 2014/03/24 16:54:22 Move this above.
apavlov 2014/03/25 10:03:45 Done.
351 WebInspector.KeyboardShortcut._keysToActionExtensions = {};
352 var extensions = WebInspector.moduleManager.extensions(WebInspector.ActionDe legate);
353 extensions.forEach(registerBindings);
354 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698