2013-10-21 00:50:37 +04:00
|
|
|
/* 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.
|
2013-10-22 16:00:51 +04:00
|
|
|
* @param boolean aNoCloverLeaf
|
|
|
|
* Pass true to use a descriptive string instead of the cloverleaf symbol. (OS X only)
|
2013-10-21 00:50:37 +04:00
|
|
|
* @return string
|
|
|
|
* A prettified and properly separated modifier keys string.
|
|
|
|
*/
|
2013-10-22 16:00:51 +04:00
|
|
|
prettifyShortcut: function(aElemKey, aNoCloverLeaf) {
|
2013-10-21 00:50:37 +04:00
|
|
|
let elemString = "";
|
|
|
|
let elemMod = aElemKey.getAttribute("modifiers");
|
2014-04-16 16:23:19 +04:00
|
|
|
let haveCloverLeaf = false;
|
2013-10-21 00:50:37 +04:00
|
|
|
|
|
|
|
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.
|
2013-10-22 16:00:51 +04:00
|
|
|
if (aNoCloverLeaf) {
|
2013-10-21 00:50:37 +04:00
|
|
|
elemString += "Cmd-";
|
|
|
|
} else {
|
2014-04-16 16:23:19 +04:00
|
|
|
haveCloverLeaf = true;
|
2013-10-21 00:50:37 +04:00
|
|
|
}
|
|
|
|
} 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");
|
|
|
|
}
|
|
|
|
|
2014-04-16 16:23:19 +04:00
|
|
|
if (haveCloverLeaf) {
|
|
|
|
elemString += PlatformKeys.GetStringFromName("VK_META") +
|
|
|
|
PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR");
|
|
|
|
}
|
|
|
|
|
2013-10-21 00:50:37 +04:00
|
|
|
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");
|
2014-01-27 03:53:22 +04:00
|
|
|
key = key.toUpperCase();
|
2013-10-21 00:50:37 +04:00
|
|
|
}
|
|
|
|
return elemString + key;
|
2014-05-08 13:14:20 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
findShortcut: function(aElemCommand) {
|
|
|
|
let document = aElemCommand.ownerDocument;
|
|
|
|
return document.querySelector("key[command=\"" + aElemCommand.getAttribute("id") + "\"]");
|
2013-10-21 00:50:37 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.freeze(ShortcutUtils);
|