Files
puddy 4b84f9c149 feat(electron-shell): 1.8.0 执行队列、流程编排、流程定时与版本回滚
- 执行队列:多任务串行排队,可单独取消,超时自动放弃,异常退出后标记已中断

- 流程编排:画布多场景依赖图,内置确保登录/等待/通知/人工确认节点

- 流程定时:场景与流程统一配置定时(每天/工作日/每周/每月/每隔N小时)

- 流程版本历史:自动留档 20 版,画布一键回滚且回滚可撤销

- 流程配置云备份:随定时任务备份到中心机,换机自动恢复

- 总览页/场景库布局调整,通知浮窗右下角堆叠显示
2026-10-27 13:36:38 +08:00

144 lines
5.3 KiB
JavaScript

// 自定义通知弹窗(右下角 toast):无边框、置顶、不抢焦点、按级别配色。
// 替代系统原生通知(原生样式跟随 Windows 主题,深色模式下发黑、不可控)。
"use strict";
const { makeLogger } = require("./logger");
const logger = makeLogger("notify-toast");
const W = 380, H = 132, GAP = 10, MARGIN = 16, TTL = 9000;
// 同屏最多几条(超出后关掉最早的那条,避免糊满屏幕)
const MAX_VISIBLE = 4;
// 当前在显示的窗口,自下而上排列:末尾 = 最新(最靠下)
const _stack = [];
// 重新排布:最新的一条贴着屏幕右下角,其余依次往上叠。
// 关键点是"有窗口消失时也要重排",否则会留下空洞、新通知从半空中冒出来。
function layout() {
let wa;
try {
wa = require("electron").screen.getPrimaryDisplay().workArea;
} catch (e) {
return; // 非 Electron 环境
}
const x = Math.round(wa.x + wa.width - W - MARGIN);
let bottom = wa.y + wa.height - MARGIN;
for (let i = _stack.length - 1; i >= 0; i--) {
const y = Math.round(bottom - H);
const win = _stack[i].win;
try {
if (!win.isDestroyed()) win.setPosition(x, y);
} catch (e) {
/* 窗口正在销毁:忽略 */
}
bottom = y - GAP;
}
}
// 从栈里摘掉并让剩下的整体下沉(补上空位)
function removeFromStack(win) {
const i = _stack.findIndex((it) => it.win === win);
if (i < 0) return;
_stack.splice(i, 1);
layout();
}
function closeToast(win) {
try {
if (!win.isDestroyed()) win.destroy();
} catch (e) {
/* 忽略 */
}
}
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, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")
.replace(/"/g, "&quot;");
}
function buildHTML(title, body, level) {
const color = levelColor(level);
const label = levelLabel(level);
return `<!doctype html><html><head><meta charset="utf-8"><style>
* { margin:0; padding:0; box-sizing:border-box; font-family:"Segoe UI","Microsoft YaHei",sans-serif; }
html,body { background:transparent; }
.toast {
position:fixed; inset:0; border-radius:14px; overflow:hidden;
background:#fff; border:1px solid #e6eaf2;
box-shadow:0 12px 32px -14px rgba(20,26,40,.45);
display:flex; flex-direction:column;
animation: in .28s ease;
}
@keyframes in { from { transform:translateY(14px); opacity:0 } to { transform:none; opacity:1 } }
.bar { padding:9px 12px 2px; display:flex; align-items:center; gap:8px; }
.logo { width:20px;height:20px;border-radius:6px;background:linear-gradient(135deg,#2f5cf0,#0f9f9b);
color:#fff;font-size:11px;font-weight:800;display:flex;align-items:center;justify-content:center; }
.lvl { font-size:11px;font-weight:700;color:${color};letter-spacing:.3px; }
.close { margin-left:auto;cursor:pointer;color:#9aa2b4;font-size:14px;line-height:1;padding:2px 5px; }
.close:hover { color:#4a5468; }
.ttl { padding:0 14px; font-size:14px;font-weight:700;color:#232a3a; }
.bdy { padding:4px 14px 12px; font-size:12.5px;color:#3b4358;line-height:1.45;
overflow:hidden; display:-webkit-box; -webkit-line-clamp:2; -webkit-box-orient:vertical; }
</style></head><body>
<div class="toast">
<div class="bar">
<span class="logo">37</span>
<span class="lvl">${label}</span>
<span class="close" onclick="window.close()">✕</span>
</div>
<div class="ttl">${esc(title)}</div>
<div class="bdy">${esc(body)}</div>
</div>
</body></html>`;
}
function show(title, body, level) {
try {
const { BrowserWindow, screen } = require("electron");
const wa = screen.getPrimaryDisplay().workArea;
// 超过上限:先关掉最早的一条(它的 closed 会把自己从栈里摘掉)
while (_stack.length >= MAX_VISIBLE) {
closeToast(_stack[0].win);
if (_stack.length >= MAX_VISIBLE) _stack.shift(); // 兜底:closed 没触发也不能死循环
}
const win = new BrowserWindow({
width: W, height: H,
x: Math.round(wa.x + wa.width - W - MARGIN),
y: Math.round(wa.y + wa.height - H - MARGIN),
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");
_stack.push({ win });
layout(); // 先摆好位置再显示,避免出现"闪一下再跳"
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(() => closeToast(win), TTL);
win.on("closed", () => {
clearTimeout(t);
removeFromStack(win); // 摘掉后剩余的立刻下沉,补上空位
});
return true;
} catch (e) {
logger.warning(`自定义通知弹窗失败:${e.message}`);
return false;
}
}
module.exports = { show };