Files
desktop/src/zen/tabs/ZenPinnedTabManager.mjs
mr. m 335c024e6d chore: Drag and drop refactor, p=#11723
* feat: Full cross-window workspace syncing, b=no-bug, c=workspaces

* feat: Also change icons and labels if the tab is pending, b=no-bug, c=tabs, workspaces

* feat: Dont session duplicate the tabs, b=no-bug, c=workspaces

* feat: Properly handle tab moves, b=no-bug, c=workspaces

* feat: Start on new session restore, b=no-bug, c=no-component

* Discard changes to prefs/browser.yaml

* feat: Start doing out own session restore, b=no-bug, c=folders, tabs

* feat: Stop using pinned manager and use zen session sidebar, b=no-bug, c=common, folders, tabs, workspaces

* feat: Dont restore windows that are already initialized, b=no-bug, c=no-component

* chore: Update patches to ff 145, b=no-bug, c=no-component

* Discard changes to src/browser/components/sessionstore/SessionStore-sys-mjs.patch

* Discard changes to src/browser/components/tabbrowser/content/tab-js.patch

* Discard changes to src/browser/components/tabbrowser/content/tabbrowser-js.patch

* Discard changes to src/zen/tabs/ZenPinnedTabsStorage.mjs

* feat: Run session saver before opening a new winodw, b=no-bug, c=tabs

* feat: Clone the previous state, b=no-bug, c=no-component

* feat: Move window sync to its own JS module, b=no-bug, c=workspaces

* feat: Run session saver before opening a new window, b=no-bug, c=no-component

* feat: Full cross-window workspace syncing, b=no-bug, c=workspaces

* feat: Also change icons and labels if the tab is pending, b=no-bug, c=tabs, workspaces

* feat: Dont session duplicate the tabs, b=no-bug, c=workspaces

* feat: Start on new session restore, b=no-bug, c=no-component

* feat: Properly handle tab moves, b=no-bug, c=workspaces

* Discard changes to prefs/browser.yaml

* feat: Start doing out own session restore, b=no-bug, c=folders, tabs

* feat: Stop using pinned manager and use zen session sidebar, b=no-bug, c=common, folders, tabs, workspaces

* feat: Dont restore windows that are already initialized, b=no-bug, c=no-component

* chore: Update patches to ff 145, b=no-bug, c=no-component

* Discard changes to src/browser/components/sessionstore/SessionStore-sys-mjs.patch

* Discard changes to src/browser/components/tabbrowser/content/tab-js.patch

* Discard changes to src/browser/components/tabbrowser/content/tabbrowser-js.patch

* Discard changes to src/zen/tabs/ZenPinnedTabsStorage.mjs

* feat: Run session saver before opening a new winodw, b=no-bug, c=tabs

* feat: Clone the previous state, b=no-bug, c=no-component

* feat: Move window sync to its own JS module, b=no-bug, c=workspaces

* feat: Run session saver before opening a new window, b=no-bug, c=no-component

* feat: Start making use of IDs instead of sync identifiers, b=no-bug, c=folders

* feat: Listen to new tab opens for new sync system, b=no-bug, c=common, folders, tabs

* feat: Listen for more tab events and properly sync them, b=no-bug, c=common, folders, tabs

* feat: Start moving browser views to the selected windows, b=no-bug, c=no-component

* chore: Remove extra patch, b=no-bug, c=no-component

* feat: Leave a screenshot of the page behind when switching windows or tabs, b=no-bug, c=common

* feat: Run session saves right before writing and quiting, b=no-bug, c=common

* fix: Fixed going back to a different window not allowing to type on inputs, b=no-bug, c=no-component

* feat: Start syncing folders as well, b=no-bug, c=folders

* Discard changes to src/browser/components/tabbrowser/content/tab-js.patch

* chore: Update patches to ff 146, b=no-bug, c=no-component

* feat: Early support for unsynced windoiws, b=no-bug, c=workspaces

* fix: Move back active views when closing a window, b=no-bug, c=no-component

* feat: Stop rendering sub-layers when swaping browsers, b=no-bug, c=common

* feat: Improved support for unsynced windows support, b=no-bug, c=workspaces, folders

* feat: Implemented 'Move To...' Button for unsynced windows, b=no-bug, c=workspaces, common

* feat: Make sure to properly flush all windows when making a new one and fix removing progress listeners, b=no-bug, c=workspaces

