From bc7556d02841cb6b205800ade68e31afd5784d25 Mon Sep 17 00:00:00 2001 From: baku Date: Wed, 11 Jan 2017 06:52:14 +0000 Subject: [PATCH] Issue #74: pinned tabs sorted --- index.js | 67 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/index.js b/index.js index 91656d9..6f31882 100644 --- a/index.js +++ b/index.js @@ -177,42 +177,51 @@ let ContainerService = { }, sortTabs() { - return new Promise(resolve => { - for (let window of windows.browserWindows) { - // From model to XUL window. - window = viewFor(window); + function sortTabsInternal(window, pinnedTabs) { + // From model to XUL window. + const xulWindow = viewFor(window); - let tabs = tabsUtils.getTabs(window); + const tabs = tabsUtils.getTabs(xulWindow); + let pos = 0; - let pos = 0; - - // Let"s collect UCIs/tabs for this window. - let map = new Map; - for (let tab of tabs) { - if (tabsUtils.isPinned(tab)) { - // pinned tabs must be consider as taken positions. - ++pos; - continue; - } - - let userContextId = parseInt(tab.getAttribute("usercontextid") || 0, 10); - if (!map.has(userContextId)) { - map.set(userContextId, []); - } - map.get(userContextId).push(tab); + // Let's collect UCIs/tabs for this window. + let map = new Map; + for (const tab of tabs) { + if (pinnedTabs && !tabsUtils.isPinned(tab)) { + // We don't have, or we already handled all the pinned tabs. + break; } - // Let"s sort the map. - let sortMap = new Map([...map.entries()].sort((a, b) => a[0] > b[0])); + if (!pinnedTabs && tabsUtils.isPinned(tab)) { + // pinned tabs must be consider as taken positions. + ++pos; + continue; + } - // Let"s move tabs. - sortMap.forEach(tabs => { - for (let tab of tabs) { - window.gBrowser.moveTabTo(tab, pos++); - } - }); + let userContextId = parseInt(tab.getAttribute("usercontextid") || 0, 10); + if (!map.has(userContextId)) { + map.set(userContextId, []); + } + map.get(userContextId).push(tab); } + // Let's sort the map. + const sortMap = new Map([...map.entries()].sort((a, b) => a[0] > b[0])); + + // Let's move tabs. + sortMap.forEach(tabs => { + for (const tab of tabs) { + xulWindow.gBrowser.moveTabTo(tab, pos++); + } + }); + } + + return new Promise(resolve => { + for (let window of windows.browserWindows) { + // First the pinned tabs, then the normal ones. + sortTabsInternal(window, true); + sortTabsInternal(window, false); + } resolve(null); }); },