Fix: Handle essential tabs and container-specific essentials

Refactor workspace switching logic to correctly handle
essential tabs and introduce container-specific essentials.

This change addresses issues with essential tabs not being
handled correctly during workspace switches.

The `changeWorkspace` function is refactored to improve
clarity and maintainability.  The logic for showing and
hiding tabs is streamlined, and tab selection is handled
more robustly.  A new `_shouldShowTab` function is
introduced to centralize the logic for determining tab
visibility based on workspace and container settings.  The
logic also handles pinned essential tabs in the different workspace types.

The pinned tab manager is updated to support container-
specific essentials and to refresh pinned tabs on workspace
changes.  The `_shouldShowPin` function is introduced to
manage visibility of pinned tabs in different workspaces
considering essential tabs, pinned tabs and containers.

This change also fixes a bug where the selected tab would
sometimes be changed unexpectedly when switching
workspaces.
This commit is contained in:
Kristijan Ribarić
2024-11-20 10:00:02 +01:00
parent 245f477760
commit 01c8032f20
4 changed files with 217 additions and 83 deletions

View File

@@ -48,13 +48,16 @@
this.observer.addPinnedTabListener(this._onPinnedTabEvent.bind(this));
this._zenClickEventListener = this._onTabClick.bind(this);
ZenWorkspaces.addChangeListeners(this.onWorkspaceChange.bind(this));
}
async initTabs() {
if (!this.enabled) {
async onWorkspaceChange(newWorkspace, onInit) {
if (!this.enabled || PrivateBrowsingUtils.isWindowPrivate(window)) {
return;
}
await ZenPinnedTabsStorage.init();
await this._refreshPinnedTabs(newWorkspace,{ init: onInit });
}
get enabled() {
@@ -68,9 +71,12 @@
return this._enabled;
}
async _refreshPinnedTabs({ init = false } = {}) {
async _refreshPinnedTabs(currentWorkspace,{ init = false } = {}) {
if(init) {
await ZenPinnedTabsStorage.init();
}
await this._initializePinsCache();
this._initializePinnedTabs(init);
await this._initializePinnedTabs(init,currentWorkspace);
}
async _initializePinsCache() {
@@ -109,12 +115,14 @@
return this._pinsCache;
}
_initializePinnedTabs(init = false) {
async _initializePinnedTabs(init = false, currentWorkspace) {
const pins = this._pinsCache;
if (!pins?.length) {
return;
}
const workspaces = await ZenWorkspaces._workspaces();
const activeTab = gBrowser.selectedTab;
const pinnedTabsByUUID = new Map();
const pinsToCreate = new Set(pins.map(p => p.uuid));
@@ -131,7 +139,7 @@
pinnedTabsByUUID.set(pinId, tab);
pinsToCreate.delete(pinId);
if(lazy.zenPinnedTabRestorePinnedTabsToPinnedUrl && init) {
if (lazy.zenPinnedTabRestorePinnedTabsToPinnedUrl && init) {
this._resetTabToStoredState(tab);
}
} else {
@@ -146,6 +154,10 @@
continue; // Skip pins that already have tabs
}
if (!this._shouldShowPin(pin, currentWorkspace, workspaces)) {
continue; // Skip pins not relevant to current workspace
}
let params = {
skipAnimation: true,
allowInheritPrincipal: false,
@@ -196,9 +208,6 @@
gBrowser.pinTab(newTab);
if(newTab.getAttribute("zen-workspace-id") !== ZenWorkspaces.activeWorkspace && newTab.getAttribute("zen-essential") !== "true") {
gBrowser.hideTab(newTab, undefined, true);
}
newTab.initialize();
}
@@ -211,6 +220,41 @@
gBrowser._updateTabBarForPinnedTabs();
}
_shouldShowPin(pin, currentWorkspace, workspaces) {
const isEssential = pin.isEssential;
const pinWorkspaceUuid = pin.workspaceUuid;
const pinContextId = pin.containerTabId ? pin.containerTabId.toString() : "0";
const workspaceContextId = currentWorkspace.containerTabId?.toString() || "0";
const containerSpecificEssentials = ZenWorkspaces.containerSpecificEssentials;
// Handle essential pins
if (isEssential) {
if (!containerSpecificEssentials) {
return true; // Show all essential pins when containerSpecificEssentials is false
}
if (workspaceContextId !== "0") {
// In workspaces with default container: Show essentials that match the container
return pinContextId === workspaceContextId;
} else {
// In workspaces without a default container: Show essentials that aren't in container-specific workspaces
// or have userContextId="0" or no userContextId
return !pinContextId || pinContextId === "0" || !workspaces.workspaces.some(
workspace => workspace.containerTabId === parseInt(pinContextId, 10)
);
}
}
// For non-essential pins
if (!pinWorkspaceUuid) {
// Pins without a workspace belong to all workspaces (if that's your desired behavior)
return true;
}
// Show if pin belongs to current workspace
return pinWorkspaceUuid === currentWorkspace.uuid;
}
_onPinnedTabEvent(action, event) {
if (!this.enabled) return;
const tab = event.target;
@@ -275,7 +319,8 @@
pin.userContextId = userContextId ? parseInt(userContextId, 10) : 0;
await ZenPinnedTabsStorage.savePin(pin);
await this._refreshPinnedTabs();
const currentWorkspace = await ZenWorkspaces.getActiveWorkspace();
await this._refreshPinnedTabs(currentWorkspace);
}
async _setPinnedAttributes(tab) {
@@ -311,8 +356,8 @@
tab.removeAttribute("zen-pinned-entry");
return;
}
await this._refreshPinnedTabs();
const currentWorkspace = await ZenWorkspaces.getActiveWorkspace();
await this._refreshPinnedTabs(currentWorkspace);
}
async _removePinnedAttributes(tab, isClosing = false) {
@@ -330,8 +375,8 @@
tab.setAttribute("zen-workspace-id", workspace.uuid);
}
}
await this._refreshPinnedTabs();
const currentWorkspace = await ZenWorkspaces.getActiveWorkspace();
await this._refreshPinnedTabs(currentWorkspace);
}
_initClosePinnedTabShortcut() {