fix: resolve did-finish-load race condition, remove unused import

- Move did-finish-load listener before loadFile() to prevent missing the event
- Remove unused nativeImage import
This commit is contained in:
thzxx
2026-05-18 10:51:38 +08:00
parent 9891feab68
commit c573266670
+10 -11
View File
@@ -1,4 +1,4 @@
const { app, BrowserWindow, dialog, ipcMain, Menu, nativeImage } = require('electron');
const { app, BrowserWindow, dialog, ipcMain, Menu } = require('electron');
const path = require('path');
const fs = require('fs');
@@ -23,6 +23,14 @@ function createWindow() {
show: false
});
// Register did-finish-load BEFORE loadFile to avoid race condition
mainWindow.webContents.on('did-finish-load', () => {
if (pendingFilePath) {
openFile(pendingFilePath);
pendingFilePath = null;
}
});
// Load the index.html
mainWindow.loadFile(path.join(__dirname, 'renderer', 'index.html'));
@@ -261,16 +269,7 @@ app.whenReady().then(() => {
if (fileToOpen) {
pendingFilePath = fileToOpen;
}
// Open pending file once window is fully ready
if (pendingFilePath) {
mainWindow.webContents.on('did-finish-load', () => {
if (pendingFilePath) {
openFile(pendingFilePath);
pendingFilePath = null;
}
});
}
// pendingFilePath will be opened by the did-finish-load handler in createWindow()
});
app.on('window-all-closed', () => {