From b6133840304f7ab7dd43c48fb0b10b7c063c9887 Mon Sep 17 00:00:00 2001 From: thzxx Date: Mon, 18 May 2026 13:11:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9Aclose=E8=B6=85?= =?UTF-8?q?=E6=97=B6=E4=BF=9D=E6=8A=A4=E3=80=81=E7=A7=BB=E9=99=A4=E5=86=97?= =?UTF-8?q?=E4=BD=99=E5=8F=98=E9=87=8F=E3=80=81=E4=BC=98=E5=8C=96=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=A4=A7=E5=B0=8F=E8=AE=A1=E7=AE=97=E3=80=81=E5=8E=BB?= =?UTF-8?q?=E9=87=8DformatBytes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.js | 22 ++++++++++++++++------ renderer/renderer.js | 20 ++++++++------------ 2 files changed, 24 insertions(+), 18 deletions(-) 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);