| 26 | #include <UniformTypeIdentifiers/UniformTypeIdentifiers.h> |
| 27 | |
| 28 | void InitMacDialog(NSSavePanel* dialog, const StringView& initialDirectory, const StringView& filter, const StringView& title) |
| 29 | { |
| 30 | if (initialDirectory.HasChars()) |
| 31 | { |
| 32 | [dialog setDirectoryURL:[NSURL fileURLWithPath:(NSString*)AppleUtils::ToString(initialDirectory) isDirectory:YES]]; |
| 33 | } |
| 34 | if (filter.HasChars()) |
| 35 | { |
| 36 | Array<String> entries; |
| 37 | String(filter).Split('\0', entries); |
| 38 | if (entries.HasItems() && !(entries.Count() == 2 && entries[1] == TEXT("*.*"))) |
| 39 | { |
| 40 | NSMutableArray* fileTypes = [[NSMutableArray alloc] init]; |
| 41 | for (int32 i = 1; i < entries.Count(); i += 2) |
| 42 | { |
| 43 | StringView extension = entries[i]; |
| 44 | if (extension.StartsWith(TEXT("*."))) |
| 45 | extension = extension.Substring(2); |
| 46 | NSString* extensionStr = (NSString*)AppleUtils::ToString(extension); |
| 47 | #if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && defined(__MAC_11_0) && __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_11_0 |
| 48 | UTType* type = [UTType typeWithFilenameExtension:extensionStr]; |
| 49 | [fileTypes addObject:type]; |
| 50 | } |
| 51 | dialog.allowedContentTypes = fileTypes; |
| 52 | #else |
| 53 | [fileTypes addObject:extensionStr]; |
| 54 | } |
| 55 | [dialog setAllowedFileTypes:fileTypes]; |
| 56 | #endif |
| 57 | } |
| 58 | } |
| 59 | if (title.HasChars()) |
| 60 | { |
| 61 | [dialog setMessage:(NSString*)AppleUtils::ToString(title)]; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | bool MacFileSystem::ShowOpenFileDialog(Window* parentWindow, const StringView& initialDirectory, const StringView& filter, bool multiSelect, const StringView& title, Array<String>& filenames) |
| 66 | { |
| 67 | bool result = true; |
| 68 | @autoreleasepool { |
| 69 | NSWindow* focusedWindow = [[NSApplication sharedApplication] keyWindow]; |
| 70 | |
| 71 | NSOpenPanel* dialog = [NSOpenPanel openPanel]; |
| 72 | [dialog setCanChooseFiles:YES]; |
| 73 | [dialog setCanChooseDirectories:NO]; |
| 74 | [dialog setAllowsMultipleSelection:multiSelect ? YES : NO]; |
| 75 | InitMacDialog(dialog, initialDirectory, filter, title); |
| 76 | |
| 77 | if ([dialog runModal] == NSModalResponseOK) |
| 78 | { |
| 79 | if (multiSelect) |
| 80 | { |
| 81 | const NSArray* urls = [dialog URLs]; |
| 82 | for (int32 i = 0; i < [urls count]; i++) |
| 83 | filenames.Add(AppleUtils::ToString((CFStringRef)[[urls objectAtIndex:i] path])); |
| 84 | } |
| 85 | else |
no test coverage detected