toolkit/modules/ShortcutUtils.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/modules/ShortcutUtils.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,106 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +this.EXPORTED_SYMBOLS = ["ShortcutUtils"];
    1.11 +
    1.12 +const Cu = Components.utils;
    1.13 +Cu.import("resource://gre/modules/Services.jsm");
    1.14 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.15 +
    1.16 +XPCOMUtils.defineLazyGetter(this, "PlatformKeys", function() {
    1.17 +  return Services.strings.createBundle(
    1.18 +    "chrome://global-platform/locale/platformKeys.properties");
    1.19 +});
    1.20 +
    1.21 +XPCOMUtils.defineLazyGetter(this, "Keys", function() {
    1.22 +  return Services.strings.createBundle(
    1.23 +    "chrome://global/locale/keys.properties");
    1.24 +});
    1.25 +
    1.26 +let ShortcutUtils = {
    1.27 +  /**
    1.28 +    * Prettifies the modifier keys for an element.
    1.29 +    *
    1.30 +    * @param Node aElemKey
    1.31 +    *        The key element to get the modifiers from.
    1.32 +    * @param boolean aNoCloverLeaf
    1.33 +    *        Pass true to use a descriptive string instead of the cloverleaf symbol. (OS X only)
    1.34 +    * @return string
    1.35 +    *         A prettified and properly separated modifier keys string.
    1.36 +    */
    1.37 +  prettifyShortcut: function(aElemKey, aNoCloverLeaf) {
    1.38 +    let elemString = "";
    1.39 +    let elemMod = aElemKey.getAttribute("modifiers");
    1.40 +    let haveCloverLeaf = false;
    1.41 +
    1.42 +    if (elemMod.match("accel")) {
    1.43 +      if (Services.appinfo.OS == "Darwin") {
    1.44 +        // XXX bug 779642 Use "Cmd-" literal vs. cloverleaf meta-key until
    1.45 +        // Orion adds variable height lines.
    1.46 +        if (aNoCloverLeaf) {
    1.47 +          elemString += "Cmd-";
    1.48 +        } else {
    1.49 +          haveCloverLeaf = true;
    1.50 +        }
    1.51 +      } else {
    1.52 +        elemString += PlatformKeys.GetStringFromName("VK_CONTROL") +
    1.53 +          PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR");
    1.54 +      }
    1.55 +    }
    1.56 +    if (elemMod.match("access")) {
    1.57 +      if (Services.appinfo.OS == "Darwin") {
    1.58 +        elemString += PlatformKeys.GetStringFromName("VK_CONTROL") +
    1.59 +          PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR");
    1.60 +      } else {
    1.61 +        elemString += PlatformKeys.GetStringFromName("VK_ALT") +
    1.62 +          PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR");
    1.63 +      }
    1.64 +    }
    1.65 +    if (elemMod.match("os")) {
    1.66 +      elemString += PlatformKeys.GetStringFromName("VK_WIN") +
    1.67 +        PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR");
    1.68 +    }
    1.69 +    if (elemMod.match("shift")) {
    1.70 +      elemString += PlatformKeys.GetStringFromName("VK_SHIFT") +
    1.71 +        PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR");
    1.72 +    }
    1.73 +    if (elemMod.match("alt")) {
    1.74 +      elemString += PlatformKeys.GetStringFromName("VK_ALT") +
    1.75 +        PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR");
    1.76 +    }
    1.77 +    if (elemMod.match("ctrl") || elemMod.match("control")) {
    1.78 +      elemString += PlatformKeys.GetStringFromName("VK_CONTROL") +
    1.79 +        PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR");
    1.80 +    }
    1.81 +    if (elemMod.match("meta")) {
    1.82 +      elemString += PlatformKeys.GetStringFromName("VK_META") +
    1.83 +        PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR");
    1.84 +    }
    1.85 +
    1.86 +    if (haveCloverLeaf) {
    1.87 +      elemString += PlatformKeys.GetStringFromName("VK_META") +
    1.88 +        PlatformKeys.GetStringFromName("MODIFIER_SEPARATOR");
    1.89 +    }
    1.90 +
    1.91 +    let key;
    1.92 +    let keyCode = aElemKey.getAttribute("keycode");
    1.93 +    if (keyCode) {
    1.94 +      try {
    1.95 +        // Some keys might not exist in the locale file, which will throw:
    1.96 +        key = Keys.GetStringFromName(keyCode.toUpperCase());
    1.97 +      } catch (ex) {
    1.98 +        Cu.reportError("Error finding " + keyCode + ": " + ex);
    1.99 +        key = keyCode.replace(/^VK_/, '');
   1.100 +      }
   1.101 +    } else {
   1.102 +      key = aElemKey.getAttribute("key");
   1.103 +      key = key.toUpperCase();
   1.104 +    }
   1.105 +    return elemString + key;
   1.106 +  }
   1.107 +};
   1.108 +
   1.109 +Object.freeze(ShortcutUtils);

mercurial