Skip to content

Commit 0b61b50

Browse files
committed
allowed file types for macos file picker
1 parent 241be1b commit 0b61b50

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

loader/src/platform/mac/util.mm

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@
1515
#import <Cocoa/Cocoa.h>
1616
#undef CommentType
1717

18+
19+
NSString* intoNS(std::string const& str) {
20+
return [NSString stringWithUTF8String:str.c_str()];
21+
}
22+
23+
1824
bool utils::clipboard::write(std::string const& data) {
1925
[[NSPasteboard generalPasteboard] clearContents];
20-
[[NSPasteboard generalPasteboard] setString:[NSString stringWithUTF8String:data.c_str()]
26+
[[NSPasteboard generalPasteboard] setString:intoNS(data)
2127
forType:NSPasteboardTypeString];
2228

2329
return true;
@@ -31,15 +37,15 @@
3137
}
3238

3339
bool utils::file::openFolder(std::filesystem::path const& path) {
34-
NSURL* fileURL = [NSURL fileURLWithPath:[NSString stringWithUTF8String:path.string().c_str()]];
40+
NSURL* fileURL = [NSURL fileURLWithPath:intoNS(path.string())];
3541
NSURL* folderURL = [fileURL URLByDeletingLastPathComponent];
3642
[[NSWorkspace sharedWorkspace] openURL:folderURL];
3743
return true;
3844
}
3945

4046
void utils::web::openLinkInBrowser(std::string const& url) {
4147
[[NSWorkspace sharedWorkspace]
42-
openURL:[NSURL URLWithString:[NSString stringWithUTF8String:url.c_str()]]];
48+
openURL:[NSURL URLWithString:intoNS(url)]];
4349
}
4450

4551
/*@interface FileDialog : NSObject
@@ -94,11 +100,21 @@ @implementation FileDialog
94100

95101
// default path
96102
if (options.defaultPath) {
97-
auto defaultPath = [NSString stringWithUTF8String:options.defaultPath->c_str()];
98-
[panel setDirectoryURL: [NSURL fileURLWithPath: defaultPath]];
103+
auto path = options.defaultPath.value();
104+
105+
if (std::filesystem::is_directory(path) || mode == file::PickMode::OpenFolder) {
106+
auto defaultPath = intoNS(options.defaultPath.value());
107+
[panel setDirectoryURL: [NSURL fileURLWithPath: defaultPath]];
108+
} else {
109+
auto defaultPath = intoNS(options.defaultPath->parent_path());
110+
auto name = intoNS(options.defaultPath->filename());
111+
112+
[panel setDirectoryURL: [NSURL fileURLWithPath: defaultPath]];
113+
[panel setNameFieldStringValue: name];
114+
}
99115
}
100116

101-
// other
117+
// title
102118
if (mode != file::PickMode::SaveFile) {
103119
auto openPanel = (NSOpenPanel*)panel;
104120

@@ -113,21 +129,15 @@ @implementation FileDialog
113129

114130
[openPanel setAllowsMultipleSelection: mult];
115131

116-
// allowed files
117-
// TODO: allowed files using the NSOpenSavePanelDelegate xd
118-
// NSMutableArray* allowed = [NSMutableArray array];
119-
120-
// for (auto& f : options.filters) {
121-
// for (auto& i : f.files) {
122-
// auto nsstr = [NSString stringWithUTF8String: i.c_str()];
123-
124-
// if (![allowed containsObject: nsstr])
125-
// [allowed addObject: nsstr];
126-
// }
127-
// }
128-
129-
// if (options.filters.size())
130-
// [panel setAllowedFileTypes: allowed];
132+
if (options.filters.size() > 0) {
133+
NSMutableArray* allowedFileTypes = [NSMutableArray new];
134+
for (auto& filter : options.filters) {
135+
for (auto& ext : filter.files) {
136+
[allowedFileTypes addObject: intoNS(ext)];
137+
}
138+
}
139+
[openPanel setAllowedFileTypes: allowedFileTypes];
140+
}
131141
}
132142

133143
// run thing
@@ -269,7 +279,7 @@ void shutdown() {
269279
auto gdExec = dirs::getGameDir() / "MacOS" / "Geometry Dash";
270280

271281
NSTask *task = [NSTask new];
272-
[task setLaunchPath: [NSString stringWithUTF8String: gdExec.string().c_str()]];
282+
[task setLaunchPath: intoNS(gdExec.string())];
273283
[task launch];
274284
};
275285

0 commit comments

Comments
 (0)