Add a share item to the link action menu
authortimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 23 Mar 2015 18:39:13 +0000 (18:39 +0000)
committertimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 23 Mar 2015 18:39:13 +0000 (18:39 +0000)
https://bugs.webkit.org/show_bug.cgi?id=142961
<rdar://problem/19198374>

Reviewed by Beth Dakin.

* UIProcess/mac/WKActionMenuController.mm:
(-[WKActionMenuController _hitLinkURL]):
Factor out _hitLinkURL from various places.

(-[WKActionMenuController _openURLFromActionMenu:]):
(-[WKActionMenuController _addToReadingListFromActionMenu:]):
(-[WKActionMenuController _defaultMenuItemsForVideo]):
(-[WKActionMenuController _defaultMenuItemsForImage]):
(-[WKActionMenuController _createActionMenuItemForTag:]):
(-[WKActionMenuController _createShareActionMenuItemForTag:withItems:]):
Adopt -[NSMenuItem standardShareMenuItemWithItems].
All of the null-checking is unnecessary as it's impossible to enter
these functions with a null image/etc.

(-[WKActionMenuController _defaultMenuItemsForLink]):
* Shared/API/c/WKActionMenuItemTypes.h:
Add a 'Share' item to the link menu.

* WebView/WebActionMenuController.mm:
(-[WebActionMenuController _defaultMenuItemsForImage]):
(-[WebActionMenuController _defaultMenuItemsForVideo]):
(-[WebActionMenuController _createActionMenuItemForTag:]):
(-[WebActionMenuController _createShareActionMenuItemForTag:withItems:]):
Adopt -[NSMenuItem standardShareMenuItemWithItems].
All of the null-checking is unnecessary as it's impossible to enter
these functions with a null image/etc.
Also, remove a duplicate isDownloadableMedia check for the 'save to downloads' item.

(-[WebActionMenuController _defaultMenuItemsForLink]):
* WebView/WebUIDelegatePrivate.h:
Add a 'Share' item to the link menu.

* English.lproj/Localizable.strings:
Remove some now-unused strings.

* platform/spi/mac/NSMenuSPI.h:
Add some SPI.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@181858 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/English.lproj/Localizable.strings
Source/WebCore/platform/spi/mac/NSMenuSPI.h
Source/WebKit/mac/ChangeLog
Source/WebKit/mac/WebView/WebActionMenuController.mm
Source/WebKit/mac/WebView/WebUIDelegatePrivate.h
Source/WebKit2/ChangeLog
Source/WebKit2/Shared/API/c/WKActionMenuItemTypes.h
Source/WebKit2/UIProcess/mac/WKActionMenuController.mm

index d9268c5..3fb50f8 100644 (file)
@@ -1,3 +1,17 @@
+2015-03-23  Tim Horton  <timothy_horton@apple.com>
+
+        Add a share item to the link action menu
+        https://bugs.webkit.org/show_bug.cgi?id=142961
+        <rdar://problem/19198374>
+
+        Reviewed by Beth Dakin.
+
+        * English.lproj/Localizable.strings:
+        Remove some now-unused strings.
+
+        * platform/spi/mac/NSMenuSPI.h:
+        Add some SPI.
+
 2015-03-23  David Kilzer  <ddkilzer@apple.com>
 
         [iOS] Build fix: CoreText deprecated kCTFontSystemFontType
index 195746c..7474dc3 100644 (file)
 /* "Save to Downloads" action menu item in image menu */
 "Save to Downloads (image action menu item)" = "Save to Downloads";
 
-/* "Share" action menu item in image menu */
-"Share (image action menu item)" = "Share";
-
 /* "Copy" action menu item in text menu */
 "Copy (text action menu item)" = "Copy";
 
 /* "Copy" action menu item in video menu */
 "Copy (video action menu item)" = "Copy";
 
-/* "Share" action menu item in video menu */
-"Share (video action menu item)" = "Share";
-
 /* "Save to Downloads" action menu item in video menu */
 "Save to Downloads (video action menu item)" = "Save to Downloads";
