From 45ced371ddffd432f70c68a63881af57f782f139 Mon Sep 17 00:00:00 2001 From: Bernhard Sirlinger Date: Fri, 21 Jun 2013 15:32:11 +0200 Subject: [PATCH 1/4] Added sorting so that platform specific keys will get added first --- src/command/KeyBindingManager.js | 60 +++++++++++++++++++------------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/src/command/KeyBindingManager.js b/src/command/KeyBindingManager.js index 53834b87e56..fe72499c1b3 100644 --- a/src/command/KeyBindingManager.js +++ b/src/command/KeyBindingManager.js @@ -1,24 +1,24 @@ /* * Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved. - * + * * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: - * + * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. - * + * */ @@ -376,7 +376,7 @@ define(function (require, exports, module) { } normalized = normalizeKeyDescriptorString(key); - // skip if the key binding is invalid + // skip if the key binding is invalid if (!normalized) { console.log("Failed to normalize " + key); return null; @@ -554,6 +554,16 @@ define(function (require, exports, module) { var keyBinding; results = []; + keyBindings.sort(function (a, b) { + if(a.platform === brackets.platform) { + return 1; + } else if(!a.platform) { + return 0; + } else { + return -1; + } + }); + keyBindings.forEach(function addSingleBinding(keyBindingRequest) { // attempt to add keybinding keyBinding = _addBinding(commandID, keyBindingRequest, keyBindingRequest.platform); @@ -609,27 +619,27 @@ define(function (require, exports, module) { } /** - * Adds a global keydown hook that gets first crack at keydown events - * before standard keybindings do. This is intended for use by modal or - * semi-modal UI elements like dialogs or the code hint list that should - * execute before normal command bindings are run. - * - * The hook is passed one parameter, the original keyboard event. If the - * hook handles the event (or wants to block other global hooks from + * Adds a global keydown hook that gets first crack at keydown events + * before standard keybindings do. This is intended for use by modal or + * semi-modal UI elements like dialogs or the code hint list that should + * execute before normal command bindings are run. + * + * The hook is passed one parameter, the original keyboard event. If the + * hook handles the event (or wants to block other global hooks from * handling the event), it should return true. Note that this will *only* * stop other global hooks and KeyBindingManager from handling the * event; to prevent further event propagation, you will need to call * stopPropagation(), stopImmediatePropagation(), and/or preventDefault() * as usual. * - * Multiple keydown hooks can be registered, and are executed in order, + * Multiple keydown hooks can be registered, and are executed in order, * most-recently-added first. - * + * * (We have to have a special API for this because (1) handlers are normally - * called in least-recently-added order, and we want most-recently-added; - * (2) native DOM events don't have a way for us to find out if + * called in least-recently-added order, and we want most-recently-added; + * (2) native DOM events don't have a way for us to find out if * stopImmediatePropagation()/stopPropagation() has been called on the - * event, so we have to have some other way for one of the hooks to + * event, so we have to have some other way for one of the hooks to * indicate that it wants to block the other hooks from running.) * * @param {function(Event): boolean} hook The global hook to add. From 8ba4ea7cc06f007b8d54f884f7292f64f25997aa Mon Sep 17 00:00:00 2001 From: Bernhard Sirlinger Date: Sat, 22 Jun 2013 07:20:50 +0200 Subject: [PATCH 2/4] Use Array.some instead of forEach --- src/command/KeyBindingManager.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/command/KeyBindingManager.js b/src/command/KeyBindingManager.js index fe72499c1b3..04b2dfc4dc2 100644 --- a/src/command/KeyBindingManager.js +++ b/src/command/KeyBindingManager.js @@ -564,13 +564,16 @@ define(function (require, exports, module) { } }); - keyBindings.forEach(function addSingleBinding(keyBindingRequest) { + keyBindings.some(function addSingleBinding(keyBindingRequest) { // attempt to add keybinding keyBinding = _addBinding(commandID, keyBindingRequest, keyBindingRequest.platform); if (keyBinding) { results.push(keyBinding); + return true; } + + return false; }); } else { results = _addBinding(commandID, keyBindings, platform); From f5d66a16d28e3f85c743cf86e7f406ab15e7daeb Mon Sep 17 00:00:00 2001 From: Bernhard Sirlinger Date: Sat, 22 Jun 2013 07:22:24 +0200 Subject: [PATCH 3/4] Minor cleanup --- src/command/KeyBindingManager.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/command/KeyBindingManager.js b/src/command/KeyBindingManager.js index 04b2dfc4dc2..1ba5f6f56ca 100644 --- a/src/command/KeyBindingManager.js +++ b/src/command/KeyBindingManager.js @@ -555,9 +555,9 @@ define(function (require, exports, module) { results = []; keyBindings.sort(function (a, b) { - if(a.platform === brackets.platform) { + if (a.platform === brackets.platform) { return 1; - } else if(!a.platform) { + } else if (!a.platform) { return 0; } else { return -1; From 71806c4be4e7c2525fed5d17ce62d3f97a08037f Mon Sep 17 00:00:00 2001 From: Bernhard Sirlinger Date: Tue, 25 Jun 2013 19:22:09 +0200 Subject: [PATCH 4/4] Changed sorting logic base on feedback --- src/command/KeyBindingManager.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/command/KeyBindingManager.js b/src/command/KeyBindingManager.js index 1ba5f6f56ca..27ae8c84e60 100644 --- a/src/command/KeyBindingManager.js +++ b/src/command/KeyBindingManager.js @@ -556,11 +556,19 @@ define(function (require, exports, module) { keyBindings.sort(function (a, b) { if (a.platform === brackets.platform) { + // "a" is platform specific and matches + return -1; + } else if (b.platform === brackets.platform) { + // "b" is platform specific and matches return 1; - } else if (!a.platform) { - return 0; - } else { + } else if (!a.platform && b.platform) { + // "a" is generic and "b" is not matching return -1; + } else if (!b.platform && a.platform) { + // "b" is generic and "a" is not matching + return 1; + } else { + return 0; } });