Skip to content

Commit

Permalink
allowed file types for macos file picker
Browse files Browse the repository at this point in the history
  • Loading branch information
camila314 committed Jan 8, 2025
1 parent 241be1b commit 0b61b50
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions loader/src/platform/mac/util.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@
#import <Cocoa/Cocoa.h>
#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;
Expand All @@ -31,15 +37,15 @@
}

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;
}

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
Expand Down Expand Up @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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];
};

Expand Down

0 comments on commit 0b61b50

Please sign in to comment.