1
0
forked from 0ad/0ad

Compare commits

...

2 Commits

Author SHA1 Message Date
c1132daeab Make progressDialog return a promise
This way the continuation hasn't to be passed as argument.
2024-09-23 17:48:51 +02:00
f2e260a251 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.
2024-09-23 17:45:11 +02:00
5 changed files with 51 additions and 36 deletions

View File

@ -19,7 +19,7 @@ class ColorMixer
this.setup(color); this.setup(color);
} }
setup(color) async setup(color)
{ {
Engine.GetGUIObjectByName("titleBar").caption = translate("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"); 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; this.panel.size = "50%-" + lRDiff + " 50%-" + uDDiff + " 50%+" + lRDiff + " 50%+" + uDDiff;
const button = []; const button = [];
setButtonCaptionsAndVisibitily(button, this.captions, cancelHotkey, "cmButton"); const closePromise =
setButtonCaptionsAndVisibility(button, this.captions, cancelHotkey, "cmButton");
distributeButtonsHorizontally(button, this.captions); distributeButtonsHorizontally(button, this.captions);
const c = color.split(" "); const c = color.split(" ");
@ -67,9 +68,11 @@ class ColorMixer
// Update return color on cancel to prevent malformed values from initial input. // Update return color on cancel to prevent malformed values from initial input.
color = this.color.join(" "); color = this.color.join(" ");
cancelHotkey.onPress = () => { Engine.PopGuiPage(color); }; const buttonIndex = await closePromise;
button[0].onPress = () => { Engine.PopGuiPage(color); }; if (buttonIndex === 0)
button[1].onPress = () => { Engine.PopGuiPage(this.color.join(" ")); }; Engine.PopGuiPage(color);
else
Engine.PopGuiPage(this.color.join(" "));
} }
updateFromSlider(index) 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) => { return new Promise(resolve => {
button[i] = Engine.GetGUIObjectByName(name + (i + 1)); captions.forEach((caption, i) => {
button[i].caption = caption; button[i] = Engine.GetGUIObjectByName(name + (i + 1));
button[i].hidden = false; button[i].caption = caption;
button[i].onPress = () => { button[i].hidden = false;
Engine.PopGuiPage(i); button[i].onPress = resolve.bind(null, i);
};
if (i == 0) if (i == 0)
cancelHotkey.onPress = button[i].onPress; cancelHotkey.onPress = button[i].onPress;
});
}); });
} }

View File

@ -131,17 +131,19 @@ var g_ModIOState = {
} }
}; };
function init(data) async function init(data)
{ {
progressDialog( const promise = progressDialog(
translate("Initializing mod.io interface."), translate("Initializing mod.io interface."),
translate("Initializing"), translate("Initializing"),
false, false,
translate("Cancel"), translate("Cancel"));
closePage);
g_Failure = false; g_Failure = false;
Engine.ModIoStartGetGameId(); Engine.ModIoStartGetGameId();
await promise;
closePage();
} }
function onTick() function onTick()
@ -242,44 +244,48 @@ function cancelModListUpdate()
Engine.GetGUIObjectByName('refreshButton').enabled = true; Engine.GetGUIObjectByName('refreshButton').enabled = true;
} }
function updateModList() async function updateModList()
{ {
clearModList(); clearModList();
Engine.GetGUIObjectByName("refreshButton").enabled = false; Engine.GetGUIObjectByName("refreshButton").enabled = false;
progressDialog( const promise = progressDialog(
translate("Fetching and updating list of available mods."), translate("Fetching and updating list of available mods."),
translate("Updating"), translate("Updating"),
false, false,
translate("Cancel Update"), translate("Cancel Update"));
cancelModListUpdate);
g_Failure = false; g_Failure = false;
g_RequestCancelled = false; g_RequestCancelled = false;
Engine.ModIoStartListMods(); Engine.ModIoStartListMods();
await promise;
cancelModListUpdate();
} }
function downloadMod() async function downloadMod()
{ {
let selected = selectedModIndex(); let selected = selectedModIndex();
if (isSelectedModInvalid(selected)) if (isSelectedModInvalid(selected))
return; return;
progressDialog( const promise = progressDialog(
sprintf(translate("Downloading “%(modname)s”"), { sprintf(translate("Downloading “%(modname)s”"), {
"modname": g_ModsAvailableOnline[selected].name "modname": g_ModsAvailableOnline[selected].name
}), }),
translate("Downloading"), translate("Downloading"),
true, true,
translate("Cancel Download"), translate("Cancel Download"));
() => { Engine.GetGUIObjectByName("downloadButton").enabled = true; });
Engine.GetGUIObjectByName("downloadButton").enabled = false; Engine.GetGUIObjectByName("downloadButton").enabled = false;
g_Failure = false; g_Failure = false;
g_RequestCancelled = false; g_RequestCancelled = false;
Engine.ModIoStartDownloadMod(selected); Engine.ModIoStartDownloadMod(selected);
await promise;
Engine.GetGUIObjectByName("downloadButton").enabled = true;
} }
function cancelRequest() function cancelRequest()
@ -304,7 +310,7 @@ function showErrorMessageBox(caption, title, buttonCaptions)
return messageBox(500, 250, caption, title, buttonCaptions); return messageBox(500, 250, caption, title, buttonCaptions);
} }
function progressDialog(dialogCaption, dialogTitle, showProgressBar, buttonCaption, buttonAction) async function progressDialog(dialogCaption, dialogTitle, showProgressBar, buttonCaption)
{ {
Engine.GetGUIObjectByName("downloadDialog_title").caption = dialogTitle; Engine.GetGUIObjectByName("downloadDialog_title").caption = dialogTitle;
@ -318,13 +324,14 @@ function progressDialog(dialogCaption, dialogTitle, showProgressBar, buttonCapti
Engine.GetGUIObjectByName("downloadDialog_progress").hidden = !showProgressBar; Engine.GetGUIObjectByName("downloadDialog_progress").hidden = !showProgressBar;
Engine.GetGUIObjectByName("downloadDialog_status").hidden = !showProgressBar; Engine.GetGUIObjectByName("downloadDialog_status").hidden = !showProgressBar;
let downloadDialog_button = Engine.GetGUIObjectByName("downloadDialog_button");
downloadDialog_button.caption = buttonCaption;
downloadDialog_button.onPress = () => { cancelRequest(); buttonAction(); };
Engine.GetGUIObjectByName("downloadDialog").hidden = false; Engine.GetGUIObjectByName("downloadDialog").hidden = false;
g_RequestStartTime = Date.now(); g_RequestStartTime = Date.now();
const downloadDialog_button = Engine.GetGUIObjectByName("downloadDialog_button");
downloadDialog_button.caption = buttonCaption;
await new Promise(resolve => {downloadDialog_button.onPress = resolve;});
cancelRequest();
} }
/* /*

View File

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

View File

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