Skip to content

Commit 4ad15b6

Browse files
Improve Windows taskbar completion badges
1 parent ea3ba7d commit 4ad15b6

6 files changed

Lines changed: 233 additions & 67 deletions

File tree

Lines changed: 105 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { assert, beforeEach, describe, it } from "@effect/vitest";
22
import { vi } from "vite-plus/test";
33

4-
const { createFromBitmap } = vi.hoisted(() => ({
5-
createFromBitmap: vi.fn(() => ({ kind: "unread-completion-overlay" })),
4+
const { createFromBuffer, overlayImage } = vi.hoisted(() => ({
5+
createFromBuffer: vi.fn(),
6+
overlayImage: { isEmpty: vi.fn(() => false) },
67
}));
78

89
vi.mock("electron", () => ({
9-
nativeImage: { createFromBitmap },
10+
nativeImage: { createFromBuffer },
1011
}));
1112

1213
import { setWindowsTaskbarUnreadIndicator } from "./WindowsTaskbarBadge.ts";
@@ -20,35 +21,119 @@ function makeWindow(destroyed = false) {
2021

2122
describe("setWindowsTaskbarUnreadIndicator", () => {
2223
beforeEach(() => {
23-
createFromBitmap.mockClear();
24+
createFromBuffer.mockReset();
25+
createFromBuffer.mockReturnValue(overlayImage);
26+
overlayImage.isEmpty.mockReset();
27+
overlayImage.isEmpty.mockReturnValue(false);
2428
});
2529

2630
it("sets and clears the Windows taskbar overlay", () => {
2731
const window = makeWindow();
32+
const badgeDataUrl = "data:image/png;base64,one";
2833

29-
assert.isTrue(setWindowsTaskbarUnreadIndicator({ platform: "win32", window, visible: true }));
30-
assert.deepEqual(window.setOverlayIcon.mock.calls, [
31-
[{ kind: "unread-completion-overlay" }, "Completed thread awaiting review"],
34+
assert.isTrue(
35+
setWindowsTaskbarUnreadIndicator({ platform: "win32", window, count: 1, badgeDataUrl }),
36+
);
37+
assert.deepEqual(createFromBuffer.mock.calls, [
38+
[Buffer.from("one", "base64"), { width: 64, height: 64, scaleFactor: 4 }],
39+
]);
40+
assert.deepEqual(window.setOverlayIcon.mock.calls[0], [
41+
overlayImage,
42+
"1 completed thread awaiting review",
3243
]);
3344

34-
assert.isTrue(setWindowsTaskbarUnreadIndicator({ platform: "win32", window, visible: false }));
45+
assert.isTrue(
46+
setWindowsTaskbarUnreadIndicator({
47+
platform: "win32",
48+
window,
49+
count: 0,
50+
badgeDataUrl: null,
51+
}),
52+
);
3553
assert.deepEqual(window.setOverlayIcon.mock.calls[1], [null, ""]);
3654
});
3755

56+
it("caches decoded overlays", () => {
57+
const window = makeWindow();
58+
const badgeDataUrl = "data:image/png;base64,two";
59+
60+
assert.isTrue(
61+
setWindowsTaskbarUnreadIndicator({ platform: "win32", window, count: 2, badgeDataUrl }),
62+
);
63+
assert.isTrue(
64+
setWindowsTaskbarUnreadIndicator({ platform: "win32", window, count: 2, badgeDataUrl }),
65+
);
66+
67+
assert.equal(createFromBuffer.mock.calls.length, 1);
68+
});
69+
70+
it("rejects missing, malformed, and empty badge images", () => {
71+
const window = makeWindow();
72+
73+
assert.isFalse(
74+
setWindowsTaskbarUnreadIndicator({
75+
platform: "win32",
76+
window,
77+
count: 1,
78+
badgeDataUrl: null,
79+
}),
80+
);
81+
assert.isFalse(
82+
setWindowsTaskbarUnreadIndicator({
83+
platform: "win32",
84+
window,
85+
count: 1,
86+
badgeDataUrl: "data:image/svg+xml;base64,badge",
87+
}),
88+
);
89+
90+
overlayImage.isEmpty.mockReturnValueOnce(true);
91+
assert.isFalse(
92+
setWindowsTaskbarUnreadIndicator({
93+
platform: "win32",
94+
window,
95+
count: 1,
96+
badgeDataUrl: "data:image/png;base64,empty",
97+
}),
98+
);
99+
assert.lengthOf(window.setOverlayIcon.mock.calls, 0);
100+
});
101+
38102
it("does nothing outside Windows or without a live window", () => {
39103
const window = makeWindow();
40104
const destroyedWindow = makeWindow(true);
105+
const badgeDataUrl = "data:image/png;base64,platform";
41106

42-
assert.isFalse(setWindowsTaskbarUnreadIndicator({ platform: "darwin", window, visible: true }));
43-
assert.isFalse(setWindowsTaskbarUnreadIndicator({ platform: "linux", window, visible: true }));
44107
assert.isFalse(
45-
setWindowsTaskbarUnreadIndicator({ platform: "win32", window: null, visible: true }),
108+
setWindowsTaskbarUnreadIndicator({
109+
platform: "darwin",
110+
window,
111+
count: 1,
112+
badgeDataUrl,
113+
}),
114+
);
115+
assert.isFalse(
116+
setWindowsTaskbarUnreadIndicator({
117+
platform: "linux",
118+
window,
119+
count: 1,
120+
badgeDataUrl,
121+
}),
122+
);
123+
assert.isFalse(
124+
setWindowsTaskbarUnreadIndicator({
125+
platform: "win32",
126+
window: null,
127+
count: 1,
128+
badgeDataUrl,
129+
}),
46130
);
47131
assert.isFalse(
48132
setWindowsTaskbarUnreadIndicator({
49133
platform: "win32",
50134
window: destroyedWindow,
51-
visible: true,
135+
count: 1,
136+
badgeDataUrl,
52137
}),
53138
);
54139
assert.lengthOf(window.setOverlayIcon.mock.calls, 0);
@@ -61,6 +146,13 @@ describe("setWindowsTaskbarUnreadIndicator", () => {
61146
throw new Error("overlay failed");
62147
});
63148

64-
assert.isFalse(setWindowsTaskbarUnreadIndicator({ platform: "win32", window, visible: true }));
149+
assert.isFalse(
150+
setWindowsTaskbarUnreadIndicator({
151+
platform: "win32",
152+
window,
153+
count: 1,
154+
badgeDataUrl: "data:image/png;base64,failure",
155+
}),
156+
);
65157
});
66158
});

‎apps/desktop/src/electron/WindowsTaskbarBadge.ts‎

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,62 @@
11
import * as Electron from "electron";
22

33
const OVERLAY_SIZE = 16;
4-
const OVERLAY_DESCRIPTION = "Completed thread awaiting review";
4+
const OVERLAY_SCALE_FACTOR = 4;
5+
const PNG_DATA_URL_PREFIX = "data:image/png;base64,";
56

6-
let unreadCompletionOverlay: Electron.NativeImage | undefined;
7+
const overlayByDataUrl = new Map<string, Electron.NativeImage>();
78

8-
function getUnreadCompletionOverlay(): Electron.NativeImage {
9-
if (unreadCompletionOverlay !== undefined) {
10-
return unreadCompletionOverlay;
9+
function getUnreadCompletionOverlay(dataUrl: string): Electron.NativeImage | null {
10+
const cached = overlayByDataUrl.get(dataUrl);
11+
if (cached !== undefined) {
12+
return cached;
1113
}
1214

13-
// The Windows bitmap representation is BGRA. Opaque pixels avoid
14-
// platform-specific alpha premultiplication while keeping the icon crisp.
15-
const bitmap = Buffer.alloc(OVERLAY_SIZE * OVERLAY_SIZE * 4);
16-
const center = (OVERLAY_SIZE - 1) / 2;
17-
for (let y = 0; y < OVERLAY_SIZE; y += 1) {
18-
for (let x = 0; x < OVERLAY_SIZE; x += 1) {
19-
const distance = Math.hypot(x - center, y - center);
20-
const offset = (y * OVERLAY_SIZE + x) * 4;
21-
if (distance <= 4.5) {
22-
bitmap.set([129, 185, 16, 255], offset);
23-
} else if (distance <= 6.25) {
24-
bitmap.set([255, 255, 255, 255], offset);
25-
}
26-
}
15+
if (!dataUrl.startsWith(PNG_DATA_URL_PREFIX)) {
16+
return null;
17+
}
18+
19+
const overlay = Electron.nativeImage.createFromBuffer(
20+
Buffer.from(dataUrl.slice(PNG_DATA_URL_PREFIX.length), "base64"),
21+
{
22+
width: OVERLAY_SIZE * OVERLAY_SCALE_FACTOR,
23+
height: OVERLAY_SIZE * OVERLAY_SCALE_FACTOR,
24+
scaleFactor: OVERLAY_SCALE_FACTOR,
25+
},
26+
);
27+
if (overlay.isEmpty()) {
28+
return null;
2729
}
2830

29-
unreadCompletionOverlay = Electron.nativeImage.createFromBitmap(bitmap, {
30-
width: OVERLAY_SIZE,
31-
height: OVERLAY_SIZE,
32-
});
33-
return unreadCompletionOverlay;
31+
overlayByDataUrl.set(dataUrl, overlay);
32+
return overlay;
3433
}
3534

3635
export function setWindowsTaskbarUnreadIndicator(input: {
3736
readonly platform: NodeJS.Platform;
3837
readonly window: Pick<Electron.BrowserWindow, "isDestroyed" | "setOverlayIcon"> | null;
39-
readonly visible: boolean;
38+
readonly count: number;
39+
readonly badgeDataUrl: string | null;
4040
}): boolean {
4141
if (input.platform !== "win32" || input.window === null || input.window.isDestroyed()) {
4242
return false;
4343
}
4444

4545
try {
46+
if (input.count === 0) {
47+
input.window.setOverlayIcon(null, "");
48+
return true;
49+
}
50+
51+
const overlay =
52+
input.badgeDataUrl === null ? null : getUnreadCompletionOverlay(input.badgeDataUrl);
53+
if (overlay === null) {
54+
return false;
55+
}
56+
4657
input.window.setOverlayIcon(
47-
input.visible ? getUnreadCompletionOverlay() : null,
48-
input.visible ? OVERLAY_DESCRIPTION : "",
58+
overlay,
59+
`${input.count} completed ${input.count === 1 ? "thread" : "threads"} awaiting review`,
4960
);
5061
return true;
5162
} catch {

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,10 @@ export const setTheme = DesktopIpc.makeIpcMethod({
271271

272272
export const setTaskbarUnreadIndicator = DesktopIpc.makeIpcMethod({
273273
channel: IpcChannels.SET_TASKBAR_UNREAD_INDICATOR_CHANNEL,
274-
payload: Schema.Struct({ visible: Schema.Boolean }),
274+
payload: Schema.Struct({
275+
count: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
276+
badgeDataUrl: Schema.NullOr(Schema.String.check(Schema.isMaxLength(65_536))),
277+
}),
275278
result: Schema.Boolean,
276279
handler: Effect.fn("desktop.ipc.window.setTaskbarUnreadIndicator")(function* (input) {
277280
const environment = yield* DesktopEnvironment.DesktopEnvironment;
@@ -280,7 +283,8 @@ export const setTaskbarUnreadIndicator = DesktopIpc.makeIpcMethod({
280283
return setWindowsTaskbarUnreadIndicator({
281284
platform: environment.platform,
282285
window: Option.getOrNull(window),
283-
visible: input.visible,
286+
count: input.count,
287+
badgeDataUrl: input.badgeDataUrl,
284288
});
285289
}),
286290
});

‎apps/web/src/components/desktop/DesktopTaskbarUnreadCoordinator.test.ts‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { describe, expect, it } from "vite-plus/test";
22
import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/models";
33
import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
44

5-
import { hasUnseenTaskbarCompletion } from "./DesktopTaskbarUnreadCoordinator";
5+
import {
6+
countUnseenTaskbarCompletions,
7+
getTaskbarBadgeLabel,
8+
} from "./DesktopTaskbarUnreadCoordinator";
69

710
const environmentId = "environment-1" as EnvironmentId;
811
const threadId = "thread-1" as ThreadId;
@@ -30,39 +33,45 @@ function completedThread(
3033
}
3134

3235
describe("hasUnseenTaskbarCompletion", () => {
36+
it("shows one digit and caps larger counts at 9+", () => {
37+
expect(getTaskbarBadgeLabel(1)).toBe("1");
38+
expect(getTaskbarBadgeLabel(9)).toBe("9");
39+
expect(getTaskbarBadgeLabel(10)).toBe("9+");
40+
});
41+
3342
it("clears after the completed thread is visited", () => {
3443
const threads = [completedThread()];
3544

3645
expect(
37-
hasUnseenTaskbarCompletion(threads, {
46+
countUnseenTaskbarCompletions(threads, {
3847
[threadKey]: "2026-09-09T12:00:05.000Z",
3948
}),
40-
).toBe(true);
49+
).toBe(1);
4150
expect(
42-
hasUnseenTaskbarCompletion(threads, {
51+
countUnseenTaskbarCompletions(threads, {
4352
[threadKey]: "2026-09-09T12:00:10.000Z",
4453
}),
45-
).toBe(false);
54+
).toBe(0);
4655
});
4756

4857
it("stays visible until every unseen completion is visited", () => {
4958
const secondThreadId = "thread-2" as ThreadId;
5059
const threads = [completedThread(), completedThread({ id: secondThreadId })];
5160

5261
expect(
53-
hasUnseenTaskbarCompletion(threads, {
62+
countUnseenTaskbarCompletions(threads, {
5463
[threadKey]: "2026-09-09T12:00:10.000Z",
5564
[`${environmentId}:${secondThreadId}`]: "2026-09-09T12:00:05.000Z",
5665
}),
57-
).toBe(true);
66+
).toBe(1);
5867
});
5968

6069
it("ignores archived and never-visited historical threads", () => {
6170
expect(
62-
hasUnseenTaskbarCompletion([completedThread({ archivedAt: "2026-09-09T12:01:00.000Z" })], {
71+
countUnseenTaskbarCompletions([completedThread({ archivedAt: "2026-09-09T12:01:00.000Z" })], {
6372
[threadKey]: "2026-09-09T12:00:05.000Z",
6473
}),
65-
).toBe(false);
66-
expect(hasUnseenTaskbarCompletion([completedThread()], {})).toBe(false);
74+
).toBe(0);
75+
expect(countUnseenTaskbarCompletions([completedThread()], {})).toBe(0);
6776
});
6877
});

0 commit comments

Comments
 (0)