index 757ea4d..9a02a6a 100644 (file)
@@ -39,4 +39,5 @@ typedef NS_ENUM(NSInteger, NSMenuType) {
 
 @interface NSMenuItem (Private)
 + (QLPreviewMenuItem *)standardQuickLookMenuItem;
++ (NSMenuItem *)standardShareMenuItemWithItems:(NSArray *)items;
 @end
index 08e8b69..008e07e 100644 (file)
@@ -1,3 +1,25 @@
+2015-03-23  Tim Horton  <timothy_horton@apple.com>
+
+        Add a share item to the link action menu
+        https://bugs.webkit.org/show_bug.cgi?id=142961
+        <rdar://problem/19198374>
+
+        Reviewed by Beth Dakin.
+
+        * WebView/WebActionMenuController.mm:
+        (-[WebActionMenuController _defaultMenuItemsForImage]):
+        (-[WebActionMenuController _defaultMenuItemsForVideo]):
+        (-[WebActionMenuController _createActionMenuItemForTag:]):
+        (-[WebActionMenuController _createShareActionMenuItemForTag:withItems:]):
+        Adopt -[NSMenuItem standardShareMenuItemWithItems].
+        All of the null-checking is unnecessary as it's impossible to enter
+        these functions with a null image/etc.
+        Also, remove a duplicate isDownloadableMedia check for the 'save to downloads' item.
+
+        (-[WebActionMenuController _defaultMenuItemsForLink]):
+        * WebView/WebUIDelegatePrivate.h:
+        Add a 'Share' item to the link menu.
+
 2015-03-22  Alexey Proskuryakov  <ap@apple.com>
 
         Build fix.
index 75c9b7a..2a34d65 100644 (file)
@@ -266,7 +266,10 @@ static IntRect elementBoundingBoxInWindowCoordinatesFromNode(Node* node)
     RetainPtr<NSMenuItem> openLinkItem = [self _createActionMenuItemForTag:WebActionMenuItemTagOpenLinkInDefaultBrowser];
     RetainPtr<NSMenuItem> readingListItem = [self _createActionMenuItemForTag:WebActionMenuItemTagAddLinkToSafariReadingList];
 
-    return @[ openLinkItem.get(), [NSMenuItem separatorItem], [NSMenuItem separatorItem], readingListItem.get() ];
+    NSURL *url = _hitTestResult.absoluteLinkURL();
+    RetainPtr<NSMenuItem> shareItem = [self _createShareActionMenuItemForTag:WebActionMenuItemTagShareLink withItems:@[ url ]];
+
+    return @[ openLinkItem.get(), shareItem.get(), [NSMenuItem separatorItem], readingListItem.get() ];
 }
 
 #pragma mark mailto: and tel: Link actions
@@ -306,18 +309,9 @@ static IntRect elementBoundingBoxInWindowCoordinatesFromNode(Node* node)
     if (!_webView.downloadDelegate)
         [saveToDownloadsItem setEnabled:NO];
 
-    RetainPtr<NSMenuItem> shareItem = [self _createActionMenuItemForTag:WebActionMenuItemTagShareImage];
-    if (Image* image = _hitTestResult.image()) {
-        RefPtr<SharedBuffer> buffer = image->data();
-        if (buffer) {
-            RetainPtr<NSData> nsData = [NSData dataWithBytes:buffer->data() length:buffer->size()];
-            RetainPtr<NSImage> nsImage = adoptNS([[NSImage alloc] initWithData:nsData.get()]);
-            _sharingServicePicker = adoptNS([[NSSharingServicePicker alloc] initWithItems:@[ nsImage.get() ]]);
-            [_sharingServicePicker setDelegate:self];
-            [shareItem setSubmenu:[_sharingServicePicker menu]];
-        } else
-            [shareItem setEnabled:NO];
-    }
+    RefPtr<SharedBuffer> buffer = _hitTestResult.image()->data();
+    RetainPtr<NSImage> nsImage = adoptNS([[NSImage alloc] initWithData:[NSData dataWithBytes:buffer->data() length:buffer->size()]]);
+    RetainPtr<NSMenuItem> shareItem = [self _createShareActionMenuItemForTag:WebActionMenuItemTagShareImage withItems:@[ nsImage.get() ]];
 
     return @[ copyImageItem.get(), addToPhotosItem.get(), saveToDownloadsItem.get(), shareItem.get() ];
 }
@@ -428,15 +422,8 @@ static NSString *pathToPhotoOnDisk(NSString *suggestedFilename)
     if (!_hitTestResult.isDownloadableMedia() || !_webView.downloadDelegate)
         [saveToDownloadsItem setEnabled:NO];
 
