Title: [262895] trunk
Revision
262895
Author
[email protected]
Date
2020-06-10 23:31:53 -0700 (Wed, 10 Jun 2020)

Log Message

[macOS]: The File Picker of the <input> file element should show the selection filter
https://bugs.webkit.org/show_bug.cgi?id=212485

Patch by Said Abou-Hallawa <[email protected]> on 2020-06-10
Reviewed by Darin Adler.

Source/WebCore/PAL:

Expose an SPI from CoreServcies to get all the UTIs known by the system.

* pal/spi/cocoa/CoreServicesSPI.h:

Source/WebKit:

Add two private helpers to WKOpenPanelParameters.

* UIProcess/API/Cocoa/WKOpenPanelParameters.mm:
(-[WKOpenPanelParameters _allowedFileExtensions]):
Returns an array of strings representing the allowed extensions.

(-[WKOpenPanelParameters _allowedFileExtensionsDescription]):
Returns a string representing a description for the allowed extensions.

* UIProcess/API/Cocoa/WKOpenPanelParametersPrivate.h:

Tools:

Create a view with an NSPopupButton for the file extensions filter. Set
this view as the AccessoryView of the OpenPanel. Create a customized target
for the NSPopupButton to respond to changing its selection.

* MiniBrowser/mac/WK2BrowserWindowController.m:
(-[FileExtensionsPopupTarget initWithOpenPanel:allowedFileExtensions:]):
(-[FileExtensionsPopupTarget popupAction:]):
(-[FileExtensionsPopupTarget dealloc]):
(-[WK2BrowserWindowController dealloc]):
(-[WK2BrowserWindowController createFilterView:popupTarget:]):
(-[WK2BrowserWindowController webView:runOpenPanelWithParameters:initiatedByFrame:completionHandler:webView:runOpenPanelWithParameters:initiatedByFrame:completionHandler:]):

Modified Paths

Diff

Modified: trunk/Source/WebCore/PAL/ChangeLog (262894 => 262895)


--- trunk/Source/WebCore/PAL/ChangeLog	2020-06-11 04:23:20 UTC (rev 262894)
+++ trunk/Source/WebCore/PAL/ChangeLog	2020-06-11 06:31:53 UTC (rev 262895)
@@ -1,3 +1,14 @@
+2020-06-10  Said Abou-Hallawa  <[email protected]>
+
+        [macOS]: The File Picker of the <input> file element should show the selection filter
+        https://bugs.webkit.org/show_bug.cgi?id=212485
+
+        Reviewed by Darin Adler.
+
+        Expose an SPI from CoreServcies to get all the UTIs known by the system.
+
+        * pal/spi/cocoa/CoreServicesSPI.h:
+
 2020-06-09  Tim Horton  <[email protected]>
 
         Stop using the wrong LaunchServices SPI for getting an app's localized name

Modified: trunk/Source/WebCore/PAL/pal/spi/cocoa/CoreServicesSPI.h (262894 => 262895)


--- trunk/Source/WebCore/PAL/pal/spi/cocoa/CoreServicesSPI.h	2020-06-11 04:23:20 UTC (rev 262894)
+++ trunk/Source/WebCore/PAL/pal/spi/cocoa/CoreServicesSPI.h	2020-06-11 06:31:53 UTC (rev 262895)
@@ -26,3 +26,5 @@
 #pragma once
 
 extern "C" void _CSCheckFixDisable();
+extern "C" CFArrayRef _UTCopyDeclaredTypeIdentifiers(void);
+

Modified: trunk/Source/WebKit/ChangeLog (262894 => 262895)