* feat: Make sure to not lose any tabs when opening from a private window, b=no-bug, c=workspaces

* feat: Allow unload to run instantly and fix closing windows on mac, b=no-bug, c=no-component

* feat: Make sure to always initialize an empty state with the sidebar object, b=no-bug, c=workspaces

* chore: Small fixes and QA checks, b=no-bug, c=tabs, workspaces

* fix: Fixed tab labels not changing on unfocused windows, b=no-bug, c=no-component

* feat: Fixed closing windows on macos not returning to the original views, b=no-bug, c=no-component

* chore: Turn off debug flags by default, b=no-bug, c=no-component

* feat: Start implementing old pinned tab behaviour we used to have, b=no-bug, c=common, tabs

* feat: Unsynced windows should always be allowed to change labels, b=no-bug, c=welcome

* feat: Make sure we wait long enough before we initialize workspaces, b=no-bug, c=workspaces

* feat: Dont mix remoteness when changing browser views and restore window spaces, b=no-bug, c=common, folders, workspaces

* test: Fixed tests for the pinned tabs manager, b=no-bug, c=tabs, folders, tests, welcome

* feat: Added partial support for split views, b=no-bug, c=split-view

* chore: Finished basic support for split views, b=no-bug, c=folders, split-view

* feat: Always make sure to save the last closed window to the sidebar object, b=no-bug, c=no-component

* feat: Implement workspace sync store into the session file, b=closes #10857, c=common, tabs, tests, workspaces

* feat: New drag and drop system, b=no-bug, c=tabs, common, folders

* feat: Add support for drag-and-dropping tabs into groups, b=no-bug, c=common, folders, tabs, workspaces

* feat: Add a default value for the workspace cache, b=no-bug, c=workspaces

* fix: Default assign an array instead of an object to the workspaces cache, b=no-bug, c=workspaces

* feat: Take into consideration win data may not have allocated spaces, b=no-bug, c=workspaces

* feat: Always make sure we are assigning the correct space ID, b=no-bug, c=workspaces

* feat: Make sure to initialize windows even if there are no tabs, b=no-bug, c=no-component

* feat: Improved drag and drop simulation, b=no-bug, c=common, tabs

* feat: Implement opacity changes to the drag image, b=no-bug, c=tabs, common, split-view

* feat: Support for drag and dropping outside the window, b=no-bug, c=split-view

* feat: Added transitions when reordering, b=no-bug, c=split-view, tabs

* feat: Started working on drag and dropping to essentials, b=no-bug, c=no-component

* Discard changes to locales/en-US/browser/browser/zen-workspaces.ftl

* Discard changes to prefs/zen/view.yaml

* Discard changes to prefs/zen/zen.yaml

* Discard changes to src/browser/base/content/zen-assets.inc.xhtml

* Discard changes to src/browser/base/content/zen-assets.jar.inc.mn

* Discard changes to src/browser/base/content/zen-panels/popups.inc

* Discard changes to src/browser/base/content/zen-preloaded.inc.xhtml

* Discard changes to src/browser/components/places/content/editBookmark-js.patch

* Discard changes to src/browser/components/sessionstore/SessionStore-sys-mjs.patch

* Discard changes to src/browser/components/sessionstore/TabState-sys-mjs.patch

* Discard changes to src/browser/components/tabbrowser/content/tab-js.patch

* Discard changes to src/browser/components/urlbar/UrlbarProviderPlaces-sys-mjs.patch

* Discard changes to src/zen/ZenComponents.manifest

* Discard changes to src/browser/components/tabbrowser/content/tabbrowser-js.patch

* feat: Finish migration, b=no-bug, c=no-component

* feat: Add support for multi tabs dragging, b=no-bug, c=tabs

* feat: Added support for essential tabs, b=no-bug, c=split-view, tabs, workspaces

* feat: Added support to switch space when holding on the side of the sidebar, b=no-bug, c=common, split-view, workspaces

* Discard changes to src/browser/components/tabbrowser/content/tabbrowser-js.patch

* chore: Fixed merge conflicts, b=no-bug, c=no-component

* feat: Added support for split views, b=no-bug, c=split-view

---------

Signed-off-by: mr. m <91018726+mr-cheffy@users.noreply.github.com>
2025-12-28 18:26:52 +01:00

