looper/backends/ui/imgui/file_browser_osx.mm

53 lines
1.9 KiB
Text
Raw Permalink Normal View History

#include <Foundation/Foundation.h>
#include <Cocoa/Cocoa.h>
#include <UniformTypeIdentifiers/UTCoreTypes.h>
#include <vector>
#include <string>
extern "C++" {
UTType *GetUTIFromExtension(NSString *extension) {
return [UTType typeWithFilenameExtension:extension];
}
NSMutableArray *ConvertExtensions(std::vector<std::string> input) {
NSMutableArray *arr = [[[NSMutableArray alloc] init] autorelease];
for (auto &str : input) {
UTType *maybeUTI = GetUTIFromExtension([NSString stringWithUTF8String:str.c_str()]);
if (maybeUTI != nil) {
[arr addObject:maybeUTI];
}
}
return arr;
}
const char *OpenDialog(std::vector<std::string> fileTypes, std::string initialDirectory) {
NSMutableArray *arr = ConvertExtensions(fileTypes);
NSOpenPanel* openFileDialog = [[[NSOpenPanel alloc] init] autorelease];
[openFileDialog setCanChooseFiles:YES];
[openFileDialog setCanChooseDirectories:NO];
[openFileDialog setAllowsMultipleSelection:NO];
[openFileDialog setAllowedContentTypes:arr];
[openFileDialog setDirectoryURL:[NSURL fileURLWithPath:[NSString stringWithUTF8String:initialDirectory.c_str()]]];
NSModalResponse response = [openFileDialog runModal];
if (response == NSModalResponseOK) {
return [[(NSURL*)[[openFileDialog URLs] objectAtIndex:0] path] UTF8String];
} else {
return nullptr;
}
}
const char *SaveDialog(std::vector<std::string> fileTypes, std::string initialDirectory) {
NSSavePanel *panel = [[[NSSavePanel alloc] init] autorelease];
[panel setCanCreateDirectories:YES];
NSMutableArray *arr = ConvertExtensions(fileTypes);
[panel setAllowedContentTypes:arr];
[panel setDirectoryURL:[NSURL fileURLWithPath:[NSString stringWithUTF8String:initialDirectory.c_str()]]];
NSModalResponse response = [panel runModal];
if (response == NSModalResponseOK) {
return [[[panel URL] path] UTF8String];
} else {
return nullptr;
}
}
}