From 4c839f02ad5a60f3678761d6abddfc786036eda3 Mon Sep 17 00:00:00 2001 From: Gijs Kruitbosch Date: Sun, 20 Oct 2013 22:50:37 +0200 Subject: [PATCH] Bug 927605 - refactor keyboard shortcut code in devtools into a JSM, r=Unfocused,vporof --HG-- rename : browser/devtools/shared/helpers.js => toolkit/modules/ShortcutUtils.jsm --- .../devtools/debugger/debugger-controller.js | 9 +- browser/devtools/debugger/debugger-toolbar.js | 20 ++-- .../debugger/test/browser_dbg_pause-resume.js | 6 +- browser/devtools/scratchpad/scratchpad.js | 10 +- browser/devtools/shared/helpers.js | 72 ------------- toolkit/modules/ShortcutUtils.jsm | 100 ++++++++++++++++++ toolkit/modules/moz.build | 1 + 7 files changed, 122 insertions(+), 96 deletions(-) delete mode 100644 browser/devtools/shared/helpers.js create mode 100644 toolkit/modules/ShortcutUtils.jsm diff --git a/browser/devtools/debugger/debugger-controller.js b/browser/devtools/debugger/debugger-controller.js index a25bc7e9c500..9c2b8bd739e9 100644 --- a/browser/devtools/debugger/debugger-controller.js +++ b/browser/devtools/debugger/debugger-controller.js @@ -96,13 +96,8 @@ XPCOMUtils.defineLazyModuleGetter(this, "devtools", XPCOMUtils.defineLazyModuleGetter(this, "DevToolsUtils", "resource://gre/modules/devtools/DevToolsUtils.jsm"); -Object.defineProperty(this, "DevtoolsHelpers", { - get: function() { - return devtools.require("devtools/shared/helpers"); - }, - configurable: true, - enumerable: true -}); +XPCOMUtils.defineLazyModuleGetter(this, "ShortcutUtils", + "resource://gre/modules/ShortcutUtils.jsm"); Object.defineProperty(this, "NetworkHelper", { get: function() { diff --git a/browser/devtools/debugger/debugger-toolbar.js b/browser/devtools/debugger/debugger-toolbar.js index e19459099720..3a7634890b60 100644 --- a/browser/devtools/debugger/debugger-toolbar.js +++ b/browser/devtools/debugger/debugger-toolbar.js @@ -38,10 +38,10 @@ ToolbarView.prototype = { this._stepOutButton = document.getElementById("step-out"); this._chromeGlobals = document.getElementById("chrome-globals"); - let resumeKey = DevtoolsHelpers.prettyKey(document.getElementById("resumeKey"), true); - let stepOverKey = DevtoolsHelpers.prettyKey(document.getElementById("stepOverKey"), true); - let stepInKey = DevtoolsHelpers.prettyKey(document.getElementById("stepInKey"), true); - let stepOutKey = DevtoolsHelpers.prettyKey(document.getElementById("stepOutKey"), true); + let resumeKey = ShortcutUtils.prettifyShortcut(document.getElementById("resumeKey"), true); + let stepOverKey = ShortcutUtils.prettifyShortcut(document.getElementById("stepOverKey"), true); + let stepInKey = ShortcutUtils.prettifyShortcut(document.getElementById("stepInKey"), true); + let stepOutKey = ShortcutUtils.prettifyShortcut(document.getElementById("stepOutKey"), true); this._resumeTooltip = L10N.getFormatStr("resumeButtonTooltip", resumeKey); this._pauseTooltip = L10N.getFormatStr("pauseButtonTooltip", resumeKey); this._stepOverTooltip = L10N.getFormatStr("stepOverTooltip", stepOverKey); @@ -696,12 +696,12 @@ FilterView.prototype = { this._variableOperatorButton = document.getElementById("variable-operator-button"); this._variableOperatorLabel = document.getElementById("variable-operator-label"); - this._fileSearchKey = DevtoolsHelpers.prettyKey(document.getElementById("fileSearchKey"), true); - this._globalSearchKey = DevtoolsHelpers.prettyKey(document.getElementById("globalSearchKey"), true); - this._filteredFunctionsKey = DevtoolsHelpers.prettyKey(document.getElementById("functionSearchKey"), true); - this._tokenSearchKey = DevtoolsHelpers.prettyKey(document.getElementById("tokenSearchKey"), true); - this._lineSearchKey = DevtoolsHelpers.prettyKey(document.getElementById("lineSearchKey"), true); - this._variableSearchKey = DevtoolsHelpers.prettyKey(document.getElementById("variableSearchKey"), true); + this._fileSearchKey = ShortcutUtils.prettifyShortcut(document.getElementById("fileSearchKey"), true); + this._globalSearchKey = ShortcutUtils.prettifyShortcut(document.getElementById("globalSearchKey"), true); + this._filteredFunctionsKey = ShortcutUtils.prettifyShortcut(document.getElementById("functionSearchKey"), true); + this._tokenSearchKey = ShortcutUtils.prettifyShortcut(document.getElementById("tokenSearchKey"), true); + this._lineSearchKey = ShortcutUtils.prettifyShortcut(document.getElementById("lineSearchKey"), true); + this._variableSearchKey = ShortcutUtils.prettifyShortcut(document.getElementById("variableSearchKey"), true); this._searchbox.addEventListener("click", this._onClick, false); this._searchbox.addEventListener("select", this._onInput, false); diff --git a/browser/devtools/debugger/test/browser_dbg_pause-resume.js b/browser/devtools/debugger/test/browser_dbg_pause-resume.js index a5f44503550a..74748c297eae 100644 --- a/browser/devtools/debugger/test/browser_dbg_pause-resume.js +++ b/browser/devtools/debugger/test/browser_dbg_pause-resume.js @@ -30,7 +30,7 @@ function testPause() { is(gResumeButton.getAttribute("tooltiptext"), gDebugger.L10N.getFormatStr("pauseButtonTooltip", - gDebugger.DevtoolsHelpers.prettyKey(gResumeKey)), + gDebugger.ShortcutUtils.prettifyShortcut(gResumeKey)), "Button tooltip should be 'pause' when running."); gDebugger.gThreadClient.addOneTimeListener("paused", () => { @@ -39,7 +39,7 @@ function testPause() { is(gResumeButton.getAttribute("tooltiptext"), gDebugger.L10N.getFormatStr("resumeButtonTooltip", - gDebugger.DevtoolsHelpers.prettyKey(gResumeKey)), + gDebugger.ShortcutUtils.prettifyShortcut(gResumeKey)), "Button tooltip should be 'resume' when paused."); is(gFrames.itemCount, 0, @@ -58,7 +58,7 @@ function testResume() { is(gResumeButton.getAttribute("tooltiptext"), gDebugger.L10N.getFormatStr("pauseButtonTooltip", - gDebugger.DevtoolsHelpers.prettyKey(gResumeKey)), + gDebugger.ShortcutUtils.prettifyShortcut(gResumeKey)), "Button tooltip should be pause when running."); closeDebuggerAndFinish(gPanel); diff --git a/browser/devtools/scratchpad/scratchpad.js b/browser/devtools/scratchpad/scratchpad.js index 545c98ca684f..b79d16d4fcc6 100644 --- a/browser/devtools/scratchpad/scratchpad.js +++ b/browser/devtools/scratchpad/scratchpad.js @@ -37,7 +37,6 @@ const Telemetry = require("devtools/shared/telemetry"); const escodegen = require("escodegen/escodegen"); const Editor = require("devtools/sourceeditor/editor"); const TargetFactory = require("devtools/framework/target").TargetFactory; -const DevtoolsHelpers = require("devtools/shared/helpers"); Cu.import("resource://gre/modules/XPCOMUtils.jsm"); Cu.import("resource://gre/modules/Services.jsm"); @@ -74,6 +73,9 @@ XPCOMUtils.defineLazyModuleGetter(this, "DebuggerClient", XPCOMUtils.defineLazyGetter(this, "REMOTE_TIMEOUT", () => Services.prefs.getIntPref("devtools.debugger.remote-timeout")); +XPCOMUtils.defineLazyModuleGetter(this, "ShortcutUtils", + "resource://gre/modules/ShortcutUtils.jsm"); + // Because we have no constructor / destructor where we can log metrics we need // to do so here. let telemetry = new Telemetry(); @@ -1248,9 +1250,9 @@ var Scratchpad = { let initialText = this.strings.formatStringFromName( "scratchpadIntro1", - [DevtoolsHelpers.prettyKey(document.getElementById("sp-key-run")), - DevtoolsHelpers.prettyKey(document.getElementById("sp-key-inspect")), - DevtoolsHelpers.prettyKey(document.getElementById("sp-key-display"))], + [ShortcutUtils.prettifyShortcut(document.getElementById("sp-key-run")), + ShortcutUtils.prettifyShortcut(document.getElementById("sp-key-inspect")), + ShortcutUtils.prettifyShortcut(document.getElementById("sp-key-display"))], 3); let args = window.arguments; diff --git a/browser/devtools/shared/helpers.js b/browser/devtools/shared/helpers.js deleted file mode 100644 index 9b9c7b376284..000000000000 --- a/browser/devtools/shared/helpers.js +++ /dev/null @@ -1,72 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -const {Cu} = require("chrome"); -Cu.import("resource://gre/modules/Services.jsm"); - -let { XPCOMUtils } = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {}); -XPCOMUtils.defineLazyGetter(this, "PlatformKeys", function() { - return Services.strings.createBundle( - "chrome://global-platform/locale/platformKeys.properties"); -}); - -/** - * Prettifies the modifier keys for an element. - * - * @param Node aElemKey - * The key element to get the modifiers from. - * @param boolean aAllowCloverleaf - * Pass true to use the cloverleaf symbol instead of a descriptive string. - * @return string - * A prettified and properly separated modifier keys string. - */ -exports.prettyKey = function Helpers_prettyKey(aElemKey, aAllowCloverleaf) { - let elemString = ""; - let elemMod = aElemKey.getAttribute("modifiers"); - - if (elemMod.match("accel")) { - if (Services.appinfo.OS == "Darwin") { - // XXX bug 779642 Use "Cmd-" literal vs. cloverleaf meta-key until - // Orion adds variable height lines. - if (!aAllowCloverleaf) { - elemString += "Cmd-"; - } else { - elemString += PlatformKeys.GetStringFromName("VK_META") + - PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); - } - } else { - elemString += PlatformKeys.GetStringFromName("VK_CONTROL") + - PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); - } - } - if (elemMod.match("access")) { - if (Services.appinfo.OS == "Darwin") { - elemString += PlatformKeys.GetStringFromName("VK_CONTROL") + - PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); - } else { - elemString += PlatformKeys.GetStringFromName("VK_ALT") + - PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); - } - } - if (elemMod.match("shift")) { - elemString += PlatformKeys.GetStringFromName("VK_SHIFT") + - PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); - } - if (elemMod.match("alt")) { - elemString += PlatformKeys.GetStringFromName("VK_ALT") + - PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); - } - if (elemMod.match("ctrl") || elemMod.match("control")) { - elemString += PlatformKeys.GetStringFromName("VK_CONTROL") + - PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); - } - if (elemMod.match("meta")) { - elemString += PlatformKeys.GetStringFromName("VK_META") + - PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); - } - - return elemString + - (aElemKey.getAttribute("keycode").replace(/^.*VK_/, "") || - aElemKey.getAttribute("key")).toUpperCase(); -} diff --git a/toolkit/modules/ShortcutUtils.jsm b/toolkit/modules/ShortcutUtils.jsm new file mode 100644 index 000000000000..9a0fc1ef47c1 --- /dev/null +++ b/toolkit/modules/ShortcutUtils.jsm @@ -0,0 +1,100 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +"use strict"; + +this.EXPORTED_SYMBOLS = ["ShortcutUtils"]; + +const Cu = Components.utils; +Cu.import("resource://gre/modules/Services.jsm"); +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); + +XPCOMUtils.defineLazyGetter(this, "PlatformKeys", function() { + return Services.strings.createBundle( + "chrome://global-platform/locale/platformKeys.properties"); +}); + +XPCOMUtils.defineLazyGetter(this, "Keys", function() { + return Services.strings.createBundle( + "chrome://global/locale/keys.properties"); +}); + +let ShortcutUtils = { + /** + * Prettifies the modifier keys for an element. + * + * @param Node aElemKey + * The key element to get the modifiers from. + * @param boolean aAllowCloverleaf + * Pass true to use the cloverleaf symbol instead of a descriptive string. (OS X only) + * @return string + * A prettified and properly separated modifier keys string. + */ + prettifyShortcut: function(aElemKey, aAllowCloverleaf) { + let elemString = ""; + let elemMod = aElemKey.getAttribute("modifiers"); + + if (elemMod.match("accel")) { + if (Services.appinfo.OS == "Darwin") { + // XXX bug 779642 Use "Cmd-" literal vs. cloverleaf meta-key until + // Orion adds variable height lines. + if (!aAllowCloverleaf) { + elemString += "Cmd-"; + } else { + elemString += PlatformKeys.GetStringFromName("VK_META") + + PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); + } + } else { + elemString += PlatformKeys.GetStringFromName("VK_CONTROL") + + PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); + } + } + if (elemMod.match("access")) { + if (Services.appinfo.OS == "Darwin") { + elemString += PlatformKeys.GetStringFromName("VK_CONTROL") + + PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); + } else { + elemString += PlatformKeys.GetStringFromName("VK_ALT") + + PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); + } + } + if (elemMod.match("os")) { + elemString += PlatformKeys.GetStringFromName("VK_WIN") + + PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); + } + if (elemMod.match("shift")) { + elemString += PlatformKeys.GetStringFromName("VK_SHIFT") + + PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); + } + if (elemMod.match("alt")) { + elemString += PlatformKeys.GetStringFromName("VK_ALT") + + PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); + } + if (elemMod.match("ctrl") || elemMod.match("control")) { + elemString += PlatformKeys.GetStringFromName("VK_CONTROL") + + PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); + } + if (elemMod.match("meta")) { + elemString += PlatformKeys.GetStringFromName("VK_META") + + PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); + } + + let key; + let keyCode = aElemKey.getAttribute("keycode"); + if (keyCode) { + try { + // Some keys might not exist in the locale file, which will throw: + key = Keys.GetStringFromName(keyCode.toUpperCase()); + } catch (ex) { + Cu.reportError("Error finding " + keyCode + ": " + ex); + key = keyCode.replace(/^VK_/, ''); + } + } else { + key = aElemKey.getAttribute("key"); + } + return elemString + key; + } +}; + +Object.freeze(ShortcutUtils); diff --git a/toolkit/modules/moz.build b/toolkit/modules/moz.build index c0c73537c6db..027bdb7f941a 100644 --- a/toolkit/modules/moz.build +++ b/toolkit/modules/moz.build @@ -35,6 +35,7 @@ EXTRA_JS_MODULES += [ 'RemoteWebProgress.jsm', 'SelectContentHelper.jsm', 'SelectParentHelper.jsm', + 'ShortcutUtils.jsm', 'Sntp.jsm', 'SpatialNavigation.jsm', 'Sqlite.jsm',