添加重启应用功能,支持通过触摸事件触发重启

This commit is contained in:
yuchenglong
2025-12-04 13:50:20 +08:00
parent 1e0822c1a0
commit 34e9c58392
4 changed files with 34 additions and 3 deletions

View File

@@ -368,6 +368,11 @@ app.whenReady().then(() => {
return "stopped";
});
ipcMain.on("restart-app", () => {
app.relaunch();
app.exit(0);
});
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();

View File

@@ -4,7 +4,8 @@ contextBridge.exposeInMainWorld("electronAPI", {
// 获取PDF绕过CORS
fetchPdf: (pdfUrl) => ipcRenderer.invoke("fetch-pdf", pdfUrl),
// 打印PDF
printPdf: (pdfUrl, options) => ipcRenderer.invoke("print-pdf", pdfUrl, options),
printPdf: (pdfUrl, options) =>
ipcRenderer.invoke("print-pdf", pdfUrl, options),
// 获取打印机列表
getPrinters: () => ipcRenderer.invoke("get-printers"),
startIdCardListen: () => ipcRenderer.invoke("start_idcard_listen"),
@@ -18,4 +19,5 @@ contextBridge.exposeInMainWorld("electronAPI", {
ipcRenderer.removeAllListeners("idcard-data");
ipcRenderer.removeAllListeners("idcard-error");
},
restartApp: () => ipcRenderer.send("restart-app"),
});

View File

@@ -1,5 +1,5 @@
import { Routes, Route } from "react-router-dom";
import { useEffect, useState } from "react";
import { useEffect, useState, useRef } from "react";
// import { listen } from "@tauri-apps/api/event";
import "./App.css";
import icon from "./assets/icon.png";
@@ -16,6 +16,7 @@ import UI81 from "./pages/UI81/UI81";
function App() {
const [time, setTime] = useState<string>(() => formatDate(new Date()));
const restartTimerRef = useRef<number | null>(null);
useEffect(() => {
const id = setInterval(() => setTime(formatDate(new Date())), 1000);
@@ -36,13 +37,35 @@ function App() {
};
}, []);
const handleTouchStart = () => {
restartTimerRef.current = window.setTimeout(() => {
window.electronAPI.restartApp();
}, 5000);
};
const handleTouchEnd = () => {
if (restartTimerRef.current) {
clearTimeout(restartTimerRef.current);
restartTimerRef.current = null;
}
};
return (
<div className="app-container">
{/* 全局背景层 */}
<div className="global-background">
{/* 顶部固定区域 - 始终显示 */}
<div className="global-header">
<img className="header-logo" alt="logo" src={icon} />
<img
className="header-logo"
alt="logo"
src={icon}
onMouseDown={handleTouchStart}
onMouseUp={handleTouchEnd}
onMouseLeave={handleTouchEnd}
onTouchStart={handleTouchStart}
onTouchEnd={handleTouchEnd}
/>
<span className="header-title"></span>
</div>
<span className="header-time">{time}</span>

1
src/electron.d.ts vendored
View File

@@ -27,6 +27,7 @@ interface ElectronAPI {
onIdCardError: (callback: (error: any) => void) => void;
log: (level: string, message: any) => void;
removeIdCardListeners: () => void;
restartApp: () => void;
}
interface Window {