From d51cb4f1e3d5098386bbb53f5f0086e08354f287 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristijan=20Ribari=C4=87?= Date: Sat, 12 Oct 2024 10:08:53 +0200 Subject: [PATCH 1/2] Fix: Add middle click to handle close pinned tab and handle tab switch This commit fixes an issue where middle clicking a pinned tab would not follow the behavior of pinned tab close shortcut. --- src/ZenPinnedTabManager.mjs | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/ZenPinnedTabManager.mjs b/src/ZenPinnedTabManager.mjs index e4e7dc9..e3a9c48 100644 --- a/src/ZenPinnedTabManager.mjs +++ b/src/ZenPinnedTabManager.mjs @@ -9,7 +9,7 @@ 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'); - + this.#listenPinnedTabEvents(); } @@ -49,9 +49,15 @@ switch (action) { case "TabPinned": this._setPinnedAttributes(tab); + tab._zenClickEventListener = this._onTabClick.bind(this, tab); + tab.addEventListener("click", tab._zenClickEventListener); break; case "TabUnpinned": this._removePinnedAttributes(tab); + if (tab._zenClickEventListener) { + tab.removeEventListener("click", tab._zenClickEventListener); + delete tab._zenClickEventListener; + } break; default: console.warn('ZenPinnedTabManager: Unhandled tab event', action); @@ -59,6 +65,12 @@ } } + _onTabClick(tab, e) { + if (e.button === 1) { + this._onCloseTabShortcut(e, tab); + } + } + resetPinnedTab(tab) { if (!tab) { @@ -113,17 +125,15 @@ !!tabData.zenPinnedIcon ? tab.setAttribute("zen-pinned-icon", tabData.zenPinnedIcon) : tab.removeAttribute("zen-pinned-icon"); } - _onCloseTabShortcut(event) { + _onCloseTabShortcut(event, selectedTab = gBrowser.selectedTab) { if ( - !event || - !(event.ctrlKey || event.metaKey || event.altKey) || - !gBrowser.selectedTab?.pinned + !selectedTab?.pinned ) { return; } - const selectedTab = gBrowser.selectedTab; - if (!selectedTab) return; + event.stopPropagation(); + event.preventDefault(); const behavior = lazy.zenPinnedTabCloseShortcutBehavior; @@ -150,11 +160,13 @@ return; } - event.stopPropagation(); - event.preventDefault(); + } _handleTabSwitch(selectedTab) { + if(selectedTab !== gBrowser.selectedTab) { + return; + } const findNextTab = (direction) => gBrowser.tabContainer.findNextTab(selectedTab, { direction, From 0183de5382a7241411e7c0f4ff01f46beba9af63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristijan=20Ribari=C4=87?= Date: Sat, 12 Oct 2024 18:15:09 +0200 Subject: [PATCH 2/2] feat: Store triggering principal for pinned tabs This commit adds the ability to store the triggering principal for pinned tabs in the session store. This allows us to correctly restore pinned tabs when a user has multiple profiles open, as the triggering principal will be used to determine which profile the pinned tab belongs to. The following changes were made: - Added a new attribute, "zen-pinned-triggering-principal", to pinned tabs to store the triggering principal. - Updated the session store to store and restore the triggering principal for pinned tabs. - Updated the `ZenPinnedTabManager` to set and get the triggering principal for pinned tabs. This change is necessary to ensure that pinned tabs are correctly restored when reset to pinned URL. --- src/ZenPinnedTabManager.mjs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/ZenPinnedTabManager.mjs b/src/ZenPinnedTabManager.mjs index e3a9c48..285e56b 100644 --- a/src/ZenPinnedTabManager.mjs +++ b/src/ZenPinnedTabManager.mjs @@ -9,7 +9,7 @@ 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'); - + ChromeUtils.defineESModuleGetters(lazy, {E10SUtils: "resource://gre/modules/E10SUtils.sys.mjs"}); this.#listenPinnedTabEvents(); } @@ -97,12 +97,14 @@ tab.setAttribute("zen-pinned-url", tab.linkedBrowser.currentURI.spec); tab.setAttribute("zen-pinned-title", tab.getAttribute("label")); tab.setAttribute("zen-pinned-icon", tab.linkedBrowser.mIconURL); + tab.setAttribute("zen-pinned-triggering-principal", lazy.E10SUtils.serializePrincipal(tab.linkedBrowser.contentPrincipal)); } _removePinnedAttributes(tab) { tab.removeAttribute("zen-pinned-url"); tab.removeAttribute("zen-pinned-title"); tab.removeAttribute("zen-pinned-icon"); + tab.removeAttribute("zen-pinned-triggering-principal"); } _initClosePinnedTabShortcut() { @@ -117,12 +119,14 @@ tabData.zenPinnedUrl = tab.getAttribute("zen-pinned-url"); tabData.zenPinnedTitle = tab.getAttribute("zen-pinned-title"); tabData.zenPinnedIcon = tab.getAttribute("zen-pinned-icon"); + tabData.zenPinnedTriggeringPrincipal = tab.getAttribute("zen-pinned-triggering-principal"); } updatePinnedTabForSessionRestore(tabData, tab) { !!tabData.zenPinnedUrl ? tab.setAttribute("zen-pinned-url", tabData.zenPinnedUrl) : tab.removeAttribute("zen-pinned-url"); !!tabData.zenPinnedTitle ? tab.setAttribute("zen-pinned-title", tabData.zenPinnedTitle) : tab.removeAttribute("zen-pinned-title"); !!tabData.zenPinnedIcon ? tab.setAttribute("zen-pinned-icon", tabData.zenPinnedIcon) : tab.removeAttribute("zen-pinned-icon"); + !!tabData.zenPinnedTriggeringPrincipal ? tab.setAttribute("zen-pinned-triggering-principal", tabData.zenPinnedTriggeringPrincipal) : tab.removeAttribute("zen-pinned-triggering-principal"); } _onCloseTabShortcut(event, selectedTab = gBrowser.selectedTab) { @@ -183,12 +187,13 @@ const url = tab.getAttribute("zen-pinned-url"); const title = tab.getAttribute("zen-pinned-title"); const icon = tab.getAttribute("zen-pinned-icon"); + const triggeringPrincipal_base64 = tab.getAttribute("zen-pinned-triggering-principal"); if (url) { const tabState = SessionStore.getTabState(tab); const state = JSON.parse(tabState); - state.entries = [{url, title}]; + state.entries = [{url, title, triggeringPrincipal_base64}]; state.image = icon; state.index = 0; @@ -212,7 +217,7 @@ resetPinnedTabData(tabData) { if (lazy.zenPinnedTabRestorePinnedTabsToPinnedUrl && tabData.pinned && tabData.zenPinnedUrl) { - tabData.entries = [{url: tabData.zenPinnedUrl, title: tabData.zenPinnedTitle}]; + tabData.entries = [{url: tabData.zenPinnedUrl, title: tabData.zenPinnedTitle, triggeringPrincipal_base64: tabData.zenPinnedTriggeringPrincipal}]; tabData.image = tabData.zenPinnedIcon; tabData.index = 0; }