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
This commit is contained in:
Gijs Kruitbosch 2013-10-20 22:50:37 +02:00
Родитель 2d8e3c93bf
Коммит 4c839f02ad
7 изменённых файлов: 122 добавлений и 96 удалений

Просмотреть файл

@ -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() {

Просмотреть файл

@ -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);

Просмотреть файл

@ -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);

Просмотреть файл

@ -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;

Просмотреть файл

@ -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();
}

Просмотреть файл

@ -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);

Просмотреть файл

@ -35,6 +35,7 @@ EXTRA_JS_MODULES += [
'RemoteWebProgress.jsm',
'SelectContentHelper.jsm',
'SelectParentHelper.jsm',
'ShortcutUtils.jsm',
'Sntp.jsm',
'SpatialNavigation.jsm',
'Sqlite.jsm',