Refactor ZenPinnedTabManager to improve performance and add TabClose event handling

This commit is contained in:
mr. M 2024-11-03 13:44:31 +01:00
parent 8fce947f5f
commit 9d41110b8b
No known key found for this signature in database
GPG key ID: CBD57A2AEDBDA1FB

View file

@ -2,7 +2,7 @@
const lazy = {}; const lazy = {};
class ZenPinnedTabsObserver { class ZenPinnedTabsObserver {
static ALL_EVENTS = ['TabPinned', 'TabUnpinned']; static ALL_EVENTS = ['TabPinned', 'TabUnpinned', 'TabClose'];
#listeners = []; #listeners = [];
@ -47,6 +47,8 @@
this._initClosePinnedTabShortcut(); this._initClosePinnedTabShortcut();
this._insertItemsIntoTabContextMenu(); this._insertItemsIntoTabContextMenu();
this.observer.addPinnedTabListener(this._onPinnedTabEvent.bind(this)); this.observer.addPinnedTabListener(this._onPinnedTabEvent.bind(this));
this._zenClickEventListener = this._onTabClick.bind(this);
} }
async _refreshPinnedTabs() { async _refreshPinnedTabs() {
@ -95,7 +97,7 @@
if (!pins?.length) { if (!pins?.length) {
// If there are no pins, we should remove any existing pinned tabs // If there are no pins, we should remove any existing pinned tabs
for (let tab of gBrowser.tabs) { for (let tab of gBrowser.tabs) {
if (tab.pinned && tab.getAttribute("zen-pin-id")) { if (tab.pinned && !tab.getAttribute("zen-pin-id")) {
gBrowser.removeTab(tab); gBrowser.removeTab(tab);
} }
} }
@ -144,6 +146,7 @@
} }
newTab.setAttribute("zen-pin-id", pin.uuid); newTab.setAttribute("zen-pin-id", pin.uuid);
gBrowser.setInitialTabTitle(newTab, pin.title);
if (pin.workspaceUuid) { if (pin.workspaceUuid) {
newTab.setAttribute("zen-workspace-id", pin.workspaceUuid); newTab.setAttribute("zen-workspace-id", pin.workspaceUuid);
@ -162,7 +165,7 @@
const tab = event.target; const tab = event.target;
switch (action) { switch (action) {
case "TabPinned": case "TabPinned":
tab._zenClickEventListener = this._onTabClick.bind(this, tab); tab._zenClickEventListener = this._zenClickEventListener;
tab.addEventListener("click", tab._zenClickEventListener); tab.addEventListener("click", tab._zenClickEventListener);
this._setPinnedAttributes(tab); this._setPinnedAttributes(tab);
break; break;
@ -173,13 +176,17 @@
delete tab._zenClickEventListener; delete tab._zenClickEventListener;
} }
break; break;
case "TabClose":
this._removePinnedAttributes(tab);
break;
default: default:
console.warn('ZenPinnedTabManager: Unhandled tab event', action); console.warn('ZenPinnedTabManager: Unhandled tab event', action);
break; break;
} }
} }
_onTabClick(tab, e) { _onTabClick(e) {
const tab = e.target;
if (e.button === 1) { if (e.button === 1) {
this._onCloseTabShortcut(e, tab); this._onCloseTabShortcut(e, tab);
} }
@ -264,18 +271,6 @@
} }
} }
setPinnedTabState(tabData, tab) {
tabData.zenPinId = tab.getAttribute("zen-pin-id");
}
updatePinnedTabForSessionRestore(tabData, tab) {
if (tabData.zenPinId) {
tab.setAttribute("zen-pin-id", tabData.zenPinId);
} else {
tab.removeAttribute("zen-pin-id");
}
}
_onCloseTabShortcut(event, selectedTab = gBrowser.selectedTab) { _onCloseTabShortcut(event, selectedTab = gBrowser.selectedTab) {
if ( if (
!selectedTab?.pinned !selectedTab?.pinned
@ -376,6 +371,7 @@
_insertItemsIntoTabContextMenu() { _insertItemsIntoTabContextMenu() {
const elements = window.MozXULElement.parseXULToFragment(` const elements = window.MozXULElement.parseXULToFragment(`
<menuseparator id="context_zen-pinned-tab-separator" hidden="true"/>
<menuitem id="context_zen-replace-pinned-url-with-current" <menuitem id="context_zen-replace-pinned-url-with-current"
data-lazy-l10n-id="tab-context-zen-replace-pinned-url-with-current" data-lazy-l10n-id="tab-context-zen-replace-pinned-url-with-current"
hidden="true" hidden="true"
@ -410,6 +406,7 @@
document.getElementById("context_zen-reset-pinned-tab").hidden = !isVisible || !contextTab.getAttribute("zen-pin-id"); document.getElementById("context_zen-reset-pinned-tab").hidden = !isVisible || !contextTab.getAttribute("zen-pin-id");
document.getElementById("context_zen-replace-pinned-url-with-current").hidden = !isVisible; document.getElementById("context_zen-replace-pinned-url-with-current").hidden = !isVisible;
document.getElementById("context_zen-pin-tab-global").hidden = contextTab.pinned; document.getElementById("context_zen-pin-tab-global").hidden = contextTab.pinned;
document.getElementById("context_zen-pinned-tab-separator").hidden = !isVisible;
} }
} }