diff --git a/desktop/src/main.ts b/desktop/src/main.ts index 246b6da1b..764c2be52 100644 --- a/desktop/src/main.ts +++ b/desktop/src/main.ts @@ -142,9 +142,10 @@ const deleteLegacyDiskCacheDirIfExists = async () => { }; const attachEventHandlers = (mainWindow: BrowserWindow) => { - // Let ipcRenderer know when mainWindow is in the foreground. + // Let ipcRenderer know when mainWindow is in the foreground so that it can + // in turn inform the renderer process. mainWindow.on("focus", () => - mainWindow.webContents.send("app-in-foreground"), + mainWindow.webContents.send("onMainWindowFocus"), ); }; diff --git a/desktop/src/preload.ts b/desktop/src/preload.ts index f3e3412e2..5e0ed1b45 100644 --- a/desktop/src/preload.ts +++ b/desktop/src/preload.ts @@ -69,9 +69,9 @@ const encryptionKey = (): Promise => const saveEncryptionKey = (encryptionKey: string): Promise => ipcRenderer.invoke("saveEncryptionKey", encryptionKey); -const registerForegroundEventListener = (onForeground: () => void) => { - ipcRenderer.removeAllListeners("app-in-foreground"); - ipcRenderer.on("app-in-foreground", onForeground); +const onMainWindowFocus = (cb?: () => void) => { + ipcRenderer.removeAllListeners("onMainWindowFocus"); + if (cb) ipcRenderer.on("onMainWindowFocus", cb); }; const fsExists = (path: string): Promise => @@ -307,7 +307,7 @@ contextBridge.exposeInMainWorld("electron", { clearStores, encryptionKey, saveEncryptionKey, - registerForegroundEventListener, + onMainWindowFocus, // - App update updateAndRestart, diff --git a/web/apps/photos/src/pages/gallery/index.tsx b/web/apps/photos/src/pages/gallery/index.tsx index b772771c4..bdbccedfb 100644 --- a/web/apps/photos/src/pages/gallery/index.tsx +++ b/web/apps/photos/src/pages/gallery/index.tsx @@ -363,16 +363,14 @@ export default function Gallery() { }, SYNC_INTERVAL_IN_MICROSECONDS); if (electron) { void clipService.setupOnFileUploadListener(); - electron.registerForegroundEventListener(() => { - syncWithRemote(false, true); - }); + electron.onMainWindowFocus(() => syncWithRemote(false, true)); } }; main(); return () => { clearInterval(syncInterval.current); if (electron) { - electron.registerForegroundEventListener(() => {}); + electron.onMainWindowFocus(undefined); clipService.removeOnFileUploadListener(); } }; diff --git a/web/packages/next/types/ipc.ts b/web/packages/next/types/ipc.ts index 5093e2554..f9656ad3e 100644 --- a/web/packages/next/types/ipc.ts +++ b/web/packages/next/types/ipc.ts @@ -85,7 +85,17 @@ export interface Electron { */ saveEncryptionKey: (encryptionKey: string) => Promise; - registerForegroundEventListener: (onForeground: () => void) => void; + /** + * Set or clear the callback {@link cb} to invoke whenever the app comes + * into the foreground. More precisely, the callback gets invoked when the + * main window gets focus. + * + * Note: Setting a callback clears any previous callbacks. + * + * @param cb The function to call when the main window gets focus. Pass + * `undefined` to clear the callback. + */ + onMainWindowFocus: (cb?: () => void) => void; /** * A subset of filesystem access APIs.