Forráskód Böngészése

Run the entire Cocoa messagebox function on the main thread.

This fixes bug https://github.com/libsdl-org/SDL/issues/4420
Sam Lantinga 3 éve
szülő
commit
51c61d7cdf
2 módosított fájl, 19 hozzáadás és 10 törlés
  1. 18 9
      src/video/cocoa/SDL_cocoamessagebox.m
  2. 1 1
      test/testmessage.c

+ 18 - 9
src/video/cocoa/SDL_cocoamessagebox.m

@@ -89,10 +89,8 @@
 @end
 
 
-/* Display a Cocoa message box */
-int
-Cocoa_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
-{ @autoreleasepool
+static void
+Cocoa_ShowMessageBoxImpl(const SDL_MessageBoxData *messageboxdata, int *buttonid, int *returnValue)
 {
     Cocoa_RegisterApp();
 
@@ -133,11 +131,8 @@ Cocoa_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
 
     SDLMessageBoxPresenter* presenter = [[[SDLMessageBoxPresenter alloc] initWithParentWindow:messageboxdata->window] autorelease];
 
-    [presenter performSelectorOnMainThread:@selector(showAlert:)
-                                withObject:alert
-                             waitUntilDone:YES];
+    [presenter showAlert:alert];
 
-    int returnValue = 0;
     NSInteger clicked = presenter->clicked;
     if (clicked >= NSAlertFirstButtonReturn) {
         clicked -= NSAlertFirstButtonReturn;
@@ -145,10 +140,24 @@ Cocoa_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
             clicked = messageboxdata->numbuttons - 1 - clicked;
         }
         *buttonid = buttons[clicked].buttonid;
+        *returnValue = 0;
     } 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;
 }}
 

+ 1 - 1
test/testmessage.c

@@ -197,7 +197,7 @@ main(int argc, char *argv[])
 
         success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
                     "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);
         if (success == -1) {
             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());