1
0
forked from 0ad/0ad

Return a promise from setButtonCaptionsAndVisibility

Now It's not hardcoded what is done if a button is pressed. The caller
can decide that.
Previously one had to call this function and then overwrite the on Press
attribute of each button.

Rename "Visibitily" to "Visibility" in setButtonCaptionsAndVisibility.
This commit is contained in:
phosit 2024-08-24 17:15:50 +02:00
parent 668ae8a20e
commit 78ef0c1682
4 changed files with 27 additions and 20 deletions

View File

@ -19,7 +19,7 @@ class ColorMixer
this.setup(color);
}
setup(color)
async setup(color)
{
Engine.GetGUIObjectByName("titleBar").caption = translate("Color");
Engine.GetGUIObjectByName("infoLabel").caption = translate("Move the sliders to change the Red, Green and Blue components of the Color");
@ -31,7 +31,8 @@ class ColorMixer
this.panel.size = "50%-" + lRDiff + " 50%-" + uDDiff + " 50%+" + lRDiff + " 50%+" + uDDiff;
const button = [];
setButtonCaptionsAndVisibitily(button, this.captions, cancelHotkey, "cmButton");
const closePromise =
setButtonCaptionsAndVisibility(button, this.captions, cancelHotkey, "cmButton");
distributeButtonsHorizontally(button, this.captions);
const c = color.split(" ");
@ -67,9 +68,11 @@ class ColorMixer
// Update return color on cancel to prevent malformed values from initial input.
color = this.color.join(" ");
cancelHotkey.onPress = () => { Engine.PopGuiPage(color); };
button[0].onPress = () => { Engine.PopGuiPage(color); };
button[1].onPress = () => { Engine.PopGuiPage(this.color.join(" ")); };
const buttonIndex = await closePromise;
if (buttonIndex === 0)
Engine.PopGuiPage(color);
else
Engine.PopGuiPage(this.color.join(" "));
}
updateFromSlider(index)

View File

@ -19,17 +19,17 @@ function distributeButtonsHorizontally(button, captions)
}
}
function setButtonCaptionsAndVisibitily(button, captions, cancelHotkey, name)
function setButtonCaptionsAndVisibility(button, captions, cancelHotkey, name)
{
captions.forEach((caption, i) => {
button[i] = Engine.GetGUIObjectByName(name + (i + 1));
button[i].caption = caption;
button[i].hidden = false;
button[i].onPress = () => {
Engine.PopGuiPage(i);
};
return new Promise(resolve => {
captions.forEach((caption, i) => {
button[i] = Engine.GetGUIObjectByName(name + (i + 1));
button[i].caption = caption;
button[i].hidden = false;
button[i].onPress = resolve.bind(null, i);
if (i == 0)
cancelHotkey.onPress = button[i].onPress;
if (i == 0)
cancelHotkey.onPress = button[i].onPress;
});
});
}

View File

@ -2,7 +2,7 @@
* Currently limited to at most 3 buttons per message box.
* The convention is to have "cancel" appear first.
*/
function init(data)
async function init(data)
{
// Set title
Engine.GetGUIObjectByName("mbTitleBar").caption = data.title;
@ -15,7 +15,6 @@ function init(data)
// Default behaviour
let mbCancelHotkey = Engine.GetGUIObjectByName("mbCancelHotkey");
mbCancelHotkey.onPress = Engine.PopGuiPage;
// Calculate size
let mbLRDiff = data.width / 2;
@ -25,6 +24,8 @@ function init(data)
let captions = data.buttonCaptions || [translate("OK")];
let mbButton = [];
setButtonCaptionsAndVisibitily(mbButton, captions, mbCancelHotkey, "mbButton");
const closePromise = setButtonCaptionsAndVisibility(mbButton, captions, mbCancelHotkey, "mbButton");
distributeButtonsHorizontally(mbButton, captions);
Engine.PopGuiPage(await closePromise);
}

View File

@ -23,7 +23,7 @@ class TimedConfirmation
this.setup(data);
}
setup(data)
async setup(data)
{
Engine.GetGUIObjectByName("tmcTitleBar").caption = data.title;
@ -46,8 +46,11 @@ class TimedConfirmation
const captions = data.buttonCaptions || [translate("OK")];
const button = [];
setButtonCaptionsAndVisibitily(button, captions, cancelHotkey, "tmcButton");
const closePromise =
setButtonCaptionsAndVisibility(button, captions, cancelHotkey, "tmcButton");
distributeButtonsHorizontally(button, captions);
Engine.PopGuiPage(await closePromise);
}
onTick()