feat: Return to previous tab when closing tabs, p=#11933

* fix: Switch to last used tab on tab close

lastAccessed time will be used to select the active tab instead of positional selection or ownership when a tab is closed, returning the user to their last used tab.

* test: Merge branch and add tests, b=no-bug, c=common, tests, tabs, workspaces

---------

Co-authored-by: mr. m <91018726+mr-cheffy@users.noreply.github.com>
This commit is contained in:
Anodev-sudo
2026-01-17 06:18:43 -06:00
committed by GitHub
parent 7668c26ce5
commit ceb540536c
9 changed files with 134 additions and 44 deletions

View File

@@ -1,5 +1,5 @@
diff --git a/browser/components/tabbrowser/content/tabbrowser.js b/browser/components/tabbrowser/content/tabbrowser.js
index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae944551f9 100644
index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..f4c8689bdb2262ed465d5c88977699b301caf925 100644
--- a/browser/components/tabbrowser/content/tabbrowser.js
+++ b/browser/components/tabbrowser/content/tabbrowser.js
@@ -398,6 +398,7 @@
@@ -650,7 +650,7 @@ index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae
// If this tab has a successor, it should be selectable, since
// hiding or closing a tab removes that tab as a successor.
@@ -5744,13 +5908,13 @@
@@ -5744,15 +5908,22 @@
!excludeTabs.has(aTab.owner) &&
Services.prefs.getBoolPref("browser.tabs.selectOwnerOnClose")
) {
@@ -662,20 +662,20 @@ index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae
let remainingTabs = Array.prototype.filter.call(
this.visibleTabs,
- tab => !excludeTabs.has(tab)
+ tab => !excludeTabs.has(tab) && gZenWorkspaces._shouldChangeToTab(tab)
+ tab => !excludeTabs.has(tab) && gZenWorkspaces._shouldChangeToTab(tab) && tab !== aTab
);
+ if (remainingTabs.length > 0 && Services.prefs.getBoolPref("zen.tabs.select-recently-used-on-close")) {
+ let mostRecentTab = remainingTabs.reduce((a, b) =>
+ b.lastAccessed > a.lastAccessed ? b : a
+ );
+ return gZenWorkspaces.findTabToBlur(mostRecentTab);
+ }
+
let tab = this.tabContainer.findNextTab(aTab, {
@@ -5766,7 +5930,7 @@
}
if (tab) {
- return tab;
+ return gZenWorkspaces.findTabToBlur(tab);
}
// If no qualifying visible tab was found, see if there is a tab in
@@ -5787,7 +5951,7 @@
direction: 1,
filter: _tab => remainingTabs.includes(_tab),
@@ -5787,7 +5958,7 @@
});
}
@@ -684,7 +684,7 @@ index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae
}
_blurTab(aTab) {
@@ -5798,7 +5962,7 @@
@@ -5798,7 +5969,7 @@
* @returns {boolean}
* False if swapping isn't permitted, true otherwise.
*/
@@ -693,7 +693,7 @@ index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae
// Do not allow transfering a private tab to a non-private window
// and vice versa.
if (
@@ -5852,6 +6016,7 @@
@@ -5852,6 +6023,7 @@
// fire the beforeunload event in the process. Close the other
// window if this was its last tab.
if (
@@ -701,7 +701,7 @@ index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae
!remoteBrowser._beginRemoveTab(aOtherTab, {
adoptedByTab: aOurTab,
closeWindowWithLastTab: true,
@@ -5863,7 +6028,7 @@
@@ -5863,7 +6035,7 @@
// If this is the last tab of the window, hide the window
// immediately without animation before the docshell swap, to avoid
// about:blank being painted.
@@ -710,7 +710,7 @@ index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae
if (closeWindow) {
let win = aOtherTab.ownerGlobal;
win.windowUtils.suppressAnimation(true);
@@ -5987,11 +6152,13 @@
@@ -5987,11 +6159,13 @@
}
// Finish tearing down the tab that's going away.
@@ -724,7 +724,7 @@ index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae
this.setTabTitle(aOurTab);
@@ -6193,10 +6360,10 @@
@@ -6193,10 +6367,10 @@
SessionStore.deleteCustomTabValue(aTab, "hiddenBy");
}
@@ -737,7 +737,7 @@ index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae
aTab.selected ||
aTab.closing ||
// Tabs that are sharing the screen, microphone or camera cannot be hidden.
@@ -6254,7 +6421,8 @@
@@ -6254,7 +6428,8 @@
*
* @param {MozTabbrowserTab|MozTabbrowserTabGroup|MozTabbrowserTabGroup.labelElement} aTab
*/
@@ -747,7 +747,7 @@ index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae
if (this.tabs.length == 1) {
return null;
}
@@ -6278,12 +6446,14 @@
@@ -6278,12 +6453,14 @@
}
// tell a new window to take the "dropped" tab
@@ -763,7 +763,7 @@ index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae
}
/**
@@ -6388,7 +6558,7 @@
@@ -6388,7 +6565,7 @@
* `true` if element is a `<tab-group>`
*/
isTabGroup(element) {
@@ -772,7 +772,7 @@ index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae
}
/**
@@ -6473,8 +6643,8 @@
@@ -6473,8 +6650,8 @@
}
// Don't allow mixing pinned and unpinned tabs.
@@ -783,7 +783,7 @@ index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae
} else {
tabIndex = Math.max(tabIndex, this.pinnedTabCount);
}
@@ -6500,10 +6670,16 @@
@@ -6500,10 +6677,16 @@
this.#handleTabMove(
element,
() => {
@@ -802,7 +802,7 @@ index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae
if (neighbor && this.isTab(element) && tabIndex > element._tPos) {
neighbor.after(element);
} else {
@@ -6561,23 +6737,31 @@
@@ -6561,23 +6744,31 @@
#moveTabNextTo(element, targetElement, moveBefore = false, metricsContext) {
if (this.isTabGroupLabel(targetElement)) {
targetElement = targetElement.group;
@@ -840,7 +840,7 @@ index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae
} else if (!element.pinned && targetElement && targetElement.pinned) {
// If the caller asks to move an unpinned element next to a pinned
// tab, move the unpinned element to be the first unpinned element
@@ -6590,14 +6774,34 @@
@@ -6590,14 +6781,34 @@
// move the tab group right before the first unpinned tab.
// 4. Moving a tab group and the first unpinned tab is grouped:
// move the tab group right before the first unpinned tab's tab group.
@@ -876,7 +876,7 @@ index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae
element.pinned
? this.tabContainer.pinnedTabsContainer
: this.tabContainer;
@@ -6606,7 +6810,7 @@
@@ -6606,7 +6817,7 @@
element,
() => {
if (moveBefore) {
@@ -885,7 +885,7 @@ index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae
} else if (targetElement) {
targetElement.after(element);
} else {
@@ -6676,10 +6880,10 @@
@@ -6676,10 +6887,10 @@
* @param {TabMetricsContext} [metricsContext]
*/
moveTabToExistingGroup(aTab, aGroup, metricsContext) {
@@ -898,7 +898,7 @@ index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae
return;
}
if (aTab.group && aTab.group.id === aGroup.id) {
@@ -6751,6 +6955,7 @@
@@ -6751,6 +6962,7 @@
let state = {
tabIndex: tab._tPos,
@@ -906,7 +906,7 @@ index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae
};
if (tab.visible) {
state.elementIndex = tab.elementIndex;
@@ -6777,7 +6982,7 @@
@@ -6777,7 +6989,7 @@
let changedTabGroup =
previousTabState.tabGroupId != currentTabState.tabGroupId;
@@ -915,7 +915,7 @@ index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae
tab.dispatchEvent(
new CustomEvent("TabMove", {
bubbles: true,
@@ -6818,6 +7023,10 @@
@@ -6818,6 +7030,10 @@
moveActionCallback();
@@ -926,7 +926,7 @@ index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae
// Clear tabs cache after moving nodes because the order of tabs may have
// changed.
this.tabContainer._invalidateCachedTabs();
@@ -6869,6 +7078,19 @@
@@ -6869,6 +7085,19 @@
* The new tab in the current window, null if the tab couldn't be adopted.
*/
adoptTab(aTab, { elementIndex, tabIndex, selectTab = false } = {}) {
@@ -946,7 +946,7 @@ index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae
// Swap the dropped tab with a new one we create and then close
// it in the other window (making it seem to have moved between
// windows). We also ensure that the tab we create to swap into has
@@ -6910,6 +7132,8 @@
@@ -6910,6 +7139,8 @@
params.userContextId = aTab.getAttribute("usercontextid");
}
let newTab = this.addWebTab("about:blank", params);
@@ -955,7 +955,7 @@ index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae
let newBrowser = this.getBrowserForTab(newTab);
aTab.container.tabDragAndDrop.finishAnimateTabMove();
@@ -7718,7 +7942,7 @@
@@ -7718,7 +7949,7 @@
// preventDefault(). It will still raise the window if appropriate.
break;
}
@@ -964,7 +964,7 @@ index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae
window.focus();
aEvent.preventDefault();
break;
@@ -7735,7 +7959,6 @@
@@ -7735,7 +7966,6 @@
}
case "TabGroupCollapse":
aEvent.target.tabs.forEach(tab => {
@@ -972,7 +972,7 @@ index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae
});
break;
case "TabGroupCreateByUser":
@@ -7895,7 +8118,9 @@
@@ -7895,7 +8125,9 @@
let filter = this._tabFilters.get(tab);
if (filter) {
@@ -982,7 +982,7 @@ index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae
let listener = this._tabListeners.get(tab);
if (listener) {
@@ -8698,6 +8923,7 @@
@@ -8698,6 +8930,7 @@
aWebProgress.isTopLevel
) {
this.mTab.setAttribute("busy", "true");
@@ -990,7 +990,7 @@ index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae
gBrowser._tabAttrModified(this.mTab, ["busy"]);
this.mTab._notselectedsinceload = !this.mTab.selected;
}
@@ -8778,6 +9004,7 @@
@@ -8778,6 +9011,7 @@
// known defaults. Note we use the original URL since about:newtab
// redirects to a prerendered page.
const shouldRemoveFavicon =
@@ -998,7 +998,7 @@ index 0eaca7a58e0026237b71b2ad515efe84d9e8c779..5f58cf2009dfe869d05b896b609c7bae
!this.mBrowser.mIconURL &&
!ignoreBlank &&
!(originalLocation.spec in FAVICON_DEFAULTS);
@@ -9803,7 +10030,7 @@ var TabContextMenu = {
@@ -9803,7 +10037,7 @@ var TabContextMenu = {
);
contextUnpinSelectedTabs.hidden =
!this.contextTab.pinned || !this.multiselected;

View File

@@ -31,6 +31,9 @@ dialog::part(dialog-button) {
&:is([dlgtype="accept"], [dlgtype="cancel"]) {
padding-inline-end: 3.7em;
@media (-moz-platform: windows) {
padding-inline-end: 4em;
}
&::after {
border-radius: 4px;
@@ -48,7 +51,7 @@ dialog::part(dialog-button) {
&[dlgtype="accept"]::after {
content: "⏎";
background: rgba(255, 255, 255, 0.1);
outline: 2px solid color-mix(in srgb, currentColor 30%, transparent);
outline: 2px solid color-mix(in srgb, currentColor 20%, transparent);
outline-offset: -2px;
}

View File

@@ -16,11 +16,12 @@
}
}
#urlbar {
.urlbar {
--urlbarView-separator-color: light-dark(hsl(0, 0%, 89%), hsl(0, 0%, 20%));
--urlbarView-hover-background: var(--toolbarbutton-hover-background);
--urlbarView-highlight-background: var(--toolbarbutton-hover-background);
border-radius: calc(var(--toolbarbutton-border-radius) - 2px);
height: var(--urlbar-height);
--urlbarView-results-padding: 8px !important;
--urlbar-container-height: 48px !important;

View File

@@ -8,6 +8,7 @@ support-files = [
]
["browser_tabs_empty_checks.js"]
["browser_tabs_close_recently.js"]
["browser_tabs_fetch_checks.js"]
["browser_tabs_cycle_by_attribute.js"]
["browser_drag_drop_vertical.js"]

View File

@@ -52,7 +52,7 @@ async function dropAfter(itemToDrag, itemToDropAfter, win) {
const midline = rect.left + 0.5 * rect.width;
// Point where bottom edge of `itemToDrag` overlaps `itemToDropAfter` enough
// for `itemToDrag` to come after.
const afterPoint = Math.ceil(rect.top + threshold * rect.height);
const afterPoint = Math.ceil(rect.top + (threshold + 0.5) * rect.height);
const dragTo = afterPoint - sourceRect.height / 2;
await drop(itemToDrag, itemToDropAfter, midline, dragTo, win);
}
@@ -71,7 +71,7 @@ async function dropBefore(itemToDrag, itemToDropBefore, win) {
const midline = rect.left + 0.5 * rect.width;
// Point where top edge of `itemToDrag` overlaps `itemToDropBefore` enough
// for `itemToDrag` to come before.
const beforePoint = Math.floor(rect.top + (1 - threshold) * rect.height);
const beforePoint = Math.floor(rect.top + (1 - threshold - 0.5) * rect.height);
const dragTo = beforePoint + sourceRect.height / 2;
await drop(itemToDrag, itemToDropBefore, midline, dragTo, win);
}

View File

@@ -0,0 +1,80 @@
/* Any copyright is dedicated to the Public Domain.
https://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const URL1 = "data:text/plain,tab1";
const URL2 = "data:text/plain,tab2";
const URL3 = "data:text/plain,tab3";
add_task(async function test_blurToRecentlyClosedTab() {
const [tab1, tab2, tab3] = [
BrowserTestUtils.addTab(gBrowser, URL1),
BrowserTestUtils.addTab(gBrowser, URL2),
BrowserTestUtils.addTab(gBrowser, URL3),
];
gBrowser.selectedTab = tab2;
gBrowser.selectedTab = tab3;
gBrowser.selectedTab = tab1;
gBrowser.selectedTab = tab2;
gBrowser.removeTab(tab2);
Assert.equal(
gBrowser.selectedTab,
tab1,
"After closing the most recently used tab, the selection moves to the last used tab"
);
gBrowser.selectedTab = tab3;
gBrowser.removeTab(tab3);
Assert.equal(
gBrowser.selectedTab,
tab1,
"After closing the most recently used tab, the selection moves to the last used tab"
);
gBrowser.removeTab(tab1);
});
add_task(async function test_closeToLastTab() {
await SpecialPowers.pushPrefEnv({
set: [
["zen.tabs.select-recently-used-on-close", false],
["zen.view.show-newtab-button-top", false],
],
});
const [tab1, tab2, tab3] = [
BrowserTestUtils.addTab(gBrowser, URL1),
BrowserTestUtils.addTab(gBrowser, URL2),
BrowserTestUtils.addTab(gBrowser, URL3),
];
gBrowser.selectedTab = tab2;
gBrowser.selectedTab = tab3;
gBrowser.selectedTab = tab1;
gBrowser.selectedTab = tab2;
gBrowser.removeTab(tab2);
Assert.equal(
gBrowser.selectedTab,
tab3,
"After closing the current tab, the selection moves to the next tab in order"
);
gBrowser.selectedTab = tab1;
gBrowser.removeTab(tab1);
Assert.equal(
gBrowser.selectedTab,
tab3,
"After closing the current tab, the selection moves to the next tab in order"
);
gBrowser.removeTab(tab3);
await SpecialPowers.popPrefEnv();
});

View File

@@ -2819,9 +2819,8 @@ class nsZenWorkspaces {
const tabWorkspaceId = aTab.getAttribute("zen-workspace-id");
const containerId = aTab.getAttribute("usercontextid") ?? "0";
// Return all tabs that are not on the same workspace
return this.allStoredTabs.filter(
return gBrowser.tabs.filter(
(tab) =>
tab.getAttribute("zen-workspace-id") !== tabWorkspaceId &&
!this._shouldShowTab(tab, tabWorkspaceId, containerId, this._workspaceCache) &&
!tab.hasAttribute("zen-empty-tab")
);