Bug 1452970 - Add auto-hide option to Download toolbar icon context menu draft
authorOriol Brufau <oriol-bugzilla@hotmail.com>
Sat, 14 Apr 2018 11:26:03 +0200
changeset 793095 ac72c3c68f3673ca7e17da98a26dadd048604bdf
parent 792051 93443d36d4bd53dba004f7b73430879f96daa681
push id109273
push userbmo:oriol-bugzilla@hotmail.com
push dateWed, 09 May 2018 12:43:21 +0000
bugs1452970
milestone61.0a1
Bug 1452970 - Add auto-hide option to Download toolbar icon context menu MozReview-Commit-ID: VVPQesdzJ6
browser/base/content/browser.js
browser/base/content/browser.xul
browser/components/downloads/test/browser/browser_downloads_autohide.js
browser/locales/en-US/chrome/browser/browser.dtd
--- a/browser/base/content/browser.js
+++ b/browser/base/content/browser.js
@@ -6384,16 +6384,32 @@ function BrowserCharsetReload() {
 function UpdateCurrentCharset(target) {
   let selectedCharset = CharsetMenu.foldCharset(gBrowser.selectedBrowser.characterSet);
   for (let menuItem of target.getElementsByTagName("menuitem")) {
     let isSelected = menuItem.getAttribute("charset") === selectedCharset;
     menuItem.setAttribute("checked", isSelected);
   }
 }
 
+function UpdateDownloadsAutoHide(popup) {
+  let checkbox = popup.querySelector(".customize-context-autoHide");
+  let isDownloads = popup.triggerNode && ["downloads-button", "wrapper-downloads-button"].includes(popup.triggerNode.id);
+  checkbox.hidden = !isDownloads;
+  if (this.window.DownloadsButton.autoHideDownloadsButton) {
+    checkbox.setAttribute("checked", "true");
+  } else {
+    checkbox.removeAttribute("checked");
+  }
+}
+
+function onDownloadsAutoHideChange(event) {
+  let autoHide = event.target.getAttribute("checked") == "true";
+  Services.prefs.setBoolPref("browser.download.autohideButton", autoHide);
+}
+
 var gPageStyleMenu = {
   // This maps from a <browser> element (or, more specifically, a
   // browser's permanentKey) to an Object that contains the most recent
   // information about the browser content's stylesheets. That Object
   // is populated via the PageStyle:StyleSheets message from the content
   // process. The Object should have the following structure:
   //
   // filteredStyleSheets (Array):
--- a/browser/base/content/browser.xul
+++ b/browser/base/content/browser.xul
@@ -327,22 +327,28 @@
                      oncommand="SidebarUI.reversePosition()"/>
       <toolbarseparator/>
       <toolbarbutton label="&sidebarMenuClose.label;"
                      class="subviewbutton"
                      oncommand="SidebarUI.hide()"/>
     </panel>
 
     <menupopup id="toolbar-context-menu"
-               onpopupshowing="onViewToolbarsPopupShowing(event, document.getElementById('viewToolbarsMenuSeparator'));">
+               onpopupshowing="onViewToolbarsPopupShowing(event, document.getElementById('viewToolbarsMenuSeparator')); UpdateDownloadsAutoHide(this)">
       <menuitem oncommand="gCustomizeMode.addToPanel(document.popupNode)"
                 accesskey="&customizeMenu.pinToOverflowMenu.accesskey;"
                 label="&customizeMenu.pinToOverflowMenu.label;"
                 contexttype="toolbaritem"
                 class="customize-context-moveToPanel"/>
+      <menuitem oncommand="onDownloadsAutoHideChange(event)"
+                type="checkbox"
+                accesskey="&customizeMenu.autoHideDownloadsButton.accesskey;"
+                label="&customizeMenu.autoHideDownloadsButton.label;"
+                contexttype="toolbaritem"
+                class="customize-context-autoHide"/>
       <menuitem oncommand="gCustomizeMode.removeFromArea(document.popupNode)"
                 accesskey="&customizeMenu.removeFromToolbar.accesskey;"
                 label="&customizeMenu.removeFromToolbar.label;"
                 contexttype="toolbaritem"
                 class="customize-context-removeFromToolbar"/>
       <menuitem id="toolbar-context-reloadAllTabs"
                 class="toolbaritem-tabsmenu"
                 contexttype="tabbar"
--- a/browser/components/downloads/test/browser/browser_downloads_autohide.js
+++ b/browser/components/downloads/test/browser/browser_downloads_autohide.js
@@ -283,24 +283,92 @@ add_task(async function checkStateWhenHi
     // It doesn't really matter if the button remains unhidden in
     // the palette, and if we move it we'll unhide it then (the other
     // tests check this).
   }
   await BrowserTestUtils.closeWindow(otherWin);
   CustomizableUI.reset();
 });
 
