Run the entire Cocoa messagebox function on the main thread.

This fixes bug https://github.com/libsdl-org/SDL/issues/4420
This commit is contained in:
Sam Lantinga 2021-07-27 14:57:18 -07:00
parent dfd3f30e88
commit 51c61d7cdf
2 changed files with 19 additions and 10 deletions

View File

@ -89,10 +89,8 @@
@end @end
/* Display a Cocoa message box */ static void
int Cocoa_ShowMessageBoxImpl(const SDL_MessageBoxData *messageboxdata, int *buttonid, int *returnValue)
Cocoa_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
{ @autoreleasepool
{ {
Cocoa_RegisterApp(); Cocoa_RegisterApp();
@ -133,11 +131,8 @@ Cocoa_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
SDLMessageBoxPresenter* presenter = [[[SDLMessageBoxPresenter alloc] initWithParentWindow:messageboxdata->window] autorelease]; SDLMessageBoxPresenter* presenter = [[[SDLMessageBoxPresenter alloc] initWithParentWindow:messageboxdata->window] autorelease];
[presenter performSelectorOnMainThread:@selector(showAlert:) [presenter showAlert:alert];
withObject:alert
waitUntilDone:YES];
int returnValue = 0;
NSInteger clicked = presenter->clicked; NSInteger clicked = presenter->clicked;
if (clicked >= NSAlertFirstButtonReturn) { if (clicked >= NSAlertFirstButtonReturn) {
clicked -= NSAlertFirstButtonReturn; clicked -= NSAlertFirstButtonReturn;
@ -145,10 +140,24 @@ Cocoa_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
clicked = messageboxdata->numbuttons - 1 - clicked; clicked = messageboxdata->numbuttons - 1 - clicked;
} }
*buttonid = buttons[clicked].buttonid; *buttonid = buttons[clicked].buttonid;
*returnValue = 0;
} else { } else {
returnValue = SDL_SetError("Did not get a valid `clicked button' id: %ld", (long)clicked); *returnValue = SDL_SetError("Did not get a valid `clicked button' id: %ld", (long)clicked);
}
} }
/* Display a Cocoa message box */
int
Cocoa_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
{ @autoreleasepool
{
__block int returnValue = 0;
if ([NSThread isMainThread]) {
Cocoa_ShowMessageBoxImpl(messageboxdata, buttonid, &returnValue);
} else {
dispatch_sync(dispatch_get_main_queue(), ^{ Cocoa_ShowMessageBoxImpl(messageboxdata, buttonid, &returnValue); });
}
return returnValue; return returnValue;
}} }}

View File

@ -197,7 +197,7 @@ main(int argc, char *argv[])
success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
"Simple MessageBox", "Simple MessageBox",
"This is a simple error MessageBox with a parent window", "This is a simple error MessageBox with a parent window. Press a key or close the window after dismissing this messagebox.",
window); window);
if (success == -1) { if (success == -1) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError()); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());