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.
This commit is contained in:
Kristijan Ribarić 2024-10-12 10:08:53 +02:00
parent 9a1beadc03
commit d51cb4f1e3

View file

@ -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,