Bug 681005 - Restore pinned tabs before normal tabs r=zpao

This commit is contained in:
Andres Hernandez [:andreshm] 2012-06-18 16:19:26 -06:00
parent f440f14fe4
commit b5f6b91365
2 changed files with 218 additions and 70 deletions

View File

@ -2823,6 +2823,108 @@ let SessionStoreInternal = {
this._sendRestoreCompletedNotifications();
},
/**
* Sets the tabs restoring order with the following priority:
* Selected tab, pinned tabs, visible tabs, unhidden tabs and hidden tabs.
* @param aTabBrowser
* Tab browser object
* @param aTabs
* Array of tab references
* @param aTabData
* Array of tab data
* @param aSelectedTab
* Index of selected tab
*/
_setTabsRestoringOrder : function ssi__setTabsRestoringOrder(
aTabBrowser, aTabs, aTabData, aSelectedTab) {
// Temporally store the pinned tabs before moving the hidden tabs and
// optimizing the visible tabs. In case the selected tab is a pinned tab,
// the index is also stored.
let pinnedTabs = aTabData.filter(function (aData) aData.pinned).length;
let pinnedTabsArray = [];
let pinnedTabsDataArray = [];
let pinnedSelectedTab = null;
if (pinnedTabs && aTabs.length > 1) {
for (let t = aTabs.length - 1; t >= 0; t--) {
if (aTabData[t].pinned) {
pinnedTabsArray.unshift(aTabs.splice(t, 1)[0]);
pinnedTabsDataArray.unshift(aTabData.splice(t, 1)[0]);
if (aSelectedTab) {
if (aSelectedTab > (t + 1))
--aSelectedTab;
else if (aSelectedTab == (t + 1)) {
aSelectedTab = null;
pinnedSelectedTab = 1;
}
} else if (pinnedSelectedTab)
++pinnedSelectedTab;
}
}
}
// Without the pinned tabs, we move the hidden tabs to the end of the list
// and optimize the visible tabs.
let unhiddenTabs = aTabData.filter(function (aData) !aData.hidden).length;
if (unhiddenTabs && aTabs.length > 1) {
// Load hidden tabs last, by pushing them to the end of the list.
for (let t = 0, tabsToReorder = aTabs.length - unhiddenTabs; tabsToReorder > 0; ) {
if (aTabData[t].hidden) {
aTabs = aTabs.concat(aTabs.splice(t, 1));
aTabData = aTabData.concat(aTabData.splice(t, 1));
if (aSelectedTab && aSelectedTab > t)
--aSelectedTab;
--tabsToReorder;
continue;
}
++t;
}
// Determine if we can optimize & load visible tabs first
let maxVisibleTabs = Math.ceil(aTabBrowser.tabContainer.mTabstrip.scrollClientSize /
aTabs[unhiddenTabs - 1].getBoundingClientRect().width);
// Make sure we restore visible tabs first, if there are enough
if (aSelectedTab && maxVisibleTabs < unhiddenTabs && aSelectedTab > 1) {
let firstVisibleTab = 0;
if (unhiddenTabs - maxVisibleTabs > aSelectedTab) {
// aSelectedTab is leftmost since we scroll to it when possible
firstVisibleTab = aSelectedTab - 1;
} else {
// aSelectedTab is rightmost or no more room to scroll right
firstVisibleTab = unhiddenTabs - maxVisibleTabs;
}
aTabs = aTabs.splice(firstVisibleTab, maxVisibleTabs).concat(aTabs);
aTabData = aTabData.splice(firstVisibleTab, maxVisibleTabs).concat(aTabData);
aSelectedTab -= firstVisibleTab;
}
}
// Load the pinned tabs at the beginning of the list and restore the
// selected tab index.
if (pinnedTabsArray) {
// Restore the selected tab index.
if (pinnedSelectedTab) {
aSelectedTab = pinnedSelectedTab;
} else {
aSelectedTab += pinnedTabsArray.length;
}
// Load the pinned tabs at the beginning of the list.
for (let t = pinnedTabsArray.length - 1; t >= 0; t--) {
aTabs.unshift(pinnedTabsArray.splice(t, 1)[0]);
aTabData.unshift(pinnedTabsDataArray.splice(t, 1)[0]);
}
}
// Load the selected tab to the first position.
if (aSelectedTab-- && aTabs[aSelectedTab]) {
aTabs.unshift(aTabs.splice(aSelectedTab, 1)[0]);
aTabData.unshift(aTabData.splice(aSelectedTab, 1)[0]);
aTabBrowser.selectedTab = aTabs[0];
}
return [aTabs, aTabData];
},
/**
* Manage history restoration for a window
* @param aWindow
@ -2875,48 +2977,9 @@ let SessionStoreInternal = {
return;
}
let unhiddenTabs = aTabData.filter(function (aData) !aData.hidden).length;
if (unhiddenTabs && aTabs.length > 1) {
// Load hidden tabs last, by pushing them to the end of the list
for (let t = 0, tabsToReorder = aTabs.length - unhiddenTabs; tabsToReorder > 0; ) {
if (aTabData[t].hidden) {
aTabs = aTabs.concat(aTabs.splice(t, 1));
aTabData = aTabData.concat(aTabData.splice(t, 1));
if (aSelectTab > t)
--aSelectTab;
--tabsToReorder;
continue;
}
++t;
}
// Determine if we can optimize & load visible tabs first
let maxVisibleTabs = Math.ceil(tabbrowser.tabContainer.mTabstrip.scrollClientSize /
aTabs[unhiddenTabs - 1].getBoundingClientRect().width);
// make sure we restore visible tabs first, if there are enough
if (maxVisibleTabs < unhiddenTabs && aSelectTab > 1) {
let firstVisibleTab = 0;
if (unhiddenTabs - maxVisibleTabs > aSelectTab) {
// aSelectTab is leftmost since we scroll to it when possible
firstVisibleTab = aSelectTab - 1;
} else {
// aSelectTab is rightmost or no more room to scroll right
firstVisibleTab = unhiddenTabs - maxVisibleTabs;
}
aTabs = aTabs.splice(firstVisibleTab, maxVisibleTabs).concat(aTabs);
aTabData = aTabData.splice(firstVisibleTab, maxVisibleTabs).concat(aTabData);
aSelectTab -= firstVisibleTab;
}
}
// make sure to restore the selected tab first (if any)
if (aSelectTab-- && aTabs[aSelectTab]) {
aTabs.unshift(aTabs.splice(aSelectTab, 1)[0]);
aTabData.unshift(aTabData.splice(aSelectTab, 1)[0]);
tabbrowser.selectedTab = aTabs[0];
}
// Sets the tabs restoring order.
[aTabs, aTabData] =
this._setTabsRestoringOrder(tabbrowser, aTabs, aTabData, aSelectTab);
// Prepare the tabs so that they can be properly restored. We'll pin/unpin
// and show/hide tabs as necessary. We'll also set the labels, user typed

View File

@ -8,15 +8,23 @@ function test() {
requestLongerTimeout(3);
// builds the tests state based on a few parameters
function buildTestState(num, selected, hidden) {
let state = { windows: [ { "tabs": [], "selected": selected } ] };
function buildTestState(num, selected, hidden, pinned) {
let state = { windows: [ { "tabs": [], "selected": selected + 1 } ] };
while (num--) {
state.windows[0].tabs.push({entries: [{url: "http://example.com/"}]});
state.windows[0].tabs.push({
entries: [
{ url: "http://example.com/?t=" + state.windows[0].tabs.length }
]
});
let i = state.windows[0].tabs.length - 1;
if (hidden.length > 0 && i == hidden[0]) {
state.windows[0].tabs[i].hidden = true;
hidden.splice(0, 1);
}
if (pinned.length > 0 && i == pinned[0]) {
state.windows[0].tabs[i].pinned = true;
pinned.splice(0, 1);
}
}
return state;
}
@ -24,56 +32,128 @@ function test() {
let tests = [
{ testNum: 1,
totalTabs: 13,
selectedTab: 1,
selectedTab: 0,
shownTabs: 6,
hiddenTabs: [],
pinnedTabs: [],
order: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
},
{ testNum: 2,
totalTabs: 13,
selectedTab: 13,
selectedTab: 12,
shownTabs: 6,
hiddenTabs: [],
pinnedTabs: [],
order: [12, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6]
},
{ testNum: 3,
totalTabs: 13,
selectedTab: 4,
selectedTab: 3,
shownTabs: 6,
hiddenTabs: [],
pinnedTabs: [],
order: [3, 4, 5, 6, 7, 8, 0, 1, 2, 9, 10, 11, 12]
},
{ testNum: 4,
totalTabs: 13,
selectedTab: 11,
selectedTab: 10,
shownTabs: 6,
hiddenTabs: [],
pinnedTabs: [],
order: [10, 7, 8, 9, 11, 12, 0, 1, 2, 3, 4, 5, 6]
},
{ testNum: 5,
totalTabs: 13,
selectedTab: 13,
selectedTab: 12,
shownTabs: 6,
hiddenTabs: [0, 4, 9],
pinnedTabs: [],
order: [12, 6, 7, 8, 10, 11, 1, 2, 3, 5, 0, 4, 9]
},
{ testNum: 6,
totalTabs: 13,
selectedTab: 4,
selectedTab: 3,
shownTabs: 6,
hiddenTabs: [1, 7, 12],
pinnedTabs: [],
order: [3, 4, 5, 6, 8, 9, 0, 2, 10, 11, 1, 7, 12]
},
{ testNum: 7,
totalTabs: 13,
selectedTab: 4,
selectedTab: 3,
shownTabs: 6,
hiddenTabs: [0, 1, 2],
pinnedTabs: [],
order: [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 1, 2]
},
{ testNum: 8,
totalTabs: 13,
selectedTab: 0,
shownTabs: 6,
hiddenTabs: [],
pinnedTabs: [0],
order: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
},
{ testNum: 9,
totalTabs: 13,
selectedTab: 1,
shownTabs: 6,
hiddenTabs: [],
pinnedTabs: [0],
order: [1, 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
},
{ testNum: 10,
totalTabs: 13,
selectedTab: 3,
shownTabs: 6,
hiddenTabs: [2],
pinnedTabs: [0,1],
order: [3, 0, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12, 2]
},
{ testNum: 11,
totalTabs: 13,
selectedTab: 12,
shownTabs: 6,
hiddenTabs: [],
pinnedTabs: [0,1,2],
order: [12, 0, 1, 2, 7, 8, 9, 10, 11, 3, 4, 5, 6]
},
{ testNum: 12,
totalTabs: 13,
selectedTab: 6,
shownTabs: 6,
hiddenTabs: [3,4,5],
pinnedTabs: [0,1,2],
order: [6, 0, 1, 2, 7, 8, 9, 10, 11, 12, 3, 4, 5]
},
{ testNum: 13,
totalTabs: 13,
selectedTab: 1,
shownTabs: 6,
hiddenTabs: [3,4,5],
pinnedTabs: [0,1,2],
order: [1, 0, 2, 6, 7, 8, 9, 10, 11, 12, 3, 4, 5]
},
{ testNum: 14,
totalTabs: 13,
selectedTab: 2,
shownTabs: 6,
hiddenTabs: [],
pinnedTabs: [0,1,2],
order: [2, 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
},
{ testNum: 15,
totalTabs: 13,
selectedTab: 3,
shownTabs: 6,
hiddenTabs: [1,4],
pinnedTabs: [0,1,2],
order: [3, 0, 1, 2, 5, 6, 7, 8, 9, 10, 11, 12, 4]
}
];
let tabMinWidth = parseInt(getComputedStyle(gBrowser.selectedTab, null).minWidth);
let tabMinWidth =
parseInt(getComputedStyle(gBrowser.selectedTab, null).minWidth);
let testIndex = 0;
function runNextTest() {
@ -84,41 +164,46 @@ function test() {
info ("Starting test " + (++testIndex));
let test = tests.shift();
let state = buildTestState(test.totalTabs, test.selectedTab, test.hiddenTabs);
let state = buildTestState(test.totalTabs, test.selectedTab,
test.hiddenTabs, test.pinnedTabs);
let tabbarWidth = Math.floor((test.shownTabs - 0.5) * tabMinWidth);
let win = openDialog(location, "_blank", "chrome,all,dialog=no");
let actualOrder = [];
let tabsRestored = [];
win.addEventListener("SSTabRestoring", function onSSTabRestoring(aEvent) {
let tab = aEvent.originalTarget;
let currentIndex = Array.indexOf(win.gBrowser.tabs, tab);
actualOrder.push(currentIndex);
let tabLink = tab.linkedBrowser.currentURI.spec;
let tabIndex =
tabLink.substring(tabLink.indexOf("?t=") + 3, tabLink.length);
if (actualOrder.length < state.windows[0].tabs.length)
// we need to compare with the tab's restoring index, no with the
// position index, since the pinned tabs change the positions in the
// tabbar.
tabsRestored.push(tabIndex);
if (tabsRestored.length < state.windows[0].tabs.length)
return;
// all of the tabs should be restoring or restored by now
is(actualOrder.length, state.windows[0].tabs.length,
is(tabsRestored.length, state.windows[0].tabs.length,
"Test #" + testIndex + ": Number of restored tabs is as expected");
is(actualOrder.join(" "), test.order.join(" "),
is(tabsRestored.join(" "), test.order.join(" "),
"Test #" + testIndex + ": 'visible' tabs restored first");
// Cleanup.
// cleanup
win.removeEventListener("SSTabRestoring", onSSTabRestoring, false);
win.close();
executeSoon(runNextTest);
}, false);
win.addEventListener("load", function onLoad(aEvent) {
win.removeEventListener("load", onLoad, false);
executeSoon(function () {
let extent = win.outerWidth - win.gBrowser.tabContainer.mTabstrip.scrollClientSize;
let windowWidth = tabbarWidth + extent;
win.resizeTo(windowWidth, win.outerHeight);
ss.setWindowState(win, JSON.stringify(state), true);
});
}, false);
whenWindowLoaded(win, function(aEvent) {
let extent =
win.outerWidth - win.gBrowser.tabContainer.mTabstrip.scrollClientSize;
let windowWidth = tabbarWidth + extent;
win.resizeTo(windowWidth, win.outerHeight);
ss.setWindowState(win, JSON.stringify(state), true);
});
};
runNextTest();