diff options
Diffstat (limited to 'contrib/SDL-3.2.8/src/video/uikit/SDL_uikitmessagebox.m')
| -rw-r--r-- | contrib/SDL-3.2.8/src/video/uikit/SDL_uikitmessagebox.m | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/video/uikit/SDL_uikitmessagebox.m b/contrib/SDL-3.2.8/src/video/uikit/SDL_uikitmessagebox.m new file mode 100644 index 0000000..a57b3f7 --- /dev/null +++ b/contrib/SDL-3.2.8/src/video/uikit/SDL_uikitmessagebox.m | |||
| @@ -0,0 +1,154 @@ | |||
| 1 | /* | ||
| 2 | Simple DirectMedia Layer | ||
| 3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
| 4 | |||
| 5 | This software is provided 'as-is', without any express or implied | ||
| 6 | warranty. In no event will the authors be held liable for any damages | ||
| 7 | arising from the use of this software. | ||
| 8 | |||
| 9 | Permission is granted to anyone to use this software for any purpose, | ||
| 10 | including commercial applications, and to alter it and redistribute it | ||
| 11 | freely, subject to the following restrictions: | ||
| 12 | |||
| 13 | 1. The origin of this software must not be misrepresented; you must not | ||
| 14 | claim that you wrote the original software. If you use this software | ||
| 15 | in a product, an acknowledgment in the product documentation would be | ||
| 16 | appreciated but is not required. | ||
| 17 | 2. Altered source versions must be plainly marked as such, and must not be | ||
| 18 | misrepresented as being the original software. | ||
| 19 | 3. This notice may not be removed or altered from any source distribution. | ||
| 20 | */ | ||
| 21 | #include "SDL_internal.h" | ||
| 22 | |||
| 23 | #ifdef SDL_VIDEO_DRIVER_UIKIT | ||
| 24 | |||
| 25 | #include "SDL_uikitvideo.h" | ||
| 26 | #include "SDL_uikitwindow.h" | ||
| 27 | |||
| 28 | // Display a UIKit message box | ||
| 29 | |||
| 30 | static bool s_showingMessageBox = false; | ||
| 31 | |||
| 32 | bool UIKit_ShowingMessageBox(void) | ||
| 33 | { | ||
| 34 | return s_showingMessageBox; | ||
| 35 | } | ||
| 36 | |||
| 37 | static void UIKit_WaitUntilMessageBoxClosed(const SDL_MessageBoxData *messageboxdata, int *clickedindex) | ||
| 38 | { | ||
| 39 | *clickedindex = messageboxdata->numbuttons; | ||
| 40 | |||
| 41 | @autoreleasepool { | ||
| 42 | // Run the main event loop until the alert has finished | ||
| 43 | // Note that this needs to be done on the main thread | ||
| 44 | s_showingMessageBox = true; | ||
| 45 | while ((*clickedindex) == messageboxdata->numbuttons) { | ||
| 46 | [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; | ||
| 47 | } | ||
| 48 | s_showingMessageBox = false; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | static BOOL UIKit_ShowMessageBoxAlertController(const SDL_MessageBoxData *messageboxdata, int *buttonID) | ||
| 53 | { | ||
| 54 | int i; | ||
| 55 | int __block clickedindex = messageboxdata->numbuttons; | ||
| 56 | UIWindow *window = nil; | ||
| 57 | UIWindow *alertwindow = nil; | ||
| 58 | |||
| 59 | if (![UIAlertController class]) { | ||
| 60 | return NO; | ||
| 61 | } | ||
| 62 | |||
| 63 | UIAlertController *alert; | ||
| 64 | alert = [UIAlertController alertControllerWithTitle:@(messageboxdata->title) | ||
| 65 | message:@(messageboxdata->message) | ||
| 66 | preferredStyle:UIAlertControllerStyleAlert]; | ||
| 67 | |||
| 68 | for (i = 0; i < messageboxdata->numbuttons; i++) { | ||
| 69 | UIAlertAction *action; | ||
| 70 | UIAlertActionStyle style = UIAlertActionStyleDefault; | ||
| 71 | const SDL_MessageBoxButtonData *sdlButton; | ||
| 72 | |||
| 73 | if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT) { | ||
| 74 | sdlButton = &messageboxdata->buttons[messageboxdata->numbuttons - 1 - i]; | ||
| 75 | } else { | ||
| 76 | sdlButton = &messageboxdata->buttons[i]; | ||
| 77 | } | ||
| 78 | |||
| 79 | if (sdlButton->flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) { | ||
| 80 | style = UIAlertActionStyleCancel; | ||
| 81 | } | ||
| 82 | |||
| 83 | action = [UIAlertAction actionWithTitle:@(sdlButton->text) | ||
| 84 | style:style | ||
| 85 | handler:^(UIAlertAction *alertAction) { | ||
| 86 | clickedindex = (int)(sdlButton - messageboxdata->buttons); | ||
| 87 | }]; | ||
| 88 | [alert addAction:action]; | ||
| 89 | |||
| 90 | if (sdlButton->flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) { | ||
| 91 | alert.preferredAction = action; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | if (messageboxdata->window) { | ||
| 96 | SDL_UIKitWindowData *data = (__bridge SDL_UIKitWindowData *)messageboxdata->window->internal; | ||
| 97 | window = data.uiwindow; | ||
| 98 | } | ||
| 99 | |||
| 100 | if (window == nil || window.rootViewController == nil) { | ||
| 101 | #ifdef SDL_PLATFORM_VISIONOS | ||
| 102 | alertwindow = [[UIWindow alloc] init]; | ||
| 103 | #else | ||
| 104 | alertwindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; | ||
| 105 | #endif | ||
| 106 | alertwindow.rootViewController = [UIViewController new]; | ||
| 107 | alertwindow.windowLevel = UIWindowLevelAlert; | ||
| 108 | |||
| 109 | window = alertwindow; | ||
| 110 | |||
| 111 | [alertwindow makeKeyAndVisible]; | ||
| 112 | } | ||
| 113 | |||
| 114 | [window.rootViewController presentViewController:alert animated:YES completion:nil]; | ||
| 115 | UIKit_WaitUntilMessageBoxClosed(messageboxdata, &clickedindex); | ||
| 116 | |||
| 117 | if (alertwindow) { | ||
| 118 | alertwindow.hidden = YES; | ||
| 119 | } | ||
| 120 | |||
| 121 | UIKit_ForceUpdateHomeIndicator(); | ||
| 122 | |||
| 123 | *buttonID = messageboxdata->buttons[clickedindex].buttonID; | ||
| 124 | return YES; | ||
| 125 | } | ||
| 126 | |||
| 127 | static void UIKit_ShowMessageBoxImpl(const SDL_MessageBoxData *messageboxdata, int *buttonID, int *result) | ||
| 128 | { | ||
| 129 | @autoreleasepool { | ||
| 130 | if (UIKit_ShowMessageBoxAlertController(messageboxdata, buttonID)) { | ||
| 131 | *result = true; | ||
| 132 | } else { | ||
| 133 | *result = SDL_SetError("Could not show message box."); | ||
| 134 | } | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | bool UIKit_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonID) | ||
| 139 | { | ||
| 140 | @autoreleasepool { | ||
| 141 | __block int result = true; | ||
| 142 | |||
| 143 | if ([NSThread isMainThread]) { | ||
| 144 | UIKit_ShowMessageBoxImpl(messageboxdata, buttonID, &result); | ||
| 145 | } else { | ||
| 146 | dispatch_sync(dispatch_get_main_queue(), ^{ | ||
| 147 | UIKit_ShowMessageBoxImpl(messageboxdata, buttonID, &result); | ||
| 148 | }); | ||
| 149 | } | ||
| 150 | return result; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | #endif // SDL_VIDEO_DRIVER_UIKIT | ||
