taskbarOverlayBadge.test.ts 13 KB

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