Skip to content

Commit

Permalink
Fix: Handle essential tabs and container-specific essentials
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kristijanribaric committed Nov 20, 2024
1 parent 6ed4f21 commit 98dc4f4
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 77 deletions.
1 change: 0 additions & 1 deletion src/browser/base/content/ZenStartup.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
// Disable smooth scroll
gBrowser.tabContainer.arrowScrollbox.smoothScroll = false;

gZenPinnedTabManager.initTabs();
ZenWorkspaces.init();
gZenUIManager.init();
gZenVerticalTabsManager.init();
Expand Down
71 changes: 60 additions & 11 deletions src/browser/base/zen-components/ZenPinnedTabManager.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
async onWorkspaceChange(newWorkspace, onInit) {
if (!this.enabled || PrivateBrowsingUtils.isWindowPrivate(window)) {
return;
}
await ZenPinnedTabsStorage.init();

await this._refreshPinnedTabs(newWorkspace,{ init: onInit });
}

get enabled() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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));
Expand All @@ -131,7 +139,7 @@
pinnedTabsByUUID.set(pinId, tab);
pinsToCreate.delete(pinId);

if(lazy.zenPinnedTabRestorePinnedTabsToPinnedUrl && init) {
if (lazy.zenPinnedTabRestorePinnedTabsToPinnedUrl && init) {
this._resetTabToStoredState(tab);
}
} else {
Expand All @@ -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,
Expand Down Expand Up @@ -196,6 +208,7 @@

gBrowser.pinTab(newTab);


newTab.initialize();
}

Expand All @@ -207,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;
Expand Down Expand Up @@ -271,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) {
Expand Down Expand Up @@ -307,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) {
Expand All @@ -326,8 +375,8 @@
tab.setAttribute("zen-workspace-id", workspace.uuid);
}
}

await this._refreshPinnedTabs();
const currentWorkspace = await ZenWorkspaces.getActiveWorkspace();
await this._refreshPinnedTabs(currentWorkspace);
}

_initClosePinnedTabShortcut() {
Expand Down
2 changes: 0 additions & 2 deletions src/browser/base/zen-components/ZenPinnedTabsStorage.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ var ZenPinnedTabsStorage = {
await db.execute(`
CREATE INDEX IF NOT EXISTS idx_zen_pins_changes_uuid ON zen_pins_changes(uuid)
`);

await gZenPinnedTabManager._refreshPinnedTabs({init: true});
});
},

Expand Down
Loading

0 comments on commit 98dc4f4

Please sign in to comment.