[Pkg-mozext-commits] [requestpolicy] 243/257: [imp] open menu via shortcut: now customizable

David Prévot taffit at moszumanska.debian.org
Thu Jan 28 03:20:18 UTC 2016


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository requestpolicy.

commit afab797158c3c2d26e4d9a412015fc3d9ca7c94f
Author: Martin Kimmerle <dev at 256k.de>
Date:   Fri Dec 25 10:33:46 2015 +0100

    [imp] open menu via shortcut: now customizable
    
    The keyboard shortcut for opening the menu, "Alt Shift R", can
    now be disabled or changed to a different combination.
    
    Resolves #616, resolves #460.
---
 src/content/controllers/keyboard-shortcuts.jsm |  4 ++-
 src/content/lib/classes/keyboard-shortcut.jsm  | 33 ++++++++++++------
 src/content/lib/classes/pref-observer.jsm      |  6 ++++
 src/content/lib/default-preferences.js         |  3 ++
 src/content/models/prefs.jsm                   |  2 ++
 tests/marionette/rp_puppeteer/ui/menu.py       |  8 +++--
 tests/marionette/tests/menu/test_open.py       | 46 ++++++++++++++++++++++++++
 7 files changed, 87 insertions(+), 15 deletions(-)

diff --git a/src/content/controllers/keyboard-shortcuts.jsm b/src/content/controllers/keyboard-shortcuts.jsm
index 891fc3a..b656d3f 100644
--- a/src/content/controllers/keyboard-shortcuts.jsm
+++ b/src/content/controllers/keyboard-shortcuts.jsm
@@ -43,7 +43,9 @@ var KeyboardShortcuts = (function() {
     keyboardShortcuts.push(new KeyboardShortcut("openMenu", "alt shift r",
         function(window) {
           window.rpcontinued.overlay.openMenu();
-        }));
+        },
+        "keyboardShortcuts.openMenu.enabled",
+        "keyboardShortcuts.openMenu.combo"));
   };
 
   self.shutdown = function() {
diff --git a/src/content/lib/classes/keyboard-shortcut.jsm b/src/content/lib/classes/keyboard-shortcut.jsm
index e4c3cab..af1a54c 100644
--- a/src/content/lib/classes/keyboard-shortcut.jsm
+++ b/src/content/lib/classes/keyboard-shortcut.jsm
@@ -31,7 +31,9 @@ let {console} = Cu.import("resource://gre/modules/devtools/Console.jsm", {});
 let {ScriptLoader: {importModule}} = Cu.import(
     "chrome://rpcontinued/content/lib/script-loader.jsm", {});
 let {Windows} = importModule("models/windows");
+let {Prefs} = importModule("models/prefs");
 let {XULUtils} = importModule("lib/utils/xul");
+let {ProcessEnvironment} = importModule("lib/environment");
 
 //==============================================================================
 // KeyboardShortcut
@@ -54,7 +56,8 @@ let {XULUtils} = importModule("lib/utils/xul");
  * - set element attributes (all windows)
  */
 
-function KeyboardShortcut(aID, aDefaultCombo, aCallback) {
+function KeyboardShortcut(aID, aDefaultCombo, aCallback, aUserEnabledPrefName,
+    aUserComboPrefName) {
   //----------------------------------------------------------------------------
   // initialize properties
   //----------------------------------------------------------------------------
@@ -63,6 +66,8 @@ function KeyboardShortcut(aID, aDefaultCombo, aCallback) {
   this._elementID = "rpKey_" + this._id;
   this._defaultCombo = aDefaultCombo;
   this._callback = aCallback;
+  this._userEnabledPrefName = aUserEnabledPrefName;
+  this._userComboPrefName = aUserComboPrefName;
 
   this._elementAttributes = {
     disabled: null,
@@ -73,8 +78,8 @@ function KeyboardShortcut(aID, aDefaultCombo, aCallback) {
   this._listeners = {
     onWindowLoad: this._loadIntoWindow.bind(this),
     onWindowUnload: this._unloadFromWindow.bind(this),
-    onKeyCommand: this._onPress.bind(this)
-    // prefChanged: this._onPrefChange.bind(this)
+    onKeyCommand: this._onPress.bind(this),
+    onPrefChange: this._onPrefChange.bind(this)
   };
 
   //----------------------------------------------------------------------------
@@ -86,6 +91,10 @@ function KeyboardShortcut(aID, aDefaultCombo, aCallback) {
 
   Windows.addListener("load", this._listeners.onWindowLoad);
   Windows.addListener("unload", this._listeners.onWindowUnload);
+  ProcessEnvironment.prefObs.addListeners([
+    this._userEnabledPrefName,
+    this._userComboPrefName
+  ], this._listeners.onPrefChange);
 }
 
 //------------------------------------------------------------------------------
@@ -96,6 +105,10 @@ KeyboardShortcut.prototype.destroy = function() {
   Windows.forEachOpenWindow(this._unloadFromWindow.bind(this));
   Windows.removeListener("load", this._listeners.onWindowLoad);
   Windows.removeListener("unload", this._listeners.onWindowUnload);
+  ProcessEnvironment.prefObs.removeListeners([
+    this._userEnabledPrefName,
+    this._userComboPrefName
+  ], this._listeners.onPrefChange);
 };
 
 KeyboardShortcut.prototype._loadIntoWindow = function(window) {
@@ -112,10 +125,10 @@ KeyboardShortcut.prototype._onPress = function(event) {
   this._callback.call(null, window);
 };
 
-// KeyboardShortcut.prototype._onPrefChange = function() {
-//   this._determineElementAttributes();
-//   Windows.forEachOpenWindow(this._loadIntoWindow.bind(this));
-// };
+KeyboardShortcut.prototype._onPrefChange = function() {
+  this._determineElementAttributes();
+  Windows.forEachOpenWindow(this._setElementAttributes.bind(this));
+};
 
 //------------------------------------------------------------------------------
 // assisting methods
@@ -152,15 +165,13 @@ KeyboardShortcut.prototype._removeElement = function(window) {
 
 Object.defineProperty(KeyboardShortcut.prototype, "userCombo", {
   get: function() {
-    // will be changed to pref
-    return "default";
+    return Prefs.get(this._userComboPrefName);
   }
 });
 
 Object.defineProperty(KeyboardShortcut.prototype, "userEnabled", {
   get: function() {
-    // will be changed to pref
-    return true;
+    return Prefs.get(this._userEnabledPrefName);
   }
 });
 
diff --git a/src/content/lib/classes/pref-observer.jsm b/src/content/lib/classes/pref-observer.jsm
index d0d8080..58817fb 100644
--- a/src/content/lib/classes/pref-observer.jsm
+++ b/src/content/lib/classes/pref-observer.jsm
@@ -84,6 +84,12 @@ PrefObserver.prototype.removeListener = function(aDomain, aListener) {
   }
 };
 
+PrefObserver.prototype.removeListeners = function(aDomains, aListener) {
+  for (let domain of aDomains) {
+    this.removeListener(domain, aListener);
+  }
+};
+
 /**
  * Remove all listeners in this Pref Observer.
  */
diff --git a/src/content/lib/default-preferences.js b/src/content/lib/default-preferences.js
index 6cf8bb8..8b768b1 100644
--- a/src/content/lib/default-preferences.js
+++ b/src/content/lib/default-preferences.js
@@ -25,6 +25,9 @@ pref("extensions.requestpolicy.contextMenu", true);
 pref("extensions.requestpolicy.menu.sorting", "numRequests");
 pref("extensions.requestpolicy.menu.info.showNumRequests", true);
 
+pref("extensions.requestpolicy.keyboardShortcuts.openMenu.enabled", true);
+pref("extensions.requestpolicy.keyboardShortcuts.openMenu.combo", "default");
+
 pref("extensions.requestpolicy.lastVersion", "0.0");
 pref("extensions.requestpolicy.lastAppVersion", "0.0");
 
diff --git a/src/content/models/prefs.jsm b/src/content/models/prefs.jsm
index f1e1592..50af705 100644
--- a/src/content/models/prefs.jsm
+++ b/src/content/models/prefs.jsm
@@ -52,6 +52,8 @@ var Prefs = (function() {
       "defaultPolicy.allowSameDomain": "BoolPref",
       "indicateBlacklistedObjects": "BoolPref",
       "indicateBlockedObjects": "BoolPref",
+      "keyboardShortcuts.openMenu.enabled": "BoolPref",
+      "keyboardShortcuts.openMenu.combo": "CharPref",
       "lastAppVersion": "CharPref",
       "lastVersion": "CharPref",
       "log": "BoolPref",
diff --git a/tests/marionette/rp_puppeteer/ui/menu.py b/tests/marionette/rp_puppeteer/ui/menu.py
index 52ec0c7..a0b5c5a 100644
--- a/tests/marionette/rp_puppeteer/ui/menu.py
+++ b/tests/marionette/rp_puppeteer/ui/menu.py
@@ -22,18 +22,20 @@ class Menu(BaseLib):
         return int(match.group(1))
 
     def open(self, trigger="api"):
-        if trigger == "button":
+        if callable(trigger):
+            trigger()
+        elif trigger == "button":
             self._toolbar_button.click()
-            Wait(self.marionette).until(lambda _: self._popup_state == "open")
         elif trigger == "shortcut":
             window = Windows(lambda: self.marionette).current
             window.send_shortcut("r", alt=True, shift=True)
-            Wait(self.marionette).until(lambda _: self._popup_state == "open")
         elif trigger == "api":
             self._ensure_popup_state("open")
         else:
             raise ValueError("Unknown opening method: \"{}\"".format(trigger))
 
+        Wait(self.marionette, timeout=1).until(lambda _: self.is_open())
+
     def is_open(self):
         return self._popup_state == "open"
 
diff --git a/tests/marionette/tests/menu/test_open.py b/tests/marionette/tests/menu/test_open.py
new file mode 100644
index 0000000..00ae4df
--- /dev/null
+++ b/tests/marionette/tests/menu/test_open.py
@@ -0,0 +1,46 @@
+# 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/.
+
+from rp_ui_harness import RequestPolicyTestCase
+from marionette_driver.errors import TimeoutException
+import time
+
+
+class TestOpenMenu(RequestPolicyTestCase):
+
+    def test_shortcut_disabled(self):
+        pref_name = ("extensions.requestpolicy."
+                     "keyboardShortcuts.openMenu.enabled")
+        self.prefs.set_pref(pref_name, False)
+        time.sleep(0.001)
+
+        self.assertFalse(self.menu.is_open())
+        with self.assertRaises(TimeoutException):
+            self.menu.open(trigger="shortcut")
+        self.assertFalse(self.menu.is_open())
+
+        self.prefs.reset_pref(pref_name)
+        time.sleep(0.001)
+
+    def test_custom_shortcut_combo(self):
+        pref_name = ("extensions.requestpolicy."
+                     "keyboardShortcuts.openMenu.combo")
+        self.prefs.set_pref(pref_name, "control alt shift x")
+        time.sleep(0.001)
+
+        def press_shortcut():
+            self.browser.send_shortcut("x", ctrl=True, alt=True, shift=True)
+
+        self.assertFalse(self.menu.is_open())
+        # The default shortcut should not open the menu anymore
+        with self.assertRaises(TimeoutException):
+            self.menu.open(trigger="shortcut")
+        self.assertFalse(self.menu.is_open())
+        # Test the custom combo
+        self.menu.open(trigger=press_shortcut)
+        self.assertTrue(self.menu.is_open())
+
+        self.menu.close()
+        self.prefs.reset_pref(pref_name)
+        time.sleep(0.001)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/requestpolicy.git



More information about the Pkg-mozext-commits mailing list