| 1 | // macOS Native File Picker using NSOpenPanel |
| 2 | // Provides a proper native file picker that maintains window focus |
| 3 | #import <Cocoa/Cocoa.h> |
| 4 | #include <string.h> |
| 5 | |
| 6 | // Show native macOS folder picker and return selected path |
| 7 | // Returns 0 on success, -1 on cancel/error |
| 8 | // NOTE: Must be called from main thread (GTK callbacks already run on main thread) |
| 9 | int macos_show_folder_picker(char* output_path, size_t max_len) { |
| 10 | int result = -1; |
| 11 | |
| 12 | @autoreleasepool { |
| 13 | NSOpenPanel* panel = [NSOpenPanel openPanel]; |
| 14 | |
| 15 | // Configure for folder selection |
| 16 | [panel setCanChooseFiles:NO]; |
| 17 | [panel setCanChooseDirectories:YES]; |
| 18 | [panel setAllowsMultipleSelection:NO]; |
| 19 | [panel setMessage:@"Select directory to scan"]; |
| 20 | [panel setPrompt:@"Select"]; |
| 21 | |
| 22 | // Run modal - this properly maintains focus |
| 23 | NSModalResponse response = [panel runModal]; |
| 24 | |
| 25 | if (response == NSModalResponseOK) { |
| 26 | NSURL* url = [[panel URLs] firstObject]; |
| 27 | if (url) { |
| 28 | const char* path = [[url path] UTF8String]; |
| 29 | if (path && strlen(path) < max_len) { |
| 30 | strncpy(output_path, path, max_len - 1); |
| 31 | output_path[max_len - 1] = '\0'; |
| 32 | result = 0; |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | return result; |
| 39 | } |
| 40 |