优化全屏和无边框窗口设置,改进触摸事件支持注入逻辑,清理代码格式
This commit is contained in:
@@ -61,6 +61,8 @@ function createWindow() {
|
|||||||
mainWindow = new BrowserWindow({
|
mainWindow = new BrowserWindow({
|
||||||
width: 1080,
|
width: 1080,
|
||||||
height: 1920,
|
height: 1920,
|
||||||
|
fullscreen: true, // 全屏
|
||||||
|
frame: false, // 无边框
|
||||||
webPreferences: {
|
webPreferences: {
|
||||||
preload: path.join(__dirname, "preload.js"),
|
preload: path.join(__dirname, "preload.js"),
|
||||||
nodeIntegration: false,
|
nodeIntegration: false,
|
||||||
@@ -71,14 +73,18 @@ function createWindow() {
|
|||||||
// 启用触摸事件支持(Windows 触摸屏)
|
// 启用触摸事件支持(Windows 触摸屏)
|
||||||
mainWindow.webContents.on("did-finish-load", () => {
|
mainWindow.webContents.on("did-finish-load", () => {
|
||||||
// 注入触摸事件支持脚本
|
// 注入触摸事件支持脚本
|
||||||
mainWindow.webContents.executeJavaScript(`
|
mainWindow.webContents
|
||||||
|
.executeJavaScript(
|
||||||
|
`
|
||||||
// 确保触摸事件可用
|
// 确保触摸事件可用
|
||||||
if (navigator.maxTouchPoints === 0) {
|
if (navigator.maxTouchPoints === 0) {
|
||||||
console.log('[Electron] 未检测到触摸设备,将使用鼠标事件模拟');
|
console.log('[Electron] 未检测到触摸设备,将使用鼠标事件模拟');
|
||||||
} else {
|
} else {
|
||||||
console.log('[Electron] 检测到触摸设备,触摸点数:', navigator.maxTouchPoints);
|
console.log('[Electron] 检测到触摸设备,触摸点数:', navigator.maxTouchPoints);
|
||||||
}
|
}
|
||||||
`).catch(err => log.error("Failed to inject touch support:", err));
|
`
|
||||||
|
)
|
||||||
|
.catch((err) => log.error("Failed to inject touch support:", err));
|
||||||
});
|
});
|
||||||
|
|
||||||
const isDev = !app.isPackaged;
|
const isDev = !app.isPackaged;
|
||||||
@@ -96,7 +102,7 @@ function createWindow() {
|
|||||||
ipcMain.handle("fetch-pdf", async (event, pdfUrl) => {
|
ipcMain.handle("fetch-pdf", async (event, pdfUrl) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const protocol = pdfUrl.startsWith("https") ? https : http;
|
const protocol = pdfUrl.startsWith("https") ? https : http;
|
||||||
|
|
||||||
protocol
|
protocol
|
||||||
.get(pdfUrl, (response) => {
|
.get(pdfUrl, (response) => {
|
||||||
// 处理重定向
|
// 处理重定向
|
||||||
@@ -145,21 +151,24 @@ ipcMain.handle("fetch-pdf", async (event, pdfUrl) => {
|
|||||||
ipcMain.handle("print-pdf", async (event, pdfDataOrUrl) => {
|
ipcMain.handle("print-pdf", async (event, pdfDataOrUrl) => {
|
||||||
let printWindow = null;
|
let printWindow = null;
|
||||||
let tempFilePath = null;
|
let tempFilePath = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let pdfPath = pdfDataOrUrl;
|
let pdfPath = pdfDataOrUrl;
|
||||||
|
|
||||||
// 如果是 base64 data URI,转换为临时文件
|
// 如果是 base64 data URI,转换为临时文件
|
||||||
if (pdfDataOrUrl.startsWith("data:application/pdf;base64,")) {
|
if (pdfDataOrUrl.startsWith("data:application/pdf;base64,")) {
|
||||||
const base64Data = pdfDataOrUrl.replace("data:application/pdf;base64,", "");
|
const base64Data = pdfDataOrUrl.replace(
|
||||||
|
"data:application/pdf;base64,",
|
||||||
|
""
|
||||||
|
);
|
||||||
const buffer = Buffer.from(base64Data, "base64");
|
const buffer = Buffer.from(base64Data, "base64");
|
||||||
|
|
||||||
// 创建临时文件
|
// 创建临时文件
|
||||||
tempFilePath = path.join(os.tmpdir(), `print_${Date.now()}.pdf`);
|
tempFilePath = path.join(os.tmpdir(), `print_${Date.now()}.pdf`);
|
||||||
fs.writeFileSync(tempFilePath, buffer);
|
fs.writeFileSync(tempFilePath, buffer);
|
||||||
pdfPath = `file://${tempFilePath}`;
|
pdfPath = `file://${tempFilePath}`;
|
||||||
log.info("PDF written to temp file:", tempFilePath);
|
log.info("PDF written to temp file:", tempFilePath);
|
||||||
}
|
}
|
||||||
// 如果是相对路径(Vite 打包后的资源),转换为绝对路径
|
// 如果是相对路径(Vite 打包后的资源),转换为绝对路径
|
||||||
else if (pdfDataOrUrl.startsWith("/assets/")) {
|
else if (pdfDataOrUrl.startsWith("/assets/")) {
|
||||||
const isDev = !app.isPackaged;
|
const isDev = !app.isPackaged;
|
||||||
@@ -175,7 +184,10 @@ ipcMain.handle("print-pdf", async (event, pdfDataOrUrl) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 如果已经是 file:// 或 http(s):// 协议,保持原样
|
// 如果已经是 file:// 或 http(s):// 协议,保持原样
|
||||||
else if (!pdfDataOrUrl.startsWith("file://") && !pdfDataOrUrl.startsWith("http")) {
|
else if (
|
||||||
|
!pdfDataOrUrl.startsWith("file://") &&
|
||||||
|
!pdfDataOrUrl.startsWith("http")
|
||||||
|
) {
|
||||||
pdfPath = `file://${pdfDataOrUrl}`;
|
pdfPath = `file://${pdfDataOrUrl}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -194,7 +206,7 @@ ipcMain.handle("print-pdf", async (event, pdfDataOrUrl) => {
|
|||||||
log.info("PDF loaded for printing");
|
log.info("PDF loaded for printing");
|
||||||
|
|
||||||
// 等待更长时间确保 PDF 完全渲染
|
// 等待更长时间确保 PDF 完全渲染
|
||||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
await new Promise((resolve) => setTimeout(resolve, 2000));
|
||||||
|
|
||||||
// 静默打印(直接调用系统打印对话框)
|
// 静默打印(直接调用系统打印对话框)
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
@@ -234,7 +246,7 @@ ipcMain.handle("print-pdf", async (event, pdfDataOrUrl) => {
|
|||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.error("Print error:", error);
|
log.error("Print error:", error);
|
||||||
|
|
||||||
// 清理临时文件
|
// 清理临时文件
|
||||||
if (tempFilePath && fs.existsSync(tempFilePath)) {
|
if (tempFilePath && fs.existsSync(tempFilePath)) {
|
||||||
try {
|
try {
|
||||||
@@ -243,7 +255,7 @@ ipcMain.handle("print-pdf", async (event, pdfDataOrUrl) => {
|
|||||||
log.error("Failed to delete temp file:", err);
|
log.error("Failed to delete temp file:", err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (printWindow && !printWindow.isDestroyed()) {
|
if (printWindow && !printWindow.isDestroyed()) {
|
||||||
printWindow.close();
|
printWindow.close();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user