Atlas - SDL_uikitmessagebox.m

Home / ext / SDL / src / video / uikit Lines: 1 | Size: 5535 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 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 30static bool s_showingMessageBox = false; 31 32bool UIKit_ShowingMessageBox(void) 33{ 34 return s_showingMessageBox; 35} 36 37static 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 52static 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 if (@available(iOS 13.0, tvOS 13.0, *)) { 102 UIWindowScene *scene = UIKit_GetActiveWindowScene(); 103 if (scene) { 104 alertwindow = [[UIWindow alloc] initWithWindowScene:scene]; 105 } 106 } 107 if (!alertwindow) { 108#ifdef SDL_PLATFORM_VISIONOS 109 alertwindow = [[UIWindow alloc] init]; 110#else 111 alertwindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; 112#endif 113 } 114 alertwindow.rootViewController = [UIViewController new]; 115 alertwindow.windowLevel = UIWindowLevelAlert; 116 117 window = alertwindow; 118 119 [alertwindow makeKeyAndVisible]; 120 } 121 122 [window.rootViewController presentViewController:alert animated:YES completion:nil]; 123 UIKit_WaitUntilMessageBoxClosed(messageboxdata, &clickedindex); 124 125 if (alertwindow) { 126 alertwindow.hidden = YES; 127 } 128 129 UIKit_ForceUpdateHomeIndicator(); 130 131 *buttonID = messageboxdata->buttons[clickedindex].buttonID; 132 return YES; 133} 134 135typedef struct UIKit_ShowMessageBoxData 136{ 137 const SDL_MessageBoxData *messageboxdata; 138 int *buttonID; 139 bool result; 140} UIKit_ShowMessageBoxData; 141 142static void SDLCALL UIKit_ShowMessageBoxMainThreadCallback(void *userdata) 143{ 144 @autoreleasepool { 145 UIKit_ShowMessageBoxData *data = (UIKit_ShowMessageBoxData *) userdata; 146 data->result = UIKit_ShowMessageBoxAlertController(data->messageboxdata, data->buttonID); 147 } 148} 149 150bool UIKit_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonID) 151{ 152 UIKit_ShowMessageBoxData data = { messageboxdata, buttonID, false }; 153 if (!SDL_RunOnMainThread(UIKit_ShowMessageBoxMainThreadCallback, &data, true)) { 154 return false; 155 } else if (!data.result) { 156 return SDL_SetError("Could not show message box."); 157 } 158 return true; 159} 160 161#endif // SDL_VIDEO_DRIVER_UIKIT 162
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.