-    RetainPtr<NSMenuItem> shareItem = [self _createActionMenuItemForTag:WebActionMenuItemTagShareVideo];
-    NSString *urlToShare = _hitTestResult.absoluteMediaURL();
-    if (!_hitTestResult.isDownloadableMedia()) {
-        [saveToDownloadsItem setEnabled:NO];
-        urlToShare = [_webView mainFrameURL];
-    }
-    _sharingServicePicker = adoptNS([[NSSharingServicePicker alloc] initWithItems:@[ urlToShare ]]);
-    [_sharingServicePicker setDelegate:self];
-    [shareItem setSubmenu:[_sharingServicePicker menu]];
+    String urlToShare = _hitTestResult.isDownloadableMedia() ? _hitTestResult.absoluteMediaURL() : [_webView mainFrameURL];
+    RetainPtr<NSMenuItem> shareItem = [self _createShareActionMenuItemForTag:WebActionMenuItemTagShareImage withItems:@[ urlToShare ]];
 
     return @[ copyVideoURLItem.get(), [NSMenuItem separatorItem], saveToDownloadsItem.get(), shareItem.get() ];
 }
@@ -701,11 +688,6 @@ static NSString *pathToPhotoOnDisk(NSString *suggestedFilename)
         image = [NSImage imageNamed:@"NSActionMenuSaveToDownloads"];
         break;
 
-    case WebActionMenuItemTagShareImage:
-        title = WEB_UI_STRING_KEY("Share (image action menu item)", "Share (image action menu item)", "image action menu item");
-        image = [NSImage imageNamed:@"NSActionMenuShare"];
-        break;
-
     case WebActionMenuItemTagCopyVideoURL:
         selector = @selector(_copyVideoURL:);
         title = WEB_UI_STRING_KEY("Copy", "Copy (video action menu item)", "video action menu item");
@@ -718,11 +700,6 @@ static NSString *pathToPhotoOnDisk(NSString *suggestedFilename)
         image = [NSImage imageNamed:@"NSActionMenuSaveToDownloads"];
         break;
 
-    case WebActionMenuItemTagShareVideo:
-        title = WEB_UI_STRING_KEY("Share", "Share (video action menu item)", "video action menu item");
-        image = [NSImage imageNamed:@"NSActionMenuShare"];
-        break;
-
     default:
         ASSERT_NOT_REACHED();
         return nil;
@@ -736,6 +713,17 @@ static NSString *pathToPhotoOnDisk(NSString *suggestedFilename)
     return item;
 }
 
