From 0b61b501c8b243bb7beea2bf25e89d0301e82c3e Mon Sep 17 00:00:00 2001 From: camila314 <47485054+camila314@users.noreply.github.com> Date: Wed, 8 Jan 2025 00:21:41 -0600 Subject: [PATCH] allowed file types for macos file picker --- loader/src/platform/mac/util.mm | 54 +++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/loader/src/platform/mac/util.mm b/loader/src/platform/mac/util.mm index 6fcd09944..8ea3c97d1 100644 --- a/loader/src/platform/mac/util.mm +++ b/loader/src/platform/mac/util.mm @@ -15,9 +15,15 @@ #import #undef CommentType + +NSString* intoNS(std::string const& str) { + return [NSString stringWithUTF8String:str.c_str()]; +} + + bool utils::clipboard::write(std::string const& data) { [[NSPasteboard generalPasteboard] clearContents]; - [[NSPasteboard generalPasteboard] setString:[NSString stringWithUTF8String:data.c_str()] + [[NSPasteboard generalPasteboard] setString:intoNS(data) forType:NSPasteboardTypeString]; return true; @@ -31,7 +37,7 @@ } bool utils::file::openFolder(std::filesystem::path const& path) { - NSURL* fileURL = [NSURL fileURLWithPath:[NSString stringWithUTF8String:path.string().c_str()]]; + NSURL* fileURL = [NSURL fileURLWithPath:intoNS(path.string())]; NSURL* folderURL = [fileURL URLByDeletingLastPathComponent]; [[NSWorkspace sharedWorkspace] openURL:folderURL]; return true; @@ -39,7 +45,7 @@ void utils::web::openLinkInBrowser(std::string const& url) { [[NSWorkspace sharedWorkspace] - openURL:[NSURL URLWithString:[NSString stringWithUTF8String:url.c_str()]]]; + openURL:[NSURL URLWithString:intoNS(url)]]; } /*@interface FileDialog : NSObject @@ -94,11 +100,21 @@ @implementation FileDialog // default path if (options.defaultPath) { - auto defaultPath = [NSString stringWithUTF8String:options.defaultPath->c_str()]; - [panel setDirectoryURL: [NSURL fileURLWithPath: defaultPath]]; + auto path = options.defaultPath.value(); + + if (std::filesystem::is_directory(path) || mode == file::PickMode::OpenFolder) { + auto defaultPath = intoNS(options.defaultPath.value()); + [panel setDirectoryURL: [NSURL fileURLWithPath: defaultPath]]; + } else { + auto defaultPath = intoNS(options.defaultPath->parent_path()); + auto name = intoNS(options.defaultPath->filename()); + + [panel setDirectoryURL: [NSURL fileURLWithPath: defaultPath]]; + [panel setNameFieldStringValue: name]; + } } - // other + // title if (mode != file::PickMode::SaveFile) { auto openPanel = (NSOpenPanel*)panel; @@ -113,21 +129,15 @@ @implementation FileDialog [openPanel setAllowsMultipleSelection: mult]; - // allowed files - // TODO: allowed files using the NSOpenSavePanelDelegate xd - // NSMutableArray* allowed = [NSMutableArray array]; - - // for (auto& f : options.filters) { - // for (auto& i : f.files) { - // auto nsstr = [NSString stringWithUTF8String: i.c_str()]; - - // if (![allowed containsObject: nsstr]) - // [allowed addObject: nsstr]; - // } - // } - - // if (options.filters.size()) - // [panel setAllowedFileTypes: allowed]; + if (options.filters.size() > 0) { + NSMutableArray* allowedFileTypes = [NSMutableArray new]; + for (auto& filter : options.filters) { + for (auto& ext : filter.files) { + [allowedFileTypes addObject: intoNS(ext)]; + } + } + [openPanel setAllowedFileTypes: allowedFileTypes]; + } } // run thing @@ -269,7 +279,7 @@ void shutdown() { auto gdExec = dirs::getGameDir() / "MacOS" / "Geometry Dash"; NSTask *task = [NSTask new]; - [task setLaunchPath: [NSString stringWithUTF8String: gdExec.string().c_str()]]; + [task setLaunchPath: intoNS(gdExec.string())]; [task launch]; };