--- trunk/Source/WebKit/ChangeLog	2020-06-11 04:23:20 UTC (rev 262894)
+++ trunk/Source/WebKit/ChangeLog	2020-06-11 06:31:53 UTC (rev 262895)
@@ -1,3 +1,21 @@
+2020-06-10  Said Abou-Hallawa  <[email protected]>
+
+        [macOS]: The File Picker of the <input> file element should show the selection filter
+        https://bugs.webkit.org/show_bug.cgi?id=212485
+
+        Reviewed by Darin Adler.
+
+        Add two private helpers to WKOpenPanelParameters.
+
+        * UIProcess/API/Cocoa/WKOpenPanelParameters.mm:
+        (-[WKOpenPanelParameters _allowedFileExtensions]):
+        Returns an array of strings representing the allowed extensions.
+
+        (-[WKOpenPanelParameters _allowedFileExtensionsDescription]):
+        Returns a string representing a description for the allowed extensions.
+
+        * UIProcess/API/Cocoa/WKOpenPanelParametersPrivate.h:
+
 2020-06-10  Beth Dakin  <[email protected]>
 
         Replace instances of blacklist in WebCore with blocklist

Modified: trunk/Source/WebKit/UIProcess/API/Cocoa/WKOpenPanelParameters.mm (262894 => 262895)


--- trunk/Source/WebKit/UIProcess/API/Cocoa/WKOpenPanelParameters.mm	2020-06-11 04:23:20 UTC (rev 262894)
+++ trunk/Source/WebKit/UIProcess/API/Cocoa/WKOpenPanelParameters.mm	2020-06-11 06:31:53 UTC (rev 262895)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016 Apple Inc. All rights reserved.
+ * Copyright (C) 2016-2020 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -25,11 +25,60 @@
 
 #import "config.h"
 #import "WKOpenPanelParametersInternal.h"
+#import <pal/spi/cocoa/CoreServicesSPI.h>
 
 #if PLATFORM(MAC)
 
 #import "WKNSArray.h"
 
+static NSDictionary<NSString *, NSSet<NSString *> *> *extensionsForMIMETypeMap()
+{
+    static auto extensionsForMIMETypeMap = makeNeverDestroyed([] {
+        auto extensionsForMIMETypeMap = adoptNS([[NSMutableDictionary alloc] init]);
+        auto allUTIs = adoptCF(_UTCopyDeclaredTypeIdentifiers());
+
+        auto addExtensionForMIMEType = ^(NSString *mimeType, NSString *extension) {
+            if (!extensionsForMIMETypeMap.get()[mimeType])
+                extensionsForMIMETypeMap.get()[mimeType] = [NSMutableSet set];
+            [extensionsForMIMETypeMap.get()[mimeType] addObject:extension];
+        };
+
+        auto addExtensionsForMIMEType = ^(NSString *mimeType, NSArray<NSString *> *extensions) {
+            auto wildcardMIMEType = [[mimeType componentsSeparatedByString:@"/"][0] stringByAppendingString:@"/*"];
+
+            for (NSString *extension in extensions) {
+                if (!extension)
+                    continue;
+                // Add extension to wildcardMIMEType, for example add "png" to "image/*"
+                addExtensionForMIMEType(wildcardMIMEType, extension);
+                // Add extension to itsmimeType, for example add "png" to "image/png"
+                addExtensionForMIMEType(mimeType, extension);
+            }
+        };
+
+        for (CFIndex i = 0, count = CFArrayGetCount(allUTIs.get()); i < count; ++i) {
+            auto uti = static_cast<CFStringRef>(CFArrayGetValueAtIndex(allUTIs.get(), i));
+            auto mimeType = adoptCF(UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType));
+            if (!mimeType)
+                continue;
+            auto extensions = adoptCF(UTTypeCopyAllTagsWithClass(uti, kUTTagClassFilenameExtension));
+            addExtensionsForMIMEType((__bridge NSString *)mimeType.get(), (__bridge NSArray<NSString *> *)extensions.get());
+        }
+
+        // Add additional mime types which _UTCopyDeclaredTypeIdentifiers() may not return.
+        addExtensionForMIMEType(@"image/webp", @"webp");
+
+        return extensionsForMIMETypeMap;
+    }());
+
+    return extensionsForMIMETypeMap.get().get();
+}
+
+static NSSet<NSString *> *extensionsForMIMEType(NSString *mimetype)
+{
+    return [extensionsForMIMETypeMap() objectForKey:mimetype];
+}
+
 @implementation WKOpenPanelParameters
 
 - (BOOL)allowsMultipleSelection
@@ -63,6 +112,62 @@
     return wrapper(_openPanelParameters->acceptFileExtensions());
 }
 
