onMainWindowFocus

This commit is contained in:
Manav Rathi 2024-04-10 11:16:21 +05:30
parent 170aef9b30
commit 3c7277a0b1
No known key found for this signature in database
4 changed files with 20 additions and 11 deletions

View file

@ -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"),
);
};

View file

@ -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,

View file

@ -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();
}
};

View file

@ -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.