Last active
April 24, 2025 01:15
-
-
Save Jonovono/51c698e039c6578c72068905321ebcac to your computer and use it in GitHub Desktop.
CreateHandle error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { app, BaseWindow, WebContentsView, dialog } = require('electron'); | |
const path = require('path'); | |
let parentWindow, childWindow; | |
let parentView, childView; | |
let quitting = false; | |
// Wait for the app to be ready | |
app.whenReady().then(() => { | |
createWindows(); | |
}); | |
function createWindows() { | |
// Create the parent window | |
parentWindow = new BaseWindow({ | |
width: 800, | |
height: 600, | |
title: 'Parent Window', | |
webPreferences: { | |
nodeIntegration: false, | |
contextIsolation: true, | |
}, | |
}); | |
// Create a WebContentsView for the parent window | |
parentView = new WebContentsView(); | |
parentWindow.contentView.addChildView(parentView); | |
// Set bounds to fill the entire parent window | |
const [width, height] = parentWindow.getSize(); | |
parentView.setBounds({ x: 0, y: 0, width, height }); | |
// Load content in the parent window's view | |
parentView.webContents | |
.loadFile(path.join(__dirname, 'parent.html')) | |
.then(() => { | |
console.log('Parent content loaded'); | |
parentWindow.show(); | |
}) | |
.catch((err) => console.error('Failed to load parent content:', err)); | |
// Create a child window (modal and always on top of parent) | |
childWindow = new BaseWindow({ | |
parent: parentWindow, // Re-enable parent relationship | |
modal: true, | |
width: 400, | |
height: 300, | |
title: 'Child Window', | |
webPreferences: { | |
nodeIntegration: false, | |
contextIsolation: true, | |
}, | |
}); | |
// Create a WebContentsView for the child window | |
childView = new WebContentsView(); | |
childWindow.contentView.addChildView(childView); | |
// Set bounds to fill the entire child window | |
const [childWidth, childHeight] = childWindow.getSize(); | |
childView.setBounds({ x: 0, y: 0, width: childWidth, height: childHeight }); | |
// Load content in the child window's view | |
childView.webContents | |
.loadFile(path.join(__dirname, 'child.html')) | |
.then(() => { | |
console.log('Child content loaded'); | |
// Center child window relative to parent | |
const [parentX, parentY] = parentWindow.getPosition(); | |
const [parentWidth, parentHeight] = parentWindow.getSize(); | |
childWindow.setPosition( | |
Math.round(parentX + (parentWidth - childWidth) / 2), | |
Math.round(parentY + (parentHeight - childHeight) / 2), | |
); | |
childWindow.show(); | |
}) | |
.catch((err) => console.error('Failed to load child content:', err)); | |
// Handle parent window close event (before it's completely closed) | |
parentWindow.on('close', async (e) => { | |
if (!quitting) { | |
e.preventDefault(); | |
const { response } = await dialog.showMessageBox(parentWindow, { | |
type: 'question', | |
buttons: ['Yes', 'No'], | |
title: 'Confirm', | |
message: 'Are you sure you want to quit?', | |
defaultId: 1, | |
cancelId: 1, | |
}); | |
if (response === 0) { | |
quitting = true; | |
cleanupResources(); | |
app.quit(); | |
} | |
} | |
}); | |
// Handle child window close | |
childWindow.on('close', () => { | |
if (!quitting && childView && !childView.webContents.isDestroyed()) { | |
childView.webContents.close(); | |
} | |
}); | |
// Handle parent window resize | |
parentWindow.on('resize', () => { | |
const [newWidth, newHeight] = parentWindow.getSize(); | |
parentView.setBounds({ x: 0, y: 0, width: newWidth, height: newHeight }); | |
}); | |
// Open dev tools (optional) | |
parentView.webContents.openDevTools(); | |
childView.webContents.openDevTools(); | |
} | |
// Clean up resources properly | |
function cleanupResources() { | |
if (childView && !childView.webContents.isDestroyed()) { | |
childView.webContents.close(); | |
childView = null; | |
} | |
if (parentView && !parentView.webContents.isDestroyed()) { | |
parentView.webContents.close(); | |
parentView = null; | |
} | |
if (childWindow && !childWindow.isDestroyed()) { | |
childWindow.destroy(); | |
childWindow = null; | |
} | |
if (parentWindow && !parentWindow.isDestroyed()) { | |
parentWindow.destroy(); | |
parentWindow = null; | |
} | |
} | |
// Handle the will-quit event | |
app.on('will-quit', () => { | |
quitting = true; | |
cleanupResources(); | |
}); | |
// Mac-specific behavior: when all windows are closed but app is still running | |
app.on('window-all-closed', () => { | |
if (process.platform !== 'darwin') { | |
app.quit(); | |
} | |
}); | |
// Mac-specific behavior: recreate windows when clicking on dock icon | |
app.on('activate', () => { | |
if (BaseWindow.getAllWindows().length === 0) { | |
createWindows(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment