FINAL FIX: Proper print preview and file loading with correct DOM timing

Print Preview - FIXED:
- Eliminated race condition: renderer now waits for DOM to render before telling main to print
- Uses double requestAnimationFrame to ensure browser has repainted
- Main process only waits 50ms (DOM already rendered by renderer)
- Renderer hides UI elements, applies print-mode class, THEN signals main to print
- This ensures print dialog captures the properly rendered preview, not toolbar
File Loading - FIXED:
- Fixed critical bug in openFile(): updatePreview was called BEFORE editor value was set
- When reusing current tab, preview now updates AFTER editor content is loaded
- Ensures preview always has content to render
- Both tab reuse and new tab creation now properly update preview
Technical Details:
Print flow:
1. User clicks Print Preview
2. Menu sends 'print-preview' to renderer
3. Renderer: hide UI, set print-mode class
4. Renderer: requestAnimationFrame x2 (waits for repaints)
5. Renderer: sends 'do-print' to main (DOM fully rendered)
6. Main: small 50ms delay, then prints
7. Result: prints preview content, not toolbar!
File loading flow:
1. User double-clicks .md file (or file association)
2. Main process stores file path
3. Renderer signals ready after theme loads
4. Main sends file-opened message
5. Renderer openFile() called
6. Editor value set
7. THEN updatePreview() called (content exists)
8. Preview renders correctly!
🤖 Generated with [Claude Code](https://claude.com/claude-code)
This commit is contained in:
2025-10-26 14:36:05 +05:30
parent bd94b9fa81
commit 7dc1cc048e
2 changed files with 58 additions and 62 deletions
+14 -18
View File
@@ -1166,37 +1166,33 @@ ipcMain.on('set-current-file', (event, filePath) => {
currentFile = filePath;
});
// Handle print preview
// Handle print preview - send signal to renderer to prepare
ipcMain.on('print-preview', (event) => {
if (mainWindow) {
// Prepare for printing preview only (black text, no colors)
mainWindow.webContents.send('prepare-print-preview', false);
// Give renderer time to prepare, then print
setTimeout(() => {
mainWindow.webContents.print({
silent: false,
printBackground: false,
color: true,
margin: { marginType: 'default' }
});
}, 100);
mainWindow.webContents.send('print-preview');
}
});
// Handle print preview with styles
// Handle print preview with styles - send signal to renderer to prepare
ipcMain.on('print-preview-styled', (event) => {
if (mainWindow) {
// Prepare for printing preview with colors
mainWindow.webContents.send('prepare-print-preview', true);
// Give renderer time to prepare, then print
mainWindow.webContents.send('print-preview-styled');
}
});
// Handle actual printing when renderer is ready
ipcMain.on('do-print', (event, { withStyles }) => {
if (mainWindow) {
// Renderer has already hidden UI and prepared the page
// Small delay to ensure everything is rendered
setTimeout(() => {
mainWindow.webContents.print({
silent: false,
printBackground: true,
printBackground: withStyles,
color: true,
margin: { marginType: 'default' }
});
}, 100);
}, 50);
}
});