π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
feat(AppLock):add-main-window-blur-event-handling
Aswin Asok2c84ce21d93a
diff --git a/desktop/src/main.ts b/desktop/src/main.tsindex b413ceef43..0312019b78 100644--- a/desktop/src/main.ts+++ b/desktop/src/main.ts@@ -438,6 +438,7 @@ const createMainWindow = () => {// Let ipcRenderer know when mainWindow is in the foreground so that it can// in turn inform the renderer process.window.on("focus", () => window.webContents.send("mainWindowFocus"));+ window.on("blur", () => window.webContents.send("mainWindowBlur"));return window;};diff --git a/desktop/src/preload.ts b/desktop/src/preload.tsindex e50257a7da..f714900ad8 100644--- a/desktop/src/preload.ts+++ b/desktop/src/preload.ts@@ -155,6 +155,11 @@ const onMainWindowFocus = (cb: (() => void) | undefined) => {if (cb) ipcRenderer.on("mainWindowFocus", cb);};+const onMainWindowBlur = (cb: (() => void) | undefined) => {+ ipcRenderer.removeAllListeners("mainWindowBlur");+ if (cb) ipcRenderer.on("mainWindowBlur", cb);+};+const onOpenEnteURL = (cb: ((url: string) => void) | undefined) => {ipcRenderer.removeAllListeners("openEnteURL");if (cb) ipcRenderer.on("openEnteURL", (_, url: string) => cb(url));@@ -389,6 +394,7 @@ contextBridge.exposeInMainWorld("electron", {getNativeDeviceLockCapability,promptDeviceLock,onMainWindowFocus,+ onMainWindowBlur,onOpenEnteURL,// - App updatediff --git a/web/packages/base/electron.ts b/web/packages/base/electron.tsindex 98d34e0399..50f52dfb41 100644--- a/web/packages/base/electron.ts+++ b/web/packages/base/electron.ts@@ -25,9 +25,12 @@ export const ensureElectron = (): Electron => {};type MainWindowFocusListener = () => void;+type MainWindowBlurListener = () => void;const mainWindowFocusListeners = new Set<MainWindowFocusListener>();let hasAttachedMainWindowFocusBridge = false;+const mainWindowBlurListeners = new Set<MainWindowBlurListener>();+let hasAttachedMainWindowBlurBridge = false;const emitMainWindowFocus = () => {for (const listener of mainWindowFocusListeners) {@@ -74,3 +77,47 @@ export const subscribeMainWindowFocus = (detachMainWindowFocusBridgeIfNeeded();};};++const emitMainWindowBlur = () => {+ for (const listener of mainWindowBlurListeners) {+ listener();+ }+};++const attachMainWindowBlurBridgeIfNeeded = () => {+ if (hasAttachedMainWindowBlurBridge) return;+ const electron = globalThis.electron;+ if (!electron) return;+ if (typeof electron.onMainWindowBlur != "function") return;+ electron.onMainWindowBlur(emitMainWindowBlur);+ hasAttachedMainWindowBlurBridge = true;+};++const detachMainWindowBlurBridgeIfNeeded = () => {+ if (!hasAttachedMainWindowBlurBridge || mainWindowBlurListeners.size > 0) {+ return;+ }++ const electron = globalThis.electron;+ if (electron && typeof electron.onMainWindowBlur == "function") {+ electron.onMainWindowBlur(undefined);+ }+ hasAttachedMainWindowBlurBridge = false;+};++/**+ * Subscribe to main window blur events on Electron.+ *+ * This helper multiplexes listeners over Electron's single-callback API,+ * allowing multiple consumers to listen concurrently.+ */+export const subscribeMainWindowBlur = (+ listener: MainWindowBlurListener,+): (() => void) => {+ mainWindowBlurListeners.add(listener);+ attachMainWindowBlurBridgeIfNeeded();+ return () => {+ mainWindowBlurListeners.delete(listener);+ detachMainWindowBlurBridgeIfNeeded();+ };+};diff --git a/web/packages/base/types/ipc.ts b/web/packages/base/types/ipc.tsindex 80a16d9824..66d4c17ecf 100644--- a/web/packages/base/types/ipc.ts+++ b/web/packages/base/types/ipc.ts@@ -141,6 +141,18 @@ export interface Electron {*/onMainWindowFocus: (cb: (() => void) | undefined) => void;+ /**+ * Set or clear the callback {@link cb} to invoke whenever the app goes to+ * the background. More precisely, the callback gets invoked when the main+ * window blurs and the app is no longer focused.+ *+ * Setting a callback clears any previous callbacks.+ *+ * @param cb The function to call when the app is backgrounded. Pass+ * `undefined` to clear the callback.+ */+ onMainWindowBlur: (cb: (() => void) | undefined) => void;+/*** Set or clear the callback {@link cb} to invoke whenever the app gets* asked to open a deeplink that begins with "ente://". This allows thediff --git a/web/packages/new/photos/components/utils/use-app-lock.ts b/web/packages/new/photos/components/utils/use-app-lock.tsindex 9604580203..692aa04dfe 100644--- a/web/packages/new/photos/components/utils/use-app-lock.ts+++ b/web/packages/new/photos/components/utils/use-app-lock.ts@@ -1,4 +1,7 @@-import { subscribeMainWindowFocus } from "ente-base/electron";+import {+ subscribeMainWindowBlur,+ subscribeMainWindowFocus,+} from "ente-base/electron";import { updateSessionFromElectronSafeStorageIfNeeded } from "ente-base/session";import { useEffect, useRef, useState } from "react";import {@@ -87,14 +90,11 @@ export const useAutoLockWhenBackgrounded = (return true;};- // Called when the app loses foreground focus (blur/visibility change).+ // Called when the app is backgrounded.// Starts auto-lock unless the app is already locked.const startAutoLockTimer = () => {if (isLocked) return;- // Avoid extending the countdown when blur + visibility events both- // fire for a single background transition.-const existingDeadline = autoLockDueAtTimestampRef.current;if (existingDeadline !== null && Date.now() < existingDeadline) {return;@@ -132,25 +132,21 @@ export const useAutoLockWhenBackgrounded = (}};- const handleWindowBlur = () => {- if (!document.hidden) {- startAutoLockTimer();- }- };-const handleWindowFocus = () => {handleAppForegrounded();};let unsubscribeMainWindowFocus: (() => void) | undefined;+ let unsubscribeMainWindowBlur: (() => void) | undefined;if (globalThis.electron) {unsubscribeMainWindowFocus = subscribeMainWindowFocus(handleAppForegrounded,);+ unsubscribeMainWindowBlur =+ subscribeMainWindowBlur(startAutoLockTimer);}document.addEventListener("visibilitychange", handleVisibilityChange);- window.addEventListener("blur", handleWindowBlur);window.addEventListener("focus", handleWindowFocus);// cleanup@@ -159,9 +155,9 @@ export const useAutoLockWhenBackgrounded = ("visibilitychange",handleVisibilityChange,);- window.removeEventListener("blur", handleWindowBlur);window.removeEventListener("focus", handleWindowFocus);unsubscribeMainWindowFocus?.();+ unsubscribeMainWindowBlur?.();clearAutoLockTimer();};}, [enabled, isLocked, autoLockTimeMs]);