From 2ef396ee877bbdcc1a438ab13ea38a7a4aa579ff Mon Sep 17 00:00:00 2001 From: Mauro Balades Date: Wed, 21 Aug 2024 20:48:13 +0200 Subject: [PATCH] refactor: Add event listener for tab close in ZenWorkspaces This commit adds an event listener for the "TabClose" event in the ZenWorkspaces module. When a tab is closed, the listener checks if it is the last tab in a workspace and creates a new tab if necessary. It also closes other tabs in the workspace. This enhancement improves the functionality and user experience of ZenWorkspaces. --- src/ZenWorkspaces.mjs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/ZenWorkspaces.mjs b/src/ZenWorkspaces.mjs index 2f28dee..851730d 100644 --- a/src/ZenWorkspaces.mjs +++ b/src/ZenWorkspaces.mjs @@ -56,6 +56,7 @@ var ZenWorkspaces = { await IOUtils.writeJSON(this._storeFile, {}); } if (this.workspaceEnabled) { + window.addEventListener("TabClose", this.handleTabClose.bind(this)); let workspaces = await this._workspaces(); if (workspaces.workspaces.length === 0) { await this.createAndSaveWorkspace("Default Workspace", true); @@ -80,6 +81,20 @@ var ZenWorkspaces = { } }, + handleTabClose(event) { + let tab = event.target; + let workspaceID = tab.getAttribute("zen-workspace-id"); + // If the tab is the last one in the workspace, create a new tab + if (workspaceID) { + let tabs = gBrowser.tabs.filter(tab => tab.getAttribute("zen-workspace-id") === workspaceID); + if (tabs.length === 1) { + this._createNewTabForWorkspace({ uuid: workspaceID }); + // We still need to close other tabs in the workspace + this.changeWorkspace({ uuid: workspaceID }); + } + } + }, + _initializeWorkspaceIcons() { const kIcons = ["🏠", "📄", "💹", "💼", "📧", "✅", "👥"]; let container = document.getElementById("PanelUI-zen-workspaces-create-icons-container");