taskbarOverlayBadge.test.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. applyTaskbarOverlayToWindows,
  4. blendBadgeIntoBitmap,
  5. createWindowsTaskbarOverlayApplier,
  6. renderTaskbarBadgePixels,
  7. TASKBAR_BADGE_IMAGE_SIZE,
  8. } from "../src/main/unreadBadge.js";
  9. import type { TaskbarOverlayWindow } from "../src/main/unreadBadge.js";
  10. const CIRCLE_RED_BGRA = [53, 57, 229, 255];
  11. const WHITE_BGRA = [255, 255, 255, 255];
  12. function pixelAt(pixels: Uint8Array, x: number, y: number): number[] {
  13. const offset = (y * TASKBAR_BADGE_IMAGE_SIZE + x) * 4;
  14. return [pixels[offset]!, pixels[offset + 1]!, pixels[offset + 2]!, pixels[offset + 3]!];
  15. }
  16. function isWhitePixel(bgra: number[]): boolean {
  17. return bgra.every((value) => value === 255);
  18. }
  19. function countWhitePixels(pixels: Uint8Array): number {
  20. let count = 0;
  21. for (let offset = 0; offset < pixels.length; offset += 4) {
  22. if (
  23. pixels[offset] === 255 &&
  24. pixels[offset + 1] === 255 &&
  25. pixels[offset + 2] === 255 &&
  26. pixels[offset + 3] === 255
  27. ) {
  28. count += 1;
  29. }
  30. }
  31. return count;
  32. }
  33. function createFakeWindow(options: { destroyed?: boolean } = {}): TaskbarOverlayWindow & {
  34. overlays: Array<{ image: unknown; description: string }>;
  35. } {
  36. const overlays: Array<{ image: unknown; description: string }> = [];
  37. return {
  38. isDestroyed: () => Boolean(options.destroyed),
  39. setOverlayIcon: (image, description) => {
  40. overlays.push({ image, description });
  41. },
  42. overlays,
  43. };
  44. }
  45. describe("windows taskbar overlay badge", () => {
  46. describe("renderTaskbarBadgePixels", () => {
  47. it("输出 32×32 BGRA 缓冲区", () => {
  48. const pixels = renderTaskbarBadgePixels("1");
  49. expect(pixels).toBeInstanceOf(Uint8Array);
  50. expect(pixels.length).toBe(TASKBAR_BADGE_IMAGE_SIZE * TASKBAR_BADGE_IMAGE_SIZE * 4);
  51. });
  52. it("圆内空白处是预乘 BGRA 的红色,数字笔画处是白色", () => {
  53. const pixels = renderTaskbarBadgePixels("1");
  54. // 圆心 (15.5, 15.5) 半径 15:(16,16) 落在数字 1 的笔画上(白),(4,16) 只落在圆上(红)。
  55. expect(pixelAt(pixels, 16, 16)).toEqual(WHITE_BGRA);
  56. expect(pixelAt(pixels, 4, 16)).toEqual(CIRCLE_RED_BGRA);
  57. });
  58. it("圆外完全透明(预乘后全 0),保证任务栏图标不被遮挡", () => {
  59. const pixels = renderTaskbarBadgePixels("1");
  60. expect(pixelAt(pixels, 0, 0)).toEqual([0, 0, 0, 0]);
  61. });
  62. it("画布四周留透明边距,气泡本体只占约 3/4,避免任务栏观感过大", () => {
  63. const pixels = renderTaskbarBadgePixels("1");
  64. // 半径 12:画布边缘 2px 内是透明边距,向内一步就是红色圆面。
  65. expect(pixelAt(pixels, 16, 2)).toEqual([0, 0, 0, 0]);
  66. expect(pixelAt(pixels, 16, 4)).toEqual(CIRCLE_RED_BGRA);
  67. expect(pixelAt(pixels, 2, 16)).toEqual([0, 0, 0, 0]);
  68. });
  69. it("红点模式只画纯色圆,不渲染数字(托盘 16px 下数字不可读)", () => {
  70. const pixels = renderTaskbarBadgePixels("9+", { size: 14, glyphScale: 1, padding: 0, dot: true });
  71. const at = (x: number, y: number): number[] => {
  72. const offset = (y * 14 + x) * 4;
  73. return [pixels[offset]!, pixels[offset + 1]!, pixels[offset + 2]!, pixels[offset + 3]!];
  74. };
  75. // 圆心与圆面都是红色,无任何白色笔画像素
  76. expect(at(7, 7)).toEqual(CIRCLE_RED_BGRA);
  77. expect(countWhitePixels(pixels)).toBe(0);
  78. });
  79. it("每个数字字形都有可见像素,未知字符不产生字形", () => {
  80. for (const label of ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+"]) {
  81. expect(countWhitePixels(renderTaskbarBadgePixels(label)), `label ${label}`).toBeGreaterThan(
  82. 0,
  83. );
  84. }
  85. const empty = renderTaskbarBadgePixels("");
  86. expect(empty.every((value) => value === 0)).toBe(true);
  87. });
  88. it("两位折叠文案 9+ 的两个字形都可见", () => {
  89. const pixels = renderTaskbarBadgePixels("9+");
  90. // 9+ 总宽 22,原点在 x=5:(8,16) 命中 "9" 的竖笔,(22,16) 命中 "+" 的横笔。
  91. expect(isWhitePixel(pixelAt(pixels, 8, 16))).toBe(true);
  92. expect(isWhitePixel(pixelAt(pixels, 22, 16))).toBe(true);
  93. });
  94. });
  95. describe("applyTaskbarOverlayToWindows", () => {
  96. it("把角标图和描述同步到所有存活窗口", () => {
  97. const winA = createFakeWindow();
  98. const winB = createFakeWindow();
  99. const image = { __badge: true };
  100. applyTaskbarOverlayToWindows({
  101. label: "3",
  102. windows: [winA, winB],
  103. badgeImageForLabel: () => image,
  104. });
  105. expect(winA.overlays).toEqual([{ image, description: "3 unread tasks" }]);
  106. expect(winB.overlays).toEqual([{ image, description: "3 unread tasks" }]);
  107. });
  108. it("label 为 null 时清除 overlay 且不生成徽章图", () => {
  109. const win = createFakeWindow();
  110. const badgeImageForLabel = vi.fn();
  111. applyTaskbarOverlayToWindows({
  112. label: null,
  113. windows: [win],
  114. badgeImageForLabel,
  115. });
  116. expect(win.overlays).toEqual([{ image: null, description: "" }]);
  117. expect(badgeImageForLabel).not.toHaveBeenCalled();
  118. });
  119. it("跳过已销毁的窗口", () => {
  120. const alive = createFakeWindow();
  121. const destroyed = createFakeWindow({ destroyed: true });
  122. applyTaskbarOverlayToWindows({
  123. label: "1",
  124. windows: [destroyed, alive],
  125. badgeImageForLabel: () => ({ __badge: true }),
  126. });
  127. expect(destroyed.overlays).toHaveLength(0);
  128. expect(alive.overlays).toHaveLength(1);
  129. });
  130. });
  131. describe("createWindowsTaskbarOverlayApplier", () => {
  132. it("遍历注入的全部窗口并按 label 缓存徽章图", () => {
  133. const winA = createFakeWindow();
  134. const winB = createFakeWindow();
  135. const createBadgeImage = vi.fn(() => ({ __badge: true }));
  136. const applier = createWindowsTaskbarOverlayApplier({
  137. getAllWindows: () => [winA, winB],
  138. createBadgeImage,
  139. });
  140. applier("2");
  141. applier("2");
  142. expect(createBadgeImage).toHaveBeenCalledTimes(1);
  143. expect(createBadgeImage).toHaveBeenCalledWith("2");
  144. const expectedImage = { __badge: true };
  145. expect(winA.overlays).toEqual([
  146. { image: expectedImage, description: "2 unread tasks" },
  147. { image: expectedImage, description: "2 unread tasks" },
  148. ]);
  149. expect(winB.overlays).toHaveLength(2);
  150. });
  151. it("label 为 null 时对所有窗口下发 null 清除角标", () => {
  152. const win = createFakeWindow();
  153. const applier = createWindowsTaskbarOverlayApplier({
  154. getAllWindows: () => [win],
  155. createBadgeImage: vi.fn(),
  156. });
  157. applier(null);
  158. expect(win.overlays).toEqual([{ image: null, description: "" }]);
  159. });
  160. });
  161. describe("blendBadgeIntoBitmap", () => {
  162. const SIZE = 4;
  163. function fillBase(): Uint8Array {
  164. // 4×4 不透明蓝底(预乘 BGRA:B=200, G=40, R=10, A=255)
  165. const base = new Uint8Array(SIZE * SIZE * 4);
  166. for (let offset = 0; offset < base.length; offset += 4) {
  167. base[offset] = 200;
  168. base[offset + 1] = 40;
  169. base[offset + 2] = 10;
  170. base[offset + 3] = 255;
  171. }
  172. return base;
  173. }
  174. function solidBadge(value: { b: number; g: number; r: number; a: number }): Uint8Array {
  175. const badge = new Uint8Array(2 * 2 * 4);
  176. for (let offset = 0; offset < badge.length; offset += 4) {
  177. badge[offset] = value.b;
  178. badge[offset + 1] = value.g;
  179. badge[offset + 2] = value.r;
  180. badge[offset + 3] = value.a;
  181. }
  182. return badge;
  183. }
  184. function pixel(buffer: Uint8Array, x: number, y: number): number[] {
  185. const offset = (y * SIZE + x) * 4;
  186. return [buffer[offset]!, buffer[offset + 1]!, buffer[offset + 2]!, buffer[offset + 3]!];
  187. }
  188. it("不透明徽章完全覆盖目标区域,其余像素不受影响", () => {
  189. const base = fillBase();
  190. blendBadgeIntoBitmap({
  191. base,
  192. size: SIZE,
  193. badge: solidBadge({ b: 1, g: 2, r: 3, a: 255 }),
  194. badgeSize: 2,
  195. originX: 2,
  196. originY: 2,
  197. });
  198. expect(pixel(base, 2, 2)).toEqual([1, 2, 3, 255]);
  199. expect(pixel(base, 3, 3)).toEqual([1, 2, 3, 255]);
  200. expect(pixel(base, 0, 0)).toEqual([200, 40, 10, 255]);
  201. expect(pixel(base, 1, 1)).toEqual([200, 40, 10, 255]);
  202. });
  203. it("半透明徽章按预乘 alpha 混合:out = src + dst × (1 - srcA)", () => {
  204. const base = fillBase();
  205. // srcA = 128 → keep ≈ 0.498;out.b = 100 + 200 × 0.498039 ≈ 200(四舍五入)
  206. blendBadgeIntoBitmap({
  207. base,
  208. size: SIZE,
  209. badge: solidBadge({ b: 100, g: 0, r: 0, a: 128 }),
  210. badgeSize: 2,
  211. originX: 0,
  212. originY: 0,
  213. });
  214. const [b, g, r, a] = pixel(base, 0, 0);
  215. expect(a).toBe(255);
  216. expect(b).toBe(Math.round(100 + 200 * ((255 - 128) / 255)));
  217. expect(g).toBe(Math.round(0 + 40 * ((255 - 128) / 255)));
  218. expect(r).toBe(Math.round(0 + 10 * ((255 - 128) / 255)));
  219. });
  220. it("徽章透明像素(a=0)不改动底图,越界区域安全裁剪", () => {
  221. const base = fillBase();
  222. const badge = new Uint8Array(2 * 2 * 4); // 全透明
  223. badge[3] = 0;
  224. blendBadgeIntoBitmap({
  225. base,
  226. size: SIZE,
  227. badge,
  228. badgeSize: 2,
  229. originX: 3,
  230. originY: 3, // 2×2 徽章只有 (3,3) 落在画布内,(4,3)(3,4)(4,4) 越界
  231. });
  232. expect(pixel(base, 0, 0)).toEqual([200, 40, 10, 255]);
  233. expect(pixel(base, 3, 3)).toEqual([200, 40, 10, 255]);
  234. });
  235. });
  236. });