Bug 1514632 - Load chat preview and cloudfile account settings only if needed; r=aceman
authorGeoff Lankow <geoff@darktrojan.net>
Mon, 31 Dec 2018 12:17:16 +1300
changeset 34085 1025d59f72597772038884ab12a4e1fc648213c5
parent 34084 3375b11b89fa06fcafed9f2fc2b3082b1fa67314
child 34086 92f2f9d3268cf833b2860a0cdf5a443e6e5562d8
push id389
push userclokep@gmail.com
push dateMon, 18 Mar 2019 19:01:53 +0000
reviewersaceman
bugs1514632
Bug 1514632 - Load chat preview and cloudfile account settings only if needed; r=aceman
mail/components/preferences/aboutPreferences.xml
mail/components/preferences/applications.inc.xul
mail/components/preferences/applications.js
mail/components/preferences/chat.inc.xul
mail/components/preferences/chat.js
mail/components/preferences/preferences.js
--- a/mail/components/preferences/aboutPreferences.xml
+++ b/mail/components/preferences/aboutPreferences.xml
@@ -117,16 +117,16 @@
                 if (aPaneElement.contentHeight + verticalPadding < targetHeight)
                   aPaneElement._content.style.height = targetHeight - verticalPadding + "px";
               }
               break;
             }
           }
           let event = document.createEvent("Events");
           event.initEvent("paneSelected", true, true);
-          this.dispatchEvent(event);
+          this.currentPane.dispatchEvent(event);
         ]]>
         </body>
       </method>
     </implementation>
   </binding>
 
 </bindings>
--- a/mail/components/preferences/applications.inc.xul
+++ b/mail/components/preferences/applications.inc.xul
@@ -150,18 +150,17 @@
                        onsyncfrompreference="return gCloudFileTab.readThreshold();"
                        onsynctopreference="return gCloudFileTab.writeThreshold();"/>
               <label control="cloudFileThreshold"
                      value="&enableCloudFileAccountOffer.mb;"/>
             </hbox>
             <hbox flex="1">
               <vbox id="provider-listing">
                 <richlistbox id="cloudFileView" orient="vertical" flex="1"
-                             seltype="single"
-                             onselect="gCloudFileTab.onSelectionChanged();">
+                             seltype="single">
                 </richlistbox>
                 <separator class="thin"/>
                 <hbox>
                   <button id="addCloudFileAccount"
                           flex="1"
                           label="&addCloudFileAccount1.label;"
                           accesskey="&addCloudFileAccount1.accesskey;"
                           oncommand="gCloudFileTab.addCloudFileAccount();"/>
--- a/mail/components/preferences/applications.js
+++ b/mail/components/preferences/applications.js
@@ -1,13 +1,14 @@
 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  * 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/. */
 
+/* import-globals-from preferences.js */
 // applications.inc.xul
 /* globals ICON_URL_APP */
 // mail/base/content/contentAreaClick.js
 /* globals openLinkExternally */
 
 // ------------------------------
 // Constants & Enumeration Values
 
