Files
Shuodedaoli-Deskpet/main/main.js

157 lines
4.2 KiB
JavaScript
Raw Normal View History

const { app, BrowserWindow, ipcMain, Menu } = require("electron");
2025-08-06 14:15:15 +08:00
const path = require("path");
const { spawn } = require("child_process");
const fs = require("fs");
2025-08-07 01:15:18 +08:00
app.disableHardwareAcceleration(); // 高 DPI 缩放修复
2025-08-07 01:15:18 +08:00
// 音效播放器
function playAudioFile(filePath) {
if (process.platform === "win32") {
spawn("cmd", ["/c", `start "" "${filePath}"`]);
} else if (process.platform === "darwin") {
spawn("afplay", [filePath]);
2025-08-07 01:15:18 +08:00
} else {
spawn("aplay", [filePath]);
2025-08-07 01:15:18 +08:00
}
}
ipcMain.on("play-sound", (_, soundFile) => {
let soundPath;
// 判断是开发环境还是生产环境
if (process.env.NODE_ENV === "development") {
2025-08-08 17:42:41 +08:00
// 在开发模式下,直接指向 renderer/public 里的文件
soundPath = path.join(
__dirname,
2025-09-03 21:02:41 +08:00
"../renderer/public/sounds",
soundFile
);
} else {
2025-08-08 17:42:41 +08:00
// 在生产模式下Vite 会把 public 里的文件复制到 dist 文件夹
soundPath = path.join(
__dirname,
2025-09-03 21:02:41 +08:00
"../renderer/dist/sounds",
soundFile
);
}
console.log("Main process trying to play sound at path:", soundPath);
if (require("fs").existsSync(soundPath)) {
playAudioFile(soundPath);
2025-08-07 01:15:18 +08:00
} else {
console.error("Sound file not found:", soundPath);
2025-08-07 01:15:18 +08:00
}
});
2025-08-05 23:30:00 +08:00
ipcMain.handle("get-sound-path", (_, soundFile) => {
let soundPath;
if (process.env.NODE_ENV === "development") {
soundPath = path.join(
__dirname,
2025-09-03 21:02:41 +08:00
"../renderer/public/sounds",
soundFile
);
} else {
soundPath = path.join(
__dirname,
2025-09-03 21:02:41 +08:00
"../renderer/dist/sounds",
soundFile
);
}
if (require("fs").existsSync(soundPath)) {
// 返回一个可供 web 环境使用的 file 协议 URL
return `file://${soundPath}`;
} else {
console.error("Sound file not found:", soundPath);
return null;
}
});
ipcMain.handle("get-sound-files", async () => {
const soundDir =
process.env.NODE_ENV === "development"
2025-09-03 21:02:41 +08:00
? path.join(__dirname, "../renderer/public/sounds")
: path.join(__dirname, "../renderer/dist/sounds");
try {
// 读取目录下的所有文件名
const files = await fs.promises.readdir(soundDir);
// 筛选出 .mp3 文件并返回
return files.filter((file) => file.endsWith(".mp3"));
} catch (error) {
console.error("无法读取声音目录:", error);
return []; // 如果出错则返回空数组
}
});
ipcMain.on("show-context-menu", () => {
const template = [
{
label: "置顶显示",
type: "checkbox",
checked: isAlwaysOnTop, // 菜单项的选中状态与变量同步
click: () => {
isAlwaysOnTop = !isAlwaysOnTop; // 点击时切换状态
mainWindow.setAlwaysOnTop(isAlwaysOnTop); // 并应用到窗口
},
},
{ type: "separator" }, // 分隔线
{
label: "退出",
click: () => {
app.quit(); // 点击时退出应用
},
},
];
const menu = Menu.buildFromTemplate(template);
menu.popup({ window: mainWindow }); // 在主窗口上弹出菜单
});
let isAlwaysOnTop = true;
2025-08-06 14:15:15 +08:00
let mainWindow;
2025-08-05 23:30:00 +08:00
function createWindow() {
mainWindow = new BrowserWindow({
width: 200,
height: 200,
2025-08-09 23:32:55 +08:00
transparent: true, // 开启透明窗口
frame: false, // 无边框窗口
resizable: false, // 禁止调整大小
title: "说的道理桌宠",
alwaysOnTop: isAlwaysOnTop, // 窗口始终在最上层
icon: path.join(__dirname, "../build/icon.png"),
2025-08-05 23:30:00 +08:00
webPreferences: {
2025-08-06 14:15:15 +08:00
preload: path.join(__dirname, "preload.js"),
contextIsolation: true,
2025-08-09 23:32:55 +08:00
webSecurity: false,
2025-08-06 14:15:15 +08:00
},
});
2025-08-05 23:30:00 +08:00
// 当渲染进程传来这个事件时,就移动窗口
ipcMain.on("move-window", (event, { x, y }) => {
// 使用 Math.round 避免非整数坐标可能带来的问题
mainWindow.setPosition(Math.round(x), Math.round(y), false);
});
// 添加一个 handle用于响应前端获取窗口位置的请求
ipcMain.handle("get-window-position", () => {
if (mainWindow) {
const [x, y] = mainWindow.getPosition();
return { x, y };
}
return { x: 0, y: 0 };
});
2025-08-06 14:15:15 +08:00
if (process.env.NODE_ENV === "development") {
mainWindow.loadURL("http://localhost:5173");
} else {
2025-08-06 14:15:15 +08:00
mainWindow.loadFile(path.join(__dirname, "../renderer/dist/index.html"));
}
2025-08-05 23:30:00 +08:00
}
2025-08-06 14:15:15 +08:00
app.whenReady().then(createWindow);