Skip to content

Commit

Permalink
AppleFileSystem: a few improvements to FileDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Jun 1, 2024
1 parent 58c8e77 commit 8a15197
Showing 1 changed file with 57 additions and 51 deletions.
108 changes: 57 additions & 51 deletions Platforms/Apple/src/AppleFileSystem.mm
Original file line number Diff line number Diff line change
Expand Up @@ -84,68 +84,74 @@
{
__block std::string Path;

dispatch_sync(dispatch_get_main_queue(), ^{
NSSavePanel* Panel = nil;

if (DialogAttribs.Type == FILE_DIALOG_TYPE_OPEN)
auto FileDialogBlock = ^{
@autoreleasepool
{
NSOpenPanel* OpenPanel = [NSOpenPanel openPanel];
OpenPanel.canChooseFiles = YES;
OpenPanel.canChooseDirectories = NO;
OpenPanel.allowsMultipleSelection = (DialogAttribs.Flags & FILE_DIALOG_FLAG_FILE_MUST_EXIST);
Panel = OpenPanel;
}

if (DialogAttribs.Type == FILE_DIALOG_TYPE_SAVE)
{
NSSavePanel* SavePanel = [NSSavePanel savePanel];
SavePanel.canCreateDirectories = YES;
if (DialogAttribs.Flags & FILE_DIALOG_FLAG_OVERWRITE_PROMPT)
SavePanel.treatsFilePackagesAsDirectories = YES;
Panel = SavePanel;
}
NSSavePanel* Panel = nil;

if (DialogAttribs.Title)
{
NSString* Title = [NSString stringWithUTF8String:DialogAttribs.Title];
[Panel setTitle:Title];
}
if (DialogAttribs.Type == FILE_DIALOG_TYPE_OPEN)
{
NSOpenPanel* OpenPanel = [NSOpenPanel openPanel];
OpenPanel.canChooseFiles = YES;
OpenPanel.canChooseDirectories = NO;
Panel = OpenPanel;
}

if (DialogAttribs.Filter)
{
NSString* Filter = [NSString stringWithUTF8String:DialogAttribs.Filter];
NSMutableArray<NSString *> *Extensions = [NSMutableArray array];
NSArray<NSString *> *Components = [Filter componentsSeparatedByString:@"\0"];
for (NSString *Component in Components)
if (DialogAttribs.Type == FILE_DIALOG_TYPE_SAVE)
{
NSSavePanel* SavePanel = [NSSavePanel savePanel];
SavePanel.canCreateDirectories = YES;
Panel = SavePanel;
}

if (DialogAttribs.Title)
{
NSString* Title = [NSString stringWithUTF8String:DialogAttribs.Title];
[Panel setTitle:Title];
}

if (DialogAttribs.Filter)
{
if ([Component hasPrefix:@"*."])
NSString* Filter = [NSString stringWithUTF8String:DialogAttribs.Filter];
NSMutableArray<NSString*>* Extensions = [NSMutableArray array];
NSArray<NSString*>* Components = [Filter componentsSeparatedByString:@"\0"];
for (NSString* Component in Components)
{
NSString *Extension = [Component substringFromIndex:2]; // Remove "*."
if ([Extension length] > 0)
[Extensions addObject:Extension];
if ([Component hasPrefix:@"*."])
{
NSString *Extension = [Component substringFromIndex:2]; // Remove "*."
if ([Extension length] > 0)
[Extensions addObject:Extension];
}
}

[Panel setAllowedFileTypes:Extensions];
}

[Panel setAllowedFileTypes:Extensions];
}
NSModalResponse Response = [Panel runModal];

if (DialogAttribs.Flags & FILE_DIALOG_FLAG_NO_CHANGE_DIR)
{
[Panel setDirectoryURL:[NSURL fileURLWithPath:NSHomeDirectory()]];
if (Response == NSModalResponseOK)
{
NSURL* SelectedFileURL = [Panel URL];
NSString* FilePath = [SelectedFileURL path];
Path = std::string([FilePath UTF8String]);
}
else
{
Path = std::string{};
}
}
};

if ([NSThread isMainThread])
{
FileDialogBlock();
}
else
{
dispatch_sync(dispatch_get_main_queue(), FileDialogBlock);
}

NSModalResponse Response = [Panel runModal];

if (Response == NSModalResponseOK)
{
NSURL* SelectedFileURL = [Panel URL];
NSString* FilePath = [SelectedFileURL path];
Path = std::string([FilePath UTF8String]);
} else
{
Path = std::string();
}
});
return Path;
}
#endif
Expand Down

0 comments on commit 8a15197

Please sign in to comment.