Updated zen pinned tab manager to use lazy prefs getter, added methods for setting Pinned Tab State and updating pinned tab for session restore

This commit is contained in:
Kristijan Ribarić 2024-10-08 14:14:43 +02:00
parent dab9e1edcc
commit 2e37d6ce0b

View file

@ -1,160 +1,180 @@
class ZenPinnedTabsObserver { {
static ALL_EVENTS = ['TabPinned', 'TabUnpinned']; const lazy = {};
#listeners = []; 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);
constructor() { class ZenPinnedTabsObserver {
this.#listenPinnedTabEvents(); static ALL_EVENTS = ['TabPinned', 'TabUnpinned'];
}
#listenPinnedTabEvents() { #listeners = [];
const eventListener = this.#eventListener.bind(this);
for (const event of ZenPinnedTabsObserver.ALL_EVENTS) { constructor() {
window.addEventListener(event, eventListener); this.#listenPinnedTabEvents();
} }
window.addEventListener('unload', () => {
#listenPinnedTabEvents() {
const eventListener = this.#eventListener.bind(this);
for (const event of ZenPinnedTabsObserver.ALL_EVENTS) { for (const event of ZenPinnedTabsObserver.ALL_EVENTS) {
window.removeEventListener(event, eventListener); window.addEventListener(event, eventListener);
} }
}); window.addEventListener('unload', () => {
} for (const event of ZenPinnedTabsObserver.ALL_EVENTS) {
window.removeEventListener(event, eventListener);
#eventListener(event) { }
for (const listener of this.#listeners) {
listener(event.type, event);
}
}
addPinnedTabListener(listener) {
this.#listeners.push(listener);
}
}
class ZenPinnedTabManager {
init() {
this.observer = new ZenPinnedTabsObserver();
this._initClosePinnedTabShortcut();
this.insertItemsIntoTabContextMenu();
this.observer.addPinnedTabListener(this._onPinnedTabEvent.bind(this));
}
_onPinnedTabEvent(action, event) {
const tab = event.target;
switch (action) {
case "TabPinned":
this._setPinnedAttributes(tab);
break;
case "TabUnpinned":
this._removePinnedAttributes(tab);
break;
default:
console.warn('ZenPinnedTabManager: Unhandled tab event', action);
break;
}
}
resetPinnedTab(tab) {
if(!tab){
tab = TabContextMenu.contextTab;
}
if (!tab || !tab.pinned) {
return;
}
const url = tab.getAttribute("zen-pinned-url");
const title = tab.getAttribute("zen-pinned-title");
const icon = tab.getAttribute("zen-pinned-icon");
if (url) {
const tabState = SessionStore.getTabState(tab);
const state = JSON.parse(tabState);
state.entries = [{url, title}];
state.image = icon;
state.index = 0;
SessionStore.setTabState(tab, state);
}
}
replacePinnedUrlWithCurrent(){
const tab = TabContextMenu.contextTab;
if (!tab || !tab.pinned) {
return;
}
this._setPinnedAttributes(tab);
}
_setPinnedAttributes(tab) {
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);
}
_removePinnedAttributes(tab) {
tab.removeAttribute("zen-pinned-url");
tab.removeAttribute("zen-pinned-title");
tab.removeAttribute("zen-pinned-icon");
}
_initClosePinnedTabShortcut() {
let cmdClose = document.getElementById('cmd_close');
if (cmdClose) {
cmdClose.addEventListener('command', this._onCloseTabShortcut.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");
const icon = selectedTab.getAttribute("zen-pinned-icon");
let nextTab = gBrowser.tabContainer.findNextTab(selectedTab, {
direction: 1,
filter: tab => !tab.hidden && !tab.pinned,
}); });
}
if (!nextTab) { #eventListener(event) {
nextTab = gBrowser.tabContainer.findNextTab(selectedTab, { for (const listener of this.#listeners) {
direction: -1, listener(event.type, event);
}
}
addPinnedTabListener(listener) {
this.#listeners.push(listener);
}
}
class ZenPinnedTabManager {
init() {
this.observer = new ZenPinnedTabsObserver();
this._initClosePinnedTabShortcut();
this._insertItemsIntoTabContextMenu();
this.observer.addPinnedTabListener(this._onPinnedTabEvent.bind(this));
}
_onPinnedTabEvent(action, event) {
const tab = event.target;
switch (action) {
case "TabPinned":
this._setPinnedAttributes(tab);
break;
case "TabUnpinned":
this._removePinnedAttributes(tab);
break;
default:
console.warn('ZenPinnedTabManager: Unhandled tab event', action);
break;
}
}
resetPinnedTab(tab) {
if (!tab) {
tab = TabContextMenu.contextTab;
}
if (!tab || !tab.pinned) {
return;
}
this._resetTabToStoredState(tab);
}
replacePinnedUrlWithCurrent() {
const tab = TabContextMenu.contextTab;
if (!tab || !tab.pinned) {
return;
}
this._setPinnedAttributes(tab);
}
_setPinnedAttributes(tab) {
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);
}
_removePinnedAttributes(tab) {
tab.removeAttribute("zen-pinned-url");
tab.removeAttribute("zen-pinned-title");
tab.removeAttribute("zen-pinned-icon");
}
_initClosePinnedTabShortcut() {
let cmdClose = document.getElementById('cmd_close');
if (cmdClose) {
cmdClose.addEventListener('command', this._onCloseTabShortcut.bind(this));
}
}
setPinnedTabState(tabData, tab) {
tabData.zenPinnedUrl = tab.getAttribute("zen-pinned-url");
tabData.zenPinnedTitle = tab.getAttribute("zen-pinned-title");
tabData.zenPinnedIcon = tab.getAttribute("zen-pinned-icon");
}
updatePinnedTabForSessionRestore(tabData, tab) {
if (tabData.zenPinnedUrl) {
tab.setAttribute("zen-pinned-url", tabData.zenPinnedUrl);
}
if (tabData.zenPinnedTitle) {
tab.setAttribute("zen-pinned-title", tabData.zenPinnedTitle);
}
if(tabData.zenPinnedIcon) {
tab.setAttribute("zen-pinned-icon", tabData.zenPinnedIcon);
}
}
_onCloseTabShortcut(event) {
if (
event &&
(event.ctrlKey || event.metaKey || event.altKey) &&
gBrowser.selectedTab.pinned
) {
const selectedTab = gBrowser.selectedTab;
let nextTab = gBrowser.tabContainer.findNextTab(selectedTab, {
direction: 1,
filter: tab => !tab.hidden && !tab.pinned, filter: tab => !tab.hidden && !tab.pinned,
}); });
}
if (selectedTab) { if (!nextTab) {
gBrowser.selectedTab = nextTab; nextTab = gBrowser.tabContainer.findNextTab(selectedTab, {
direction: -1,
if (url && Services.prefs.getBoolPref('zen.pinned-tab-manager.reset-pinned-tab-on-close-shortcut',false)) { filter: tab => !tab.hidden && !tab.pinned,
const tabState = SessionStore.getTabState(selectedTab); });
const state = JSON.parse(tabState);
state.entries = [{url, title}];
state.image = icon;
state.index = 0;
SessionStore.setTabState(selectedTab, state);
} }
gBrowser.discardBrowser(selectedTab); if (selectedTab) {
gBrowser.selectedTab = nextTab;
event.stopPropagation(); if (lazy.zenPinnedTabResetOnCloseShortcut) {
event.preventDefault(); this._resetTabToStoredState(selectedTab);
}
gBrowser.discardBrowser(selectedTab);
event.stopPropagation();
event.preventDefault();
}
} }
} }
}
insertItemsIntoTabContextMenu() { _resetTabToStoredState(tab) {
const elements = window.MozXULElement.parseXULToFragment(` const url = tab.getAttribute("zen-pinned-url");
const title = tab.getAttribute("zen-pinned-title");
const icon = tab.getAttribute("zen-pinned-icon");
if (url) {
const tabState = SessionStore.getTabState(tab);
const state = JSON.parse(tabState);
state.entries = [{url, title}];
state.image = icon;
state.index = 0;
SessionStore.setTabState(tab, state);
}
}
_insertItemsIntoTabContextMenu() {
const elements = window.MozXULElement.parseXULToFragment(`
<menuitem id="context_zen-replace-pinned-url-with-current" <menuitem id="context_zen-replace-pinned-url-with-current"
data-lazy-l10n-id="tab-context-zen-replace-pinned-url-with-current" data-lazy-l10n-id="tab-context-zen-replace-pinned-url-with-current"
hidden="true" hidden="true"
@ -164,8 +184,23 @@ class ZenPinnedTabManager {
hidden="true" hidden="true"
oncommand="gZenPinnedTabManager.resetPinnedTab();"/> oncommand="gZenPinnedTabManager.resetPinnedTab();"/>
`); `);
document.getElementById('tabContextMenu').appendChild(elements); document.getElementById('tabContextMenu').appendChild(elements);
} }
}
window.gZenPinnedTabManager = new ZenPinnedTabManager(); resetPinnedTabData(tabData) {
if (lazy.zenPinnedTabRestorePinnedTabsToPinnedUrl && tabData.pinned && tabData.zenPinnedUrl) {
tabData.entries = [{url: tabData.zenPinnedUrl, title: tabData.zenPinnedTitle}];
tabData.image = tabData.zenPinnedIcon;
tabData.index = 0;
}
}
updatePinnedTabContextMenu(contextTab) {
const isVisible = contextTab.pinned && contextTab.getAttribute("zen-pinned-url") && !contextTab.multiselected;
document.getElementById("context_zen-reset-pinned-tab").hidden = !isVisible;
document.getElementById("context_zen-replace-pinned-url-with-current").hidden = !isVisible;
}
}
window.gZenPinnedTabManager = new ZenPinnedTabManager();
}