I have a messagebox that will open when the user click close on dashboardWindow (X button top right on windows os)
dashboardWindow.on("close", (event) => {
event.preventDefault();
console.log("before message box");
dialog.showMessageBox(
dashboardWindows,
{
message: "Test",
buttons: ["Default Button", "Cancel Button"],
defaultId: 0, // bound to buttons array
cancelId: 1 // bound to buttons array
},
(response) => {
if (response === 0) {
// bound to buttons array
console.log("Default button clicked.");
} else if (response === 1) {
// bound to buttons array
console.log("Cancel button clicked.");
}
}
);
console.log("after message box");
});
}
The messagebox opened when i close the dashboardWindow but i can't get response === 0
to work. Samehow console.log("after message box");
already run even when there is no click on the buttons. How I can make the response work (return index button on messageBox)?
log on window close
Please refer to the most recent API doc about dialog.showMessageBox: this method returns a Promise object and doesn't make use of a callback function any more, like it used to until Electron v5.x.x.
Returns
Promise<Object>
- resolves with a promise containing the following properties:
response
Number - The index of the clicked button.checkboxChecked
Boolean - The checked state of the checkbox ifcheckboxLabel
was set. Otherwisefalse
.
This should work then (untested in your context though):
dashboardWindow.on("close", (event) => {
event.preventDefault();
console.log("before message box");
dialog.showMessageBox(
dashboardWindows,
{
message: "Test",
buttons: ["Default Button", "Cancel Button"],
defaultId: 0, // bound to buttons array
cancelId: 1 // bound to buttons array
})
.then(result => {
if (result.response === 0) {
// bound to buttons array
console.log("Default button clicked.");
} else if (result.response === 1) {
// bound to buttons array
console.log("Cancel button clicked.");
}
}
);
console.log("after message box");
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With