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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: Source/devtools/front_end/KeyboardShortcut.js
diff --git a/Source/devtools/front_end/KeyboardShortcut.js b/Source/devtools/front_end/KeyboardShortcut.js
index 47e120d49d800077ec68340f069dac8f45d26001..328423ec174a28de433454f0f53f75d9ea75c79f 100644
--- a/Source/devtools/front_end/KeyboardShortcut.js
+++ b/Source/devtools/front_end/KeyboardShortcut.js
@@ -104,6 +104,21 @@ WebInspector.KeyboardShortcut.Keys = {
},
};
+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
+ // These must be in sync with WebInspector.KeyboardShortcut.Keys.
+ ";": { code: 186 },
+ "+": { code: 187, shiftKey: true},
+ ",": { code: 188 },
+ "-": { code: 189 },
+ ".": { code: 190 },
+ "/": { code: 191 },
+ "?": { code: 191, shiftKey: true },
+ "`": { code: 192 },
+ "~": { code: 192, shiftKey: true },
+ "\\": { code: 220 },
+ "'": { code: 222 }
+}
+
/**
* Creates a number encoding keyCode in the lower 8 bits and modifiers mask in the higher 8 bits.
* It is useful for matching pressed keys.
@@ -173,6 +188,29 @@ WebInspector.KeyboardShortcut.makeDescriptor = function(key, modifiers)
}
/**
+ * @param {string} shortcut
+ * @return {number}
+ */
+WebInspector.KeyboardShortcut.makeKeyFromBindingShortcut = function(shortcut)
+{
+ var parts = shortcut.split(/\+(?!$)/);
+ var modifiers = 0;
+ for (var i = 0; i < parts.length; ++i) {
+ if (typeof WebInspector.KeyboardShortcut.Modifiers[parts[i]] !== "undefined") {
+ modifiers |= WebInspector.KeyboardShortcut.Modifiers[parts[i]];
+ continue;
+ }
+ console.assert(i === parts.length - 1, "Modifiers-only shortcuts are not allowed (encountered <" + shortcut + ">)");
+ var key = WebInspector.KeyboardShortcut.Keys[parts[i]] || WebInspector.KeyboardShortcut.KeyBindings[parts[i]];
+ if (key && key.shiftKey)
+ modifiers |= WebInspector.KeyboardShortcut.Modifiers.Shift;
+ return WebInspector.KeyboardShortcut.makeKey(key ? key.code : parts[i].toLowerCase(), modifiers)
+ }
+ console.assert(false);
+ return 0;
+}
+
+/**
* @param {string|!WebInspector.KeyboardShortcut.Key} key
* @param {number=} modifiers
* @return {string}
@@ -230,4 +268,99 @@ WebInspector.KeyboardShortcut._modifiersToString = function(modifiers)
return res;
};
+/**
+ * @param {!KeyboardEvent} event
+ */
+WebInspector.KeyboardShortcut.handleShortcut = function(event)
+{
+ var key = WebInspector.KeyboardShortcut.makeKeyFromEvent(event);
+ var extensions = WebInspector.KeyboardShortcut._keysToActionExtensions[key];
+ if (!extensions)
+ return;
+
+ function handler(extension)
+ {
+ var result = extension.instance().handleAction(event);
+ if (result)
+ event.consume(true);
+ delete WebInspector.KeyboardShortcut._pendingActionTimer;
+ return result;
+ }
+
+ for (var i = 0; i < extensions.length; ++i) {
+ var ident = event.keyIdentifier;
+ if (/^F\d+|Control|Shift|Alt|U+001B$/.test(ident) || event.ctrlKey || event.altKey || event.metaKey) {
+ if (handler(extensions[i]))
+ return;
+ } else {
+ WebInspector.KeyboardShortcut._pendingActionTimer = setTimeout(handler.bind(null, extensions[i]), 0);
+ break;
+ }
+ }
+}
+
WebInspector.KeyboardShortcut.SelectAll = WebInspector.KeyboardShortcut.makeKey("a", WebInspector.KeyboardShortcut.Modifiers.CtrlOrMeta);
+
+WebInspector.KeyboardShortcut._onkeypress = function(event)
+{
+ if (!WebInspector.KeyboardShortcut._pendingActionTimer)
+ return;
+
+ var target = event.target;
+ if (WebInspector.isBeingEdited(event.target)) {
+ clearTimeout(WebInspector.KeyboardShortcut._pendingActionTimer);
+ delete WebInspector.KeyboardShortcut._pendingActionTimer;
+ }
+}
+
+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
+{
+ /**
+ * @param {string=} platformsString
+ * @return {boolean}
+ */
+ function platformMatches(platformsString)
+ {
+ if (!platformsString)
+ return true;
+ var platforms = platformsString.split(",");
+ var isMatch = false;
+ for (var i = 0; i < platforms.length; ++i) {
+ var currentPlatform = platforms[i];
+ var negate = false;
+ if (currentPlatform.charAt(0) === "!") {
+ currentPlatform = currentPlatform.substring(1);
+ negate = true;
+ }
+ var match = WebInspector.platform() === currentPlatform;
+ isMatch |= negate ? !match : match;
+ }
+ return isMatch;
+ }
+
+ /**
+ * @param {!WebInspector.ModuleManager.Extension} extension
+ */
+ function registerBindings(extension)
+ {
+ var bindings = extension.descriptor().bindings;
+ if (!bindings)
+ return;
+ for (var i = 0; i < bindings.length; ++i) {
+ if (!platformMatches(bindings[i].platform))
+ continue;
+ var key = WebInspector.KeyboardShortcut.makeKeyFromBindingShortcut(bindings[i].shortcut);
+ if (key) {
+ if (WebInspector.KeyboardShortcut._keysToActionExtensions[key])
+ WebInspector.KeyboardShortcut._keysToActionExtensions[key].push(extension);
+ else
+ WebInspector.KeyboardShortcut._keysToActionExtensions[key] = [extension];
+ }
+ }
+ }
+
+ document.addEventListener("keypress", WebInspector.KeyboardShortcut._onkeypress, true);
+ WebInspector.KeyboardShortcut._keysToActionExtensions = {};
+ var extensions = WebInspector.moduleManager.extensions(WebInspector.ActionDelegate);
+ extensions.forEach(registerBindings);
+}

Powered by Google App Engine
This is Rietveld 408576698