Skip to content

Commit

Permalink
Cocoa: Fix opening dialogs from another thread
Browse files Browse the repository at this point in the history
  • Loading branch information
jay3d authored and Jamil Halabi committed Oct 14, 2024
1 parent 388549a commit e2ffbad
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions src/nfd_cocoa.m
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,10 @@ nfdresult_t NFD_OpenDialogN_With_Impl(nfdversion_t version,
// We haven't needed to bump the interface version yet.
(void)version;

nfdresult_t result = NFD_CANCEL;
@autoreleasepool {
__block nfdresult_t result = NFD_CANCEL;
dispatch_sync(dispatch_get_main_queue(), ^{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

NSWindow* keyWindow = GetNativeWindowHandle(&args->parentWindow);
if (keyWindow) {
[keyWindow makeKeyAndOrderFront:nil];
Expand All @@ -261,7 +263,9 @@ nfdresult_t NFD_OpenDialogN_With_Impl(nfdversion_t version,

// return focus to the key window (i.e. main window)
[keyWindow makeKeyAndOrderFront:nil];
}

[pool release];
});
return result;
}

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

nfdresult_t result = NFD_CANCEL;
@autoreleasepool {
__block nfdresult_t result = NFD_CANCEL;
dispatch_sync(dispatch_get_main_queue(), ^{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

NSWindow* keyWindow = GetNativeWindowHandle(&args->parentWindow);
if (keyWindow) {
[keyWindow makeKeyAndOrderFront:nil];
Expand Down Expand Up @@ -326,7 +332,9 @@ nfdresult_t NFD_OpenDialogMultipleN_With_Impl(nfdversion_t version,

// return focus to the key window (i.e. main window)
[keyWindow makeKeyAndOrderFront:nil];
}

[pool release];
});
return result;
}

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

nfdresult_t result = NFD_CANCEL;
@autoreleasepool {
__block nfdresult_t result = NFD_CANCEL;
dispatch_sync(dispatch_get_main_queue(), ^{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

NSWindow* keyWindow = GetNativeWindowHandle(&args->parentWindow);
if (keyWindow) {
[keyWindow makeKeyAndOrderFront:nil];
Expand Down Expand Up @@ -394,7 +404,9 @@ nfdresult_t NFD_SaveDialogN_With_Impl(nfdversion_t version,

// return focus to the key window (i.e. main window)
[keyWindow makeKeyAndOrderFront:nil];
}

[pool release];
});
return result;
}

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

nfdresult_t result = NFD_CANCEL;
@autoreleasepool {
__block nfdresult_t result = NFD_CANCEL;
dispatch_sync(dispatch_get_main_queue(), ^{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

NSWindow* keyWindow = GetNativeWindowHandle(&args->parentWindow);
if (keyWindow) {
[keyWindow makeKeyAndOrderFront:nil];
Expand All @@ -450,7 +464,9 @@ nfdresult_t NFD_PickFolderN_With_Impl(nfdversion_t version,

// return focus to the key window (i.e. main window)
[keyWindow makeKeyAndOrderFront:nil];
}

[pool release];
});
return result;
}

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

nfdresult_t result = NFD_CANCEL;
@autoreleasepool {
__block nfdresult_t result = NFD_CANCEL;
dispatch_sync(dispatch_get_main_queue(), ^{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

NSWindow* keyWindow = GetNativeWindowHandle(&args->parentWindow);
if (keyWindow) {
[keyWindow makeKeyAndOrderFront:nil];
Expand Down Expand Up @@ -507,7 +525,9 @@ nfdresult_t NFD_PickFolderMultipleN_With_Impl(nfdversion_t version,

// return focus to the key window (i.e. main window)
[keyWindow makeKeyAndOrderFront:nil];
}

[pool release];
});
return result;
}

Expand Down

0 comments on commit e2ffbad

Please sign in to comment.