feat: add pinned tab reset on close shortcut

Adds a new feature allowing users to reset a pinned tab's URL and title when closing it using the Ctrl+W shortcut. This option can be enabled/disabled in the Zen preferences.

The feature also adds a new context menu item to manually reset a pinned tab's URL and title. This menu item appears only when right-clicking a pinned tab.
This commit is contained in:
Kristijan Ribarić 2024-10-01 15:53:07 +02:00
parent fc7f08c827
commit a8df1907b9

View file

@ -7,6 +7,13 @@
XPCOMUtils.defineLazyPreferenceGetter(lazy, 'zenTabUnloaderExcludedUrls', 'zen.tab-unloader.excluded-urls', '');
XPCOMUtils.defineLazyPreferenceGetter(
lazy,
"zenPinnedTabResetOnCloseShortcutEnabled",
"zen.tab-unloader.reset-pinned-tab-on-close-shortcut",
false
);
const ZEN_TAB_UNLOADER_DEFAULT_EXCLUDED_URLS = [
'^about:',
'^chrome:',
@ -128,20 +135,114 @@
return;
}
this.insertIntoContextMenu();
this.insertResetTabIntoContextMenu();
this.initClosePinnedTabShortcut();
this.observer = new ZenTabsObserver();
this.intervalUnloader = new ZenTabsIntervalUnloader(this);
this.observer.addTabsListener(this.onTabEvent.bind(this));
}
onCloseTabShortcut(event) {
if (
event &&
(event.ctrlKey || event.metaKey || event.altKey) &&
gBrowser.selectedTab.pinned
) {
const selectedTab = gBrowser.selectedTab;
const url = selectedTab.getAttribute("zen-pinned-url");
const title = selectedTab.getAttribute("zen-pinned-title");
let nextTab = gBrowser.tabContainer.findNextTab(selectedTab, {
direction: 1,
filter: tab => !tab.hidden && !tab.pinned,
});
if (!nextTab) {
// If there's no next tab, try to find the previous one
nextTab = gBrowser.tabContainer.findNextTab(selectedTab, {
direction: -1,
filter: tab => !tab.hidden && !tab.pinned,
});
}
if (selectedTab) {
// Switch to the next tab
gBrowser.selectedTab = nextTab;
if (url && this.zenPinnedTabResetOnCloseShortcutEnabled) {
const tabState = SessionStore.getTabState(selectedTab);
const state = JSON.parse(tabState);
let activeIndex = (state.index || state.entries.length) - 1;
// Ensure the index is in bounds.
activeIndex = Math.min(activeIndex, state.entries.length - 1);
activeIndex = Math.max(activeIndex, 0);
state.entries[activeIndex].url = url;
state.entries[activeIndex].title = title;
SessionStore.setTabState(selectedTab, state);
}
gBrowser.discardBrowser(selectedTab);
event.stopPropagation();
event.preventDefault();
}
}
}
initClosePinnedTabShortcut() {
let cmdClose = document.getElementById('cmd_close');
if (cmdClose) {
cmdClose.addEventListener('command', this.onCloseTabShortcut.bind(lazy));
}
}
ResetPinnedTab(){
const selectedTab = TabContextMenu.contextTab;
const url = selectedTab.getAttribute("zen-pinned-url");
const title = selectedTab.getAttribute("zen-pinned-title");
if (url) {
const tabState = SessionStore.getTabState(selectedTab);
const state = JSON.parse(tabState);
let activeIndex = (state.index || state.entries.length) - 1;
// Ensure the index is in bounds.
activeIndex = Math.min(activeIndex, state.entries.length - 1);
activeIndex = Math.max(activeIndex, 0);
state.entries[activeIndex].url = url;
state.entries[activeIndex].title = title;
SessionStore.setTabState(selectedTab, state);
}
}
insertResetTabIntoContextMenu() {
const element = window.MozXULElement.parseXULToFragment(`
<menuitem id="context_zen-reset-pinned-tab"
data-lazy-l10n-id="tab-context-zen-reset-pinned-tab"
hidden="true"
oncommand="gZenTabUnloader.ResetPinnedTab();"/>
`);
document.getElementById('context_zenUnloadTab').before(element);
}
onTabEvent(action, event) {
const tab = event.target;
switch (action) {
case 'TabPinned':
case 'TabUnpinned':
case 'TabBrowserInserted':
case 'TabBrowserDiscarded':
case 'TabShow':
case 'TabHide':
case "TabPinned":
tab.setAttribute("zen-pinned-url", tab.linkedBrowser.currentURI.spec);
tab.setAttribute("zen-pinned-title", tab.getAttribute("label"));
break;
case "TabUnpinned":
tab.removeAttribute("zen-pinned-url");
tab.removeAttribute("zen-pinned-title");
break;
case "TabBrowserInserted":
case "TabBrowserDiscarded":
case "TabShow":
case "TabHide":
break;
case 'TabAttrModified':
this.handleTabAttrModified(tab, event);