9#if defined(YAZE_ENABLE_NFD) && YAZE_ENABLE_NFD
13#if defined(__APPLE__) && defined(__MACH__)
15#include <Foundation/Foundation.h>
16#include <TargetConditionals.h>
18#import <CoreText/CoreText.h>
20#if TARGET_IPHONE_SIMULATOR == 1 || TARGET_OS_IPHONE == 1
22#import <UIKit/UIKit.h>
23#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
28static std::string selectedFile;
30void ShowOpenFileDialogImpl(
void (^completionHandler)(std::string)) {
32 [appDelegate PresentDocumentPickerWithCompletionHandler:^(NSString *filePath) {
33 selectedFile = std::string([filePath UTF8String]);
34 completionHandler(selectedFile);
38std::string ShowOpenFileDialogSync() {
39 __block std::string result;
41 ShowOpenFileDialogImpl(^(std::string filePath) {
54 const std::string &folder) {
59 const std::string &folder) {
64 NSBundle* bundle = [NSBundle mainBundle];
65 NSString* resourceDirectoryPath = [bundle bundlePath];
66 NSString* path = [resourceDirectoryPath stringByAppendingString:
@"/"];
67 return [path UTF8String];
70#elif TARGET_OS_MAC == 1
73#import <Cocoa/Cocoa.h>
74#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
77 NSOpenPanel* openPanel = [NSOpenPanel openPanel];
78 [openPanel setCanChooseFiles:YES];
79 [openPanel setCanChooseDirectories:NO];
80 [openPanel setAllowsMultipleSelection:NO];
82 if ([openPanel runModal] == NSModalResponseOK) {
83 NSURL* url = [[openPanel URLs] objectAtIndex:0];
84 NSString* path = [url path];
85 return std::string([path UTF8String]);
92 const std::string& default_extension) {
93 NSSavePanel* savePanel = [NSSavePanel savePanel];
95 if (!default_name.empty()) {
96 [savePanel setNameFieldStringValue:[NSString stringWithUTF8String:default_name.c_str()]];
99 if (!default_extension.empty()) {
100 NSString* ext = [NSString stringWithUTF8String:default_extension.c_str()];
101 [savePanel setAllowedFileTypes:@[ext]];
104 if ([savePanel runModal] == NSModalResponseOK) {
105 NSURL* url = [savePanel URL];
106 NSString* path = [url path];
107 return std::string([path UTF8String]);
116 return ShowOpenFileDialogNFD();
118 return ShowOpenFileDialogBespoke();
124 return ShowOpenFolderDialogNFD();
126 return ShowOpenFolderDialogBespoke();
131 const std::string& default_extension) {
133 return ShowSaveFileDialogNFD(default_name, default_extension);
135 return ShowSaveFileDialogBespoke(default_name, default_extension);
141#if defined(YAZE_ENABLE_NFD) && YAZE_ENABLE_NFD
143 nfdu8char_t *out_path = NULL;
144 nfdu8filteritem_t filters[1] = {{
"Rom File",
"sfc,smc"}};
145 nfdopendialogu8args_t args = {0};
146 args.filterList = filters;
147 args.filterCount = 1;
149 nfdresult_t result = NFD_OpenDialogU8_With(&out_path, &args);
150 if (result == NFD_OKAY) {
151 std::string file_path(out_path);
152 NFD_FreePath(out_path);
155 }
else if (result == NFD_CANCEL) {
163 return ShowOpenFileDialogBespoke();
168#if defined(YAZE_ENABLE_NFD) && YAZE_ENABLE_NFD
170 nfdu8char_t *out_path = NULL;
171 nfdresult_t result = NFD_PickFolderU8(&out_path, NULL);
173 if (result == NFD_OKAY) {
174 std::string folder_path(out_path);
175 NFD_FreePath(out_path);
178 }
else if (result == NFD_CANCEL) {
186 return ShowOpenFolderDialogBespoke();
191 const std::string& default_extension) {
192#if defined(YAZE_ENABLE_NFD) && YAZE_ENABLE_NFD
194 nfdu8char_t *out_path = NULL;
196 nfdsavedialogu8args_t args = {0};
197 if (!default_extension.empty()) {
199 static nfdu8filteritem_t filters[3] = {
200 {
"Theme File",
"theme"},
201 {
"Project File",
"yaze"},
202 {
"ROM File",
"sfc,smc"}
205 if (default_extension ==
"theme") {
206 args.filterList = &filters[0];
207 args.filterCount = 1;
208 }
else if (default_extension ==
"yaze") {
209 args.filterList = &filters[1];
210 args.filterCount = 1;
211 }
else if (default_extension ==
"sfc" || default_extension ==
"smc") {
212 args.filterList = &filters[2];
213 args.filterCount = 1;
217 if (!default_name.empty()) {
218 args.defaultName = default_name.c_str();
221 nfdresult_t result = NFD_SaveDialogU8_With(&out_path, &args);
222 if (result == NFD_OKAY) {
223 std::string file_path(out_path);
224 NFD_FreePath(out_path);
227 }
else if (result == NFD_CANCEL) {
235 return ShowSaveFileDialogBespoke(default_name, default_extension);
240 NSOpenPanel* openPanel = [NSOpenPanel openPanel];
241 [openPanel setCanChooseFiles:NO];
242 [openPanel setCanChooseDirectories:YES];
243 [openPanel setAllowsMultipleSelection:NO];
245 if ([openPanel runModal] == NSModalResponseOK) {
246 NSURL* url = [[openPanel URLs] objectAtIndex:0];
247 NSString* path = [url path];
248 return std::string([path UTF8String]);
255 const std::string& folder) {
256 std::vector<std::string> filenames;
257 NSFileManager* fileManager = [NSFileManager defaultManager];
258 NSDirectoryEnumerator* enumerator =
259 [fileManager enumeratorAtPath:[NSString stringWithUTF8String:folder.c_str()]];
261 while (file = [enumerator nextObject]) {
262 if ([file hasPrefix:
@"."]) {
265 filenames.push_back(std::string([file UTF8String]));
271 const std::string& folder) {
272 std::vector<std::string> subdirectories;
273 NSFileManager* fileManager = [NSFileManager defaultManager];
274 NSDirectoryEnumerator* enumerator =
275 [fileManager enumeratorAtPath:[NSString stringWithUTF8String:folder.c_str()]];
277 while (file = [enumerator nextObject]) {
278 if ([file hasPrefix:
@"."]) {
283 [NSString stringWithFormat:
@"%@/%@", [NSString stringWithUTF8String:folder.c_str()], file];
284 [fileManager fileExistsAtPath:path isDirectory:&isDirectory];
286 subdirectories.push_back(std::string([file UTF8String]));
289 return subdirectories;
293 NSBundle* bundle = [NSBundle mainBundle];
294 NSString* resourceDirectoryPath = [bundle bundlePath];
295 NSString* path = [resourceDirectoryPath stringByAppendingString:
@"/"];
296 return [path UTF8String];
static std::string ShowSaveFileDialogBespoke(const std::string &default_name="", const std::string &default_extension="")
static std::string ShowOpenFileDialogBespoke()
static std::string ShowSaveFileDialogNFD(const std::string &default_name="", const std::string &default_extension="")
static std::string ShowOpenFolderDialogNFD()
static std::string ShowSaveFileDialog(const std::string &default_name="", const std::string &default_extension="")
ShowSaveFileDialog opens a save file dialog and returns the selected filepath. Uses global feature fl...
static std::string ShowOpenFileDialog()
ShowOpenFileDialog opens a file dialog and returns the selected filepath. Uses global feature flag to...
static std::string ShowOpenFolderDialog()
ShowOpenFolderDialog opens a file dialog and returns the selected folder path. Uses global feature fl...
static std::vector< std::string > GetFilesInFolder(const std::string &folder_path)
static std::vector< std::string > GetSubdirectoriesInFolder(const std::string &folder_path)
static std::string ShowOpenFolderDialogBespoke()
static std::string ShowOpenFileDialogNFD()
std::string GetBundleResourcePath()
GetBundleResourcePath returns the path to the bundle resource directory. Specific to MacOS.