+- (RetainPtr<NSMenuItem>)_createShareActionMenuItemForTag:(uint32_t)tag withItems:(NSArray *)items
+{
+    RetainPtr<NSMenuItem> shareItem = [NSMenuItem standardShareMenuItemWithItems:items];
+    [shareItem setTag:tag];
+
+    NSSharingServicePicker *sharingServicePicker = [shareItem representedObject];
+    sharingServicePicker.delegate = self;
+
+    return shareItem;
+}
+
 - (NSArray *)_defaultMenuItems
 {
     NSURL *url = _hitTestResult.absoluteLinkURL();
index cf65a88..667a789 100644 (file)
@@ -127,7 +127,8 @@ enum {
     WebActionMenuItemTagShareImage,
     WebActionMenuItemTagCopyVideoURL,
     WebActionMenuItemTagSaveVideoToDownloads,
-    WebActionMenuItemTagShareVideo
+    WebActionMenuItemTagShareVideo,
+    WebActionMenuItemTagShareLink
 };
 
 typedef enum {
index 5148e96..b3e57b7 100644 (file)
@@ -1,3 +1,29 @@
+2015-03-23  Tim Horton  <timothy_horton@apple.com>
+
+        Add a share item to the link action menu
+        https://bugs.webkit.org/show_bug.cgi?id=142961
+        <rdar://problem/19198374>
+
+        Reviewed by Beth Dakin.
+
+        * UIProcess/mac/WKActionMenuController.mm:
+        (-[WKActionMenuController _hitLinkURL]):
+        Factor out _hitLinkURL from various places.
+
+        (-[WKActionMenuController _openURLFromActionMenu:]):
+        (-[WKActionMenuController _addToReadingListFromActionMenu:]):
+        (-[WKActionMenuController _defaultMenuItemsForVideo]):
+        (-[WKActionMenuController _defaultMenuItemsForImage]):
+        (-[WKActionMenuController _createActionMenuItemForTag:]):
+        (-[WKActionMenuController _createShareActionMenuItemForTag:withItems:]):
+        Adopt -[NSMenuItem standardShareMenuItemWithItems].
+        All of the null-checking is unnecessary as it's impossible to enter
+        these functions with a null image/etc.
+
+        (-[WKActionMenuController _defaultMenuItemsForLink]):
+        * Shared/API/c/WKActionMenuItemTypes.h:
+        Add a 'Share' item to the link menu.
+
 2015-03-23  Zan Dobersek  <zdobersek@igalia.com>
 
         [GTK] Use std::abs() in ClickCounter::currentClickCountForGdkButtonEvent()
index c28df52..3bad235 100644 (file)
@@ -47,7 +47,8 @@ enum {
     kWKContextActionItemTagTextSuggestions,
     kWKContextActionItemTagCopyVideoURL,
     kWKContextActionItemTagSaveVideoToDownloads,
-    kWKContextActionItemTagShareVideo
+    kWKContextActionItemTagShareVideo,
+    kWKContextActionItemTagShareLink
 };
 
 #ifdef __cplusplus
index 2de8dad..c0a554d 100644 (file)
@@ -180,25 +180,30 @@ using namespace WebKit;
 
 #pragma mark Link actions
 
+- (NSURL *)_hitLinkURL
+{
+    RefPtr<WebHitTestResult> hitTestResult = [self _webHitTestResult];
+    return [NSURL _web_URLWithWTFString:hitTestResult->absoluteLinkURL()];
+}
+
 - (NSArray *)_defaultMenuItemsForLink
 {
     RetainPtr<NSMenuItem> openLinkItem = [self _createActionMenuItemForTag:kWKContextActionItemTagOpenLinkInDefaultBrowser];
     RetainPtr<NSMenuItem> readingListItem = [self _createActionMenuItemForTag:kWKContextActionItemTagAddLinkToSafariReadingList];
+    RetainPtr<NSMenuItem> shareItem = [self _createShareActionMenuItemForTag:kWKContextActionItemTagShareLink withItems:@[ [self _hitLinkURL] ]];
 
-    return @[ openLinkItem.get(), [NSMenuItem separatorItem], [NSMenuItem separatorItem], readingListItem.get() ];
+    return @[ openLinkItem.get(), shareItem.get(), [NSMenuItem separatorItem], readingListItem.get() ];
 }
 
 - (void)_openURLFromActionMenu:(id)sender
 {
-    RefPtr<WebHitTestResult> hitTestResult = [self _webHitTestResult];
-    [[NSWorkspace sharedWorkspace] openURL:[NSURL _web_URLWithWTFString:hitTestResult->absoluteLinkURL()]];
+    [[NSWorkspace sharedWorkspace] openURL:[self _hitLinkURL]];
 }
 
 - (void)_addToReadingListFromActionMenu:(id)sender
 {
-    RefPtr<WebHitTestResult> hitTestResult = [self _webHitTestResult];
     NSSharingService *service = [NSSharingService sharingServiceNamed:NSSharingServiceNameAddToSafariReadingList];
-    [service performWithItems:@[ [NSURL _web_URLWithWTFString:hitTestResult->absoluteLinkURL()] ]];
+    [service performWithItems:@[ [self _hitLinkURL] ]];
 }
 
 #pragma mark Video actions
@@ -206,22 +211,11 @@ using namespace WebKit;
 - (NSArray *)_defaultMenuItemsForVideo
 {
     RetainPtr<NSMenuItem> copyVideoURLItem = [self _createActionMenuItemForTag:kWKContextActionItemTagCopyVideoURL];
-
-    RefPtr<WebHitTestResult> hitTestResult = [self _webHitTestResult];
     RetainPtr<NSMenuItem> saveToDownloadsItem = [self _createActionMenuItemForTag:kWKContextActionItemTagSaveVideoToDownloads];
-    RetainPtr<NSMenuItem> shareItem = [self _createActionMenuItemForTag:kWKContextActionItemTagShareVideo];
-
-    String urlToShare = hitTestResult->absoluteMediaURL();
-    if (!hitTestResult->isDownloadableMedia()) {
-        [saveToDownloadsItem setEnabled:NO];
-        urlToShare = _page->mainFrame()->url();
-    }
 
-    if (!urlToShare.isEmpty()) {
-        _sharingServicePicker = adoptNS([[NSSharingServicePicker alloc] initWithItems:@[ urlToShare ]]);
-        [_sharingServicePicker setDelegate:self];
-        [shareItem setSubmenu:[_sharingServicePicker menu]];
-    }
+    RefPtr<WebHitTestResult> hitTestResult = [self _webHitTestResult];
+    String urlToShare = hitTestResult->isDownloadableMedia() ? hitTestResult->absoluteMediaURL() : _page->mainFrame()->url();
+    RetainPtr<NSMenuItem> shareItem = [self _createShareActionMenuItemForTag:kWKContextActionItemTagShareVideo withItems:@[ urlToShare ]];
 
     return @[ copyVideoURLItem.get(), [NSMenuItem separatorItem], saveToDownloadsItem.get(), shareItem.get() ];
 }
@@ -264,13 +258,7 @@ using namespace WebKit;
     else
         addToPhotosItem = [NSMenuItem separatorItem];
     RetainPtr<NSMenuItem> saveToDownloadsItem = [self _createActionMenuItemForTag:kWKContextActionItemTagSaveImageToDownloads];
-    RetainPtr<NSMenuItem> shareItem = [self _createActionMenuItemForTag:kWKContextActionItemTagShareImage];
-
-    if (RetainPtr<NSImage> image = [self _hitTestResultImage]) {
-        _sharingServicePicker = adoptNS([[NSSharingServicePicker alloc] initWithItems:@[ image.get() ]]);
-        [_sharingServicePicker setDelegate:self];
-        [shareItem setSubmenu:[_sharingServicePicker menu]];
-    }
+    RetainPtr<NSMenuItem> shareItem = [self _createShareActionMenuItemForTag:kWKContextActionItemTagShareImage withItems:@[ [self _hitTestResultImage] ]];
 
     return @[ copyImageItem.get(), addToPhotosItem.get(), saveToDownloadsItem.get(), shareItem.get() ];
 }
@@ -604,11 +592,6 @@ static NSString *pathToPhotoOnDisk(NSString *suggestedFilename)
         image = [NSImage imageNamed:@"NSActionMenuSaveToDownloads"];
         break;
 
-    case kWKContextActionItemTagShareImage:
-        title = WEB_UI_STRING_KEY("Share (image action menu item)", "Share (image action menu item)", "image action menu item");
-        image = [NSImage imageNamed:@"NSActionMenuShare"];
-        break;
-
     case kWKContextActionItemTagCopyText:
         selector = @selector(_copySelection:);
         title = WEB_UI_STRING_KEY("Copy", "Copy (text action menu item)", "text action menu item");
@@ -639,11 +622,6 @@ static NSString *pathToPhotoOnDisk(NSString *suggestedFilename)
         image = [NSImage imageNamed:@"NSActionMenuSaveToDownloads"];
         break;
 
-    case kWKContextActionItemTagShareVideo:
-        title = WEB_UI_STRING_KEY("Share", "Share (video action menu item)", "video action menu item");
-        image = [NSImage imageNamed:@"NSActionMenuShare"];
-        break;
-
     default:
         ASSERT_NOT_REACHED();
         return nil;
@@ -657,6 +635,17 @@ static NSString *pathToPhotoOnDisk(NSString *suggestedFilename)
     return item;
 }
 
+- (RetainPtr<NSMenuItem>)_createShareActionMenuItemForTag:(uint32_t)tag withItems:(NSArray *)items
+{
+    RetainPtr<NSMenuItem> shareItem = [NSMenuItem standardShareMenuItemWithItems:items];
+    [shareItem setTag:tag];
+
+    NSSharingServicePicker *sharingServicePicker = [shareItem representedObject];
+    sharingServicePicker.delegate = self;
+
+    return shareItem;
+}
+
 - (PassRefPtr<WebHitTestResult>)_webHitTestResult
 {
     RefPtr<WebHitTestResult> hitTestResult;