addon-sdk/source/lib/sdk/hotkeys.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/lib/sdk/hotkeys.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,40 @@
     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 +module.metadata = {
    1.11 +  "stability": "stable"
    1.12 +};
    1.13 +
    1.14 +const INVALID_HOTKEY = "Hotkey must have at least one modifier.";
    1.15 +
    1.16 +const { toJSON: jsonify, toString: stringify,
    1.17 +        isFunctionKey } = require("./keyboard/utils");
    1.18 +const { register, unregister } = require("./keyboard/hotkeys");
    1.19 +
    1.20 +const Hotkey = exports.Hotkey = function Hotkey(options) {
    1.21 +  if (!(this instanceof Hotkey))
    1.22 +    return new Hotkey(options);
    1.23 +
    1.24 +  // Parsing key combination string.
    1.25 +  let hotkey = jsonify(options.combo);
    1.26 +  if (!isFunctionKey(hotkey.key) && !hotkey.modifiers.length) {
    1.27 +    throw new TypeError(INVALID_HOTKEY);
    1.28 +  }
    1.29 +
    1.30 +  this.onPress = options.onPress && options.onPress.bind(this);
    1.31 +  this.toString = stringify.bind(null, hotkey);
    1.32 +  // Registering listener on keyboard combination enclosed by this hotkey.
    1.33 +  // Please note that `this.toString()` is a normalized version of
    1.34 +  // `options.combination` where order of modifiers is sorted and `accel` is
    1.35 +  // replaced with platform specific key.
    1.36 +  register(this.toString(), this.onPress);
    1.37 +  // We freeze instance before returning it in order to make it's properties
    1.38 +  // read-only.
    1.39 +  return Object.freeze(this);
    1.40 +};
    1.41 +Hotkey.prototype.destroy = function destroy() {
    1.42 +  unregister(this.toString(), this.onPress);
    1.43 +};

mercurial