@@ -406,79 +407,100 @@ var gApplicationsTabController = {
   mDefaultIndex: 1,
 
   init() {
     if (this.mInitialized)
       return;
 
     gApplicationsPane.init();
 
-    let tabbox = document.getElementById("attachmentPrefs");
+    this.mTabBox = document.getElementById("attachmentPrefs");
 
     // If BigFiles is disabled, hide the "Outgoing" tab, and the tab
     // selectors, and bail out.
     if (!Services.prefs.getBoolPref("mail.cloud_files.enabled")) {
       // Default to the first tab, "Incoming"
-      tabbox.selectedIndex = 0;
+      this.mTabBox.selectedIndex = 0;
       // Hide the tab selector
       let tabs = document.getElementById("attachmentPrefsTabs");
       tabs.hidden = true;
       this.mInitialized = true;
       return;
     }
 
     gCloudFileTab.init();
+    this.paneSelectionChanged = this.paneSelectionChanged.bind(this);
+    window.addEventListener("paneSelected", this.paneSelectionChanged);
 
     if (!(("arguments" in window) && window.arguments[1])) {
       // If no tab was specified, select the last used tab.
       let preference = document.getElementById("mail.preferences.applications.selectedTabIndex");
-      tabbox.selectedIndex = preference.value != null ? preference.value : this.mDefaultIndex;
+      this.mTabBox.selectedIndex = preference.value != null ? preference.value : this.mDefaultIndex;
     }
 
     this.mInitialized = true;
   },
 
-  tabSelectionChanged() {
-    if (this.mInitialized)
-      document.getElementById("mail.preferences.applications.selectedTabIndex")
-              .valueFromPreferences = document.getElementById("attachmentPrefs")
-              .selectedIndex;
+  paneSelectionChanged() {
+    gCloudFileTab.init();
   },
 
+  tabSelectionChanged() {
+    if (this.mInitialized) {
+      document.getElementById("mail.preferences.applications.selectedTabIndex")
+              .valueFromPreferences = this.mTabBox.selectedIndex;
+    }
+
+    gCloudFileTab.init();
+  },
 };
 
 var gCloudFileTab = {
   _initialized: false,
+  _initializationStarted: false,
   _list: null,
   _settings: null,
   _settingsDeck: null,
   _tabpanel: null,
   _accountCache: {},
   _settingsPanelWrap: null,
   _defaultPanel: null,
   _loadingPanel: null,
   _authErrorPanel: null,
 
   get _strings() {
     return Services.strings
                    .createBundle("chrome://messenger/locale/preferences/applications.properties");
   },
 
   init() {
-    if (this._initialized)
+    // Because this leads to another document being loaded, do it only when really necessary.
+    if (this._initializationStarted) {
+      return;
+    }
+    if (getCurrentPaneID() != "paneApplications") {
       return;
+    }
+    if (gApplicationsTabController.mTabBox.selectedIndex != 1) {
+      return;
+    }
+
+    this._initializationStarted = true;
+    window.removeEventListener("paneSelected", gApplicationsTabController.paneSelectionChanged);
 
     this._list = document.getElementById("cloudFileView");
     this._removeAccountButton = document.getElementById("removeCloudFileAccount");
     this._settingsDeck = document.getElementById("cloudFileSettingsDeck");
     this._defaultPanel = document.getElementById("cloudFileDefaultPanel");
     this._settingsPanelWrap = document.getElementById("cloudFileSettingsWrapper");
     this._loadingPanel = document.getElementById("cloudFileLoadingPanel");
     this._authErrorPanel = document.getElementById("cloudFileAuthErrorPanel");
 
+    this.onSelectionChanged = this.onSelectionChanged.bind(this);
+    this._list.addEventListener("select", this.onSelectionChanged);
     this.rebuildView();
 
     if (this._list.itemCount > 0) {
       this._list.selectedIndex = 0;
       this._removeAccountButton.disabled = false;
     }
 
     window.addEventListener("unload", this, {capture: false, once: true});
--- a/mail/components/preferences/chat.inc.xul
+++ b/mail/components/preferences/chat.inc.xul
@@ -1,16 +1,18 @@
 # 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/.
   <prefpane id="paneChat" label="&paneChat.title;" onpaneload="gChatPane.init();">
     <script type="application/javascript" src="chrome://messenger/content/preferences/chat.js"/>
     <script type="application/javascript" src="chrome://messenger/content/preferences/messagestyle.js"/>
 
     <preferences id="chatPreferences">
+      <preference id="mail.preferences.chat.selectedTabIndex"
+                  name="mail.preferences.chat.selectedTabIndex" type="int"/>
       <preference id="messenger.startup.action"            name="messenger.startup.action"     type="int"/>
       <preference id="purple.conversations.im.send_typing" name="purple.conversations.im.send_typing" type="bool"/>
       <preference id="messenger.status.reportIdle"         name="messenger.status.reportIdle"         type="bool"
                   onchange="gChatPane.updateDisabledState();"/>
       <preference id="messenger.status.timeBeforeIdle"     name="messenger.status.timeBeforeIdle"     type="int"/>
       <preference id="messenger.status.awayWhenIdle"       name="messenger.status.awayWhenIdle"       type="bool"
                   onchange="gChatPane.updateMessageDisabledState();"/>
       <preference id="messenger.status.defaultIdleAwayMessage" name="messenger.status.defaultIdleAwayMessage" type="wstring"/>
@@ -27,17 +29,17 @@
       <preference id="messenger.options.messagesStyle.theme"      name="messenger.options.messagesStyle.theme"      type="string"/>
       <preference id="messenger.options.messagesStyle.variant"    name="messenger.options.messagesStyle.variant"    type="string"/>
       <preference id="messenger.options.messagesStyle.showHeader" name="messenger.options.messagesStyle.showHeader" type="bool"/>
     </preferences>
 
     <stringbundle id="themesBundle"
                   src="chrome://messenger/locale/preferences/messagestyle.properties"/>
 
-    <tabbox id="chatPrefs" flex="1">
+    <tabbox id="chatPrefs" flex="1" onselect="gChatPane.tabSelectionChanged();">
       <tabs id="chatPrefsTabs">
         <tab id="chatGeneralTab"       label="&generalTab.label;"/>
         <tab id="chatMessageStylesTab" label="&messageStyleTab.title;"/>
       </tabs>
 
       <tabpanels id="chatPrefsPanels" flex="1">
 
         <tabpanel orient="vertical">
@@ -202,19 +204,16 @@
             <vbox flex="1" id="noPreviewScreen" align="center" pack="center">
               <hbox id="noPreviewBox" align="top">
                 <vbox id="noPreviewInnerBox" flex="1">
                   <label id="noPreviewTitle" value="&messageStyleNoPreview.title;"/>
                   <description id="noAccountDesc">&messageStyleNoPreview.description;</description>
                 </vbox>
               </hbox>
             </vbox>
-            <browser flex="1" id="previewbrowser" type="content"
-                     style="-moz-binding: url(chrome://chat/content/convbrowser.xml#browser)"
-                     tooltip="aHTMLTooltip"/>
           </deck>
           <hbox align="baseline">
             <label value="&messageStyleVariant.label;" control="themevariant"
                    accesskey="&messageStyleVariant.accesskey;"/>
             <menulist id="themevariant"
                       preference="messenger.options.messagesStyle.variant"
                       onselect="previewObserver.currentVariantChanged();"/>
           </hbox>
--- a/mail/components/preferences/chat.js
+++ b/mail/components/preferences/chat.js
@@ -1,20 +1,68 @@
 /* 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/. */
 
+/* import-globals-from preferences.js */
 // messagestyle.js
 /* globals previewObserver */
 
 var gChatPane = {
+  mInitialized: false,
+
   init() {
-    previewObserver.load();
     this.updateDisabledState();
     this.updatePlaySound();
+
+    let preference = document.getElementById("mail.preferences.chat.selectedTabIndex");
+    this.mTabBox = document.getElementById("chatPrefs");
+    this.mTabBox.selectedIndex = preference.value != null ? preference.value : 0;
+
+    window.addEventListener("paneSelected", this.paneSelectionChanged);
+
+    this.mInitialized = true;
+  },
+
+  paneSelectionChanged() {
+    gChatPane.initPreview(); // Can't use "this", as it's probably not gChatPane.
+  },
+
+  tabSelectionChanged() {
+    if (this.mInitialized) {
+      document.getElementById("mail.preferences.chat.selectedTabIndex")
+              .valueFromPreferences = this.mTabBox.selectedIndex;
+    }
+
+    this.initPreview();
+  },
+
+  initPreview() {
+    // We add this browser only when really necessary.
+    let previewDeck = document.getElementById("previewDeck");
+    if (previewDeck.querySelector("browser")) {
+      return;
+    }
+    if (getCurrentPaneID() != "paneChat") {
+      return;
+    }
+    if (this.mTabBox.selectedIndex != 1) {
+      return;
+    }
+
+    window.removeEventListener("paneSelected", this.paneSelectionChanged);
+
+    let browser = document.createElement("browser");
+    browser.setAttribute("id", "previewbrowser");
+    browser.setAttribute("type", "content");
+    browser.setAttribute("flex", "1");
+    browser.setAttribute("style", "-moz-binding: url(chrome://chat/content/convbrowser.xml#browser)");
+    browser.setAttribute("tooltip", "aHTMLTooltip");
+    previewDeck.appendChild(browser);
+    previewObserver.load();
   },
 
   updateDisabledState() {
     let checked = document.getElementById("messenger.status.reportIdle").value;
     document.querySelectorAll(".idle-reporting-enabled").forEach(e => {
       e.disabled = !checked;
     });
   },
--- a/mail/components/preferences/preferences.js
+++ b/mail/components/preferences/preferences.js
@@ -75,8 +75,16 @@ function selectPaneAndTab(prefWindow, aP
  * @param aTabID        ID of tab to select on the prefpane
  * @param aSubdialogID  ID of button to activate, opening a subdialog
  */
 function showTab(aPane, aTabID, aSubdialogID) {
   aPane.querySelector("tabbox").selectedTab = document.getElementById(aTabID);
   if (aSubdialogID)
     setTimeout(function() { document.getElementById(aSubdialogID).click(); }, 0);
 }
+
+/**
+ * Get the ID of the current pane.
+ */
+function getCurrentPaneID() {
+  let prefWindow = document.getElementById("MailPreferences");
+  return prefWindow.currentPane ? prefWindow.currentPane.id : null;
+}