Skip to content

Commit c0fc76f

Browse files
committed
fix(desktop): discard badges while another app window is focused
1 parent 1b4eb96 commit c0fc76f

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

‎apps/desktop/src/ipc/methods/notificationBadge.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ it.effect("validates IPC and clears on native focus, quit, and disposal", () =>
110110
native.listeners.get("browser-window-focus")!();
111111
expect(native.setBadgeCount).toHaveBeenLastCalledWith(0);
112112
expect(native.webContents.send).toHaveBeenCalledWith("desktop:set-notification-badge");
113+
native.getFocusedWindow.mockReturnValue({});
114+
yield* Effect.promise(() => Promise.resolve(handler(event, badge)));
115+
expect(native.setBadgeCount).toHaveBeenLastCalledWith(0);
116+
expect(native.webContents.send).toHaveBeenCalledTimes(2);
117+
yield* Effect.promise(() => Promise.resolve(handler(event, { count: 0, image: null })));
118+
expect(native.webContents.send).toHaveBeenCalledTimes(2);
119+
native.getFocusedWindow.mockReturnValue(null);
113120
yield* Effect.promise(() => Promise.resolve(handler(event, badge)));
114121
native.listeners.get("before-quit")!();
115122
expect(native.setBadgeCount).toHaveBeenLastCalledWith(0);

‎apps/desktop/src/ipc/methods/notificationBadge.ts‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,25 @@ export const installNotificationBadge = Effect.fn("desktop.ipc.installNotificati
4646
const ipc = yield* DesktopIpc.DesktopIpc;
4747
const app = yield* ElectronApp.ElectronApp;
4848
const platform = yield* HostProcessPlatform;
49-
const clear = () => applyNotificationBadge(platform, { count: 0, image: null });
49+
const clear = () => {
50+
applyNotificationBadge(platform, { count: 0, image: null });
51+
for (const window of Electron.BrowserWindow.getAllWindows()) {
52+
if (!window.isDestroyed()) window.webContents.send(SET_NOTIFICATION_BADGE_CHANNEL);
53+
}
54+
};
5055
yield* ipc.handle(
5156
DesktopIpc.makeIpcMethod({
5257
channel: SET_NOTIFICATION_BADGE_CHANNEL,
5358
payload: NotificationBadge,
5459
result: Schema.Void,
55-
handler: (badge) => Effect.sync(() => applyNotificationBadge(platform, badge)),
60+
handler: (badge) =>
61+
Effect.sync(() => {
62+
if (badge.count > 0 && Electron.BrowserWindow.getFocusedWindow()) clear();
63+
else applyNotificationBadge(platform, badge);
64+
}),
5665
}),
5766
);
58-
yield* app.on("browser-window-focus", () => {
59-
clear();
60-
for (const window of Electron.BrowserWindow.getAllWindows()) {
61-
if (!window.isDestroyed()) window.webContents.send(SET_NOTIFICATION_BADGE_CHANNEL);
62-
}
63-
});
67+
yield* app.on("browser-window-focus", clear);
6468
yield* app.on("before-quit", clear);
6569
yield* Effect.addFinalizer(() => Effect.sync(clear));
6670
},

0 commit comments

Comments
 (0)