Bug 536027 - Remove the commandDispatcher.focusedWindow hack. r=asuth
authorSiddharth Agarwal <sid.bugzilla@gmail.com>
Wed, 20 Jan 2010 17:31:52 +0530
changeset 4723 6e6301bf636df777632ce7f55234efd06579bed8
parent 4722 cac44d84ba0a1e05483d35d3d69ffd722035be03
child 4724 c93c2e58a3feaa0f9528d89db83aeeef4273ecf7
push id3694
push usersid.bugzilla@gmail.com
push dateWed, 20 Jan 2010 12:02:58 +0000
treeherdercomm-central@c93c2e58a3fe [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersasuth
bugs536027
Bug 536027 - Remove the commandDispatcher.focusedWindow hack. r=asuth We had to use a hack to set focus on the message pane in 1.9.1 -- if the enclosing window was not focused, we had to set the document's commandDispatcher.focusedWindow to the message pane. This is no longer necessary with 1.9.2 -- we can simply call focus() on the message pane's browser element.
mail/base/content/mailTabs.js
--- a/mail/base/content/mailTabs.js
+++ b/mail/base/content/mailTabs.js
@@ -367,18 +367,17 @@ let mailTabType = {
         // wouldn't have selected a message at that point, so set the title
         // here
         aTab.mode.onTitleChanged.call(this, aTab, null, aArgs.msgHdr);
 
         aTab.folderDisplay.selectMessage(aArgs.msgHdr);
 
         // Once we're brought into the foreground, the message pane should
         // get focus
-        aTab._focusedWindow = GetMessagePaneFrame();
-        aTab._focusedElement = null;
+        aTab._focusedElement = document.getElementById("messagepane");
 
         // we only want to make it active after setting up the view and the message
         //  to avoid generating bogus summarization events.
         if (!background) {
           aTab.folderDisplay.makeActive();
           this.restoreFocus(aTab);
         }
         else {
@@ -545,33 +544,41 @@ let mailTabType = {
   },
 
   closeTab: function(aTab) {
     aTab.folderDisplay.close();
   },
 
   /**
    * Save off the tab's currently focused element or window.
-   * - If the message pane or summary is currently focused, we'll save this as
-   *   our focused window. We won't try to save the focused element, as they'll
-   *   be rebuilt every time we switch panes and it'll be too hard for us to do
-   *   that.
+   * - If the message pane or summary is currently focused, save the
+   *   corresponding browser element as the focused element.
    * - If the thread tree or folder tree is focused, save that as the focused
-   *   element and the toplevel window as the focused window.
+   *   element.
    */
   saveFocus: function mailTabType_saveFocus(aTab) {
+    dump("focused window = " + document.commandDispatcher.focusedWindow.top + "\n");
+    dump("focused element = " + document.commandDispatcher.focusedElement + "\n");
+    let focusManager = Components.classes["@mozilla.org/focus-manager;1"]
+                                 .getService(Components.interfaces.nsIFocusManager);
+    let fw = {};
+    let fe = focusManager.getFocusedElementForWindow(window, true, fw);
+    dump("focusmanager focused window = " + fw.value + "\n");
+    dump("focusmanager focused element = " + fe + "\n");
     let focusedWindow = document.commandDispatcher.focusedWindow.top;
-    if (focusedWindow == document.getElementById("messagepane").contentWindow ||
-        focusedWindow == document.getElementById("multimessage").contentWindow) {
-      aTab._focusedWindow = focusedWindow;
-      aTab._focusedElement = null;
+
+    let messagepane = document.getElementById("messagepane");
+    let multimessage = document.getElementById("multimessage");
+    if (focusedWindow == messagepane.contentWindow) {
+      aTab._focusedElement = messagepane;
+    }
+    else if (focusedWindow == multimessage.contentWindow) {
+      aTab._focusedElement = multimessage;
     }
     else {
-      aTab._focusedWindow = window;
-
       // Look for children as well. This logic is copied from the mail 3pane
       // version of WhichPaneHasFocus().
       let focusedElement = document.commandDispatcher.focusedElement;
       let threadTree = document.getElementById("threadTree");
       let folderTree = document.getElementById("folderTree");
       while (focusedElement && focusedElement != threadTree &&
              focusedElement != folderTree)
         focusedElement = focusedElement.parentNode;
@@ -584,35 +591,21 @@ let mailTabType = {
 
   /**
    * Restore the tab's focused element or window.
    */
   restoreFocus: function mailTabType_restoreFocus(aTab) {
     // There seem to be issues with opening multiple messages at once, so allow
     // things to stabilize a bit before proceeding
     let reallyRestoreFocus = function mailTabType_reallyRestoreFocus(aTab) {
-      if ("_focusedWindow" in aTab && aTab._focusedWindow) {
-        let windowWatcher = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
-                              .getService(Components.interfaces.nsIWindowWatcher);
-        if (windowWatcher.activeWindow == window)
-          aTab._focusedWindow.focus();
-        else
-          // We can't focus() the window if it's in the background relative to
-          // other windows within this process, or it'll steal focus. Set the
-          // focused window instead, so that when this window receives focus
-          // again, we'll focus the right thing.
-          // XXX This should only be needed for 1.9.1
-          document.commandDispatcher.focusedWindow = aTab._focusedWindow;
-      }
       if ("_focusedElement" in aTab && aTab._focusedElement)
         aTab._focusedElement.focus();
-      aTab._focusedWindow = aTab._focusedElement = null;
+      aTab._focusedElement = null;
     };
 
-    let self = this;
     window.setTimeout(reallyRestoreFocus, 0, aTab);
   },
 
   saveTabState: function(aTab) {
     // Now let other tabs have a primary browser if they want.
     document.getElementById("messagepane").setAttribute("type",
                                                         "content-targetable");