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

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: 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 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..9bfd39adc667bf17285202d39d969f04e599403b 100644
--- a/Source/devtools/front_end/KeyboardShortcut.js
+++ b/Source/devtools/front_end/KeyboardShortcut.js
@@ -56,21 +56,21 @@ WebInspector.KeyboardShortcut.Key;
/** @type {!Object.<string, !WebInspector.KeyboardShortcut.Key>} */
WebInspector.KeyboardShortcut.Keys = {
- Backspace: { code: 8, name: "\u21a4" },
- Tab: { code: 9, name: { mac: "\u21e5", other: "Tab" } },
- Enter: { code: 13, name: { mac: "\u21a9", other: "Enter" } },
- Ctrl: { code: 17, name: "Ctrl" },
- Esc: { code: 27, name: { mac: "\u238b", other: "Esc" } },
- Space: { code: 32, name: "Space" },
- PageUp: { code: 33, name: { mac: "\u21de", other: "PageUp" } }, // also NUM_NORTH_EAST
- PageDown: { code: 34, name: { mac: "\u21df", other: "PageDown" } }, // also NUM_SOUTH_EAST
- End: { code: 35, name: { mac: "\u2197", other: "End" } }, // also NUM_SOUTH_WEST
- Home: { code: 36, name: { mac: "\u2196", other: "Home" } }, // also NUM_NORTH_WEST
- Left: { code: 37, name: "\u2190" }, // also NUM_WEST
- Up: { code: 38, name: "\u2191" }, // also NUM_NORTH
- Right: { code: 39, name: "\u2192" }, // also NUM_EAST
- Down: { code: 40, name: "\u2193" }, // also NUM_SOUTH
- Delete: { code: 46, name: "Del" },
+ 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.
+ Tab: { code: 9, name: { mac: "\u21e5", other: "Tab" }, shortcut: "tab" },
+ Enter: { code: 13, name: { mac: "\u21a9", other: "Enter" }, shortcut: "enter" },
+ Ctrl: { code: 17, name: "Ctrl", shortcut: "Control" },
+ Esc: { code: 27, name: { mac: "\u238b", other: "Esc" }, shortcut: "Esc" },
+ Space: { code: 32, name: "Space", shortcut: " " },
+ PageUp: { code: 33, name: { mac: "\u21de", other: "PageUp" }, shortcut: "PageUp" }, // also NUM_NORTH_EAST
+ PageDown: { code: 34, name: { mac: "\u21df", other: "PageDown" }, shortcut: "PageDown" }, // also NUM_SOUTH_EAST
+ End: { code: 35, name: { mac: "\u2197", other: "End" }, shortcut: "End" }, // also NUM_SOUTH_WEST
+ Home: { code: 36, name: { mac: "\u2196", other: "Home" }, shortcut: "Home" }, // also NUM_NORTH_WEST
+ Left: { code: 37, name: "\u2190", shortcut: "Left" }, // also NUM_WEST
+ Up: { code: 38, name: "\u2191", shortcut: "Up" }, // also NUM_NORTH
+ Right: { code: 39, name: "\u2192", shortcut: "Right" }, // also NUM_EAST
+ Down: { code: 40, name: "\u2193", shortcut: "Down" }, // also NUM_SOUTH
+ Delete: { code: 46, name: "Del", shortcut: "Delete" },
Zero: { code: 48, name: "0" },
H: { code: 72, name: "H" },
Meta: { code: 91, name: "Meta" },
@@ -94,7 +94,7 @@ WebInspector.KeyboardShortcut.Keys = {
Slash: { code: 191, name: "/" },
QuestionMark: { code: 191, name: "?" },
Apostrophe: { code: 192, name: "`" },
- Tilde: { code: 192, name: "Tilde" },
+ Tilde: { code: 192, name: "Tilde", shortcut: "~" },
Backslash: { code: 220, name: "\\" },
SingleQuote: { code: 222, name: "\'" },
get CtrlOrMeta()
@@ -104,18 +104,28 @@ WebInspector.KeyboardShortcut.Keys = {
},
};
+WebInspector.KeyboardShortcut.KeyBindings = {};
+
+(function() {
+ for (var key in WebInspector.KeyboardShortcut.Keys) {
+ var descriptor = WebInspector.KeyboardShortcut.Keys[key];
+ if (typeof descriptor === "object" && descriptor["code"])
+ WebInspector.KeyboardShortcut.KeyBindings[descriptor["shortcut"] || descriptor["name"]] = { code: descriptor["code"] };
+ }
+})();
+
/**
* 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.
*
* @param {number|string} keyCode The code of the key, or a character "a-z" which is converted to a keyCode value.
- * @param {number=} modifiers Optional list of modifiers passed as additional paramerters.
+ * @param {number=} modifiers Optional list of modifiers passed as additional parameters.
* @return {number}
*/
WebInspector.KeyboardShortcut.makeKey = function(keyCode, modifiers)
{
if (typeof keyCode === "string")
- keyCode = keyCode.charCodeAt(0) - 32;
+ keyCode = keyCode.charCodeAt(0) - (/^[a-z]/.test(keyCode) ? 32 : 0);
modifiers = modifiers || WebInspector.KeyboardShortcut.Modifiers.None;
return WebInspector.KeyboardShortcut._makeKeyFromCodeAndModifiers(keyCode, modifiers);
}
@@ -173,6 +183,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 +263,92 @@ 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|Meta|Win|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)
pfeldman 2014/03/24 16:54:22 _onKeyPress
apavlov 2014/03/25 10:03:45 Done.
+{
+ if (!WebInspector.KeyboardShortcut._pendingActionTimer)
+ return;
+
+ var target = event.target;
+ if (WebInspector.isBeingEdited(event.target)) {
+ clearTimeout(WebInspector.KeyboardShortcut._pendingActionTimer);
+ delete WebInspector.KeyboardShortcut._pendingActionTimer;
+ }
+}
+
+WebInspector.KeyboardShortcut.registerActions = function()
+{
+ /**
+ * @param {string=} platformsString
+ * @return {boolean}
+ */
+ function platformMatches(platformsString)
+ {
+ if (!platformsString)
+ return true;
+ var platforms = platformsString.split(",");
+ var isMatch = false;
+ var currentPlatform = WebInspector.platform();
+ for (var i = 0; !isMatch && i < platforms.length; ++i)
+ isMatch |= (platforms[i] === currentPlatform);
pfeldman 2014/03/24 16:54:22 There is no logical |=
apavlov 2014/03/25 10:03:45 Done.
+ 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) {
pfeldman 2014/03/24 16:54:22 bindings &&
apavlov 2014/03/25 10:03:45 Done.
+ if (!platformMatches(bindings[i].platform))
+ continue;
+ var key = WebInspector.KeyboardShortcut.makeKeyFromBindingShortcut(bindings[i].shortcut);
+ if (!key)
+ continue;
+ if (WebInspector.KeyboardShortcut._keysToActionExtensions[key])
+ WebInspector.KeyboardShortcut._keysToActionExtensions[key].push(extension);
+ else
+ WebInspector.KeyboardShortcut._keysToActionExtensions[key] = [extension];
+ }
+ }
+
+ document.addEventListener("keypress", WebInspector.KeyboardShortcut._onkeypress, true);
pfeldman 2014/03/24 16:54:22 Move this above.
apavlov 2014/03/25 10:03:45 Done.
+ WebInspector.KeyboardShortcut._keysToActionExtensions = {};
+ var extensions = WebInspector.moduleManager.extensions(WebInspector.ActionDelegate);
+ extensions.forEach(registerBindings);
+}

Powered by Google App Engine
This is Rietveld 408576698