+- (NSArray<NSString *> *)_allowedFileExtensions
+{
+    // Aggregate extensions based on specified MIME types.
+    auto acceptedMIMETypes = [self _acceptedMIMETypes];
+    auto acceptedFileExtensions = [self _acceptedFileExtensions];
+
+    auto allowedFileExtensions = adoptNS([[NSMutableSet alloc] init]);
+
+    [acceptedMIMETypes enumerateObjectsUsingBlock:^(NSString *mimeType, NSUInteger index, BOOL* stop) {
+        ASSERT([mimeType containsString:@"/"]);
+        [allowedFileExtensions unionSet:extensionsForMIMEType(mimeType)];
+    }];
+
+    auto additionalAllowedFileExtensions = adoptNS([[NSMutableArray alloc] init]);
+
+    [acceptedFileExtensions enumerateObjectsUsingBlock:^(NSString *extension, NSUInteger index, BOOL *stop) {
+        ASSERT([extension characterAtIndex:0] == '.');
+        [additionalAllowedFileExtensions addObject:[extension substringFromIndex:1]];
+    }];
+    
+    [allowedFileExtensions addObjectsFromArray:additionalAllowedFileExtensions.get()];
+    return [allowedFileExtensions allObjects];
+}
+
+- (NSArray<NSString *> *)_allowedFileExtensionsTitles
+{
+    auto acceptedMIMETypes = [self _acceptedMIMETypes];
+    auto acceptedFileExtensions = [self _acceptedFileExtensions];
+
+    constexpr auto AllFilesTitle = @"All Files";
+    constexpr auto CustomFilesTitle = @"Custom Files";
+    
+    if (![acceptedMIMETypes count] && ![acceptedFileExtensions count])
+        return @[AllFilesTitle];
+
+    if (!([acceptedMIMETypes count] == 1 && ![acceptedFileExtensions count]))
+        return @[CustomFilesTitle, AllFilesTitle];
+
+    auto mimeType = [acceptedMIMETypes firstObject];
+    auto range = [mimeType rangeOfString:@"/"];
+    
+    if (!range.length)
+        return @[CustomFilesTitle, AllFilesTitle];
+    
+    auto mimeTypePrefix = [mimeType substringToIndex:range.location];
+    auto mimeTypeSuffix = [mimeType substringFromIndex:range.location + 1];
+    
+    if ([mimeTypeSuffix isEqualToString:@"*"])
+        return @[[NSString stringWithFormat:@"%@%@ Files", [[mimeTypePrefix substringToIndex:1] uppercaseString], [mimeTypePrefix substringFromIndex:1]], AllFilesTitle];
+
+    if ([mimeTypeSuffix length] <= 4)
+        return @[[NSString stringWithFormat:@"%@ %@", [mimeTypeSuffix uppercaseString], mimeTypePrefix], AllFilesTitle];
+
+    return @[[NSString stringWithFormat:@"%@ %@", mimeTypeSuffix, mimeTypePrefix], AllFilesTitle];
+}
+
 @end
 
 #endif

Modified: trunk/Source/WebKit/UIProcess/API/Cocoa/WKOpenPanelParametersPrivate.h (262894 => 262895)


--- trunk/Source/WebKit/UIProcess/API/Cocoa/WKOpenPanelParametersPrivate.h	2020-06-11 04:23:20 UTC (rev 262894)
+++ trunk/Source/WebKit/UIProcess/API/Cocoa/WKOpenPanelParametersPrivate.h	2020-06-11 06:31:53 UTC (rev 262895)
@@ -31,6 +31,8 @@
 
 @property (nonatomic, readonly, copy) NSArray<NSString *> *_acceptedMIMETypes WK_API_AVAILABLE(macos(10.13.4));
 @property (nonatomic, readonly, copy) NSArray<NSString *> *_acceptedFileExtensions WK_API_AVAILABLE(macos(10.13.4));
+@property (nonatomic, readonly, copy) NSArray<NSString *> *_allowedFileExtensions WK_API_AVAILABLE(macos(10.13.4));
+@property (nonatomic, readonly, copy) NSArray<NSString *> *_allowedFileExtensionsTitles WK_API_AVAILABLE(macos(10.13.4));
 
 @end
 

