From 86f8c97be849ba6a63fc7349edf941e237659138 Mon Sep 17 00:00:00 2001 From: mbnuqw Date: Sat, 17 Feb 2024 21:58:50 +0500 Subject: [PATCH] fix: preview: don't spam "Recently Closed Windows" (#301) --- src/services/tabs.preview.ts | 33 ++++++++++++++++++++++++++++----- src/types/web-ext.d.ts | 6 ++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/services/tabs.preview.ts b/src/services/tabs.preview.ts index 7d6115ba..42c848ed 100644 --- a/src/services/tabs.preview.ts +++ b/src/services/tabs.preview.ts @@ -1,4 +1,5 @@ -import { NOID } from 'src/defaults' +import { ADDON_HOST, NOID } from 'src/defaults' +import { InstanceType } from 'src/types' import { Sidebar } from './sidebar' import { Tabs } from './tabs.fg' import { Styles } from './styles' @@ -30,6 +31,7 @@ let currentWinX = 0 let currentWinOffsetY = 0 let currentWinOffsetX = 0 let deadOnArrival = false +let listening = false export async function showPreviewPopup(tabId: ID, y?: number) { const tab = Tabs.byId[tabId] @@ -45,6 +47,8 @@ export async function showPreviewPopup(tabId: ID, y?: number) { if (currentWinWidth === 0 || currentWinHeight === 0) return + if (!listening) setupPopupDisconnectionListener() + currentWinOffsetY = 0 currentWinOffsetX = 0 @@ -157,8 +161,7 @@ export function updatePreviewPopup(tabId: ID) { if (IPC.state.previewConnection) { IPC.sendToPreview('updatePreview', tabId, tab.title, tab.url, !!tab.discarded) } else { - if (state.winId !== NOID) browser.windows.remove(state.winId) - state.winId = NOID + closePreviewPopup() } } @@ -168,8 +171,10 @@ export function closePreviewPopup() { return } - if (state.winId !== NOID) browser.windows.remove(state.winId) - state.winId = NOID + if (state.winId !== NOID) { + browser.windows.remove(state.winId) + state.winId = NOID + } } function getPopupX() { @@ -187,6 +192,24 @@ function getPopupX() { } } +export function setupPopupDisconnectionListener() { + const checkPart = ADDON_HOST.slice(50, 52) + '/p' + + IPC.onDisconnected(InstanceType.preview, async () => { + if (!Settings.state.previewTabs) return + + const recentlyClosedItems = await browser.sessions.getRecentlyClosed({ maxResults: 3 }) + for (const rc of recentlyClosedItems) { + if (!rc.window?.sessionId) continue + if (rc.window.tabs?.length === 1) { + const url = rc.window.tabs[0].url + if (url.startsWith(checkPart, 50)) browser.sessions.forgetClosedWindow(rc.window.sessionId) + } + } + }) + listening = true +} + export async function showPreviewInline(tabId: ID) { if (state.tabId === tabId) return diff --git a/src/types/web-ext.d.ts b/src/types/web-ext.d.ts index baef9bdc..3efcb50d 100644 --- a/src/types/web-ext.d.ts +++ b/src/types/web-ext.d.ts @@ -451,6 +451,12 @@ declare namespace browser { function getWindowValue(id: ID, key: string): Promise function setTabValue(tabId: ID, key: string, value: T): Promise function getTabValue(tabId: ID, key: string): Promise + function forgetClosedWindow(sessionId: string): Promise + function forgetClosedTab(windowId: number, sessionId: string): Promise + + type SessionChangeListener = () => void + + const onChanged: EventTarget } /**