-
Notifications
You must be signed in to change notification settings - Fork 5.8k
feat: badge background thread notifications on desktop and web #11569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
maria-rcks
merged 9 commits into
pingdotgg:main
from
Bil0000:t3code/desktop-notification-badges
Sep 13, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1b4eb96
feat(desktop): badge background thread notifications
Bil0000 c0fc76f
fix(desktop): discard badges while another app window is focused
Bil0000 82a98c9
fix(web): clear badge alerts for removed environments
Bil0000 bfb254c
fix(web): preserve in-app alerts when merging badge support
Bil0000 c1ac3c1
feat(web): show pending notification count in favicon
maria-rcks 5055274
fix(web): overlay notification count on original favicon
maria-rcks 72707e4
fix(web): place favicon badge over outer icon corner
maria-rcks de63127
fix(web): preserve full favicon size under notification count
maria-rcks f5f48d2
fix(web): use counter-only notification favicon
maria-rcks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| import * as Effect from "effect/Effect"; | ||
| import { beforeEach, expect, vi } from "vite-plus/test"; | ||
| import { it } from "@effect/vitest"; | ||
| import { HostProcessPlatform } from "@t3tools/shared/hostProcess"; | ||
|
|
||
| const native = vi.hoisted(() => ({ | ||
| setBadgeCount: vi.fn(), | ||
| setOverlayIcon: vi.fn(), | ||
| isDestroyed: vi.fn(() => false), | ||
| getFocusedWindow: vi.fn(() => null as object | null), | ||
| image: { isEmpty: vi.fn(() => false) }, | ||
| createFromDataURL: vi.fn(), | ||
| webContents: { send: vi.fn() }, | ||
| listeners: new Map<string, () => void>(), | ||
| })); | ||
| vi.mock("electron", () => ({ | ||
| app: { | ||
| setBadgeCount: native.setBadgeCount, | ||
| on: (event: string, listener: () => void) => native.listeners.set(event, listener), | ||
| removeListener: (event: string) => native.listeners.delete(event), | ||
| }, | ||
| BrowserWindow: { | ||
| getFocusedWindow: native.getFocusedWindow, | ||
| getAllWindows: () => [native], | ||
| }, | ||
| nativeImage: { createFromDataURL: native.createFromDataURL }, | ||
| })); | ||
|
|
||
| import * as ElectronApp from "../../electron/ElectronApp.ts"; | ||
| import * as DesktopIpc from "../DesktopIpc.ts"; | ||
| import { applyNotificationBadge, installNotificationBadge } from "./notificationBadge.ts"; | ||
|
|
||
| const badge = { count: 2, image: "data:image/png;base64,aGVsbG8=" }; | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| native.getFocusedWindow.mockReturnValue(null); | ||
| native.isDestroyed.mockReturnValue(false); | ||
| native.image.isEmpty.mockReturnValue(false); | ||
| native.createFromDataURL.mockReturnValue(native.image); | ||
| native.setBadgeCount.mockImplementation(() => true); | ||
| native.listeners.clear(); | ||
| }); | ||
|
|
||
| it.each(["darwin", "linux"] as const)("sets and clears the native %s count", (platform) => { | ||
| applyNotificationBadge(platform, badge); | ||
| applyNotificationBadge(platform, { count: 0, image: null }); | ||
| expect(native.setBadgeCount.mock.calls).toEqual([[2], [0]]); | ||
| expect(native.createFromDataURL).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("sets and clears the Windows taskbar overlay", () => { | ||
| applyNotificationBadge("win32", badge); | ||
| expect(native.setOverlayIcon).toHaveBeenLastCalledWith( | ||
| native.image, | ||
| "2 threads with new notifications", | ||
| ); | ||
| applyNotificationBadge("win32", { count: 0, image: null }); | ||
| expect(native.setOverlayIcon).toHaveBeenLastCalledWith(null, ""); | ||
| }); | ||
|
|
||
| it.each(["win32", "darwin", "linux"] as const)( | ||
| "rejects a late positive count while %s is focused", | ||
| (platform) => { | ||
| native.getFocusedWindow.mockReturnValue({}); | ||
| applyNotificationBadge(platform, badge); | ||
| if (platform === "win32") expect(native.setOverlayIcon).toHaveBeenCalledWith(null, ""); | ||
| else expect(native.setBadgeCount).toHaveBeenCalledWith(0); | ||
| expect(native.createFromDataURL).not.toHaveBeenCalled(); | ||
| }, | ||
| ); | ||
|
|
||
| it("ignores destroyed windows and clears invalid images", () => { | ||
| native.isDestroyed.mockReturnValue(true); | ||
| applyNotificationBadge("win32", badge); | ||
| expect(native.setOverlayIcon).not.toHaveBeenCalled(); | ||
| native.isDestroyed.mockReturnValue(false); | ||
| native.image.isEmpty.mockReturnValue(true); | ||
| applyNotificationBadge("win32", badge); | ||
| expect(native.setOverlayIcon.mock.calls[0]?.[0]).toBeNull(); | ||
| }); | ||
|
|
||
| it("keeps notifications working when the native badge API fails", () => { | ||
| native.setBadgeCount.mockImplementation(() => { | ||
| throw new Error("Unavailable"); | ||
| }); | ||
| expect(() => applyNotificationBadge("linux", badge)).not.toThrow(); | ||
| }); | ||
|
|
||
| it.effect("validates IPC and clears on native focus, quit, and disposal", () => | ||
| Effect.gen(function* () { | ||
| const handlers = new Map<string, DesktopIpc.DesktopIpcHandleListener>(); | ||
| yield* Effect.scoped( | ||
| Effect.gen(function* () { | ||
| yield* installNotificationBadge(); | ||
| const handler = handlers.get("desktop:set-notification-badge")!; | ||
| const event = { sender: { id: 1 } }; | ||
| for (const invalid of [ | ||
| { ...badge, count: -1 }, | ||
| { ...badge, count: 0.5 }, | ||
| { ...badge, count: Infinity }, | ||
| { ...badge, image: "https://example.com/icon.png" }, | ||
| { ...badge, image: `data:image/png;base64,${"a".repeat(16_384)}` }, | ||
| ]) { | ||
| yield* Effect.promise(() => expect(handler(event, invalid)).rejects.toBeDefined()); | ||
| } | ||
| expect(native.setBadgeCount).not.toHaveBeenCalled(); | ||
| yield* Effect.promise(() => Promise.resolve(handler(event, badge))); | ||
| expect(native.setBadgeCount).toHaveBeenLastCalledWith(2); | ||
| native.listeners.get("browser-window-focus")!(); | ||
| expect(native.setBadgeCount).toHaveBeenLastCalledWith(0); | ||
| expect(native.webContents.send).toHaveBeenCalledWith("desktop:set-notification-badge"); | ||
| native.getFocusedWindow.mockReturnValue({}); | ||
| yield* Effect.promise(() => Promise.resolve(handler(event, badge))); | ||
| expect(native.setBadgeCount).toHaveBeenLastCalledWith(0); | ||
| expect(native.webContents.send).toHaveBeenCalledTimes(2); | ||
| yield* Effect.promise(() => Promise.resolve(handler(event, { count: 0, image: null }))); | ||
| expect(native.webContents.send).toHaveBeenCalledTimes(2); | ||
| native.getFocusedWindow.mockReturnValue(null); | ||
| yield* Effect.promise(() => Promise.resolve(handler(event, badge))); | ||
| native.listeners.get("before-quit")!(); | ||
| expect(native.setBadgeCount).toHaveBeenLastCalledWith(0); | ||
| }), | ||
| ).pipe( | ||
| Effect.provideService(HostProcessPlatform, "linux"), | ||
| Effect.provide([ | ||
| ElectronApp.layer, | ||
| DesktopIpc.layer({ | ||
| handle: (channel, handler) => { | ||
| handlers.set(channel, handler); | ||
| }, | ||
| removeHandler: (channel) => { | ||
| handlers.delete(channel); | ||
| }, | ||
| on: vi.fn(), | ||
| removeAllListeners: vi.fn(), | ||
| }), | ||
| ]), | ||
| ); | ||
| expect(native.setBadgeCount).toHaveBeenLastCalledWith(0); | ||
| expect(native.listeners.size).toBe(0); | ||
| expect(handlers.size).toBe(0); | ||
| }), | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import * as Electron from "electron"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Schema from "effect/Schema"; | ||
| import { HostProcessPlatform } from "@t3tools/shared/hostProcess"; | ||
|
|
||
| import * as ElectronApp from "../../electron/ElectronApp.ts"; | ||
| import * as DesktopIpc from "../DesktopIpc.ts"; | ||
| import { SET_NOTIFICATION_BADGE_CHANNEL } from "../channels.ts"; | ||
|
|
||
| const NotificationBadge = Schema.Struct({ | ||
| count: Schema.Int.check(Schema.isBetween({ minimum: 0, maximum: 2_147_483_647 })), | ||
| image: Schema.NullOr( | ||
| Schema.String.check( | ||
| Schema.isMaxLength(16_384), | ||
| Schema.isPattern(/^data:image\/png;base64,[a-z0-9+/]+={0,2}$/i), | ||
| ), | ||
| ), | ||
| }); | ||
|
|
||
| export function applyNotificationBadge( | ||
| platform: NodeJS.Platform, | ||
| { count, image }: typeof NotificationBadge.Type, | ||
| ): void { | ||
| try { | ||
| if (Electron.BrowserWindow.getFocusedWindow()) count = 0; | ||
| if (platform === "win32") { | ||
| const overlay = count > 0 && image ? Electron.nativeImage.createFromDataURL(image) : null; | ||
| for (const window of Electron.BrowserWindow.getAllWindows()) { | ||
| if (!window.isDestroyed()) { | ||
| window.setOverlayIcon( | ||
| overlay?.isEmpty() ? null : overlay, | ||
| count > 0 ? `${count} threads with new notifications` : "", | ||
| ); | ||
| } | ||
| } | ||
| } else if (platform === "darwin" || platform === "linux") { | ||
| Electron.app.setBadgeCount(count); | ||
| } | ||
| } catch (error) { | ||
| Effect.runSync(Effect.logWarning("Could not update notification badge", error)); | ||
| } | ||
| } | ||
|
|
||
| export const installNotificationBadge = Effect.fn("desktop.ipc.installNotificationBadge")( | ||
| function* () { | ||
| const ipc = yield* DesktopIpc.DesktopIpc; | ||
| const app = yield* ElectronApp.ElectronApp; | ||
| const platform = yield* HostProcessPlatform; | ||
| const clear = () => { | ||
| applyNotificationBadge(platform, { count: 0, image: null }); | ||
| for (const window of Electron.BrowserWindow.getAllWindows()) { | ||
| if (!window.isDestroyed()) window.webContents.send(SET_NOTIFICATION_BADGE_CHANNEL); | ||
| } | ||
| }; | ||
| yield* ipc.handle( | ||
| DesktopIpc.makeIpcMethod({ | ||
| channel: SET_NOTIFICATION_BADGE_CHANNEL, | ||
| payload: NotificationBadge, | ||
| result: Schema.Void, | ||
| handler: (badge) => | ||
| Effect.sync(() => { | ||
| if (badge.count > 0 && Electron.BrowserWindow.getFocusedWindow()) clear(); | ||
| else applyNotificationBadge(platform, badge); | ||
| }), | ||
| }), | ||
| ); | ||
| yield* app.on("browser-window-focus", clear); | ||
| yield* app.on("before-quit", clear); | ||
| yield* Effect.addFinalizer(() => Effect.sync(clear)); | ||
| }, | ||
| ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.