+add_task(async function checkContextMenu() {
+  let contextMenu = document.getElementById("toolbar-context-menu");
+  let checkbox = contextMenu.querySelector(".customize-context-autoHide");
+  let button = document.getElementById("downloads-button");
+
+  is(Services.prefs.getBoolPref(kDownloadAutoHidePref), true,
+     "Pref should be causing us to autohide");
+  is(DownloadsIndicatorView.hasDownloads, false,
+     "Should be no downloads when starting the test");
+  is(button.hidden, true, "Downloads button is hidden");
+
+  info("Simulate a download to show the downloads button.");
+  DownloadsIndicatorView.hasDownloads = true;
+  is(button.hidden, false, "Downloads button is visible");
+
+  info("Check context menu");
+  await openContextMenu(button);
+  is(checkbox.hidden, false, "Auto-hide checkbox is visible");
+  is(checkbox.getAttribute("checked"), "true", "Auto-hide is enabled");
+
+  info("Disable auto-hide via context menu");
+  clickCheckbox(checkbox);
+  is(Services.prefs.getBoolPref(kDownloadAutoHidePref), false,
+     "Pref has been set to false");
+
+  info("Clear downloads");
+  DownloadsIndicatorView.hasDownloads = false;
+  is(button.hidden, false, "Downloads button is still visible");
+
+  info("Check context menu");
+  await openContextMenu(button);
+  is(checkbox.hidden, false, "Auto-hide checkbox is visible");
+  is(checkbox.hasAttribute("checked"), false, "Auto-hide is disabled");
+
+  info("Enable auto-hide via context menu");
+  clickCheckbox(checkbox);
+  is(button.hidden, true, "Downloads button is hidden");
+  is(Services.prefs.getBoolPref(kDownloadAutoHidePref), true,
+     "Pref has been set to true");
+
+  info("Check context menu in another button");
+  await openContextMenu(document.getElementById("home-button"));
+  is(checkbox.hidden, true, "Auto-hide checkbox is hidden");
+  contextMenu.hidePopup();
+
+  info("Open popup directly");
+  contextMenu.openPopup();
+  is(checkbox.hidden, true, "Auto-hide checkbox is hidden");
+  contextMenu.hidePopup();
+});
+
 function promiseCustomizeStart(aWindow = window) {
   return new Promise(resolve => {
     aWindow.gNavToolbox.addEventListener("customizationready", resolve,
                                          {once: true});
     aWindow.gCustomizeMode.enter();
   });
 }
 
 function promiseCustomizeEnd(aWindow = window) {
   return new Promise(resolve => {
     aWindow.gNavToolbox.addEventListener("aftercustomization", resolve,
                                          {once: true});
     aWindow.gCustomizeMode.exit();
   });
 }
 
+async function openContextMenu(element) {
+  let popupShownPromise = BrowserTestUtils.waitForEvent(document, "popupshown");
+  EventUtils.synthesizeMouseAtCenter(element, {type: "contextmenu", button: 2});
+  await popupShownPromise;
+}
+
+function clickCheckbox(checkbox) {
+  // Clicking a checkbox toggles its checkedness first.
+  if (checkbox.getAttribute("checked") == "true") {
+    checkbox.removeAttribute("checked");
+  } else {
+    checkbox.setAttribute("checked", "true");
+  }
+  // Then it runs the command and closes the popup.
+  checkbox.doCommand();
+  checkbox.parentElement.hidePopup();
+}
--- a/browser/locales/en-US/chrome/browser/browser.dtd
+++ b/browser/locales/en-US/chrome/browser/browser.dtd
@@ -421,16 +421,18 @@ These should match what Safari and other
 <!ENTITY customizeMenu.unpinFromOverflowMenu.label "Unpin from Overflow Menu">
 <!ENTITY customizeMenu.unpinFromOverflowMenu.accesskey "U">
 <!ENTITY customizeMenu.removeFromToolbar.label "Remove from Toolbar">
 <!ENTITY customizeMenu.removeFromToolbar.accesskey "R">
 <!ENTITY customizeMenu.removeFromMenu.label "Remove from Menu">
 <!ENTITY customizeMenu.removeFromMenu.accesskey "R">
 <!ENTITY customizeMenu.addMoreItems.label "Add More Items…">
 <!ENTITY customizeMenu.addMoreItems.accesskey "A">
+<!ENTITY customizeMenu.autoHideDownloadsButton.label "Auto-Hide in Toolbar">
+<!ENTITY customizeMenu.autoHideDownloadsButton.accesskey "A">
 
 <!-- LOCALIZATION NOTE (moreMenu.label) This label is used in the new Photon
     app (hamburger) menu. When clicked, it opens a subview that contains
     secondary commands. -->
 <!ENTITY moreMenu.label "More">
 
 <!ENTITY openCmd.commandkey           "l">
 <!ENTITY urlbar.placeholder2          "Search or enter address">