15
15
#import < Cocoa/Cocoa.h>
16
16
#undef CommentType
17
17
18
+
19
+ NSString * intoNS (std::string const & str) {
20
+ return [NSString stringWithUTF8String: str.c_str ()];
21
+ }
22
+
23
+
18
24
bool utils::clipboard::write (std::string const & data) {
19
25
[[NSPasteboard generalPasteboard ] clearContents ];
20
- [[NSPasteboard generalPasteboard ] setString: [ NSString stringWithUTF8String: data. c_str ()]
26
+ [[NSPasteboard generalPasteboard ] setString: intoNS (data)
21
27
forType: NSPasteboardTypeString ];
22
28
23
29
return true ;
31
37
}
32
38
33
39
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 ()) ];
35
41
NSURL * folderURL = [fileURL URLByDeletingLastPathComponent ];
36
42
[[NSWorkspace sharedWorkspace ] openURL: folderURL];
37
43
return true ;
38
44
}
39
45
40
46
void utils::web::openLinkInBrowser (std::string const & url) {
41
47
[[NSWorkspace sharedWorkspace ]
42
- openURL: [NSURL URLWithString: [ NSString stringWithUTF8String: url. c_str ()] ]];
48
+ openURL: [NSURL URLWithString: intoNS (url) ]];
43
49
}
44
50
45
51
/* @interface FileDialog : NSObject
@@ -94,11 +100,21 @@ @implementation FileDialog
94
100
95
101
// default path
96
102
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
+ }
99
115
}
100
116
101
- // other
117
+ // title
102
118
if (mode != file::PickMode::SaveFile) {
103
119
auto openPanel = (NSOpenPanel *)panel;
104
120
@@ -113,21 +129,15 @@ @implementation FileDialog
113
129
114
130
[openPanel setAllowsMultipleSelection: mult];
115
131
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
+ }
131
141
}
132
142
133
143
// run thing
@@ -269,7 +279,7 @@ void shutdown() {
269
279
auto gdExec = dirs::getGameDir () / " MacOS" / " Geometry Dash" ;
270
280
271
281
NSTask *task = [NSTask new ];
272
- [task setLaunchPath: [ NSString stringWithUTF8String: gdExec.string (). c_str ()] ];
282
+ [task setLaunchPath: intoNS ( gdExec.string ()) ];
273
283
[task launch ];
274
284
};
275
285
0 commit comments