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

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: Created 6 years, 10 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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 Tilde: { code: 192, name: "Tilde" }, 97 Tilde: { code: 192, name: "Tilde" },
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 = {
pfeldman 2014/02/21 16:29:16 Lets not copy these definitions.
apavlov 2014/02/24 09:57:57 OK. But do we want to support "?" or replace it by
pfeldman 2014/03/13 05:24:02 I don't really see how these correlate. I was just
apavlov 2014/03/17 13:40:59 Okay, I'll build up the required map automatically
108 // These must be in sync with WebInspector.KeyboardShortcut.Keys.
109 ";": { code: 186 },
110 "+": { code: 187, shiftKey: true},
111 ",": { code: 188 },
112 "-": { code: 189 },
113 ".": { code: 190 },
114 "/": { code: 191 },
115 "?": { code: 191, shiftKey: true },
116 "`": { code: 192 },
117 "~": { code: 192, shiftKey: true },
118 "\\": { code: 220 },
119 "'": { code: 222 }
120 }
121
107 /** 122 /**
108 * Creates a number encoding keyCode in the lower 8 bits and modifiers mask in t he higher 8 bits. 123 * 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. 124 * It is useful for matching pressed keys.
110 * 125 *
111 * @param {number|string} keyCode The code of the key, or a character "a-z" whic h is converted to a keyCode value. 126 * @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. 127 * @param {number=} modifiers Optional list of modifiers passed as additional pa ramerters.
113 * @return {number} 128 * @return {number}
114 */ 129 */
115 WebInspector.KeyboardShortcut.makeKey = function(keyCode, modifiers) 130 WebInspector.KeyboardShortcut.makeKey = function(keyCode, modifiers)
116 { 131 {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 */ 181 */
167 WebInspector.KeyboardShortcut.makeDescriptor = function(key, modifiers) 182 WebInspector.KeyboardShortcut.makeDescriptor = function(key, modifiers)
168 { 183 {
169 return { 184 return {
170 key: WebInspector.KeyboardShortcut.makeKey(typeof key === "string" ? key : key.code, modifiers), 185 key: WebInspector.KeyboardShortcut.makeKey(typeof key === "string" ? key : key.code, modifiers),
171 name: WebInspector.KeyboardShortcut.shortcutToString(key, modifiers) 186 name: WebInspector.KeyboardShortcut.shortcutToString(key, modifiers)
172 }; 187 };
173 } 188 }
174 189
175 /** 190 /**
191 * @param {string} shortcut
192 * @return {number}
193 */
194 WebInspector.KeyboardShortcut.makeKeyFromBindingShortcut = function(shortcut)
195 {
196 var parts = shortcut.split(/\+(?!$)/);
197 var modifiers = 0;
198 for (var i = 0; i < parts.length; ++i) {
199 if (typeof WebInspector.KeyboardShortcut.Modifiers[parts[i]] !== "undefi ned") {
200 modifiers |= WebInspector.KeyboardShortcut.Modifiers[parts[i]];
201 continue;
202 }
203 console.assert(i === parts.length - 1, "Modifiers-only shortcuts are not allowed (encountered <" + shortcut + ">)");
204 var key = WebInspector.KeyboardShortcut.Keys[parts[i]] || WebInspector.K eyboardShortcut.KeyBindings[parts[i]];
205 if (key && key.shiftKey)
206 modifiers |= WebInspector.KeyboardShortcut.Modifiers.Shift;
207 return WebInspector.KeyboardShortcut.makeKey(key ? key.code : parts[i].t oLowerCase(), modifiers)
208 }
209 console.assert(false);
210 return 0;
211 }
212
213 /**
176 * @param {string|!WebInspector.KeyboardShortcut.Key} key 214 * @param {string|!WebInspector.KeyboardShortcut.Key} key
177 * @param {number=} modifiers 215 * @param {number=} modifiers
178 * @return {string} 216 * @return {string}
179 */ 217 */
180 WebInspector.KeyboardShortcut.shortcutToString = function(key, modifiers) 218 WebInspector.KeyboardShortcut.shortcutToString = function(key, modifiers)
181 { 219 {
182 return WebInspector.KeyboardShortcut._modifiersToString(modifiers) + WebInsp ector.KeyboardShortcut._keyName(key); 220 return WebInspector.KeyboardShortcut._modifiersToString(modifiers) + WebInsp ector.KeyboardShortcut._keyName(key);
183 } 221 }
184 222
185 /** 223 /**
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Alt) 261 if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Alt)
224 res += isMac ? optKey : "Alt + "; 262 res += isMac ? optKey : "Alt + ";
225 if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Shift) 263 if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Shift)
226 res += isMac ? shiftKey : "Shift + "; 264 res += isMac ? shiftKey : "Shift + ";
227 if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Meta) 265 if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Meta)
228 res += isMac ? cmdKey : "Win + "; 266 res += isMac ? cmdKey : "Win + ";
229 267
230 return res; 268 return res;
231 }; 269 };
232 270
271 /**
272 * @param {!KeyboardEvent} event
273 */
274 WebInspector.KeyboardShortcut.handleShortcut = function(event)
275 {
276 var key = WebInspector.KeyboardShortcut.makeKeyFromEvent(event);
277 var extensions = WebInspector.KeyboardShortcut._keysToActionExtensions[key];
278 if (!extensions)
279 return;
280
281 function handler(extension)
282 {
283 var result = extension.instance().handleAction(event);
284 if (result)
285 event.consume(true);
286 delete WebInspector.KeyboardShortcut._pendingActionTimer;
287 return result;
288 }
289
290 for (var i = 0; i < extensions.length; ++i) {
291 var ident = event.keyIdentifier;
292 if (/^F\d+|Control|Shift|Alt|U+001B$/.test(ident) || event.ctrlKey || ev ent.altKey || event.metaKey) {
293 if (handler(extensions[i]))
294 return;
295 } else {
296 WebInspector.KeyboardShortcut._pendingActionTimer = setTimeout(handl er.bind(null, extensions[i]), 0);
297 break;
298 }
299 }
300 }
301
233 WebInspector.KeyboardShortcut.SelectAll = WebInspector.KeyboardShortcut.makeKey( "a", WebInspector.KeyboardShortcut.Modifiers.CtrlOrMeta); 302 WebInspector.KeyboardShortcut.SelectAll = WebInspector.KeyboardShortcut.makeKey( "a", WebInspector.KeyboardShortcut.Modifiers.CtrlOrMeta);
303
304 WebInspector.KeyboardShortcut._onkeypress = function(event)
305 {
306 if (!WebInspector.KeyboardShortcut._pendingActionTimer)
307 return;
308
309 var target = event.target;
310 if (WebInspector.isBeingEdited(event.target)) {
311 clearTimeout(WebInspector.KeyboardShortcut._pendingActionTimer);
312 delete WebInspector.KeyboardShortcut._pendingActionTimer;
313 }
314 }
315
316 WebInspector.KeyboardShortcut.loadBindings = function()
pfeldman 2014/02/21 16:29:16 What is this loading?
apavlov 2014/02/24 09:57:57 It is called from inspector.js once modules have b
pfeldman 2014/03/13 05:24:02 It should be called differently then - loadBinding
317 {
318 /**
319 * @param {string=} platformsString
320 * @return {boolean}
321 */
322 function platformMatches(platformsString)
323 {
324 if (!platformsString)
325 return true;
326 var platforms = platformsString.split(",");
327 var isMatch = false;
328 for (var i = 0; i < platforms.length; ++i) {
329 var currentPlatform = platforms[i];
330 var negate = false;
331 if (currentPlatform.charAt(0) === "!") {
332 currentPlatform = currentPlatform.substring(1);
333 negate = true;
334 }
335 var match = WebInspector.platform() === currentPlatform;
336 isMatch |= negate ? !match : match;
337 }
338 return isMatch;
339 }
340
341 /**
342 * @param {!WebInspector.ModuleManager.Extension} extension
343 */
344 function registerBindings(extension)
345 {
346 var bindings = extension.descriptor().bindings;
347 if (!bindings)
348 return;
349 for (var i = 0; i < bindings.length; ++i) {
350 if (!platformMatches(bindings[i].platform))
351 continue;
352 var key = WebInspector.KeyboardShortcut.makeKeyFromBindingShortcut(b indings[i].shortcut);
353 if (key) {
354 if (WebInspector.KeyboardShortcut._keysToActionExtensions[key])
355 WebInspector.KeyboardShortcut._keysToActionExtensions[key].p ush(extension);
356 else
357 WebInspector.KeyboardShortcut._keysToActionExtensions[key] = [extension];
358 }
359 }
360 }
361
362 document.addEventListener("keypress", WebInspector.KeyboardShortcut._onkeypr ess, true);
363 WebInspector.KeyboardShortcut._keysToActionExtensions = {};
364 var extensions = WebInspector.moduleManager.extensions(WebInspector.ActionDe legate);
365 extensions.forEach(registerBindings);
366 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698