Merge pull request #50 from kristijanribaric/fix/middle-mouse-clicking-pinned-tab-should-follow-shortcut-behavior

Fix: Add middle click listener to pinned tabs to follow the behavior of pinned tab close shortcut
This commit is contained in:
mr. m 🤙 2024-10-12 18:45:17 +02:00 committed by GitHub
commit c5b907ade6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -9,7 +9,7 @@
constructor() { constructor() {
XPCOMUtils.defineLazyPreferenceGetter(lazy, 'zenPinnedTabRestorePinnedTabsToPinnedUrl', 'zen.pinned-tab-manager.restore-pinned-tabs-to-pinned-url', false); 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, 'zenPinnedTabCloseShortcutBehavior', 'zen.pinned-tab-manager.close-shortcut-behavior', 'switch');
ChromeUtils.defineESModuleGetters(lazy, {E10SUtils: "resource://gre/modules/E10SUtils.sys.mjs"});
this.#listenPinnedTabEvents(); this.#listenPinnedTabEvents();
} }
@ -49,9 +49,15 @@
switch (action) { switch (action) {
case "TabPinned": case "TabPinned":
this._setPinnedAttributes(tab); this._setPinnedAttributes(tab);
tab._zenClickEventListener = this._onTabClick.bind(this, tab);
tab.addEventListener("click", tab._zenClickEventListener);
break; break;
case "TabUnpinned": case "TabUnpinned":
this._removePinnedAttributes(tab); this._removePinnedAttributes(tab);
if (tab._zenClickEventListener) {
tab.removeEventListener("click", tab._zenClickEventListener);
delete tab._zenClickEventListener;
}
break; break;
default: default:
console.warn('ZenPinnedTabManager: Unhandled tab event', action); console.warn('ZenPinnedTabManager: Unhandled tab event', action);
@ -59,6 +65,12 @@
} }
} }
_onTabClick(tab, e) {
if (e.button === 1) {
this._onCloseTabShortcut(e, tab);
}
}
resetPinnedTab(tab) { resetPinnedTab(tab) {
if (!tab) { if (!tab) {
@ -85,12 +97,14 @@
tab.setAttribute("zen-pinned-url", tab.linkedBrowser.currentURI.spec); tab.setAttribute("zen-pinned-url", tab.linkedBrowser.currentURI.spec);
tab.setAttribute("zen-pinned-title", tab.getAttribute("label")); tab.setAttribute("zen-pinned-title", tab.getAttribute("label"));
tab.setAttribute("zen-pinned-icon", tab.linkedBrowser.mIconURL); tab.setAttribute("zen-pinned-icon", tab.linkedBrowser.mIconURL);
tab.setAttribute("zen-pinned-triggering-principal", lazy.E10SUtils.serializePrincipal(tab.linkedBrowser.contentPrincipal));
} }
_removePinnedAttributes(tab) { _removePinnedAttributes(tab) {
tab.removeAttribute("zen-pinned-url"); tab.removeAttribute("zen-pinned-url");
tab.removeAttribute("zen-pinned-title"); tab.removeAttribute("zen-pinned-title");
tab.removeAttribute("zen-pinned-icon"); tab.removeAttribute("zen-pinned-icon");
tab.removeAttribute("zen-pinned-triggering-principal");
} }
_initClosePinnedTabShortcut() { _initClosePinnedTabShortcut() {
@ -105,25 +119,25 @@
tabData.zenPinnedUrl = tab.getAttribute("zen-pinned-url"); tabData.zenPinnedUrl = tab.getAttribute("zen-pinned-url");
tabData.zenPinnedTitle = tab.getAttribute("zen-pinned-title"); tabData.zenPinnedTitle = tab.getAttribute("zen-pinned-title");
tabData.zenPinnedIcon = tab.getAttribute("zen-pinned-icon"); tabData.zenPinnedIcon = tab.getAttribute("zen-pinned-icon");
tabData.zenPinnedTriggeringPrincipal = tab.getAttribute("zen-pinned-triggering-principal");
} }
updatePinnedTabForSessionRestore(tabData, tab) { updatePinnedTabForSessionRestore(tabData, tab) {
!!tabData.zenPinnedUrl ? tab.setAttribute("zen-pinned-url", tabData.zenPinnedUrl) : tab.removeAttribute("zen-pinned-url"); !!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.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.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) { _onCloseTabShortcut(event, selectedTab = gBrowser.selectedTab) {
if ( if (
!event || !selectedTab?.pinned
!(event.ctrlKey || event.metaKey || event.altKey) ||
!gBrowser.selectedTab?.pinned
) { ) {
return; return;
} }
const selectedTab = gBrowser.selectedTab; event.stopPropagation();
if (!selectedTab) return; event.preventDefault();
const behavior = lazy.zenPinnedTabCloseShortcutBehavior; const behavior = lazy.zenPinnedTabCloseShortcutBehavior;
@ -150,11 +164,13 @@
return; return;
} }
event.stopPropagation();
event.preventDefault();
} }
_handleTabSwitch(selectedTab) { _handleTabSwitch(selectedTab) {
if(selectedTab !== gBrowser.selectedTab) {
return;
}
const findNextTab = (direction) => const findNextTab = (direction) =>
gBrowser.tabContainer.findNextTab(selectedTab, { gBrowser.tabContainer.findNextTab(selectedTab, {
direction, direction,
@ -171,12 +187,13 @@
const url = tab.getAttribute("zen-pinned-url"); const url = tab.getAttribute("zen-pinned-url");
const title = tab.getAttribute("zen-pinned-title"); const title = tab.getAttribute("zen-pinned-title");
const icon = tab.getAttribute("zen-pinned-icon"); const icon = tab.getAttribute("zen-pinned-icon");
const triggeringPrincipal_base64 = tab.getAttribute("zen-pinned-triggering-principal");
if (url) { if (url) {
const tabState = SessionStore.getTabState(tab); const tabState = SessionStore.getTabState(tab);
const state = JSON.parse(tabState); const state = JSON.parse(tabState);
state.entries = [{url, title}]; state.entries = [{url, title, triggeringPrincipal_base64}];
state.image = icon; state.image = icon;
state.index = 0; state.index = 0;
@ -200,7 +217,7 @@
resetPinnedTabData(tabData) { resetPinnedTabData(tabData) {
if (lazy.zenPinnedTabRestorePinnedTabsToPinnedUrl && tabData.pinned && tabData.zenPinnedUrl) { 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.image = tabData.zenPinnedIcon;
tabData.index = 0; tabData.index = 0;
} }