Modified: trunk/Tools/ChangeLog (262894 => 262895)


--- trunk/Tools/ChangeLog	2020-06-11 04:23:20 UTC (rev 262894)
+++ trunk/Tools/ChangeLog	2020-06-11 06:31:53 UTC (rev 262895)
@@ -1,3 +1,22 @@
+2020-06-10  Said Abou-Hallawa  <[email protected]>
+
+        [macOS]: The File Picker of the <input> file element should show the selection filter
+        https://bugs.webkit.org/show_bug.cgi?id=212485
+
+        Reviewed by Darin Adler.
+
+        Create a view with an NSPopupButton for the file extensions filter. Set
+        this view as the AccessoryView of the OpenPanel. Create a customized target
+        for the NSPopupButton to respond to changing its selection.
+
+        * MiniBrowser/mac/WK2BrowserWindowController.m:
+        (-[FileExtensionsPopupTarget initWithOpenPanel:allowedFileExtensions:]):
+        (-[FileExtensionsPopupTarget popupAction:]):
+        (-[FileExtensionsPopupTarget dealloc]):
+        (-[WK2BrowserWindowController dealloc]):
+        (-[WK2BrowserWindowController createFilterView:popupTarget:]):
+        (-[WK2BrowserWindowController webView:runOpenPanelWithParameters:initiatedByFrame:completionHandler:webView:runOpenPanelWithParameters:initiatedByFrame:completionHandler:]):
+
 2020-06-10  Jonathan Bedard  <[email protected]>
 
         Generalize configure-xcode-for-ios-development (Part 2)

Modified: trunk/Tools/MiniBrowser/mac/WK2BrowserWindowController.m (262894 => 262895)


--- trunk/Tools/MiniBrowser/mac/WK2BrowserWindowController.m	2020-06-11 04:23:20 UTC (rev 262894)
+++ trunk/Tools/MiniBrowser/mac/WK2BrowserWindowController.m	2020-06-11 06:31:53 UTC (rev 262895)
@@ -30,6 +30,7 @@
 #import <WebKit/WKFrameInfo.h>
 #import <WebKit/WKNavigationActionPrivate.h>
 #import <WebKit/WKNavigationDelegate.h>
+#import <WebKit/WKOpenPanelParametersPrivate.h>
 #import <WebKit/WKPreferencesPrivate.h>
 #import <WebKit/WKUIDelegate.h>
 #import <WebKit/WKUIDelegatePrivate.h>
@@ -71,6 +72,45 @@
 
 @end
 
+// Target for the FileExtensions NSPopupButton in the OpenPanel's AccessoryView.
+@interface FileExtensionsPopupTarget : NSObject {
+@private
+    NSOpenPanel *_openPanel;
+    NSArray<NSString *> *_allowedFileExtensions;
+}
+@end
+
+@implementation FileExtensionsPopupTarget
+
+- (id)initWithOpenPanel:(NSOpenPanel*)openPanel allowedFileExtensions:(NSArray<NSString *> *)allowedFileExtensions {
+    if ((self = [super init])) {
+        _openPanel = openPanel;
+        _allowedFileExtensions = [allowedFileExtensions copy];
+    }
+    return self;
+}
+
+- (void)popupAction:(id)sender {
+    // Last item.
+    if ([sender indexOfSelectedItem] == [sender numberOfItems] - 1)
+        [_openPanel setAllowedFileTypes:nil];
+    else if (![sender indexOfSelectedItem]) {
+        // First item.
+        if (![_allowedFileExtensions count])
+            [_openPanel setAllowedFileTypes:@[@""]];
+        else
+            [_openPanel setAllowedFileTypes:_allowedFileExtensions];
+    }
+}
+
+- (void)dealloc
+{
+    [_allowedFileExtensions release];
+    [super dealloc];
+}
+
+@end
+
 @interface WK2BrowserWindowController () <NSTextFinderBarContainer, WKNavigationDelegate, WKUIDelegate, _WKIconLoadingDelegate>
 @end
 
