From 2fbca008c777fe49698b1264d9648899bc2eef90 Mon Sep 17 00:00:00 2001 From: scawful Date: Fri, 2 Aug 2024 15:34:49 -0400 Subject: [PATCH] add iOS UIDocumentPicker delegation for the AppDelegate --- src/app/core/platform/file_dialog.h | 5 +-- src/app/core/platform/file_dialog.mm | 61 ++++++++++++++++++---------- 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/src/app/core/platform/file_dialog.h b/src/app/core/platform/file_dialog.h index 8d7257c2..850faeea 100644 --- a/src/app/core/platform/file_dialog.h +++ b/src/app/core/platform/file_dialog.h @@ -50,8 +50,7 @@ class FileDialogWrapper { #include #include -#ifdef TARGET_OS_MAC -// Other kinds of Mac OS +#if TARGET_OS_MAC == 1 class FileDialogWrapper { public: @@ -63,7 +62,7 @@ class FileDialogWrapper { const std::string& folder_path); }; -#elif TARGET_OS_IPHONE +#elif TARGET_OS_IPHONE == 1 // iOS class FileDialogWrapper { diff --git a/src/app/core/platform/file_dialog.mm b/src/app/core/platform/file_dialog.mm index a81a47cb..e73a2fce 100644 --- a/src/app/core/platform/file_dialog.mm +++ b/src/app/core/platform/file_dialog.mm @@ -1,8 +1,8 @@ +#include "app/core/platform/file_dialog.h" #include #include - -#include "app/core/platform/file_dialog.h" +#include "imgui/imgui.h" #if defined(__APPLE__) && defined(__MACH__) /* Apple OSX and iOS (Darwin). */ @@ -10,32 +10,51 @@ #import -#if TARGET_IPHONE_SIMULATOR == 1 +#if TARGET_IPHONE_SIMULATOR == 1 || TARGET_OS_IPHONE == 1 /* iOS in Xcode simulator */ -std::string FileDialogWrapper::ShowOpenFileDialog() { return ""; } +#import +#import + +#include "app/core/platform/app_delegate.h" + +namespace { +static std::string selectedFile; + +void ShowOpenFileDialogImpl(void (^completionHandler)(std::string)) { + AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; + [appDelegate presentDocumentPickerWithCompletionHandler:^(NSString *filePath) { + selectedFile = std::string([filePath UTF8String]); + completionHandler(selectedFile); + }]; +} + +std::string ShowOpenFileDialogSync() { + __block std::string result; + + ShowOpenFileDialogImpl(^(std::string filePath) { + result = filePath; + }); + + // Check if the documentPicker is done +// while (result.empty()) { +// [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; +// } + + return result; +} +} + +std::string FileDialogWrapper::ShowOpenFileDialog() { + return ShowOpenFileDialogSync(); +} std::string FileDialogWrapper::ShowOpenFolderDialog() { return ""; } -std::vector FileDialogWrapper::GetFilesInFolder(const std::string& folder) { +std::vector FileDialogWrapper::GetFilesInFolder(const std::string &folder) { return {}; } -std::vector FileDialogWrapper::GetSubdirectoriesInFolder(const std::string& folder) { - return {}; -} - -#elif TARGET_OS_IPHONE == 1 -/* iOS */ - -std::string FileDialogWrapper::ShowOpenFileDialog() { return ""; } - -std::string FileDialogWrapper::ShowOpenFolderDialog() { return ""; } - -std::vector FileDialogWrapper::GetFilesInFolder(const std::string& folder) { - return {}; -} - -std::vector FileDialogWrapper::GetSubdirectoriesInFolder(const std::string& folder) { +std::vector FileDialogWrapper::GetSubdirectoriesInFolder(const std::string &folder) { return {}; }