michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: this.EXPORTED_SYMBOLS = ["ShortcutUtils"]; michael@0: michael@0: const Cu = Components.utils; michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyGetter(this, "PlatformKeys", function() { michael@0: return Services.strings.createBundle( michael@0: "chrome://global-platform/locale/platformKeys.properties"); michael@0: }); michael@0: michael@0: XPCOMUtils.defineLazyGetter(this, "Keys", function() { michael@0: return Services.strings.createBundle( michael@0: "chrome://global/locale/keys.properties"); michael@0: }); michael@0: michael@0: let ShortcutUtils = { michael@0: /** michael@0: * Prettifies the modifier keys for an element. michael@0: * michael@0: * @param Node aElemKey michael@0: * The key element to get the modifiers from. michael@0: * @param boolean aNoCloverLeaf michael@0: * Pass true to use a descriptive string instead of the cloverleaf symbol. (OS X only) michael@0: * @return string michael@0: * A prettified and properly separated modifier keys string. michael@0: */ michael@0: prettifyShortcut: function(aElemKey, aNoCloverLeaf) { michael@0: let elemString = ""; michael@0: let elemMod = aElemKey.getAttribute("modifiers"); michael@0: let haveCloverLeaf = false; michael@0: michael@0: if (elemMod.match("accel")) { michael@0: if (Services.appinfo.OS == "Darwin") { michael@0: // XXX bug 779642 Use "Cmd-" literal vs. cloverleaf meta-key until michael@0: // Orion adds variable height lines. michael@0: if (aNoCloverLeaf) { michael@0: elemString += "Cmd-"; michael@0: } else { michael@0: haveCloverLeaf = true; michael@0: } michael@0: } else { michael@0: elemString += PlatformKeys.GetStringFromName("VK_CONTROL") + michael@0: PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); michael@0: } michael@0: } michael@0: if (elemMod.match("access")) { michael@0: if (Services.appinfo.OS == "Darwin") { michael@0: elemString += PlatformKeys.GetStringFromName("VK_CONTROL") + michael@0: PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); michael@0: } else { michael@0: elemString += PlatformKeys.GetStringFromName("VK_ALT") + michael@0: PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); michael@0: } michael@0: } michael@0: if (elemMod.match("os")) { michael@0: elemString += PlatformKeys.GetStringFromName("VK_WIN") + michael@0: PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); michael@0: } michael@0: if (elemMod.match("shift")) { michael@0: elemString += PlatformKeys.GetStringFromName("VK_SHIFT") + michael@0: PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); michael@0: } michael@0: if (elemMod.match("alt")) { michael@0: elemString += PlatformKeys.GetStringFromName("VK_ALT") + michael@0: PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); michael@0: } michael@0: if (elemMod.match("ctrl") || elemMod.match("control")) { michael@0: elemString += PlatformKeys.GetStringFromName("VK_CONTROL") + michael@0: PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); michael@0: } michael@0: if (elemMod.match("meta")) { michael@0: elemString += PlatformKeys.GetStringFromName("VK_META") + michael@0: PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); michael@0: } michael@0: michael@0: if (haveCloverLeaf) { michael@0: elemString += PlatformKeys.GetStringFromName("VK_META") + michael@0: PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR"); michael@0: } michael@0: michael@0: let key; michael@0: let keyCode = aElemKey.getAttribute("keycode"); michael@0: if (keyCode) { michael@0: try { michael@0: // Some keys might not exist in the locale file, which will throw: michael@0: key = Keys.GetStringFromName(keyCode.toUpperCase()); michael@0: } catch (ex) { michael@0: Cu.reportError("Error finding " + keyCode + ": " + ex); michael@0: key = keyCode.replace(/^VK_/, ''); michael@0: } michael@0: } else { michael@0: key = aElemKey.getAttribute("key"); michael@0: key = key.toUpperCase(); michael@0: } michael@0: return elemString + key; michael@0: } michael@0: }; michael@0: michael@0: Object.freeze(ShortcutUtils);