Skip to content

Commit e2ffbad

Browse files
jay3dJamil Halabi
authored andcommitted
Cocoa: Fix opening dialogs from another thread
1 parent 388549a commit e2ffbad

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

src/nfd_cocoa.m

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,10 @@ nfdresult_t NFD_OpenDialogN_With_Impl(nfdversion_t version,
235235
// We haven't needed to bump the interface version yet.
236236
(void)version;
237237

238-
nfdresult_t result = NFD_CANCEL;
239-
@autoreleasepool {
238+
__block nfdresult_t result = NFD_CANCEL;
239+
dispatch_sync(dispatch_get_main_queue(), ^{
240+
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
241+
240242
NSWindow* keyWindow = GetNativeWindowHandle(&args->parentWindow);
241243
if (keyWindow) {
242244
[keyWindow makeKeyAndOrderFront:nil];
@@ -261,7 +263,9 @@ nfdresult_t NFD_OpenDialogN_With_Impl(nfdversion_t version,
261263

262264
// return focus to the key window (i.e. main window)
263265
[keyWindow makeKeyAndOrderFront:nil];
264-
}
266+
267+
[pool release];
268+
});
265269
return result;
266270
}
267271

@@ -295,8 +299,10 @@ nfdresult_t NFD_OpenDialogMultipleN_With_Impl(nfdversion_t version,
295299
// We haven't needed to bump the interface version yet.
296300
(void)version;
297301

298-
nfdresult_t result = NFD_CANCEL;
299-
@autoreleasepool {
302+
__block nfdresult_t result = NFD_CANCEL;
303+
dispatch_sync(dispatch_get_main_queue(), ^{
304+
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
305+
300306
NSWindow* keyWindow = GetNativeWindowHandle(&args->parentWindow);
301307
if (keyWindow) {
302308
[keyWindow makeKeyAndOrderFront:nil];
@@ -326,7 +332,9 @@ nfdresult_t NFD_OpenDialogMultipleN_With_Impl(nfdversion_t version,
326332

327333
// return focus to the key window (i.e. main window)
328334
[keyWindow makeKeyAndOrderFront:nil];
329-
}
335+
336+
[pool release];
337+
});
330338
return result;
331339
}
332340

@@ -362,8 +370,10 @@ nfdresult_t NFD_SaveDialogN_With_Impl(nfdversion_t version,
362370
// We haven't needed to bump the interface version yet.
363371
(void)version;
364372

365-
nfdresult_t result = NFD_CANCEL;
366-
@autoreleasepool {
373+
__block nfdresult_t result = NFD_CANCEL;
374+
dispatch_sync(dispatch_get_main_queue(), ^{
375+
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
376+
367377
NSWindow* keyWindow = GetNativeWindowHandle(&args->parentWindow);
368378
if (keyWindow) {
369379
[keyWindow makeKeyAndOrderFront:nil];
@@ -394,7 +404,9 @@ nfdresult_t NFD_SaveDialogN_With_Impl(nfdversion_t version,
394404

395405
// return focus to the key window (i.e. main window)
396406
[keyWindow makeKeyAndOrderFront:nil];
397-
}
407+
408+
[pool release];
409+
});
398410
return result;
399411
}
400412

@@ -424,8 +436,10 @@ nfdresult_t NFD_PickFolderN_With_Impl(nfdversion_t version,
424436
// We haven't needed to bump the interface version yet.
425437
(void)version;
426438

427-
nfdresult_t result = NFD_CANCEL;
428-
@autoreleasepool {
439+
__block nfdresult_t result = NFD_CANCEL;
440+
dispatch_sync(dispatch_get_main_queue(), ^{
441+
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
442+
429443
NSWindow* keyWindow = GetNativeWindowHandle(&args->parentWindow);
430444
if (keyWindow) {
431445
[keyWindow makeKeyAndOrderFront:nil];
@@ -450,7 +464,9 @@ nfdresult_t NFD_PickFolderN_With_Impl(nfdversion_t version,
450464

451465
// return focus to the key window (i.e. main window)
452466
[keyWindow makeKeyAndOrderFront:nil];
453-
}
467+
468+
[pool release];
469+
});
454470
return result;
455471
}
456472

@@ -476,8 +492,10 @@ nfdresult_t NFD_PickFolderMultipleN_With_Impl(nfdversion_t version,
476492
// We haven't needed to bump the interface version yet.
477493
(void)version;
478494

479-
nfdresult_t result = NFD_CANCEL;
480-
@autoreleasepool {
495+
__block nfdresult_t result = NFD_CANCEL;
496+
dispatch_sync(dispatch_get_main_queue(), ^{
497+
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
498+
481499
NSWindow* keyWindow = GetNativeWindowHandle(&args->parentWindow);
482500
if (keyWindow) {
483501
[keyWindow makeKeyAndOrderFront:nil];
@@ -507,7 +525,9 @@ nfdresult_t NFD_PickFolderMultipleN_With_Impl(nfdversion_t version,
507525

508526
// return focus to the key window (i.e. main window)
509527
[keyWindow makeKeyAndOrderFront:nil];
510-
}
528+
529+
[pool release];
530+
});
511531
return result;
512532
}
513533

0 commit comments

Comments
 (0)