Last active
May 9, 2024 22:49
-
-
Save CliftonH/60160decd9eac483ec946a8ecbba4bdc to your computer and use it in GitHub Desktop.
Electron bug in `dialog.showMessageBox`
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
<!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> |
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, 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() | |
}) |
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
{ | |
"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" | |
} | |
} |
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 { contextBridge, ipcRenderer } = require('electron') | |
contextBridge.exposeInMainWorld('electronAPI', { | |
showDialog: (hasParent) => ipcRenderer.send('show-dialog', hasParent) | |
}) |
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
document.getElementById('parent_button').onclick = () => { | |
window.electronAPI.showDialog(true) | |
} | |
document.getElementById('no_parent_button').onclick = () => { | |
window.electronAPI.showDialog(false) | |
} |
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
/* 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