Emscripten: Support Custom Message Boxes (#12583)

* Allow custom message boxes with colors and multiple buttons to work if Asyncify is enabled
* Keep old functionality of using alert when Asyncify is not available
* Update testmessage to allow for setting random colors as the color scheme of the message box
This commit is contained in:
Temdog007
2025-03-20 16:33:06 -07:00
committed by GitHub
parent 54f5b73333
commit 581b614291
3 changed files with 224 additions and 42 deletions

View File

@@ -36,6 +36,7 @@ quit(int rc)
static int SDLCALL
button_messagebox(void *eventNumber)
{
int i;
const SDL_MessageBoxButtonData buttons[] = {
{ SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT,
0,
@@ -43,52 +44,78 @@ button_messagebox(void *eventNumber)
{ SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT,
1,
"Cancel" },
{ 0,
2,
"Retry" }
};
SDL_MessageBoxData data = {
SDL_MESSAGEBOX_INFORMATION,
NULL, /* no parent window */
"Custom MessageBox",
"This is a custom messagebox",
2,
sizeof(buttons) / sizeof(SDL_MessageBoxButtonData),
NULL, /* buttons */
NULL /* Default color scheme */
};
int button = -1;
int success = 0;
data.buttons = buttons;
if (eventNumber) {
data.message = "This is a custom messagebox from a background thread.";
}
for (i = 0; ; ++i) {
SDL_MessageBoxColorScheme colorScheme;
if (i != 0) {
int j;
for (j = 0; j < SDL_MESSAGEBOX_COLOR_COUNT; ++j) {
colorScheme.colors[j].r = SDL_rand(256);
colorScheme.colors[j].g = SDL_rand(256);
colorScheme.colors[j].b = SDL_rand(256);
}
data.colorScheme = &colorScheme;
} else {
data.colorScheme = NULL;
}
int button = -1;
data.buttons = buttons;
if (eventNumber) {
data.message = "This is a custom messagebox from a background thread.";
}
if (!SDL_ShowMessageBox(&data, &button)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError());
if (eventNumber) {
SDL_Event event;
event.type = (Uint32)(intptr_t)eventNumber;
SDL_PushEvent(&event);
return 1;
} else {
quit(2);
}
}
const char* text;
if (button == 1) {
text = "Cancel";
} else if (button == 2) {
text = "Retry";
} else {
text = "OK";
}
SDL_Log("Pressed button: %d, %s", button, button == -1 ? "[closed]" : text);
success = SDL_ShowMessageBox(&data, &button);
if (success == -1) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s", SDL_GetError());
if (eventNumber) {
SDL_Event event;
event.type = (Uint32)(intptr_t)eventNumber;
SDL_PushEvent(&event);
return 1;
} else {
quit(2);
}
}
SDL_Log("Pressed button: %d, %s", button, button == -1 ? "[closed]" : button == 1 ? "Cancel"
: "OK");
if (eventNumber) {
SDL_Event event;
event.type = (Uint32)(intptr_t)eventNumber;
SDL_PushEvent(&event);
if (button == 2) {
continue;
}
return 0;
}
return 0;
}
int main(int argc, char *argv[])
{
int success;
bool success;
SDLTest_CommonState *state;
/* Initialize test framework */