86 lines
2.1 KiB
JavaScript
86 lines
2.1 KiB
JavaScript
const { app, BrowserWindow, ipcMain, dialog, shell } = require("electron");
|
|
const path = require("path");
|
|
const fs = require("fs");
|
|
|
|
// 便携版:设置用户数据目录为程序目录下
|
|
const portableDataPath = path.join(__dirname, "data");
|
|
|
|
let mainWindow;
|
|
|
|
function createWindow() {
|
|
mainWindow = new BrowserWindow({
|
|
width: 820,
|
|
height: 580,
|
|
minWidth: 720,
|
|
minHeight: 500,
|
|
title: "备份管理器",
|
|
webPreferences: {
|
|
nodeIntegration: false,
|
|
contextIsolation: true,
|
|
sandbox: false,
|
|
preload: path.join(__dirname, "preload.js")
|
|
},
|
|
frame: false,
|
|
backgroundColor: "#faf7f2"
|
|
});
|
|
|
|
mainWindow.loadFile("index.html");
|
|
|
|
// 开发时打开DevTools
|
|
// mainWindow.webContents.openDevTools();
|
|
}
|
|
|
|
// 确保数据目录存在
|
|
function ensureDataDir() {
|
|
if (!fs.existsSync(portableDataPath)) {
|
|
fs.mkdirSync(portableDataPath, { recursive: true });
|
|
}
|
|
}
|
|
|
|
app.whenReady().then(() => {
|
|
ensureDataDir();
|
|
createWindow();
|
|
|
|
app.on("activate", () => {
|
|
if (BrowserWindow.getAllWindows().length === 0) {
|
|
createWindow();
|
|
}
|
|
});
|
|
});
|
|
|
|
app.on("window-all-closed", () => {
|
|
if (process.platform !== "darwin") {
|
|
app.quit();
|
|
}
|
|
});
|
|
|
|
// ============ IPC 处理 ============
|
|
|
|
// 窗口控制
|
|
ipcMain.handle("window-minimize", () => mainWindow.minimize());
|
|
ipcMain.handle("window-maximize", () => {
|
|
if (mainWindow.isMaximized()) {
|
|
mainWindow.unmaximize();
|
|
} else {
|
|
mainWindow.maximize();
|
|
}
|
|
});
|
|
ipcMain.handle("window-close", () => mainWindow.close());
|
|
|
|
// 获取数据目录路径
|
|
ipcMain.handle("get-data-path", () => portableDataPath);
|
|
|
|
// 选择导出目录
|
|
ipcMain.handle("select-export-dir", async () => {
|
|
const result = await dialog.showOpenDialog(mainWindow, {
|
|
properties: ["openDirectory"],
|
|
title: "选择导出目录"
|
|
});
|
|
return result.canceled ? null : result.filePaths[0];
|
|
});
|
|
|
|
// 在文件管理器中显示文件
|
|
ipcMain.handle("show-in-folder", (event, filePath) => {
|
|
shell.showItemInFolder(filePath);
|
|
});
|