Bug 1551902 - Hide protections panel when focus is lost. r=johannh
authorNihanth Subramanya <nhnt11@gmail.com>
Thu, 06 Jun 2019 15:17:58 +0000
changeset 477618 62c30d2773bcb1841ae354f455308c8505f641fc
parent 477617 a24793c196ea825cd3feb85f59cd3e76395d7d0e
child 477619 b8c5f94d8702448dad548ae94d72365103e3a2ce
push id36119
push userncsoregi@mozilla.com
push dateThu, 06 Jun 2019 21:52:09 +0000
treeherdermozilla-central@6a81efd823db [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjohannh
bugs1551902
milestone69.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1551902 - Hide protections panel when focus is lost. r=johannh Differential Revision: https://phabricator.services.mozilla.com/D32980
browser/base/content/browser-siteProtections.js
browser/components/controlcenter/content/protectionsPanel.inc.xul
--- a/browser/base/content/browser-siteProtections.js
+++ b/browser/base/content/browser-siteProtections.js
@@ -45,27 +45,59 @@ var gProtectionsHandler = {
 
     // Now open the popup, anchored off the primary chrome element
     PanelMultiView.openPopup(this._protectionsPopup, gIdentityHandler._identityIcon, {
       position: "bottomcenter topleft",
       triggerEvent: event,
     }).catch(Cu.reportError);
   },
 
+  onPopupShown(event) {
+    if (event.target == this._protectionsPopup) {
+      window.addEventListener("focus", this, true);
+    }
+  },
+
+  onPopupHidden(event) {
+    if (event.target == this._protectionsPopup) {
+      window.removeEventListener("focus", this, true);
+      this._protectionsPopup.removeAttribute("open");
+    }
+  },
+
+  handleEvent(event) {
+    let elem = document.activeElement;
+    let position = elem.compareDocumentPosition(this._protectionsPopup);
+
+    if (!(position & (Node.DOCUMENT_POSITION_CONTAINS |
+                      Node.DOCUMENT_POSITION_CONTAINED_BY)) &&
+        !this._protectionsPopup.hasAttribute("noautohide")) {
+      // Hide the panel when focusing an element that is
+      // neither an ancestor nor descendant unless the panel has
+      // @noautohide (e.g. for a tour).
+      PanelMultiView.hidePopup(this._protectionsPopup);
+    }
+  },
+
   refreshProtectionsPopup() {
     // Refresh the state of the TP toggle switch.
     this._protectionsPopupTPSwitch.toggleAttribute("enabled",
       !this._protectionsPopup.hasAttribute("hasException"));
 
     let host = gIdentityHandler.getHostForDisplay();
 
     // Push the appropriate strings out to the UI.
     this._protectionsPopupMainViewHeaderLabel.textContent =
       // gNavigatorBundle.getFormattedString("protections.header", [host]);
       `Tracking Protections for ${host}`;
+
+    let currentlyEnabled =
+      !this._protectionsPopup.hasAttribute("hasException");
+
+    this._protectionsPopupTPSwitch.toggleAttribute("enabled", currentlyEnabled);
   },
 
   async onTPSwitchCommand(event) {
     // When the switch is clicked, we wait 500ms and then disable/enable
     // protections, causing the page to refresh, and close the popup.
     // We need to ensure we don't handle more clicks during the 500ms delay,
     // so we keep track of state and return early if needed.
     if (this._TPSwitchCommanding) {
--- a/browser/components/controlcenter/content/protectionsPanel.inc.xul
+++ b/browser/components/controlcenter/content/protectionsPanel.inc.xul
@@ -2,16 +2,18 @@
    - 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/. -->
 
 <panel id="protections-popup"
        type="arrow"
        hidden="true"
        photon="true"
        role="alertdialog"
+       onpopupshown="gProtectionsHandler.onPopupShown(event);"
+       onpopuphidden="gProtectionsHandler.onPopupHidden(event);"
        orient="vertical">
 
   <panelmultiview id="protections-popup-multiView"
                   mainViewId="protections-popup-mainView">
     <panelview id="protections-popup-mainView"
                descriptionheightworkaround="true">
       <vbox id="protections-popup-mainView-panel-header">
         <label>