Skip to content

Instantly share code, notes, and snippets.

@CliftonH
Last active May 9, 2024 22:49
Show Gist options
  • Save CliftonH/60160decd9eac483ec946a8ecbba4bdc to your computer and use it in GitHub Desktop.
Save CliftonH/60160decd9eac483ec946a8ecbba4bdc to your computer and use it in GitHub Desktop.
Electron bug in `dialog.showMessageBox`
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'">
<link href="./styles.css" rel="stylesheet">
<title>Hello World!</title>
</head>
<body>
<button id="parent_button">
With Parent Window
</button>
<br />
<button id="no_parent_button">
No Parent Window
</button>
<!-- You can also require other files to run in this process -->
<script src="./renderer.js"></script>
</body>
</html>
const {app, BrowserWindow, dialog, ipcMain} = require('electron')
const path = require('path')
let mainWindow
function createWindow () {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
mainWindow.loadFile('index.html')
}
function showDialog (_, shouldAttachToMainWindow) {
const abortCtrl = new AbortController()
setTimeout(() => abortCtrl.abort(), 3_000)
const parentWindow = shouldAttachToMainWindow ? mainWindow : undefined
dialog.showMessageBox(
// On MacOS if parentWindow is undefined this will behave like showMessageBoxSync
// and will ignore the abort signal. Windows will dismiss the dialog
parentWindow,
{
message: 'This should dismiss in 3 seconds',
signal: abortCtrl.signal
}
)
}
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
ipcMain.on('show-dialog', showDialog)
})
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
{
"name": "therapeutic-garden-face-zac1g",
"productName": "therapeutic-garden-face-zac1g",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "chensley",
"scripts": {
"start": "electron ."
},
"dependencies": {},
"devDependencies": {
"electron": "30.0.2"
}
}
const { contextBridge, ipcRenderer } = require('electron')
contextBridge.exposeInMainWorld('electronAPI', {
showDialog: (hasParent) => ipcRenderer.send('show-dialog', hasParent)
})
document.getElementById('parent_button').onclick = () => {
window.electronAPI.showDialog(true)
}
document.getElementById('no_parent_button').onclick = () => {
window.electronAPI.showDialog(false)
}
/* styles.css */
/* Add styles here to customize the appearance of your app */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment