From 72340994da9ce2dddbfd7e1fbe7be9a6697ed8ec Mon Sep 17 00:00:00 2001 From: flashwave Date: Sun, 21 Apr 2024 19:36:58 +0000 Subject: [PATCH] Allow single button dialogs to be dismissed by clicking the background. --- src/mami.js/controls/msgbox.jsx | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/mami.js/controls/msgbox.jsx b/src/mami.js/controls/msgbox.jsx index 5514218..f431a63 100644 --- a/src/mami.js/controls/msgbox.jsx +++ b/src/mami.js/controls/msgbox.jsx @@ -11,9 +11,16 @@ const MamiMessageBoxContainer = function() { if(typeof dialog !== 'object' || dialog === null) throw 'dialog must be a non-null object'; + const backgroundClick = () => { + dialog.clickButtonIndex(0); + }; + try { + if(dialog.buttonCount === 1) + container.addEventListener('click', backgroundClick); return await dialog.show(container); } finally { + container.removeEventListener('click', backgroundClick); dialog.dismiss(); } }, @@ -117,6 +124,14 @@ const MamiMessageBoxDialog = function(info) { return { getElement: () => dialog, + + get buttonCount() { + return buttons.childElementCount; + }, + clickButtonIndex: num => { + buttons.children[num].click(); + }, + show: container => { return new Promise((resolve, reject) => { showResolve = resolve;