Skip to content

Commit

Permalink
Fix osx implementation of oc_file_dialog_desc filters
Browse files Browse the repository at this point in the history
  • Loading branch information
martinfouilleul committed Jan 10, 2025
1 parent d0afc81 commit 787a2d0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion scripts/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ def build_platform_layer_lib_mac(release):
"-o", "build/bin/liborca.dylib",
"build/orca_c.o", "build/orca_objc.o",
"-Lbuild/bin", "-lc",
"-framework", "Carbon", "-framework", "Cocoa", "-framework", "Metal", "-framework", "QuartzCore",
"-framework", "Carbon", "-framework", "Cocoa", "-framework", "Metal", "-framework", "QuartzCore", "-framework", "UniformTypeIdentifiers",
"-weak-lEGL", "-weak-lGLESv2", "-weak-lwebgpu"
], check=True)

Expand Down
32 changes: 30 additions & 2 deletions src/app/osx_app.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
*
**************************************************************************/

#import <QuartzCore/QuartzCore.h> //CATransaction
#import <QuartzCore/QuartzCore.h> //CATransaction
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h> // for file dialog

#include <stdlib.h> // malloc/free

Expand Down Expand Up @@ -1829,6 +1830,28 @@ i32 oc_dispatch_on_main_thread_sync(oc_window main_window, oc_dispatch_proc proc
// system dialogs windows
//--------------------------------------------------------------------

@interface OCOpenSavePanelDelegate : NSObject <NSOpenSavePanelDelegate>
{
}
@end

@implementation OCOpenSavePanelDelegate

- (BOOL)panel:(id)sender shouldEnableURL:(NSURL*)url
{
UTType* fileType = [UTType typeWithFilenameExtension:[url pathExtension]];
if(!fileType)
{
return YES;
}
else
{
return [[sender allowedContentTypes] containsObject:fileType];
}
}

@end

ORCA_API oc_file_dialog_result oc_file_dialog_for_table(oc_arena* arena, oc_file_dialog_desc* desc, oc_file_table* table)
{
__block oc_file_dialog_result result = { 0 };
Expand Down Expand Up @@ -1921,11 +1944,16 @@ ORCA_API oc_file_dialog_result oc_file_dialog_for_table(oc_arena* arena, oc_file
{
oc_str8 string = elt->string;
NSString* filter = [[NSString alloc] initWithBytes:string.ptr length:string.len encoding:NSUTF8StringEncoding];
[fileTypesArray addObject:filter];
UTType* type = [UTType typeWithFilenameExtension:filter];
[fileTypesArray addObject:type];
}
[dialog setAllowedContentTypes:fileTypesArray];
}

//NOTE: set delegate. This is needed for filters to actually work as expected.
OCOpenSavePanelDelegate* delegate = [[OCOpenSavePanelDelegate alloc] init];
[dialog setDelegate:delegate];

// Display the dialog box. If the OK pressed,
// process the files.

Expand Down
1 change: 1 addition & 0 deletions tests/file_dialog/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ int main(int argc, char** argv)
};

oc_str8_list_push(scratch.arena, &desc.filters, OC_STR8("txt"));
oc_str8_list_push(scratch.arena, &desc.filters, OC_STR8("sh"));

oc_file_dialog_result res = oc_file_dialog(scratch.arena, &desc);

Expand Down

0 comments on commit 787a2d0

Please sign in to comment.