feat: Unified pinned tab close shortcut behavior

This commit introduces a unified behavior for the pinned tab close shortcut, controlled by a single preference `zen.pinned-tab-manager.close-shortcut-behavior`.

The previous implementation relied on multiple separate preferences:

- `zen.pinned-tab-manager.reset-pinned-tab-on-close-shortcut`
- `zen.pinned-tab-manager.unload-pinned-tab-on-close-shortcut`
- `zen.pinned-tab-manager.change-pinned-tab-on-close-shortcut`
- `zen.pinned-tab-manager.close-pinned-tab-on-close-shortcut`

This new unified approach simplifies the configuration and offers more granular control over the desired behavior.

The new preference `zen.pinned-tab-manager.close-shortcut-behavior` accepts the following values:

- `close`: Closes the selected pinned tab.
- `reset`: Resets the selected pinned tab to its stored state.
- `unload`: Unloads the selected pinned tab.
- `switch`: Switches to the next unpinned tab.
- `reset-unload-switch`: Resets, unloads, and switches the selected pinned tab.
- `unload-switch`: Unloads and switches the selected pinned tab.
- `reset-switch`: Resets and switches the selected pinned tab.

This unified approach provides greater flexibility and simplifies the user experience.
This commit is contained in:
Kristijan Ribarić 2024-10-08 21:07:57 +02:00
parent e28f8e131e
commit 4217882981

View file

@ -2,10 +2,7 @@
const lazy = {}; const lazy = {};
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, 'zenPinnedTabResetOnCloseShortcut', 'zen.pinned-tab-manager.reset-pinned-tab-on-close-shortcut', false); XPCOMUtils.defineLazyPreferenceGetter(lazy, 'zenPinnedTabCloseShortcutBehavior', 'zen.pinned-tab-manager.close-shortcut-behavior', 'switch');
XPCOMUtils.defineLazyPreferenceGetter(lazy, 'zenPinnedTabUnloadOnCloseShortcut', 'zen.pinned-tab-manager.unload-pinned-tab-on-close-shortcut', false);
XPCOMUtils.defineLazyPreferenceGetter(lazy, 'zenPinnedTabChangeOnCloseShortcut', 'zen.pinned-tab-manager.change-pinned-tab-on-close-shortcut', false);
XPCOMUtils.defineLazyPreferenceGetter(lazy, 'zenPinnedTabCloseOnCloseShortcut', 'zen.pinned-tab-manager.close-pinned-tab-on-close-shortcut', false);
class ZenPinnedTabsObserver { class ZenPinnedTabsObserver {
static ALL_EVENTS = ['TabPinned', 'TabUnpinned']; static ALL_EVENTS = ['TabPinned', 'TabUnpinned'];
@ -126,51 +123,55 @@
_onCloseTabShortcut(event) { _onCloseTabShortcut(event) {
if ( if (
event && !event ||
(event.ctrlKey || event.metaKey || event.altKey) && !(event.ctrlKey || event.metaKey || event.altKey) ||
gBrowser.selectedTab?.pinned !gBrowser.selectedTab?.pinned
) { ) {
const selectedTab = gBrowser.selectedTab; return;
}
if (selectedTab) { const selectedTab = gBrowser.selectedTab;
if (!selectedTab) return;
if (lazy.zenPinnedTabCloseOnCloseShortcut) { const behavior = lazy.zenPinnedTabCloseShortcutBehavior;
gBrowser.removeTab(selectedTab, { animate: true });
event.stopPropagation(); switch (behavior) {
event.preventDefault(); case 'close':
return; gBrowser.removeTab(selectedTab, { animate: true });
} break;
case 'reset-unload-switch':
if(lazy.zenPinnedTabChangeOnCloseShortcut) { case 'unload-switch':
case 'reset-switch':
let nextTab = gBrowser.tabContainer.findNextTab(selectedTab, { case 'switch':
direction: 1, this._handleTabSwitch(selectedTab);
filter: tab => !tab.hidden && !tab.pinned, if (behavior.includes('reset')) {
});
if (!nextTab) {
nextTab = gBrowser.tabContainer.findNextTab(selectedTab, {
direction: -1,
filter: tab => !tab.hidden && !tab.pinned,
});
}
gBrowser.selectedTab = nextTab;
}
if (lazy.zenPinnedTabResetOnCloseShortcut) {
this._resetTabToStoredState(selectedTab); this._resetTabToStoredState(selectedTab);
} }
if (behavior.includes('unload')) {
if(lazy.zenPinnedTabUnloadOnCloseShortcut) {
gBrowser.discardBrowser(selectedTab); gBrowser.discardBrowser(selectedTab);
} }
event.stopPropagation(); break;
event.preventDefault(); case 'reset':
} this._resetTabToStoredState(selectedTab);
break;
default:
return;
}
event.stopPropagation();
event.preventDefault();
}
_handleTabSwitch(selectedTab) {
const findNextTab = (direction) =>
gBrowser.tabContainer.findNextTab(selectedTab, {
direction,
filter: tab => !tab.hidden && !tab.pinned,
});
const nextTab = findNextTab(1) || findNextTab(-1);
if (nextTab) {
gBrowser.selectedTab = nextTab;
} }
} }