From 309efc2e269924bc6599bc3731a5b64281606d90 Mon Sep 17 00:00:00 2001 From: Avijit Das Date: Sat, 22 Feb 2025 17:19:15 +0530 Subject: [PATCH] Windows: Prevent Notifications from Showing on Lock Screen This update ensures that notifications are not displayed when the system is locked on Windows. Using Electron's powerMonitor, the app detects when the screen is locked and blocks notifications to prevent potential leaks of sensitive information. --- app/main/index.ts | 1 + app/renderer/js/electron-bridge.ts | 4 ++-- app/renderer/js/notification/index.ts | 18 +++++++++++++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/app/main/index.ts b/app/main/index.ts index 3a4da14c0..40a3b405d 100644 --- a/app/main/index.ts +++ b/app/main/index.ts @@ -185,6 +185,7 @@ function createMainWindow(): BrowserWindow { mainWindow.show(); } // Handle deep link when opened from protocol + const url = commandLine.find((argument) => argument.startsWith("zulip://")); if (url) { mainWindow.webContents.send("open-url", url); diff --git a/app/renderer/js/electron-bridge.ts b/app/renderer/js/electron-bridge.ts index 76aac16ec..88f1faf63 100644 --- a/app/renderer/js/electron-bridge.ts +++ b/app/renderer/js/electron-bridge.ts @@ -17,7 +17,7 @@ export type ElectronBridge = { title: string, options: NotificationOptions, dispatch: (type: string, eventInit: EventInit) => boolean, - ) => NotificationData; + ) => NotificationData | null; get_idle_on_system: () => boolean; get_last_active_on_system: () => number; get_send_notification_reply_message_supported: () => boolean; @@ -47,7 +47,7 @@ const electron_bridge: ElectronBridge = { title: string, options: NotificationOptions, dispatch: (type: string, eventInit: EventInit) => boolean, - ): NotificationData => newNotification(title, options, dispatch), + ): NotificationData | null => newNotification(title, options, dispatch), get_idle_on_system: (): boolean => idle, diff --git a/app/renderer/js/notification/index.ts b/app/renderer/js/notification/index.ts index 988ae4f50..3429bf018 100644 --- a/app/renderer/js/notification/index.ts +++ b/app/renderer/js/notification/index.ts @@ -1,5 +1,16 @@ +import {powerMonitor} from "electron/main"; + import {ipcRenderer} from "../typed-ipc-renderer.js"; +let isLocked = false; + +powerMonitor.on("lock-screen", () => { + isLocked = true; +}); +powerMonitor.on("unlock-screen", () => { + isLocked = false; +}); + export type NotificationData = { close: () => void; title: string; @@ -15,7 +26,12 @@ export function newNotification( title: string, options: NotificationOptions, dispatch: (type: string, eventInit: EventInit) => boolean, -): NotificationData { +): NotificationData | null { + if (isLocked) { + console.log("Notification blocked: Screen is locked."); + return null; // Prevent showing notification when the screen is locked + } + const notification = new Notification(title, {...options, silent: true}); for (const type of ["click", "close", "error", "show"]) { notification.addEventListener(type, (event) => {