UI调整优化
This commit is contained in:
@@ -1,11 +1,21 @@
|
||||
const { Menu, app, BrowserWindow, dialog } = require("electron");
|
||||
let fs = require('fs')
|
||||
const { ipcMain, Menu, shell, app, BrowserWindow, dialog } = require("electron");
|
||||
const fs = require('fs');
|
||||
const fontList = require("font-list");
|
||||
const path = require("path");
|
||||
const child_process = require("child_process");
|
||||
|
||||
console.log(`Node.js: ${process.versions.node}`);
|
||||
|
||||
// 初始化 Remote 模块
|
||||
require("@electron/remote/main").initialize();
|
||||
|
||||
let preFilePath = "";
|
||||
|
||||
// 1. 定义数据路径(放在全局,方便后续调用)
|
||||
const exePath = app.getPath('userData');
|
||||
const historyFilePath = path.join(exePath, 'data.json');
|
||||
console.log('User data path:', exePath);
|
||||
|
||||
app.on("will-finish-launching", () => {
|
||||
app.on("open-file", (e, filePath) => {
|
||||
preFilePath = filePath;
|
||||
@@ -16,15 +26,13 @@ app.on("will-finish-launching", () => {
|
||||
preFilePath = process.argv[1] === "." ? "" : process.argv[1];
|
||||
}
|
||||
});
|
||||
// 使用用户数据目录,避免管理员权限问题
|
||||
const exePath = app.getPath('userData')
|
||||
console.log('User data path:', exePath);
|
||||
|
||||
function createWindow() {
|
||||
// Create the browser window.
|
||||
let size = require("electron").screen.getPrimaryDisplay().workAreaSize;
|
||||
//console.log(require('electron').screen.getPrimaryDisplay());
|
||||
let width = parseInt(size.width);
|
||||
var mainWindow;
|
||||
|
||||
mainWindow = new BrowserWindow({
|
||||
show: false,
|
||||
width: size.width,
|
||||
@@ -32,25 +40,31 @@ function createWindow() {
|
||||
resizable: false,
|
||||
icon: "public/images/favicon.ico", // sets window icon
|
||||
webPreferences: {
|
||||
preload: path.join(__dirname, "preload.js"),
|
||||
preload: path.join(__dirname, "preload.js"), // 确保你有这个文件
|
||||
nodeIntegration: true,
|
||||
contextIsolation: false,
|
||||
contextIsolation: false, // 注意:为了安全建议后续开启,但目前保持你的配置
|
||||
enableRemoteModule: true,
|
||||
},
|
||||
});
|
||||
|
||||
mainWindow.maximize();
|
||||
// Menu.setApplicationMenu(null);//关闭菜单
|
||||
// and load the index.html of the app.
|
||||
//mainWindow.maximize();
|
||||
//Menu.setApplicationMenu(null); // 关闭菜单
|
||||
|
||||
// 启动逻辑:如果有预打开的文件,直接跳转
|
||||
if (preFilePath) {
|
||||
let soonData = fs.readFileSync(preFilePath)
|
||||
let j = JSON.parse(soonData.toString());
|
||||
let type = j.soonType ? j.soonType : j.backBlackPic ? 2 : 1;
|
||||
mainWindow.loadURL(`file://${__dirname}/design${type}.html?file=${preFilePath}&type=${type}`);
|
||||
try {
|
||||
let soonData = fs.readFileSync(preFilePath)
|
||||
let j = JSON.parse(soonData.toString());
|
||||
let type = j.soonType ? j.soonType : j.backBlackPic ? 2 : 1;
|
||||
mainWindow.loadURL(`file://${__dirname}/design${type}.html?file=${preFilePath}&type=${type}`);
|
||||
} catch (e) {
|
||||
console.error("Error loading preFilePath:", e);
|
||||
mainWindow.loadFile("index.html");
|
||||
}
|
||||
} else {
|
||||
mainWindow.loadFile("index.html");
|
||||
}
|
||||
|
||||
var close_flag = 0;
|
||||
mainWindow.on("close", function (e) {
|
||||
if (close_flag == 0) {
|
||||
@@ -59,59 +73,132 @@ function createWindow() {
|
||||
}
|
||||
});
|
||||
mainWindow.show();
|
||||
// mainWindow.webContents.openDevTools({mode:'bottom'});
|
||||
//1150 width 720 height
|
||||
// Open the DevTools.
|
||||
//mainWindow.webContents.openDevTools()
|
||||
const { ipcMain } = require("electron");
|
||||
|
||||
// ==========================================
|
||||
// 新增:安全的文件读写处理 (IPC Handle)
|
||||
// ==========================================
|
||||
|
||||
// 1. 读取历史记录
|
||||
ipcMain.handle('history:read', async () => {
|
||||
try {
|
||||
if (!fs.existsSync(historyFilePath)) {
|
||||
return { history: [] };
|
||||
}
|
||||
const data = fs.readFileSync(historyFilePath, 'utf-8');
|
||||
return JSON.parse(data);
|
||||
} catch (err) {
|
||||
console.error('主进程读取历史失败:', err);
|
||||
return { history: [] };
|
||||
}
|
||||
});
|
||||
|
||||
// 2. 写入历史记录 (包含权限检查)
|
||||
ipcMain.handle('history:write', async (event, data) => {
|
||||
try {
|
||||
// 简单的权限检查
|
||||
if (fs.existsSync(historyFilePath)) {
|
||||
try {
|
||||
fs.accessSync(historyFilePath, fs.constants.R_OK | fs.constants.W_OK);
|
||||
} catch (e) {
|
||||
console.error("无权限写入配置文件");
|
||||
return { success: false, error: "PERMISSION_DENIED" };
|
||||
}
|
||||
}
|
||||
fs.writeFileSync(historyFilePath, JSON.stringify(data));
|
||||
return { success: true };
|
||||
} catch (err) {
|
||||
console.error('主进程写入历史失败:', err);
|
||||
return { success: false, error: err.message };
|
||||
}
|
||||
});
|
||||
// 新增:获取软件版本号
|
||||
ipcMain.handle('app:get-version', () => {
|
||||
return app.getVersion(); // 自动读取 package.json 中的 version 字段
|
||||
});
|
||||
|
||||
// 3. 读取 .soon 项目文件 (用于获取缩略图等)
|
||||
ipcMain.handle('file:read-json', async (event, filePath) => {
|
||||
try {
|
||||
if (!fs.existsSync(filePath)) return null;
|
||||
const data = fs.readFileSync(filePath, 'utf-8');
|
||||
return JSON.parse(data);
|
||||
} catch (err) {
|
||||
// console.error('读取文件失败:', filePath, err);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
// ==========================================
|
||||
// 原有的 IPC 监听
|
||||
// ==========================================
|
||||
|
||||
ipcMain.on("get-sys-fonts", (event) => {
|
||||
fontList
|
||||
.getFonts()
|
||||
fontList.getFonts()
|
||||
.then((fonts) => {
|
||||
event.reply("font-list", fonts);
|
||||
})
|
||||
.catch((err) => {
|
||||
//console.log(err);
|
||||
console.log(err);
|
||||
});
|
||||
});
|
||||
|
||||
ipcMain.on("get-sys-language", (event) => {
|
||||
event.reply("sys-lan", app.getLocale());
|
||||
});
|
||||
ipcMain.on("open-help-file", (event) => {
|
||||
// help 文件在应用目录,不在用户数据目录
|
||||
let helpPath = !app.isPackaged
|
||||
? path.join(process.cwd(), 'help', 'User Manual.pdf')
|
||||
: path.join(path.dirname(process.execPath), 'resources', 'help', 'User Manual.pdf')
|
||||
child_process.exec(`start "" "${helpPath}"`);
|
||||
|
||||
// 修复后的 Help 文件打开逻辑
|
||||
ipcMain.on("open-help-file", async (event) => {
|
||||
let helpPath;
|
||||
const fileName = 'User Manual.pdf';
|
||||
|
||||
if (app.isPackaged) {
|
||||
helpPath = path.join(process.resourcesPath, 'help', fileName);
|
||||
} else {
|
||||
helpPath = path.join(process.cwd(), 'help', fileName);
|
||||
}
|
||||
|
||||
console.log("Help file path: ", helpPath);
|
||||
|
||||
if (fs.existsSync(helpPath)) {
|
||||
try {
|
||||
const errorMessage = await shell.openPath(helpPath);
|
||||
if (errorMessage) {
|
||||
console.error('Error opening help file:', errorMessage);
|
||||
} else {
|
||||
console.log('Help file opened successfully');
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Exception when opening file:', err);
|
||||
}
|
||||
} else {
|
||||
console.error('Help file not found at:', helpPath);
|
||||
}
|
||||
});
|
||||
|
||||
// 监听从渲染进程发送来的请求
|
||||
ipcMain.on("print-pdf", (event, pdfData) => {
|
||||
// 打印 PDF 数据
|
||||
printPDF(pdfData);
|
||||
});
|
||||
|
||||
function printPDF(pdfData) {
|
||||
// 使用 Electron 的打印对话框或默认打印机进行打印
|
||||
// 创建一个隐藏的webview以加载PDF数据并进行打印
|
||||
const printWindow = new BrowserWindow({ show: true });
|
||||
const printWindow = new BrowserWindow({ show: true }); // 如果不想显示窗口,改 show: false
|
||||
printWindow.loadURL(pdfData);
|
||||
|
||||
printWindow.webContents.on("did-finish-load", () => {
|
||||
// 使用默认打印机打印PDF
|
||||
printWindow.webContents.print({});
|
||||
console.log("123")
|
||||
//printWindow.close();
|
||||
// printWindow.close(); // 打印通常是异步的,直接关闭可能导致打印失败
|
||||
});
|
||||
}
|
||||
|
||||
ipcMain.on("open-design-page", (event, arg, type) => {
|
||||
arg = arg ? arg : 'empty'
|
||||
type = type ? type : '1'
|
||||
mainWindow.loadURL(`file://${__dirname}/design${type}.html?file=${arg}&type=${type}`);
|
||||
});
|
||||
|
||||
ipcMain.on("open-first-page", (event, arg) => {
|
||||
mainWindow.loadURL(`file://${__dirname}/index.html`);
|
||||
});
|
||||
|
||||
ipcMain.on("run-close", (event, arg) => {
|
||||
close_flag = 1;
|
||||
app.quit();
|
||||
@@ -120,26 +207,18 @@ function createWindow() {
|
||||
require("@electron/remote/main").enable(mainWindow.webContents);
|
||||
}
|
||||
|
||||
// This method will be called when Electron has finished
|
||||
// initialization and is ready to create browser windows.
|
||||
// Some APIs can only be used after this event occurs.
|
||||
app.commandLine.appendSwitch("no-sandbox");
|
||||
|
||||
app.whenReady().then(() => {
|
||||
createWindow();
|
||||
require("@electron/remote/main").initialize();
|
||||
// initialize 已经在顶部调用过,这里不需要重复调用,但保留也无害
|
||||
// require("@electron/remote/main").initialize();
|
||||
|
||||
app.on("activate", function () {
|
||||
// On macOS it's common to re-create a window in the app when the
|
||||
// dock icon is clicked and there are no other windows open.
|
||||
if (BrowserWindow.getAllWindows().length === 0) createWindow();
|
||||
});
|
||||
});
|
||||
|
||||
// Quit when all windows are closed, except on macOS. There, it's common
|
||||
// for applications and their menu bar to stay active until the user quits
|
||||
// explicitly with Cmd + Q.
|
||||
app.on("window-all-closed", function () {
|
||||
if (process.platform !== "darwin") app.quit();
|
||||
});
|
||||
|
||||
// In this file you can include the rest of your app's specific main process
|
||||
// code. You can also put them in separate files and require them here.
|
||||
});
|
||||
Reference in New Issue
Block a user