// 自定义通知弹窗(右下角 toast):无边框、置顶、不抢焦点、按级别配色。 // 替代系统原生通知(原生样式跟随 Windows 主题,深色模式下发黑、不可控)。 "use strict"; const { makeLogger } = require("./logger"); const logger = makeLogger("notify-toast"); const W = 380, H = 132, GAP = 10, MARGIN = 16, TTL = 9000; let lastBottom = null; function levelColor(level) { return { info: "#2f5cf0", warning: "#d97706", important: "#d6453d" }[level] || "#2f5cf0"; } function levelLabel(level) { return { info: "通知", warning: "提醒", important: "重要" }[level] || "通知"; } function esc(s) { return String(s || "") .replace(/&/g, "&").replace(//g, ">") .replace(/"/g, """); } function buildHTML(title, body, level) { const color = levelColor(level); const label = levelLabel(level); return `
${label} ✕
${esc(title)}
${esc(body)}
`; } function show(title, body, level) { try { const { BrowserWindow, screen } = require("electron"); const display = screen.getPrimaryDisplay(); const wa = display.workArea; const x = wa.x + wa.width - W - MARGIN; if (lastBottom === null) lastBottom = wa.y + wa.height - H - MARGIN; const y = lastBottom; lastBottom = lastBottom - H - GAP; if (lastBottom < wa.y + MARGIN) lastBottom = null; // 满屏后下一轮回到底部 const win = new BrowserWindow({ width: W, height: H, x, y, frame: false, transparent: true, resizable: false, movable: false, alwaysOnTop: true, skipTaskbar: true, hasShadow: false, show: false, focusable: false, backgroundColor: "#00000000", webPreferences: { contextIsolation: true, nodeIntegration: false, sandbox: true }, }); win.setAlwaysOnTop(true, "pop-up-menu"); win.loadURL("data:text/html;charset=utf-8," + encodeURIComponent(buildHTML(title, body, level))); win.once("ready-to-show", () => { try { win.showInactive(); } catch (e) { win.show(); } }); const t = setTimeout(() => { try { win.destroy(); } catch (e) { /* 忽略 */ } }, TTL); win.on("closed", () => { clearTimeout(t); }); return true; } catch (e) { logger.warning(`自定义通知弹窗失败:${e.message}`); return false; } } module.exports = { show };