Files
37duAutomation/desktop/electron-shell/electron/tray-menu.js
T
puddy 843ff244a6 feat: 新增多插件 UI 支持及运行日志集中回传功能
本次提交包含多项功能更新:
1. 为多个插件(xq0002/0003/0004/0008/0010/0011/0020/0031_xq0032/0033/0035/0037/0045_xq0046/0047/0048)新增 UI 界面及配置支持
2. 新增运行日志集中回传功能,客户端自动上传运行日志到中心机集中存储
3. 新增插件分类字段支持,用于场景库分组管理
4. 优化 xq0034/0037/0048 等插件的功能及描述
5. 修复控制面响应兼容问题,支持新旧版本数据格式
6. 新增 xq0033 插件的状态存储模块,用于历史记录管理
7. 优化 CRM 登录窗口模式,支持全屏置顶
2026-10-26 15:42:50 +08:00

185 lines
7.7 KiB
JavaScript

// 自定义托盘右键菜单:无边框卡片式弹窗,替代系统原生菜单(原生样式跟随系统、不可控)。
// 用法:attach(tray, { onOpen, onQuit }),主进程负责窗口与菜单项动作。
"use strict";
const path = require("path");
const { app, BrowserWindow, ipcMain, screen } = require("electron");
const { makeLogger } = require("./logger");
const logger = makeLogger("tray-menu");
const W = 236;
const H = 178; // 与 buildHTML 里的内容高度一致(头部 44 + 三分隔 + 三菜单项)
const MARGIN = 8; // 与托盘图标 / 屏幕边缘留白
let win = null;
let trayRef = null;
let handlers = { onOpen: null, onQuit: null };
let wired = false;
function buildHTML() {
const version = app.getVersion();
const autoOn = app.getLoginItemSettings().openAtLogin ? "on" : "";
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;
-webkit-user-select:none;user-select:none}
html,body{background:transparent;overflow:hidden}
.card{position:fixed;inset:0;background:#fff;border:1px solid #E8EAF1;border-radius:14px;
box-shadow:0 16px 38px -16px rgba(18,20,31,.38),0 4px 12px -6px rgba(18,20,31,.12);
padding:6px;display:flex;flex-direction:column;
animation:pop .16s cubic-bezier(.2,.8,.3,1)}
@keyframes pop{from{opacity:0;transform:translateY(6px) scale(.985)}to{opacity:1;transform:none}}
.head{display:flex;align-items:center;gap:9px;padding:6px 8px 10px}
.logo{width:26px;height:26px;border-radius:8px;background:linear-gradient(135deg,#4F5AE8,#0F9F9B);
color:#fff;font-size:11px;font-weight:800;display:flex;align-items:center;justify-content:center;letter-spacing:.2px}
.hi{min-width:0;flex:1}
.hi b{display:block;font-size:12.5px;color:#12141B;font-weight:600;
white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.hi span{display:flex;align-items:center;gap:5px;font-size:11px;color:#6E7686;margin-top:3px}
.dot{width:5px;height:5px;border-radius:50%;background:#0E9F76;box-shadow:0 0 0 2px rgba(14,159,118,.14)}
.sep{height:1px;background:#F0F2F7;margin:4px 6px}
.item{display:flex;align-items:center;gap:9px;height:34px;padding:0 9px;border-radius:9px;
font-size:12.5px;color:#3C4250;cursor:pointer;transition:background .12s,color .12s}
.item:hover{background:#F5F6FA;color:#12141B}
.item .ic{width:15px;height:15px;flex-shrink:0;color:#8A92A3}
.item:hover .ic{color:#4F5AE8}
.item.brand{color:#3D47D4;font-weight:600}
.item.brand .ic{color:#4F5AE8}
.item.danger:hover{background:#FDEDED;color:#CC3B40}
.item.danger:hover .ic{color:#E5484D}
.sw{margin-left:auto;width:26px;height:15px;border-radius:8px;background:#E3E6EF;position:relative;
transition:background .16s;flex-shrink:0}
.sw::after{content:"";position:absolute;top:2px;left:2px;width:11px;height:11px;border-radius:50%;
background:#fff;box-shadow:0 1px 2px rgba(0,0,0,.2);transition:transform .16s}
.sw.on{background:#4F5AE8}
.sw.on::after{transform:translateX(11px)}
</style></head><body>
<div class="card">
<div class="head">
<div class="logo">37</div>
<div class="hi">
<b>37du 自动化工作台</b>
<span><i class="dot"></i>服务运行中 · v${version}</span>
</div>
</div>
<div class="sep"></div>
<div class="item brand" data-act="open">
<svg class="ic" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8"
stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="4" width="18" height="14" rx="2.5"/>
<path d="M3 9h18"/><path d="M9 22h6"/></svg>
<span>打开工作台</span>
</div>
<div class="item" data-act="autostart">
<svg class="ic" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8"
stroke-linecap="round" stroke-linejoin="round"><path d="M12 3v9"/>
<path d="M6.4 6.4a8 8 0 1 0 11.2 0"/></svg>
<span>开机自启</span>
<span class="sw ${autoOn}" id="sw"></span>
</div>
<div class="sep"></div>
<div class="item danger" data-act="quit">
<svg class="ic" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8"
stroke-linecap="round" stroke-linejoin="round"><path d="M12 21a9 9 0 1 0-9-9"/>
<path d="M3 16v5h5"/></svg>
<span>退出</span>
</div>
</div>
<script>
document.addEventListener("click", async (e) => {
const el = e.target.closest(".item");
if (!el) return;
const act = el.dataset.act;
if (act === "autostart") {
const on = await window.trayMenu.toggleAutostart();
document.getElementById("sw").classList.toggle("on", !!on);
return;
}
window.trayMenu.action(act);
});
document.addEventListener("keydown", (e) => {
if (e.key === "Escape") window.trayMenu.action("close");
});
document.addEventListener("contextmenu", (e) => e.preventDefault());
</script>
</body></html>`;
}
function closeMenu() {
if (win && !win.isDestroyed()) win.destroy();
win = null;
}
function openMenu() {
if (win && !win.isDestroyed()) { closeMenu(); return; } // 再次右键 = 收起
if (!trayRef) return;
try {
// 托盘图标可能被折叠进"隐藏的图标"面板(bounds 无效),此时以鼠标位置为锚点
let b = trayRef.getBounds();
if (!b || !b.width) {
const p = screen.getCursorScreenPoint();
b = { x: p.x - 8, y: p.y - 8, width: 16, height: 16 };
}
const pt = { x: b.x + Math.round(b.width / 2), y: b.y + Math.round(b.height / 2) };
const wa = screen.getDisplayNearestPoint(pt).workArea;
// 任务栏在屏幕下半部分时菜单向上弹出,否则向下
let x = Math.round(b.x + b.width / 2 - W / 2);
let y = b.y > wa.y + wa.height / 2 ? b.y - H - MARGIN : b.y + b.height + MARGIN;
x = Math.max(wa.x + MARGIN, Math.min(x, wa.x + wa.width - W - MARGIN));
y = Math.max(wa.y + MARGIN, Math.min(y, wa.y + wa.height - H - MARGIN));
win = new BrowserWindow({
width: W, height: H, x, y,
frame: false, transparent: true, resizable: false, movable: false,
minimizable: false, maximizable: false, fullscreenable: false,
skipTaskbar: true, alwaysOnTop: true, show: false, hasShadow: false,
backgroundColor: "#00000000",
webPreferences: {
preload: path.join(__dirname, "tray-menu-preload.js"),
contextIsolation: true, nodeIntegration: false, sandbox: true,
},
});
win.setAlwaysOnTop(true, "pop-up-menu");
win.loadURL("data:text/html;charset=utf-8," + encodeURIComponent(buildHTML()));
win.once("ready-to-show", () => {
if (win && !win.isDestroyed()) { win.show(); win.focus(); }
});
win.on("blur", () => closeMenu());
win.on("closed", () => { win = null; });
} catch (e) {
logger.warning(`托盘菜单打开失败:${e.message}`);
closeMenu();
}
}
function wire() {
if (wired) return;
wired = true;
ipcMain.on("tray-menu:action", (_e, act) => {
if (act === "open") {
closeMenu();
if (handlers.onOpen) handlers.onOpen();
} else if (act === "quit") {
closeMenu();
if (handlers.onQuit) handlers.onQuit();
} else {
closeMenu();
}
});
ipcMain.handle("tray-menu:toggle-autostart", () => {
const next = !app.getLoginItemSettings().openAtLogin;
app.setLoginItemSettings({ openAtLogin: next });
return next;
});
}
function attach(tray, opts) {
trayRef = tray;
handlers = opts || { onOpen: null, onQuit: null };
wire();
tray.on("right-click", openMenu);
}
module.exports = { attach, close: closeMenu };