Skip to content

Commit d14a6b6

Browse files
committed
Treat new blank tab as a child of the active pinned tab on certain cases #3296
1 parent 3cace4a commit d14a6b6

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

webextensions/background/handle-new-tabs.js

+43-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,30 @@ function log(...args) {
2727
}
2828

2929

30+
Tab.onBeforeCreate.addListener(async (tab, info) => {
31+
const activeTab = info.activeTab || Tab.getActiveTab(tab.windowId);
32+
33+
// Special case, when all these conditions are true:
34+
// 1) A new blank tab is configured to be opened as a child of the active tab.
35+
// 2) The active tab is pinned.
36+
// 3) Tabs opened from a pinned parent are configured to be placed near the
37+
// opener pinned tab.
38+
// then we fakely attach the new blank tab to the active pinned tab.
39+
// See also https://github.com/piroor/treestyletab/issues/3296
40+
const shouldAttachToPinnedOpener = (
41+
!tab.openerTabId &&
42+
activeTab && activeTab.pinned &&
43+
!tab.pinned &&
44+
tab.$TST.isNewTabCommandTab &&
45+
configs.autoAttachOnNewTabCommand >= Constants.kNEWTAB_OPEN_AS_CHILD &&
46+
(configs.insertNewTabFromPinnedTabAt == Constants.kINSERT_TOP ||
47+
configs.insertNewTabFromPinnedTabAt == Constants.kINSERT_NEAREST ||
48+
configs.insertNewTabFromPinnedTabAt == Constants.kINSERT_NEXT_TO_LAST_RELATED_TAB)
49+
);
50+
if (shouldAttachToPinnedOpener)
51+
tab.openerTabId = activeTab.id;
52+
});
53+
3054
// this should return false if the tab is / may be moved while processing
3155
Tab.onCreating.addListener((tab, info = {}) => {
3256
if (info.duplicatedInternally)
@@ -296,7 +320,7 @@ async function handleTabsFromPinnedOpener(tab, opener, { activeTab } = {}) {
296320

297321
switch (configs.insertNewTabFromPinnedTabAt) {
298322
case Constants.kINSERT_NEXT_TO_LAST_RELATED_TAB: {
299-
const lastRelatedTab = opener.$TST.lastRelatedTab;
323+
const lastRelatedTab = opener.$TST.lastRelatedTab != tab && opener.$TST.lastRelatedTab;
300324
if (lastRelatedTab) {
301325
log(`handleTabsFromPinnedOpener: place after last related tab ${dumpTab(lastRelatedTab)}`);
302326
tab.$TST.temporaryMetadata.set('alreadyMovedAsOpenedFromPinnedOpener', true);
@@ -305,6 +329,24 @@ async function handleTabsFromPinnedOpener(tab, opener, { activeTab } = {}) {
305329
broadcast: true
306330
});
307331
}
332+
const lastPinnedTab = Tab.getLastPinnedTab(tab.windowId);
333+
if (lastPinnedTab) {
334+
log(`handleTabsFromPinnedOpener: place after last pinned tab ${dumpTab(lastPinnedTab)}`);
335+
tab.$TST.temporaryMetadata.set('alreadyMovedAsOpenedFromPinnedOpener', true);
336+
return TabsMove.moveTabAfter(tab, lastPinnedTab, {
337+
delayedMove: true,
338+
broadcast: true
339+
});
340+
}
341+
const firstNormalTab = Tab.getFirstNormalTab(tab.windowId);
342+
if (firstNormalTab) {
343+
log(`handleTabsFromPinnedOpener: place before first unpinned tab ${dumpTab(firstNormalTab)}`);
344+
tab.$TST.temporaryMetadata.set('alreadyMovedAsOpenedFromPinnedOpener', true);
345+
return TabsMove.moveTabBefore(tab, firstNormalTab, {
346+
delayedMove: true,
347+
broadcast: true
348+
});
349+
}
308350
};
309351
case Constants.kINSERT_TOP: {
310352
const lastPinnedTab = Tab.getLastPinnedTab(tab.windowId);

0 commit comments

Comments
 (0)