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,4 +1,10 @@
class ZenPinnedTabsObserver { {
const lazy = {};
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);
class ZenPinnedTabsObserver {
static ALL_EVENTS = ['TabPinned', 'TabUnpinned']; static ALL_EVENTS = ['TabPinned', 'TabUnpinned'];
#listeners = []; #listeners = [];
@ -28,13 +34,13 @@ class ZenPinnedTabsObserver {
addPinnedTabListener(listener) { addPinnedTabListener(listener) {
this.#listeners.push(listener); this.#listeners.push(listener);
} }
} }
class ZenPinnedTabManager { class ZenPinnedTabManager {
init() { init() {
this.observer = new ZenPinnedTabsObserver(); this.observer = new ZenPinnedTabsObserver();
this._initClosePinnedTabShortcut(); this._initClosePinnedTabShortcut();
this.insertItemsIntoTabContextMenu(); this._insertItemsIntoTabContextMenu();
this.observer.addPinnedTabListener(this._onPinnedTabEvent.bind(this)); this.observer.addPinnedTabListener(this._onPinnedTabEvent.bind(this));
} }
@ -55,7 +61,7 @@ class ZenPinnedTabManager {
resetPinnedTab(tab) { resetPinnedTab(tab) {
if(!tab){ if (!tab) {
tab = TabContextMenu.contextTab; tab = TabContextMenu.contextTab;
} }
@ -63,23 +69,10 @@ class ZenPinnedTabManager {
return; return;
} }
const url = tab.getAttribute("zen-pinned-url"); this._resetTabToStoredState(tab);
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(){ replacePinnedUrlWithCurrent() {
const tab = TabContextMenu.contextTab; const tab = TabContextMenu.contextTab;
if (!tab || !tab.pinned) { if (!tab || !tab.pinned) {
return; return;
@ -108,6 +101,26 @@ class ZenPinnedTabManager {
} }
} }
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) { _onCloseTabShortcut(event) {
if ( if (
event && event &&
@ -115,9 +128,6 @@ class ZenPinnedTabManager {
gBrowser.selectedTab.pinned gBrowser.selectedTab.pinned
) { ) {
const selectedTab = gBrowser.selectedTab; 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, { let nextTab = gBrowser.tabContainer.findNextTab(selectedTab, {
direction: 1, direction: 1,
@ -134,15 +144,8 @@ class ZenPinnedTabManager {
if (selectedTab) { if (selectedTab) {
gBrowser.selectedTab = nextTab; gBrowser.selectedTab = nextTab;
if (url && Services.prefs.getBoolPref('zen.pinned-tab-manager.reset-pinned-tab-on-close-shortcut',false)) { if (lazy.zenPinnedTabResetOnCloseShortcut) {
const tabState = SessionStore.getTabState(selectedTab); this._resetTabToStoredState(selectedTab);
const state = JSON.parse(tabState);
state.entries = [{url, title}];
state.image = icon;
state.index = 0;
SessionStore.setTabState(selectedTab, state);
} }
gBrowser.discardBrowser(selectedTab); gBrowser.discardBrowser(selectedTab);
@ -153,7 +156,24 @@ class ZenPinnedTabManager {
} }
} }
insertItemsIntoTabContextMenu() { _resetTabToStoredState(tab) {
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(` 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"
@ -166,6 +186,21 @@ class ZenPinnedTabManager {
`); `);
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();
}