Skip to content

Commit

Permalink
Refactor tab closing logic to handle new tab creation when closing th…
Browse files Browse the repository at this point in the history
…e last unpinned tab and prevent reentrancy issues during window closure
  • Loading branch information
mauro-balades committed Jan 11, 2025
1 parent 8981160 commit 8562cfb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
21 changes: 11 additions & 10 deletions src/browser/base/zen-components/ZenWorkspaces.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -429,11 +429,8 @@ var ZenWorkspaces = new (class extends ZenMultiWindowFeature {
return null;
}

let tabs = gBrowser.tabs.filter(
(t) =>
(t.getAttribute('zen-workspace-id') === workspaceID || t.hasAttribute('zen-essential')) &&
(!this.shouldOpenNewTabIfLastUnpinnedTabIsClosed || !t.pinned || t.getAttribute('pending') !== 'true')
);
let tabs = gBrowser.visibleTabs;
let tabsPinned = tabs.filter((t) => !this.shouldOpenNewTabIfLastUnpinnedTabIsClosed || !t.pinned);

const shouldCloseWindow = this.shouldCloseWindow();
if (tabs.length === 1 && tabs[0] === tab) {
Expand All @@ -448,13 +445,17 @@ var ZenWorkspaces = new (class extends ZenMultiWindowFeature {
if (!gBrowser._removingTabs.size) {
// This call actually closes the window, unless the user
// cancels the operation. We are finished here in both cases.
gBrowser._windowIsClosing = window.closeWindow(true, window.warnAboutClosingWindow, 'close-last-tab');
return null;
this._isClosingWindow = true;
// Inside a setTimeout to avoid reentrancy issues.
setTimeout(() => {
document.getElementById('cmd_closeWindow').doCommand();
}, 100);
return this._createNewTabForWorkspace({ uuid: workspaceID });
}
return null;
}
let newTab = this._createNewTabForWorkspace({ uuid: workspaceID });
return newTab;
} else if (tabsPinned.length === 1 && tabsPinned[0] === tab) {
return this._createNewTabForWorkspace({ uuid: workspaceID });
}

return null;
Expand Down Expand Up @@ -1543,7 +1544,7 @@ var ZenWorkspaces = new (class extends ZenMultiWindowFeature {
}

async onLocationChange(browser) {
if (!this.workspaceEnabled || this._inChangingWorkspace) {
if (!this.workspaceEnabled || this._inChangingWorkspace || this._isClosingWindow) {
return;
}

Expand Down
12 changes: 10 additions & 2 deletions src/browser/components/tabbrowser/content/tabbrowser-js.patch
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/browser/components/tabbrowser/content/tabbrowser.js b/browser/components/tabbrowser/content/tabbrowser.js
index ce68c339f35416574b7bc7ebf8c93378f653242b..46d25e4381eaae71f3aec1025788684c593a60c7 100644
index ce68c339f35416574b7bc7ebf8c93378f653242b..07e3f7fcdb3e219c523201929cf07b6878d4d394 100644
--- a/browser/components/tabbrowser/content/tabbrowser.js
+++ b/browser/components/tabbrowser/content/tabbrowser.js
@@ -409,11 +409,39 @@
Expand Down Expand Up @@ -237,7 +237,7 @@ index ce68c339f35416574b7bc7ebf8c93378f653242b..46d25e4381eaae71f3aec1025788684c
if (
!this._beginRemoveTab(aTab, {
closeWindowFastpath: true,
@@ -4556,7 +4657,7 @@
@@ -4556,14 +4657,14 @@
!!this.tabsInCollapsedTabGroups.length;
if (
aTab.visible &&
Expand All @@ -246,6 +246,14 @@ index ce68c339f35416574b7bc7ebf8c93378f653242b..46d25e4381eaae71f3aec1025788684c
!anyRemainingTabsInCollapsedTabGroups
) {
closeWindow =
closeWindowWithLastTab != null
? closeWindowWithLastTab
: !window.toolbar.visible ||
- Services.prefs.getBoolPref("browser.tabs.closeWindowWithLastTab");
+ Services.prefs.getBoolPref("browser.tabs.closeWindowWithLastTab") && !ZenWorkspaces._isClosingWindow;

if (closeWindow) {
// We've already called beforeunload on all the relevant tabs if we get here,
@@ -5411,10 +5512,10 @@
SessionStore.deleteCustomTabValue(aTab, "hiddenBy");
}
Expand Down

0 comments on commit 8562cfb

Please sign in to comment.