browser-customization.js (4658B)
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- 2 * This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 /** 7 * Customization handler prepares this browser window for entering and exiting 8 * customization mode by handling customizationstarting and aftercustomization 9 * events. 10 */ 11 var CustomizationHandler = { 12 handleEvent(aEvent) { 13 switch (aEvent.type) { 14 case "customizationstarting": 15 this._customizationStarting(); 16 break; 17 case "aftercustomization": 18 this._afterCustomization(); 19 break; 20 } 21 }, 22 23 isCustomizing() { 24 return document.documentElement.hasAttribute("customizing"); 25 }, 26 27 _customizationStarting() { 28 // Disable the toolbar context menu items 29 let menubar = document.getElementById("main-menubar"); 30 for (let childNode of menubar.children) { 31 childNode.setAttribute("disabled", true); 32 } 33 34 UpdateUrlbarSearchSplitterState(); 35 36 PlacesToolbarHelper.customizeStart(); 37 }, 38 39 _afterCustomization() { 40 // Update global UI elements that may have been added or removed 41 if (AppConstants.platform != "macosx") { 42 updateEditUIVisibility(); 43 } 44 45 PlacesToolbarHelper.customizeDone(); 46 47 XULBrowserWindow.asyncUpdateUI(); 48 // Re-enable parts of the UI we disabled during the dialog 49 let menubar = document.getElementById("main-menubar"); 50 for (let childNode of menubar.children) { 51 childNode.removeAttribute("disabled"); 52 } 53 54 gBrowser.selectedBrowser.focus(); 55 56 // Update the urlbar 57 gURLBar.setURI(); 58 UpdateUrlbarSearchSplitterState(); 59 }, 60 }; 61 62 var AutoHideMenubar = { 63 get _node() { 64 delete this._node; 65 return (this._node = document.getElementById("toolbar-menubar")); 66 }, 67 68 _contextMenuListener: { 69 contextMenu: null, 70 71 get active() { 72 return !!this.contextMenu; 73 }, 74 75 init(event) { 76 // Ignore mousedowns in <menupopup>s. 77 if (event.target.closest("menupopup")) { 78 return; 79 } 80 81 let contextMenuId = AutoHideMenubar._node.getAttribute("context"); 82 this.contextMenu = document.getElementById(contextMenuId); 83 this.contextMenu.addEventListener("popupshown", this); 84 this.contextMenu.addEventListener("popuphiding", this); 85 AutoHideMenubar._node.addEventListener("mousemove", this); 86 }, 87 handleEvent(event) { 88 switch (event.type) { 89 case "popupshown": 90 AutoHideMenubar._node.removeEventListener("mousemove", this); 91 break; 92 case "popuphiding": 93 case "mousemove": 94 AutoHideMenubar._setInactiveAsync(); 95 AutoHideMenubar._node.removeEventListener("mousemove", this); 96 this.contextMenu.removeEventListener("popuphiding", this); 97 this.contextMenu.removeEventListener("popupshown", this); 98 this.contextMenu = null; 99 break; 100 } 101 }, 102 }, 103 104 init() { 105 this._node.addEventListener("toolbarvisibilitychange", this); 106 if (this._node.hasAttribute("autohide")) { 107 this._enable(); 108 } 109 }, 110 111 _updateState() { 112 if (this._node.hasAttribute("autohide")) { 113 this._enable(); 114 } else { 115 this._disable(); 116 } 117 }, 118 119 _events: [ 120 "DOMMenuBarInactive", 121 "DOMMenuBarActive", 122 "popupshowing", 123 "mousedown", 124 ], 125 _enable() { 126 this._node.setAttribute("inactive", "true"); 127 for (let event of this._events) { 128 this._node.addEventListener(event, this); 129 } 130 }, 131 132 _disable() { 133 this._setActive(); 134 for (let event of this._events) { 135 this._node.removeEventListener(event, this); 136 } 137 }, 138 139 handleEvent(event) { 140 switch (event.type) { 141 case "toolbarvisibilitychange": 142 this._updateState(); 143 break; 144 case "popupshowing": 145 // fall through 146 case "DOMMenuBarActive": 147 this._setActive(); 148 break; 149 case "mousedown": 150 if (event.button == 2) { 151 this._contextMenuListener.init(event); 152 } 153 break; 154 case "DOMMenuBarInactive": 155 if (!this._contextMenuListener.active) { 156 this._setInactiveAsync(); 157 } 158 break; 159 } 160 }, 161 162 _setInactiveAsync() { 163 this._inactiveTimeout = setTimeout(() => { 164 if (this._node.hasAttribute("autohide")) { 165 this._inactiveTimeout = null; 166 this._node.setAttribute("inactive", "true"); 167 } 168 }, 0); 169 }, 170 171 _setActive() { 172 if (this._inactiveTimeout) { 173 clearTimeout(this._inactiveTimeout); 174 this._inactiveTimeout = null; 175 } 176 this._node.removeAttribute("inactive"); 177 }, 178 };