diff --git a/Ladybird/AppKit/UI/LadybirdWebView.mm b/Ladybird/AppKit/UI/LadybirdWebView.mm index 22b5e668c6f..e55f08b9c3e 100644 --- a/Ladybird/AppKit/UI/LadybirdWebView.mm +++ b/Ladybird/AppKit/UI/LadybirdWebView.mm @@ -18,6 +18,7 @@ #import #import #import +#import #import #if !__has_feature(objc_arc) @@ -626,7 +627,7 @@ static void copy_data_to_clipboard(StringView data, NSPasteboardType pasteboard_ [panel makeKeyAndOrderFront:nil]; }; - m_web_view_bridge->on_request_file_picker = [self](auto const&, auto allow_multiple_files) { + m_web_view_bridge->on_request_file_picker = [self](auto const& accepted_file_types, auto allow_multiple_files) { auto* panel = [NSOpenPanel openPanel]; [panel setCanChooseFiles:YES]; [panel setCanChooseDirectories:NO]; @@ -639,6 +640,43 @@ static void copy_data_to_clipboard(StringView data, NSPasteboardType pasteboard_ [panel setMessage:@"Select file"]; } + NSMutableArray* accepted_file_filters = [NSMutableArray array]; + + for (auto const& filter : accepted_file_types.filters) { + filter.visit( + [&](Web::HTML::FileFilter::FileType type) { + switch (type) { + case Web::HTML::FileFilter::FileType::Audio: + [accepted_file_filters addObject:UTTypeAudio]; + break; + case Web::HTML::FileFilter::FileType::Image: + [accepted_file_filters addObject:UTTypeImage]; + break; + case Web::HTML::FileFilter::FileType::Video: + [accepted_file_filters addObject:UTTypeVideo]; + break; + } + }, + [&](Web::HTML::FileFilter::MimeType const& filter) { + auto* ns_mime_type = Ladybird::string_to_ns_string(filter.value); + + if (auto* ut_type = [UTType typeWithMIMEType:ns_mime_type]) { + [accepted_file_filters addObject:ut_type]; + } + }, + [&](Web::HTML::FileFilter::Extension const& filter) { + auto* ns_extension = Ladybird::string_to_ns_string(filter.value); + + if (auto* ut_type = [UTType typeWithFilenameExtension:ns_extension]) { + [accepted_file_filters addObject:ut_type]; + } + }); + } + + // FIXME: Create an accessory view to allow selecting the active file filter. + [panel setAllowedContentTypes:accepted_file_filters]; + [panel setAllowsOtherFileTypes:YES]; + [panel beginSheetModalForWindow:[self window] completionHandler:^(NSInteger result) { Vector selected_files; diff --git a/Ladybird/CMakeLists.txt b/Ladybird/CMakeLists.txt index 2a52717bf08..d4217ba86c3 100644 --- a/Ladybird/CMakeLists.txt +++ b/Ladybird/CMakeLists.txt @@ -149,7 +149,7 @@ elseif (APPLE) AppKit/Utilities/Conversions.mm ) target_include_directories(ladybird PRIVATE AppKit) - target_link_libraries(ladybird PRIVATE "-framework Cocoa" LibUnicode) + target_link_libraries(ladybird PRIVATE "-framework Cocoa -framework UniformTypeIdentifiers" LibUnicode) target_compile_options(ladybird PRIVATE -fobjc-arc -Wno-deprecated-anon-enum-enum-conversion # Required for CGImageCreate diff --git a/Meta/gn/secondary/Ladybird/BUILD.gn b/Meta/gn/secondary/Ladybird/BUILD.gn index dc7a6dbbf89..36b80995a55 100644 --- a/Meta/gn/secondary/Ladybird/BUILD.gn +++ b/Meta/gn/secondary/Ladybird/BUILD.gn @@ -134,7 +134,10 @@ executable("ladybird_executable") { "//Userland", ] - frameworks = [ "Cocoa.framework" ] + frameworks = [ + "Cocoa.framework", + "UniformTypeIdentifiers.framework", + ] } if (current_os != "mac") {