修复:close超时保护、移除冗余变量、优化文件大小计算、去重formatBytes
This commit is contained in:
@@ -6,7 +6,6 @@ let mainWindow = null;
|
||||
let currentFilePath = null;
|
||||
let pendingFilePath = null;
|
||||
let fileWatcher = null;
|
||||
let isModifiedExternally = false;
|
||||
|
||||
function createWindow() {
|
||||
mainWindow = new BrowserWindow({
|
||||
@@ -39,11 +38,24 @@ function createWindow() {
|
||||
|
||||
mainWindow.setMenu(null);
|
||||
|
||||
// Unsaved changes warning on close
|
||||
// Unsaved changes warning on close (with timeout fallback)
|
||||
mainWindow.on('close', (e) => {
|
||||
mainWindow.webContents.send('window:closing');
|
||||
e.preventDefault();
|
||||
mainWindow.webContents.send('window:confirmClose');
|
||||
try {
|
||||
mainWindow.webContents.send('window:confirmClose');
|
||||
} catch (err) {
|
||||
// Renderer is gone, force close
|
||||
mainWindow.removeAllListeners('close');
|
||||
mainWindow.close();
|
||||
return;
|
||||
}
|
||||
// Safety: if renderer doesn't respond within 5s, force close
|
||||
setTimeout(() => {
|
||||
if (mainWindow && !mainWindow.isDestroyed()) {
|
||||
mainWindow.removeAllListeners('close');
|
||||
mainWindow.close();
|
||||
}
|
||||
}, 5000);
|
||||
});
|
||||
|
||||
mainWindow.on('closed', () => {
|
||||
@@ -56,7 +68,6 @@ function createWindow() {
|
||||
function startWatching(filePath) {
|
||||
stopWatching();
|
||||
try {
|
||||
isModifiedExternally = false;
|
||||
fileWatcher = fs.watch(filePath, (eventType) => {
|
||||
if (eventType === 'change' && mainWindow && !mainWindow.isDestroyed()) {
|
||||
isModifiedExternally = true;
|
||||
@@ -201,7 +212,6 @@ ipcMain.handle('file:reload', async () => {
|
||||
if (!currentFilePath) return { success: false, error: '没有打开的文件' };
|
||||
try {
|
||||
const content = fs.readFileSync(currentFilePath, 'utf-8');
|
||||
isModifiedExternally = false;
|
||||
return { success: true, content, filePath: currentFilePath };
|
||||
} catch (err) {
|
||||
return { success: false, error: err.message };
|
||||
|
||||
Reference in New Issue
Block a user