Skip to content

Commit

Permalink
Windows: Prevent Notifications from Showing on Lock Screen
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
avijitdas126 committed Feb 22, 2025
1 parent a573141 commit 309efc2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions app/renderer/js/electron-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,

Expand Down
18 changes: 17 additions & 1 deletion app/renderer/js/notification/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) => {
Expand Down

0 comments on commit 309efc2

Please sign in to comment.