Bläddra i källkod

onMainWindowFocus

Manav Rathi 1 år sedan
förälder
incheckning
3c7277a0b1

+ 3 - 2
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"),
     );
 };
 

+ 4 - 4
desktop/src/preload.ts

@@ -69,9 +69,9 @@ const encryptionKey = (): Promise<string | undefined> =>
 const saveEncryptionKey = (encryptionKey: string): Promise<void> =>
     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<boolean> =>
@@ -307,7 +307,7 @@ contextBridge.exposeInMainWorld("electron", {
     clearStores,
     encryptionKey,
     saveEncryptionKey,
-    registerForegroundEventListener,
+    onMainWindowFocus,
 
     // - App update
     updateAndRestart,

+ 2 - 4
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();
             }
         };

+ 11 - 1
web/packages/next/types/ipc.ts

@@ -85,7 +85,17 @@ export interface Electron {
      */
     saveEncryptionKey: (encryptionKey: string) => Promise<void>;
 
-    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.