@@ -85,6 +125,7 @@
     MiniBrowserNSTextFinder *_textFinder;
     NSView *_textFindBarView;
     BOOL _findBarVisible;
+    FileExtensionsPopupTarget *_fileExtensionsPopupTarget;
 }
 
 - (void)awakeFromNib
@@ -170,6 +211,7 @@
 
     [_webView release];
     [_configuration release];
+    [_fileExtensionsPopupTarget release];
 
     [super dealloc];
 }
@@ -587,6 +629,54 @@
     }];
 }
 
+- (nullable NSView *)createFilterView:(NSArray *)titles popupTarget:(FileExtensionsPopupTarget*)popupTarget
+{
+    NSTextField* label = [[[NSTextField alloc] initWithFrame:NSZeroRect] autorelease];
+    [label setStringValue:@"Format:"];
+    [label setEditable:NO];
+    [label setSelectable:NO];
+    [label setBordered:NO];
+    [label setBackgroundColor:[NSColor clearColor]];
+    [label sizeToFit];
+
+    NSPopUpButton *button = [[[NSPopUpButton alloc] initWithFrame:NSZeroRect pullsDown:NO] autorelease];
+    [button addItemsWithTitles:titles];
+    [button setTarget:popupTarget];
+    [button setAction:@selector(popupAction:)];
+    [button sizeToFit];
+    [button selectItemAtIndex:0];
+    [popupTarget popupAction:button];
+    
+    NSView* filterView = [[[NSView alloc] initWithFrame:NSZeroRect] autorelease];
+    [filterView addSubview:label];
+    [filterView addSubview:button];
+
+    const CGFloat margin = 20;
+    const CGFloat spacing = 2;
+    const CGFloat minButtonWidth = 230;
+
+    NSRect labelFrame = [label frame];
+    NSRect buttonFrame = [button frame];
+    NSRect filterViewFrame = [filterView frame];
+
+    // Set a minimum width for 'button'.
+    buttonFrame.size = NSMakeSize(MAX(NSWidth(buttonFrame), minButtonWidth), NSHeight(buttonFrame));
+    
+    // FilterView will lay out 'label' and 'button' horizontally and have a vertical margins.
+    filterViewFrame.size = NSMakeSize(NSWidth(labelFrame) + NSWidth(buttonFrame) + spacing, MAX(NSHeight(labelFrame), NSHeight(buttonFrame)) + margin * 2);
+    [filterView setFrame:filterViewFrame];
+
+    // 'label' will be laid out center vertically.
+    labelFrame.origin = NSMakePoint(NSMinX(labelFrame), (NSHeight(filterViewFrame) - NSHeight(labelFrame)) / 2);
+    [label setFrame:labelFrame];
+    
+    // 'button' will be laid out center vertically and it will come after 'label' horizontally.
+    buttonFrame.origin = NSMakePoint(NSMaxX(labelFrame) + spacing, (NSHeight(filterViewFrame) - NSHeight(buttonFrame)) / 2);
+    [button setFrame:buttonFrame];
+
+    return filterView;
+}
+
 #if __has_feature(objc_generics)
 - (void)webView:(WKWebView *)webView runOpenPanelWithParameters:(WKOpenPanelParameters *)parameters initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSArray<NSURL *> * URLs))completionHandler
 #else
@@ -597,6 +687,14 @@
 
     openPanel.allowsMultipleSelection = parameters.allowsMultipleSelection;
 
+    [_fileExtensionsPopupTarget release];
+    _fileExtensionsPopupTarget = [[FileExtensionsPopupTarget alloc] initWithOpenPanel: openPanel allowedFileExtensions: parameters._allowedFileExtensions];
+
+    NSArray *allowedFileExtensionsTitles = parameters._allowedFileExtensionsTitles;
+    NSView *filterView = [self createFilterView:allowedFileExtensionsTitles popupTarget:_fileExtensionsPopupTarget];
+    if (filterView)
+        [openPanel setAccessoryView:filterView];
+
     [openPanel beginSheetModalForWindow:webView.window completionHandler:^(NSInteger result) {
         if (result == NSModalResponseOK)
             completionHandler(openPanel.URLs);
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to