831 lines
27 KiB
JavaScript

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
import { nsZenDOMOperatedFeature } from 'chrome://browser/content/zen-components/ZenCommonUtils.mjs';
const lazy = {};
class ZenPinnedTabsObserver {
static ALL_EVENTS = ['TabPinned', 'TabUnpinned'];
#listeners = [];
constructor() {
XPCOMUtils.defineLazyPreferenceGetter(
lazy,
'zenPinnedTabRestorePinnedTabsToPinnedUrl',
'zen.pinned-tab-manager.restore-pinned-tabs-to-pinned-url',
false
);
XPCOMUtils.defineLazyPreferenceGetter(
lazy,
'zenPinnedTabCloseShortcutBehavior',
'zen.pinned-tab-manager.close-shortcut-behavior',
'switch'
);
XPCOMUtils.defineLazyPreferenceGetter(
lazy,
'zenTabsEssentialsMax',
'zen.tabs.essentials.max',
12
);
ChromeUtils.defineESModuleGetters(lazy, {
E10SUtils: 'resource://gre/modules/E10SUtils.sys.mjs',
TabStateCache: 'resource:///modules/sessionstore/TabStateCache.sys.mjs',
});
this.#listenPinnedTabEvents();
}
#listenPinnedTabEvents() {
const eventListener = this.#eventListener.bind(this);
for (const event of ZenPinnedTabsObserver.ALL_EVENTS) {
window.addEventListener(event, eventListener);
}
window.addEventListener('unload', () => {
for (const event of ZenPinnedTabsObserver.ALL_EVENTS) {
window.removeEventListener(event, eventListener);
}
});
}
#eventListener(event) {
for (const listener of this.#listeners) {
listener(event.type, event);
}
}
addPinnedTabListener(listener) {
this.#listeners.push(listener);
}
}
class nsZenPinnedTabManager extends nsZenDOMOperatedFeature {
init() {
if (!this.enabled) {
return;
}
this._canLog = Services.prefs.getBoolPref('zen.pinned-tab-manager.debug', false);
this.observer = new ZenPinnedTabsObserver();
this._initClosePinnedTabShortcut();
this._insertItemsIntoTabContextMenu();
this.observer.addPinnedTabListener(this._onPinnedTabEvent.bind(this));
this._zenClickEventListener = this._onTabClick.bind(this);
gZenWorkspaces._resolvePinnedInitialized();
}
log(message) {
if (this._canLog) {
console.log(`[ZenPinnedTabManager] ${message}`);
}
}
onTabIconChanged(tab, url = null) {
tab.dispatchEvent(new CustomEvent('ZenTabIconChanged', { bubbles: true, detail: { tab } }));
if (tab.hasAttribute('zen-essential')) {
this.setEssentialTabIcon(tab, url);
}
}
setEssentialTabIcon(tab, url = null) {
const iconUrl = url ?? tab.getAttribute('image') ?? '';
tab.style.setProperty('--zen-essential-tab-icon', `url(${iconUrl})`);
}
_onTabResetPinButton(event, tab) {
event.stopPropagation();
this._resetTabToStoredState(tab);
}
get enabled() {
return !gZenWorkspaces.privateWindowOrDisabled;
}
get maxEssentialTabs() {
return lazy.zenTabsEssentialsMax;
}
_onPinnedTabEvent(action, event) {
if (!this.enabled) return;
const tab = event.target;
if (this._ignoreNextTabPinnedEvent) {
delete this._ignoreNextTabPinnedEvent;
return;
}
switch (action) {
case 'TabPinned':
tab._zenClickEventListener = this._zenClickEventListener;
tab.addEventListener('click', tab._zenClickEventListener);
break;
// [Fall through]
case 'TabUnpinned':
if (tab._zenClickEventListener) {
tab.removeEventListener('click', tab._zenClickEventListener);
delete tab._zenClickEventListener;
}
break;
default:
console.warn('ZenPinnedTabManager: Unhandled tab event', action);
break;
}
}
#getTabState(tab) {
return JSON.parse(SessionStore.getTabState(tab));
}
async _onTabClick(e) {
const tab = e.target?.closest('tab');
if (e.button === 1 && tab) {
await this.onCloseTabShortcut(e, tab, {
closeIfPending: Services.prefs.getBoolPref('zen.pinned-tab-manager.wheel-close-if-pending'),
});
}
}
resetPinnedTab(tab) {
if (!tab) {
tab = TabContextMenu.contextTab;
}
if (!tab || !tab.pinned) {
return;
}
this._resetTabToStoredState(tab);
}
async replacePinnedUrlWithCurrent(tab = undefined) {
tab ??= TabContextMenu.contextTab;
if (!tab || !tab.pinned) {
return;
}
window.gZenWindowSync.setPinnedTabState(tab);
this.resetPinChangedUrl(tab);
gZenUIManager.showToast('zen-pinned-tab-replaced');
}
_initClosePinnedTabShortcut() {
let cmdClose = document.getElementById('cmd_close');
if (cmdClose) {
cmdClose.addEventListener('command', this.onCloseTabShortcut.bind(this));
}
}
async onCloseTabShortcut(
event,
selectedTab = gBrowser.selectedTab,
{
behavior = lazy.zenPinnedTabCloseShortcutBehavior,
noClose = false,
closeIfPending = false,
alwaysUnload = false,
folderToUnload = null,
} = {}
) {
try {
const tabs = Array.isArray(selectedTab) ? selectedTab : [selectedTab];
const pinnedTabs = [
...new Set(
tabs
.flatMap((tab) => {
if (tab.group?.hasAttribute('split-view-group')) {
return tab.group.tabs;
}
return tab;
})
.filter((tab) => tab?.pinned)
),
];
if (!pinnedTabs.length) {
return;
}
const selectedTabs = pinnedTabs.filter((tab) => tab.selected);
event.stopPropagation();
event.preventDefault();
if (noClose && behavior === 'close') {
behavior = 'unload-switch';
}
if (alwaysUnload && ['close', 'reset', 'switch', 'reset-switch'].includes(behavior)) {
behavior = behavior.contains('reset') ? 'reset-unload-switch' : 'unload-switch';
}
switch (behavior) {
case 'close': {
for (const tab of pinnedTabs) {
gBrowser.removeTab(tab, { animate: true });
}
break;
}
case 'reset-unload-switch':
case 'unload-switch':
case 'reset-switch':
case 'switch':
if (behavior.includes('unload')) {
for (const tab of pinnedTabs) {
if (tab.hasAttribute('glance-id')) {
// We have a glance tab inside the tab we are trying to unload,
// before we used to just ignore it but now we need to fully close
// it as well.
gZenGlanceManager.manageTabClose(tab.glanceTab);
await new Promise((resolve) => {
let hasRan = false;
const onGlanceClose = () => {
hasRan = true;
resolve();
};
window.addEventListener('GlanceClose', onGlanceClose, { once: true });
// Set a timeout to resolve the promise if the event doesn't fire.
// We do this to prevent any future issues where glance woudnt close such as
// glance requering to ask for permit unload.
setTimeout(() => {
if (!hasRan) {
console.warn('GlanceClose event did not fire within 3 seconds');
resolve();
}
}, 3000);
});
return;
}
const isSpltView = tab.group?.hasAttribute('split-view-group');
const group = isSpltView ? tab.group.group : tab.group;
if (!folderToUnload && tab.hasAttribute('folder-active')) {
await gZenFolders.animateUnload(group, tab);
}
}
if (folderToUnload) {
await gZenFolders.animateUnloadAll(folderToUnload);
}
const allAreUnloaded = pinnedTabs.every(
(tab) => tab.hasAttribute('pending') && !tab.hasAttribute('zen-essential')
);
for (const tab of pinnedTabs) {
if (allAreUnloaded && closeIfPending) {
return await this.onCloseTabShortcut(event, tab, { behavior: 'close' });
}
}
await gBrowser.explicitUnloadTabs(pinnedTabs);
for (const tab of pinnedTabs) {
tab.removeAttribute('discarded');
}
}
if (selectedTabs.length) {
this._handleTabSwitch(selectedTabs[0]);
}
if (behavior.includes('reset')) {
for (const tab of pinnedTabs) {
this._resetTabToStoredState(tab);
}
}
break;
case 'reset':
for (const tab of pinnedTabs) {
this._resetTabToStoredState(tab);
}
break;
default:
return;
}
} catch (ex) {
console.error('Error handling close tab shortcut for pinned tab:', ex);
}
}
_handleTabSwitch(selectedTab) {
if (selectedTab !== gBrowser.selectedTab) {
return;
}
const findNextTab = (direction) =>
gBrowser.tabContainer.findNextTab(selectedTab, {
direction,
filter: (tab) => !tab.hidden && !tab.pinned,
});
let nextTab = findNextTab(1) || findNextTab(-1);
if (!nextTab) {
gZenWorkspaces.selectEmptyTab();
return;
}
if (nextTab) {
gBrowser.selectedTab = nextTab;
}
}
_resetTabToStoredState(tab) {
const state = this.#getTabState(tab);
const initialState = tab._zenPinnedInitialState;
if (!initialState?.entry) {
return;
}
// Remove everything except the entry we want to keep
state.entries = [initialState.entry];
state.image = tab.zenStaticIcon || initialState.image;
state.index = 0;
SessionStore.setTabState(tab, state);
this.resetPinChangedUrl(tab);
}
async getFaviconAsBase64(pageUrl) {
try {
const faviconData = await PlacesUtils.favicons.getFaviconForPage(pageUrl);
if (!faviconData) {
// empty favicon
return null;
}
return faviconData.dataURI;
} catch (ex) {
console.error('Failed to get favicon:', ex);
return null;
}
}
addToEssentials(tab) {
const tabs = tab
? // if it's already an array, dont make it [tab]
tab?.length
? tab
: [tab]
: TabContextMenu.contextTab.multiselected
? gBrowser.selectedTabs
: [TabContextMenu.contextTab];
let movedAll = true;
for (let i = 0; i < tabs.length; i++) {
let tab = tabs[i];
const section = gZenWorkspaces.getEssentialsSection(tab);
if (!this.canEssentialBeAdded(tab)) {
movedAll = false;
continue;
}
if (tab.hasAttribute('zen-essential')) {
continue;
}
tab.setAttribute('zen-essential', 'true');
if (tab.hasAttribute('zen-workspace-id')) {
tab.removeAttribute('zen-workspace-id');
}
if (tab.pinned) {
gBrowser.zenHandleTabMove(tab, () => {
if (tab.ownerGlobal !== window) {
tab = gBrowser.adoptTab(tab, {
selectTab: tab.selected,
});
tab.setAttribute('zen-essential', 'true');
}
section.appendChild(tab);
});
} else {
gBrowser.pinTab(tab);
this._ignoreNextTabPinnedEvent = true;
}
tab.setAttribute('zenDefaultUserContextId', true);
if (tab.selected) {
gZenWorkspaces.switchTabIfNeeded(tab);
}
this.onTabIconChanged(tab);
// Dispatch the event to update the UI
const event = new CustomEvent('TabAddedToEssentials', {
detail: { tab },
bubbles: true,
cancelable: false,
});
tab.dispatchEvent(event);
}
gZenUIManager.updateTabsToolbar();
return movedAll;
}
removeEssentials(tab, unpin = true) {
const tabs = tab
? [tab]
: TabContextMenu.contextTab.multiselected
? gBrowser.selectedTabs
: [TabContextMenu.contextTab];
for (let i = 0; i < tabs.length; i++) {
const tab = tabs[i];
tab.removeAttribute('zen-essential');
if (gZenWorkspaces.workspaceEnabled && gZenWorkspaces.getActiveWorkspaceFromCache().uuid) {
tab.setAttribute('zen-workspace-id', gZenWorkspaces.getActiveWorkspaceFromCache().uuid);
}
if (unpin) {
gBrowser.unpinTab(tab);
} else {
gBrowser.zenHandleTabMove(tab, () => {
const pinContainer = gZenWorkspaces.pinnedTabsContainer;
pinContainer.prepend(tab);
});
}
// Dispatch the event to update the UI
const event = new CustomEvent('TabRemovedFromEssentials', {
detail: { tab },
bubbles: true,
cancelable: false,
});
tab.dispatchEvent(event);
}
gZenUIManager.updateTabsToolbar();
}
_insertItemsIntoTabContextMenu() {
if (!this.enabled) {
return;
}
const elements = window.MozXULElement.parseXULToFragment(`
<menuseparator id="context_zen-pinned-tab-separator" hidden="true"/>
<menuitem id="context_zen-replace-pinned-url-with-current"
data-lazy-l10n-id="tab-context-zen-replace-pinned-url-with-current"
hidden="true"
command="cmd_zenReplacePinnedUrlWithCurrent"/>
<menuitem id="context_zen-reset-pinned-tab"
data-lazy-l10n-id="tab-context-zen-reset-pinned-tab"
hidden="true"
command="cmd_zenPinnedTabResetNoTab"/>
`);
document.getElementById('tabContextMenu').appendChild(elements);
const element = window.MozXULElement.parseXULToFragment(`
<menuitem id="context_zen-add-essential"
data-l10n-id="tab-context-zen-add-essential"
hidden="true"
disabled="true"
command="cmd_contextZenAddToEssentials"/>
<menuitem id="context_zen-remove-essential"
data-lazy-l10n-id="tab-context-zen-remove-essential"
hidden="true"
command="cmd_contextZenRemoveFromEssentials"/>
<menuseparator/>
<menuitem id="context_zen-edit-tab-title"
data-lazy-l10n-id="tab-context-zen-edit-title"
hidden="true"/>
<menuitem id="context_zen-edit-tab-icon"
data-lazy-l10n-id="tab-context-zen-edit-icon"/>
<menuseparator/>
`);
document.getElementById('context_pinTab')?.before(element);
document.getElementById('context_zen-edit-tab-title').addEventListener('command', (event) => {
gZenVerticalTabsManager.renameTabStart(event);
});
document.getElementById('context_zen-edit-tab-icon').addEventListener('command', () => {
const tab = TabContextMenu.contextTab;
gZenEmojiPicker
.open(tab.iconImage, { emojiAsSVG: true })
.then((icon) => {
if (icon) {
tab.zenStaticIcon = icon;
} else {
delete tab.zenStaticIcon;
}
gBrowser.setIcon(tab, icon);
lazy.TabStateCache.update(tab.permanentKey, {
image: null,
});
})
.catch((err) => {
console.error(err);
return;
});
});
}
updatePinnedTabContextMenu(contextTab) {
if (!this.enabled) {
document.getElementById('context_pinTab').hidden = true;
return;
}
const isVisible = contextTab.pinned && !contextTab.multiselected;
const isEssential = contextTab.getAttribute('zen-essential');
const zenAddEssential = document.getElementById('context_zen-add-essential');
document.getElementById('context_zen-reset-pinned-tab').hidden = !isVisible;
document.getElementById('context_zen-replace-pinned-url-with-current').hidden = !isVisible;
zenAddEssential.hidden = isEssential || !!contextTab.group;
document.l10n
.formatValue('tab-context-zen-add-essential-badge', {
num: gBrowser._numZenEssentials,
max: this.maxEssentialTabs,
})
.then((badgeText) => {
zenAddEssential.setAttribute('badge', badgeText);
});
document
.getElementById('cmd_contextZenAddToEssentials')
.setAttribute('disabled', !this.canEssentialBeAdded(contextTab));
document.getElementById('context_closeTab').hidden = contextTab.hasAttribute('zen-essential');
document.getElementById('context_zen-remove-essential').hidden = !isEssential;
document.getElementById('context_unpinTab').hidden =
document.getElementById('context_unpinTab').hidden || isEssential;
document.getElementById('context_unpinSelectedTabs').hidden =
document.getElementById('context_unpinSelectedTabs').hidden || isEssential;
document.getElementById('context_zen-pinned-tab-separator').hidden = !isVisible;
document.getElementById('context_zen-edit-tab-title').hidden =
isEssential ||
!Services.prefs.getBoolPref('zen.tabs.rename-tabs') ||
!gZenVerticalTabsManager._prefsSidebarExpanded;
}
moveToAnotherTabContainerIfNecessary(event, movingTabs) {
movingTabs = [...movingTabs];
if (!this.enabled) {
return false;
}
try {
const pinnedTabsTarget = event.target.closest(
':is(.zen-current-workspace-indicator, .zen-workspace-pinned-tabs-section)'
);
const essentialTabsTarget = event.target.closest('.zen-essentials-container');
const tabsTarget = !pinnedTabsTarget;
// TODO: Solve the issue of adding a tab between two groups
// Remove group labels from the moving tabs and replace it
// with the sub tabs
for (let i = 0; i < movingTabs.length; i++) {
const draggedTab = movingTabs[i];
if (gBrowser.isTabGroupLabel(draggedTab)) {
const group = draggedTab.group;
// remove label and add sub tabs to moving tabs
if (group) {
movingTabs.splice(i, 1, ...group.tabs);
}
}
}
let isVertical = this.expandedSidebarMode;
let moved = false;
let hasActuallyMoved;
for (const draggedTab of movingTabs) {
let isRegularTabs = false;
// Check for essentials container
if (essentialTabsTarget) {
if (!draggedTab.hasAttribute('zen-essential') && !draggedTab?.group) {
moved = true;
isVertical = false;
hasActuallyMoved = this.addToEssentials(draggedTab);
}
}
// Check for pinned tabs container
else if (pinnedTabsTarget) {
if (!draggedTab.pinned) {
gBrowser.pinTab(draggedTab);
} else if (draggedTab.hasAttribute('zen-essential')) {
this.removeEssentials(draggedTab, false);
moved = true;
}
}
// Check for normal tabs container
else if (tabsTarget || event.target.id === 'zen-tabs-wrapper') {
if (draggedTab.pinned && !draggedTab.hasAttribute('zen-essential')) {
gBrowser.unpinTab(draggedTab);
isRegularTabs = true;
} else if (draggedTab.hasAttribute('zen-essential')) {
this.removeEssentials(draggedTab);
moved = true;
isRegularTabs = true;
}
}
if (typeof hasActuallyMoved === 'undefined') {
hasActuallyMoved = moved;
}
// If the tab was moved, adjust its position relative to the target tab
if (hasActuallyMoved) {
const targetTab = event.target.closest('.tabbrowser-tab');
const targetFolder = event.target.closest('zen-folder');
let targetElem = targetTab || targetFolder?.labelElement;
if (targetElem?.group?.activeGroups?.length > 0) {
const activeGroup = targetElem.group.activeGroups.at(-1);
targetElem = activeGroup.labelElement;
}
if (targetElem) {
const rect = targetElem.getBoundingClientRect();
let elementIndex = targetElem.elementIndex;
if (isVertical || !this.expandedSidebarMode) {
const middleY = targetElem.screenY + rect.height / 2;
if (!isRegularTabs && event.screenY > middleY) {
elementIndex++;
} else if (isRegularTabs && event.screenY < middleY) {
elementIndex--;
}
} else {
const middleX = targetElem.screenX + rect.width / 2;
if (event.screenX > middleX) {
elementIndex++;
}
}
// If it's the last tab, move it to the end
if (tabsTarget === gBrowser.tabs.at(-1)) {
elementIndex++;
}
gBrowser.moveTabTo(draggedTab, {
elementIndex,
forceUngrouped: targetElem?.group?.collapsed !== false,
});
}
}
}
return moved;
} catch (ex) {
console.error('Error moving tabs:', ex);
return false;
}
}
onLocationChange(browser) {
const tab = gBrowser.getTabForBrowser(browser);
if (
!tab ||
!tab.pinned ||
tab.hasAttribute('zen-essential') ||
!tab._zenPinnedInitialState?.entry
) {
return;
}
// Remove # and ? from the URL
const pinUrl = tab._zenPinnedInitialState.entry.url.split('#')[0];
const currentUrl = browser.currentURI.spec.split('#')[0];
// Add an indicator that the pin has been changed
if (pinUrl === currentUrl) {
this.resetPinChangedUrl(tab);
return;
}
this.pinHasChangedUrl(tab);
}
resetPinChangedUrl(tab) {
if (!tab.hasAttribute('zen-pinned-changed')) {
return;
}
tab.removeAttribute('zen-pinned-changed');
tab.removeAttribute('had-zen-pinned-changed');
tab.style.removeProperty('--zen-original-tab-icon');
}
pinHasChangedUrl(tab) {
if (tab.hasAttribute('zen-pinned-changed')) {
return;
}
if (tab.group?.hasAttribute('split-view-group')) {
tab.setAttribute('had-zen-pinned-changed', 'true');
} else {
tab.setAttribute('zen-pinned-changed', 'true');
}
tab.style.setProperty('--zen-original-tab-icon', `url(${tab._zenPinnedInitialState.image})`);
}
removeTabContainersDragoverClass(hideIndicator = true) {
this.dragIndicator.remove();
this._dragIndicator = null;
if (hideIndicator) {
gZenWorkspaces.activeWorkspaceIndicator?.removeAttribute('open');
}
}
get dragIndicator() {
if (!this._dragIndicator) {
this._dragIndicator = document.createElement('div');
this._dragIndicator.id = 'zen-drag-indicator';
gNavToolbox.appendChild(this._dragIndicator);
}
return this._dragIndicator;
}
get expandedSidebarMode() {
return document.documentElement.getAttribute('zen-sidebar-expanded') === 'true';
}
canEssentialBeAdded(tab) {
return (
!(
(tab.getAttribute('usercontextid') || 0) !=
gZenWorkspaces.getActiveWorkspaceFromCache().containerTabId &&
gZenWorkspaces.containerSpecificEssentials
) && gBrowser._numZenEssentials < this.maxEssentialTabs
);
}
applyDragoverClass(event, draggedTab) {
if (!this.enabled) {
return;
}
let isVertical = this.expandedSidebarMode;
if (
gBrowser.isTabGroupLabel(draggedTab) &&
!draggedTab?.group?.hasAttribute('split-view-group')
) {
// If the target is a tab group label, we don't want to apply the dragover class
this.removeTabContainersDragoverClass();
return;
}
const pinnedTabsTarget = event.target.closest('.zen-workspace-pinned-tabs-section');
const essentialTabsTarget = event.target.closest('.zen-essentials-container');
const tabsTarget = event.target.closest('.zen-workspace-normal-tabs-section');
const folderTarget = event.target.closest('zen-folder');
let targetTab = event.target.closest('.tabbrowser-tab');
targetTab = targetTab?.group || targetTab;
draggedTab = draggedTab?.group?.hasAttribute('split-view-group')
? draggedTab.group
: draggedTab;
const isHoveringIndicator = !!event.target.closest('.zen-current-workspace-indicator');
if (isHoveringIndicator) {
this.removeTabContainersDragoverClass(false);
gZenWorkspaces.activeWorkspaceIndicator?.setAttribute('open', true);
} else {
gZenWorkspaces.activeWorkspaceIndicator?.removeAttribute('open');
}
if (draggedTab?._dragData?.movingTabs) {
gZenFolders.ungroupTabsFromActiveGroups(draggedTab._dragData.movingTabs);
}
let shouldAddDragOverElement = false;
// Decide whether we should show a dragover class for the given target
if (essentialTabsTarget) {
if (!draggedTab.hasAttribute('zen-essential') && this.canEssentialBeAdded(draggedTab)) {
shouldAddDragOverElement = true;
isVertical = false;
}
} else if (pinnedTabsTarget) {
if (draggedTab.hasAttribute('zen-essential')) {
shouldAddDragOverElement = true;
}
} else if (tabsTarget) {
if (draggedTab.hasAttribute('zen-essential')) {
shouldAddDragOverElement = true;
}
}
if (!shouldAddDragOverElement || (!targetTab && !folderTarget) || !targetTab) {
this.removeTabContainersDragoverClass(!isHoveringIndicator);
return;
}
// Calculate middle to decide 'before' or 'after'
const rect = targetTab.getBoundingClientRect();
let shouldPlayHapticFeedback = false;
if (isVertical || !this.expandedSidebarMode) {
const separation = 8;
const middleY = targetTab.screenY + rect.height / 2;
const indicator = this.dragIndicator;
let top = 0;
if (event.screenY > middleY) {
top = Math.round(rect.top + rect.height) + 'px';
} else {
top = Math.round(rect.top) + 'px';
}
if (indicator.style.top !== top) {
shouldPlayHapticFeedback = true;
}
indicator.setAttribute('orientation', 'horizontal');
indicator.style.setProperty('--indicator-left', rect.left + separation / 2 + 'px');
indicator.style.setProperty('--indicator-width', rect.width - separation + 'px');
indicator.style.top = top;
indicator.style.removeProperty('left');
} else {
const separation = 8;
const middleX = targetTab.screenX + rect.width / 2;
const indicator = this.dragIndicator;
let left = 0;
if (event.screenX > middleX) {
left = Math.round(rect.left + rect.width + 1) + 'px';
} else {
left = Math.round(rect.left - 2) + 'px';
}
if (indicator.style.left !== left) {
shouldPlayHapticFeedback = true;
}
indicator.setAttribute('orientation', 'vertical');
indicator.style.setProperty('--indicator-top', rect.top + separation / 2 + 'px');
indicator.style.setProperty('--indicator-height', rect.height - separation + 'px');
indicator.style.left = left;
indicator.style.removeProperty('top');
}
if (shouldPlayHapticFeedback) {
Services.zen.playHapticFeedback();
}
}
onTabLabelChanged(tab) {
tab.dispatchEvent(new CustomEvent('ZenTabLabelChanged', { bubbles: true, detail: { tab } }));
}
}
window.gZenPinnedTabManager = new nsZenPinnedTabManager();