21
21
#include < UIKit/UIKit.h>
22
22
23
23
@interface DocumentPickerDelegate : NSObject <UIDocumentPickerDelegate>
24
- @property DarwinDirectoryPanelCallback callback ;
24
+ @property DarwinDirectoryPanelCallback panelCallback ;
25
25
@end
26
26
27
+ void *DarwinFileSystemServices::__pickerDelegate = nullptr ;
28
+
29
+ void DarwinFileSystemServices::ClearDelegate () {
30
+ // TODO: Figure out how to free the delegate.
31
+ // CFRelease((__bridge DocumentPickerDelegate *)__pickerDelegate);
32
+ __pickerDelegate = NULL ;
33
+ }
34
+
27
35
@implementation DocumentPickerDelegate
28
- -(instancetype )initWithCallback : (DarwinDirectoryPanelCallback)callback {
36
+ -(instancetype )initWithCallback : (DarwinDirectoryPanelCallback)panelCallback {
29
37
if (self = [super init ]) {
30
- self.callback = callback ;
38
+ self.panelCallback = panelCallback ;
31
39
}
32
40
return self;
33
41
}
34
42
35
- - (void )didPickDocumentsAtURLs : (NSArray <NSURL *> *)urls {
43
+ - (void )documentPicker : (UIDocumentPickerViewController *) controller didPickDocumentsAtURLs : (NSArray <NSURL *> *)urls {
36
44
if (urls.count >= 1 )
37
- self.callback (true , Path (urls[0 ].path .UTF8String ));
45
+ self.panelCallback (true , Path (urls[0 ].path .UTF8String ));
38
46
else
39
- self.callback (false , Path ());
47
+ self.panelCallback (false , Path ());
40
48
41
49
INFO_LOG (SYSTEM, " Callback processed, pre-emptively hide keyboard" );
42
50
[sharedViewController hideKeyboard ];
51
+ DarwinFileSystemServices::ClearDelegate ();
43
52
}
44
53
45
54
- (void )documentPickerWasCancelled : (UIDocumentPickerViewController *)controller {
46
- self.callback (false , Path ());
55
+ self.panelCallback (false , Path ());
47
56
48
57
INFO_LOG (SYSTEM, " Picker cancelled, pre-emptively hide keyboard" );
49
58
[sharedViewController hideKeyboard ];
@@ -56,7 +65,7 @@ - (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller
56
65
#endif // __has_include(<UIKit/UIKit.h>)
57
66
58
67
void DarwinFileSystemServices::presentDirectoryPanel (
59
- DarwinDirectoryPanelCallback callback ,
68
+ DarwinDirectoryPanelCallback panelCallback ,
60
69
bool allowFiles, bool allowDirectories,
61
70
BrowseFileType fileType) {
62
71
dispatch_async (dispatch_get_main_queue (), ^{
@@ -86,14 +95,17 @@ - (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller
86
95
}
87
96
// if (!allowFiles && allowDirectories)
88
97
// panel.allowedFileTypes = @[(__bridge NSString *)kUTTypeFolder];
89
-
90
- NSModalResponse modalResponse = [panel runModal ];
91
- INFO_LOG (SYSTEM, " Mac: Received response from modal" );
92
- if (modalResponse == NSModalResponseOK && panel.URLs .firstObject ) {
93
- callback (true , Path (panel.URLs .firstObject .path .UTF8String ));
94
- } else if (modalResponse == NSModalResponseCancel ) {
95
- callback (false , Path ());
96
- }
98
+ NSModalResponse modalResponse = [panel runModal ];
99
+ if (modalResponse == NSModalResponseOK && panel.URLs .firstObject ) {
100
+ INFO_LOG (SYSTEM, " Mac: Received OK response from modal" );
101
+ panelCallback (true , Path (panel.URLs .firstObject .path .UTF8String ));
102
+ } else if (modalResponse == NSModalResponseCancel ) {
103
+ INFO_LOG (SYSTEM, " Mac: Received Cancel response from modal" );
104
+ panelCallback (false , Path ());
105
+ } else {
106
+ WARN_LOG (SYSTEM, " Mac: Received unknown responsde from modal" );
107
+ panelCallback (false , Path ());
108
+ }
97
109
#elif PPSSPP_PLATFORM(IOS)
98
110
UIViewController *rootViewController = UIApplication.sharedApplication
99
111
.keyWindow
@@ -116,7 +128,7 @@ - (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller
116
128
UIDocumentPickerViewController *pickerVC = [[UIDocumentPickerViewController alloc ] initWithDocumentTypes: types inMode: pickerMode];
117
129
// What if you wanted to go to heaven, but then God showed you the next few lines?
118
130
// serious note: have to do this, because __pickerDelegate has to stay retained as a class property
119
- __pickerDelegate = (void *)CFBridgingRetain ([[DocumentPickerDelegate alloc ] initWithCallback: callback ]);
131
+ __pickerDelegate = (void *)CFBridgingRetain ([[DocumentPickerDelegate alloc ] initWithCallback: panelCallback ]);
120
132
pickerVC.delegate = (__bridge DocumentPickerDelegate *)__pickerDelegate;
121
133
[rootViewController presentViewController: pickerVC animated: true completion: nil ];
122
134
#endif
0 commit comments