diff --git a/main.js b/main.js index 4fbab82..4a9930f 100644 --- a/main.js +++ b/main.js @@ -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 }; diff --git a/renderer/renderer.js b/renderer/renderer.js index d0ebfb7..77bcd19 100644 --- a/renderer/renderer.js +++ b/renderer/renderer.js @@ -140,13 +140,15 @@ } // ===== File Size ===== + function formatBytes(bytes) { + if (bytes < 1024) return bytes + ' B'; + if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB'; + return (bytes / (1024 * 1024)).toFixed(1) + ' MB'; + } + function updateFileSize() { - const bytes = new TextEncoder().encode(editor.value).length; - let display; - if (bytes < 1024) display = bytes + ' B'; - else if (bytes < 1024 * 1024) display = (bytes / 1024).toFixed(1) + ' KB'; - else display = (bytes / (1024 * 1024)).toFixed(1) + ' MB'; - statusSize.textContent = display; + const bytes = new Blob([editor.value]).size; + statusSize.textContent = formatBytes(bytes); statusSizeDivider.classList.add('visible'); } @@ -215,12 +217,6 @@ return filePath.split(/[/\\]/).pop(); } - function formatBytes(bytes) { - if (bytes < 1024) return bytes + ' B'; - if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB'; - return (bytes / (1024 * 1024)).toFixed(1) + ' MB'; - } - async function updateFileStats(filePath) { if (typeof window.electronAPI !== 'undefined' && filePath) { const stats = await window.electronAPI.getFileStats(filePath);