Files
Shuodedaoli-Deskpet/main/main.js

84 lines
2.2 KiB
JavaScript
Raw Normal View History

2025-08-06 14:15:15 +08:00
const { app, BrowserWindow, ipcMain } = require("electron");
const path = require("path");
2025-08-07 01:15:18 +08:00
const { spawn } = require('child_process')
// 音效播放器
function playAudioFile(filePath) {
if (process.platform === 'win32') {
spawn('cmd', ['/c', `start "" "${filePath}"`])
} else if (process.platform === 'darwin') {
spawn('afplay', [filePath])
} else {
spawn('aplay', [filePath])
}
}
ipcMain.on('play-sound', (_, soundFile) => {
let soundPath;
// 判断是开发环境还是生产环境
if (process.env.NODE_ENV === 'development') {
// 开发环境下,路径指向 src
soundPath = path.join(__dirname, '../renderer/src/assets/sounds', soundFile);
} else {
// 生产环境下,路径指向打包后的 dist 目录
soundPath = path.join(__dirname, '../renderer/dist/assets/sounds', soundFile);
}
console.log('Main process trying to play sound at path:', soundPath);
2025-08-07 01:15:18 +08:00
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
2025-08-06 14:15:15 +08:00
let mainWindow;
2025-08-05 23:30:00 +08:00
function createWindow() {
mainWindow = new BrowserWindow({
width: 300,
height: 300,
transparent: true,
frame: false,
2025-08-06 14:15:15 +08:00
resizable: false, // 固定大小
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-05 23:30:00 +08:00
// 开发模式加载Vite服务器
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-06 14:15:15 +08:00
// 窗口拖拽功能
let isDragging = false;
mainWindow.webContents.on("before-input-event", (_, input) => {
if (input.type === "mouseDown") {
isDragging = true;
mainWindow.webContents.executeJavaScript(`
window.dragOffset = { x: ${input.x}, y: ${input.y} }
`);
} else if (input.type === "mouseUp") {
isDragging = false;
}
});
mainWindow.on("moved", () => {
if (isDragging) {
mainWindow.webContents.executeJavaScript(`
window.electronAPI.updatePosition()
`);
}
const [x, y] = mainWindow.getPosition();
mainWindow.webContents.send("update-position", { x, y });
});
2025-08-05 23:30:00 +08:00
}
2025-08-06 14:15:15 +08:00
app.whenReady().then(createWindow);