Title: [289851] trunk
Revision
289851
Author
wenson_hs...@apple.com
Date
2022-02-15 13:57:03 -0800 (Tue, 15 Feb 2022)

Log Message

[macOS] Add a context menu item to "Copy Cropped Image"
https://bugs.webkit.org/show_bug.cgi?id=236602
rdar://88924479

Reviewed by Megan Gardner.

Source/WebCore:

Add ContextMenuItemTagCopyCroppedImage, and handle it in various context menu codepaths throughout WebCore.

* loader/EmptyClients.cpp:
* page/ContextMenuClient.h:

Add a client hook to allow us to disable this item in WebKitLegacy, but enable it in the modern WebKit port.

* page/ContextMenuController.cpp:
(WebCore::ContextMenuController::contextMenuItemSelected):
(WebCore::ContextMenuController::populate):

Insert this new item, adjacent to the existing "Copy Image" item.

(WebCore::ContextMenuController::checkOrEnableIfNeeded const):
* platform/ContextMenuItem.cpp:
(WebCore::isValidContextMenuAction):
* platform/ContextMenuItem.h:

Add a new context menu item tag for ContextMenuItemTagCopyCroppedImage; additionally, add a new special tag,
ContextMenuItemLastNonCustomTag, that will always point to the last non-custom (engine-supported) context menu
item in this enumeration. This makes it so that we can stop fiddling with the API test
`WebCore.ContextMenuAction_IsValidEnum` every time we add a new context menu type, as long as we update the
ContextMenuItemLastNonCustomTag.

* platform/LocalizedStrings.h:
* platform/cocoa/LocalizedStringsCocoa.mm:

Pull the localized string for "Copy Cropped Image" out into a separate localized string helper function.

(WebCore::contextMenuItemTagCopyCroppedImage):

Source/WebKit:

Add support for a new context menu item that invokes markup when copying an image. See below for more details.

* Shared/API/c/WKContextMenuItemTypes.h:
* Shared/API/c/WKSharedAPICast.h:
(WebKit::toAPI):
(WebKit::toImpl):

Handle the new context menu enum tag.

* Shared/WebHitTestResultData.cpp:
(WebKit::WebHitTestResultData::WebHitTestResultData):
(WebKit::WebHitTestResultData::encode const):
(WebKit::WebHitTestResultData::decode):
* Shared/WebHitTestResultData.h:

Add a new `sourceImageMIMEType` member that provides the original MIME type of the source image corresponding to
the image bitmap in WebHitTestResultData. We use this below, in `handleContextMenuCopyCroppedImage`.

* UIProcess/API/Cocoa/_WKElementAction.mm:
(+[_WKElementAction _elementActionWithType:customTitle:assistant:]):

Replace the localized string macro with a call to the new localized string helper function.

* UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::contextMenuItemSelected):
* UIProcess/WebPageProxy.h:
* UIProcess/mac/WebPageProxyMac.mm:
(WebKit::WebPageProxy::handleContextMenuCopyCroppedImage):

Handle the new context menu item action by calling into `requestImageAnalysisMarkup` and writing the result
to the pasteboard (transcoding back to the MIME type of the source image, if possible).

* WebProcess/WebCoreSupport/WebContextMenuClient.h:

Source/WebKitLegacy/mac:

See WebCore and WebKit ChangeLogs for more details.

* WebCoreSupport/WebContextMenuClient.h:
* WebView/WebHTMLView.mm:
(toTag):

Tools:

Adjust an existing API test to account for the fact that ContextMenuItemTagTranslate is no longer the last non-
custom context menu item tag.

* TestWebKitAPI/Tests/WebCore/ContextMenuAction.cpp:
(TestWebKitAPI::TEST):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (289850 => 289851)


--- trunk/Source/WebCore/ChangeLog	2022-02-15 21:51:11 UTC (rev 289850)
+++ trunk/Source/WebCore/ChangeLog	2022-02-15 21:57:03 UTC (rev 289851)
@@ -1,3 +1,42 @@
+2022-02-15  Wenson Hsieh  <wenson_hs...@apple.com>
+
+        [macOS] Add a context menu item to "Copy Cropped Image"
+        https://bugs.webkit.org/show_bug.cgi?id=236602
+        rdar://88924479
+
+        Reviewed by Megan Gardner.
+
+        Add ContextMenuItemTagCopyCroppedImage, and handle it in various context menu codepaths throughout WebCore.
+
+        * loader/EmptyClients.cpp:
+        * page/ContextMenuClient.h:
+
+        Add a client hook to allow us to disable this item in WebKitLegacy, but enable it in the modern WebKit port.
+
+        * page/ContextMenuController.cpp:
+        (WebCore::ContextMenuController::contextMenuItemSelected):
+        (WebCore::ContextMenuController::populate):
+
+        Insert this new item, adjacent to the existing "Copy Image" item.
+
+        (WebCore::ContextMenuController::checkOrEnableIfNeeded const):
+        * platform/ContextMenuItem.cpp:
+        (WebCore::isValidContextMenuAction):
+        * platform/ContextMenuItem.h:
+
+        Add a new context menu item tag for ContextMenuItemTagCopyCroppedImage; additionally, add a new special tag,
+        ContextMenuItemLastNonCustomTag, that will always point to the last non-custom (engine-supported) context menu
+        item in this enumeration. This makes it so that we can stop fiddling with the API test
+        `WebCore.ContextMenuAction_IsValidEnum` every time we add a new context menu type, as long as we update the
+        ContextMenuItemLastNonCustomTag.
+
+        * platform/LocalizedStrings.h:
+        * platform/cocoa/LocalizedStringsCocoa.mm:
+
+        Pull the localized string for "Copy Cropped Image" out into a separate localized string helper function.
+
+        (WebCore::contextMenuItemTagCopyCroppedImage):
+
 2022-02-15  Tim Nguyen  <n...@apple.com>
 
         Copy PDF.js in WebCore.framework bundle at build-time

Modified: trunk/Source/WebCore/loader/EmptyClients.cpp (289850 => 289851)


--- trunk/Source/WebCore/loader/EmptyClients.cpp	2022-02-15 21:51:11 UTC (rev 289850)
+++ trunk/Source/WebCore/loader/EmptyClients.cpp	2022-02-15 21:57:03 UTC (rev 289851)
@@ -142,6 +142,10 @@
 #if ENABLE(IMAGE_ANALYSIS)
     bool supportsLookUpInImages() final { return false; }
 #endif
+
+#if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
+    bool supportsCopyCroppedImage() final { return false; }
+#endif
 };
 
 #endif // ENABLE(CONTEXT_MENUS)

Modified: trunk/Source/WebCore/page/ContextMenuClient.h (289850 => 289851)


--- trunk/Source/WebCore/page/ContextMenuClient.h	2022-02-15 21:51:11 UTC (rev 289850)
+++ trunk/Source/WebCore/page/ContextMenuClient.h	2022-02-15 21:57:03 UTC (rev 289851)
@@ -55,6 +55,10 @@
     virtual bool supportsLookUpInImages() = 0;
 #endif
 
+#if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
+    virtual bool supportsCopyCroppedImage() = 0;
+#endif
+
 #if HAVE(TRANSLATION_UI_SERVICES)
     virtual void handleTranslation(const TranslationContextMenuInfo&) = 0;
 #endif

Modified: trunk/Source/WebCore/page/ContextMenuController.cpp (289850 => 289851)


--- trunk/Source/WebCore/page/ContextMenuController.cpp	2022-02-15 21:51:11 UTC (rev 289850)
+++ trunk/Source/WebCore/page/ContextMenuController.cpp	2022-02-15 21:57:03 UTC (rev 289851)
@@ -526,8 +526,9 @@
     case ContextMenuItemTagDictationAlternative:
         frame->editor().applyDictationAlternative(title);
         break;
+    case ContextMenuItemTagCopyCroppedImage:
     case ContextMenuItemTagQuickLookImage:
-        // This should be handled at the client layer.
+        // These should be handled at the client layer.
         ASSERT_NOT_REACHED();
         break;
     case ContextMenuItemTagTranslate:
@@ -842,6 +843,10 @@
     ContextMenuItem ShareMenuItem(SubmenuType, ContextMenuItemTagShareMenu, emptyString());
 #endif
 
+#if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
+    ContextMenuItem copyCroppedImageItem { ActionType, ContextMenuItemTagCopyCroppedImage, contextMenuItemTagCopyCroppedImage() };
+#endif
+
     Node* node = m_context.hitTestResult().innerNonSharedNode();
     if (!node)
         return;
@@ -912,10 +917,15 @@
             if (imageURL.isLocalFile() || image) {
                 appendItem(CopyImageItem, m_contextMenu.get());
 
+                if (image && !image->isAnimated()) {
+#if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
+                    if (m_client.supportsCopyCroppedImage())
+                        appendItem(copyCroppedImageItem, m_contextMenu.get());
+#endif
 #if ENABLE(IMAGE_ANALYSIS)
-                if (m_client.supportsLookUpInImages() && image && !image->isAnimated())
-                    shouldAppendQuickLookImageItem = true;
+                    shouldAppendQuickLookImageItem = m_client.supportsLookUpInImages();
 #endif
+                }
             }
 #if PLATFORM(GTK)
             appendItem(CopyImageUrlItem, m_contextMenu.get());
@@ -1417,6 +1427,7 @@
         case ContextMenuItemTagCopyLinkToClipboard:
         case ContextMenuItemTagOpenImageInNewWindow:
         case ContextMenuItemTagCopyImageToClipboard:
+        case ContextMenuItemTagCopyCroppedImage:
 #if PLATFORM(GTK)
         case ContextMenuItemTagCopyImageUrlToClipboard:
 #endif

Modified: trunk/Source/WebCore/platform/ContextMenuItem.cpp (289850 => 289851)


--- trunk/Source/WebCore/platform/ContextMenuItem.cpp	2022-02-15 21:51:11 UTC (rev 289850)
+++ trunk/Source/WebCore/platform/ContextMenuItem.cpp	2022-02-15 21:57:03 UTC (rev 289851)
@@ -153,6 +153,7 @@
     case ContextMenuAction::ContextMenuItemTagOpenImageInNewWindow:
     case ContextMenuAction::ContextMenuItemTagDownloadImageToDisk:
     case ContextMenuAction::ContextMenuItemTagCopyImageToClipboard:
+    case ContextMenuAction::ContextMenuItemTagCopyCroppedImage:
 #if PLATFORM(GTK)
     case ContextMenuAction::ContextMenuItemTagCopyImageUrlToClipboard:
 #endif

Modified: trunk/Source/WebCore/platform/ContextMenuItem.h (289850 => 289851)


--- trunk/Source/WebCore/platform/ContextMenuItem.h	2022-02-15 21:51:11 UTC (rev 289850)
+++ trunk/Source/WebCore/platform/ContextMenuItem.h	2022-02-15 21:57:03 UTC (rev 289851)
@@ -149,6 +149,8 @@
     ContextMenuItemTagAddHighlightToNewQuickNote,
     ContextMenuItemTagQuickLookImage,
     ContextMenuItemTagTranslate,
+    ContextMenuItemTagCopyCroppedImage,
+    ContextMenuItemLastNonCustomTag = ContextMenuItemTagCopyCroppedImage,
     ContextMenuItemBaseCustomTag = 5000,
     ContextMenuItemLastCustomTag = 5999,
     ContextMenuItemBaseApplicationTag = 10000

Modified: trunk/Source/WebCore/platform/LocalizedStrings.h (289850 => 289851)


--- trunk/Source/WebCore/platform/LocalizedStrings.h	2022-02-15 21:51:11 UTC (rev 289850)
+++ trunk/Source/WebCore/platform/LocalizedStrings.h	2022-02-15 21:57:03 UTC (rev 289851)
@@ -377,6 +377,10 @@
     WEBCORE_EXPORT String contextMenuItemTagQuickLookImageForVisualSearch();
 #endif // ENABLE(IMAGE_ANALYSIS)
 
+#if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
+    WEBCORE_EXPORT String contextMenuItemTagCopyCroppedImage();
+#endif
+
 #if HAVE(TRANSLATION_UI_SERVICES)
     String contextMenuItemTagTranslate(const String& selectedString);
 #endif

Modified: trunk/Source/WebCore/platform/cocoa/LocalizedStringsCocoa.mm (289850 => 289851)


--- trunk/Source/WebCore/platform/cocoa/LocalizedStringsCocoa.mm	2022-02-15 21:51:11 UTC (rev 289850)
+++ trunk/Source/WebCore/platform/cocoa/LocalizedStringsCocoa.mm	2022-02-15 21:57:03 UTC (rev 289851)
@@ -319,4 +319,13 @@
 
 #endif // ENABLE(IMAGE_ANALYSIS)
 
+#if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
+
+String contextMenuItemTagCopyCroppedImage()
+{
+    return WEB_UI_STRING("Copy Cropped Image", "Title for Copy Cropped Image");
+}
+
+#endif
+
 } // namespace WebCore

Modified: trunk/Source/WebKit/ChangeLog (289850 => 289851)


--- trunk/Source/WebKit/ChangeLog	2022-02-15 21:51:11 UTC (rev 289850)
+++ trunk/Source/WebKit/ChangeLog	2022-02-15 21:57:03 UTC (rev 289851)
@@ -1,3 +1,45 @@
+2022-02-15  Wenson Hsieh  <wenson_hs...@apple.com>
+
+        [macOS] Add a context menu item to "Copy Cropped Image"
+        https://bugs.webkit.org/show_bug.cgi?id=236602
+        rdar://88924479
+
+        Reviewed by Megan Gardner.
+
+        Add support for a new context menu item that invokes markup when copying an image. See below for more details.
+
+        * Shared/API/c/WKContextMenuItemTypes.h:
+        * Shared/API/c/WKSharedAPICast.h:
+        (WebKit::toAPI):
+        (WebKit::toImpl):
+
+        Handle the new context menu enum tag.
+
+        * Shared/WebHitTestResultData.cpp:
+        (WebKit::WebHitTestResultData::WebHitTestResultData):
+        (WebKit::WebHitTestResultData::encode const):
+        (WebKit::WebHitTestResultData::decode):
+        * Shared/WebHitTestResultData.h:
+
+        Add a new `sourceImageMIMEType` member that provides the original MIME type of the source image corresponding to
+        the image bitmap in WebHitTestResultData. We use this below, in `handleContextMenuCopyCroppedImage`.
+
+        * UIProcess/API/Cocoa/_WKElementAction.mm:
+        (+[_WKElementAction _elementActionWithType:customTitle:assistant:]):
+
+        Replace the localized string macro with a call to the new localized string helper function.
+
+        * UIProcess/WebPageProxy.cpp:
+        (WebKit::WebPageProxy::contextMenuItemSelected):
+        * UIProcess/WebPageProxy.h:
+        * UIProcess/mac/WebPageProxyMac.mm:
+        (WebKit::WebPageProxy::handleContextMenuCopyCroppedImage):
+
+        Handle the new context menu item action by calling into `requestImageAnalysisMarkup` and writing the result
+        to the pasteboard (transcoding back to the MIME type of the source image, if possible).
+
+        * WebProcess/WebCoreSupport/WebContextMenuClient.h:
+
 2022-02-15  Fujii Hironori  <hironori.fu...@sony.com>
 
         [WinCairo][WK2] animations/background-position.html is timing out

Modified: trunk/Source/WebKit/Shared/API/c/WKContextMenuItemTypes.h (289850 => 289851)


--- trunk/Source/WebKit/Shared/API/c/WKContextMenuItemTypes.h	2022-02-15 21:51:11 UTC (rev 289850)
+++ trunk/Source/WebKit/Shared/API/c/WKContextMenuItemTypes.h	2022-02-15 21:57:03 UTC (rev 289851)
@@ -127,6 +127,7 @@
     kWKContextMenuItemTagAddHighlightToNewQuickNote,
     kWKContextMenuItemTagRevealImage,
     kWKContextMenuItemTagTranslate,
+    kWKContextMenuItemTagCopyCroppedImage,
     kWKContextMenuItemBaseApplicationTag = 10000
 };
 typedef uint32_t WKContextMenuItemTag;

Modified: trunk/Source/WebKit/Shared/API/c/WKSharedAPICast.h (289850 => 289851)


--- trunk/Source/WebKit/Shared/API/c/WKSharedAPICast.h	2022-02-15 21:51:11 UTC (rev 289850)
+++ trunk/Source/WebKit/Shared/API/c/WKSharedAPICast.h	2022-02-15 21:57:03 UTC (rev 289851)
@@ -547,6 +547,8 @@
         return kWKContextMenuItemTagRevealImage;
     case WebCore::ContextMenuItemTagTranslate:
         return kWKContextMenuItemTagTranslate;
+    case WebCore::ContextMenuItemTagCopyCroppedImage:
+        return kWKContextMenuItemTagCopyCroppedImage;
     default:
         if (action < WebCore::ContextMenuItemBaseApplicationTag && !(action >= WebCore::ContextMenuItemBaseCustomTag && action <= WebCore::ContextMenuItemLastCustomTag))
             LOG_ERROR("ContextMenuAction %i is an unknown tag but is below the allowable custom tag value of %i", action, WebCore::ContextMenuItemBaseApplicationTag);
@@ -749,6 +751,8 @@
         return WebCore::ContextMenuItemTagQuickLookImage;
     case kWKContextMenuItemTagTranslate:
         return WebCore::ContextMenuItemTagTranslate;
+    case kWKContextMenuItemTagCopyCroppedImage:
+        return WebCore::ContextMenuItemTagCopyCroppedImage;
     case kWKContextMenuItemTagOpenLinkInThisWindow:
     default:
         if (tag < kWKContextMenuItemBaseApplicationTag && !(tag >= WebCore::ContextMenuItemBaseCustomTag && tag <= WebCore::ContextMenuItemLastCustomTag))

Modified: trunk/Source/WebKit/Shared/WebHitTestResultData.cpp (289850 => 289851)


--- trunk/Source/WebKit/Shared/WebHitTestResultData.cpp	2022-02-15 21:51:11 UTC (rev 289850)
+++ trunk/Source/WebKit/Shared/WebHitTestResultData.cpp	2022-02-15 21:57:03 UTC (rev 289851)
@@ -39,7 +39,7 @@
 {
 }
 
-WebHitTestResultData::WebHitTestResultData(const WebCore::HitTestResult& hitTestResult, const String& toolTipText)
+WebHitTestResultData::WebHitTestResultData(const HitTestResult& hitTestResult, const String& toolTipText)
     : absoluteImageURL(hitTestResult.absoluteImageURL().string())
     , absolutePDFURL(hitTestResult.absolutePDFURL().string())
     , absoluteLinkURL(hitTestResult.absoluteLinkURL().string())
@@ -61,7 +61,7 @@
         isScrollbar = scrollbar->orientation() == ScrollbarOrientation::Horizontal ? IsScrollbar::Horizontal : IsScrollbar::Vertical;
 }
 
-WebHitTestResultData::WebHitTestResultData(const WebCore::HitTestResult& hitTestResult, bool includeImage)
+WebHitTestResultData::WebHitTestResultData(const HitTestResult& hitTestResult, bool includeImage)
     : absoluteImageURL(hitTestResult.absoluteImageURL().string())
     , absolutePDFURL(hitTestResult.absolutePDFURL().string())
     , absoluteLinkURL(hitTestResult.absoluteLinkURL().string())
@@ -92,8 +92,15 @@
         }
     }
 
-    if (auto target = RefPtr { hitTestResult.innerNonSharedNode() }; target && is<RenderImage>(target->renderer()))
-        imageBitmap = createShareableBitmap(*downcast<RenderImage>(target->renderer()));
+    if (auto target = RefPtr { hitTestResult.innerNonSharedNode() }) {
+        if (auto renderer = dynamicDowncast<RenderImage>(target->renderer())) {
+            imageBitmap = createShareableBitmap(*downcast<RenderImage>(target->renderer()));
+            if (auto* cachedImage = renderer->cachedImage()) {
+                if (auto* image = cachedImage->image())
+                    sourceImageMIMEType = image->mimeType();
+            }
+        }
+    }
 }
 
 WebHitTestResultData::~WebHitTestResultData()
@@ -130,6 +137,7 @@
     if (imageBitmap)
         imageBitmap->createHandle(imageBitmapHandle, SharedMemory::Protection::ReadOnly);
     encoder << imageBitmapHandle;
+    encoder << sourceImageMIMEType;
 
     bool hasLinkTextIndicator = linkTextIndicator;
     encoder << hasLinkTextIndicator;
@@ -183,6 +191,9 @@
     if (!imageBitmapHandle.isNull())
         hitTestResultData.imageBitmap = ShareableBitmap::create(imageBitmapHandle, SharedMemory::Protection::ReadOnly);
 
+    if (!decoder.decode(hitTestResultData.sourceImageMIMEType))
+        return false;
+
     bool hasLinkTextIndicator;
     if (!decoder.decode(hasLinkTextIndicator))
         return false;

Modified: trunk/Source/WebKit/Shared/WebHitTestResultData.h (289850 => 289851)


--- trunk/Source/WebKit/Shared/WebHitTestResultData.h	2022-02-15 21:51:11 UTC (rev 289850)
+++ trunk/Source/WebKit/Shared/WebHitTestResultData.h	2022-02-15 21:57:03 UTC (rev 289851)
@@ -66,6 +66,7 @@
     RefPtr<SharedMemory> imageSharedMemory;
     uint64_t imageSize;
     RefPtr<ShareableBitmap> imageBitmap;
+    String sourceImageMIMEType;
 
 #if PLATFORM(MAC)
     RetainPtr<DDActionContext> detectedDataActionContext;

Modified: trunk/Source/WebKit/UIProcess/API/Cocoa/_WKElementAction.mm (289850 => 289851)


--- trunk/Source/WebKit/UIProcess/API/Cocoa/_WKElementAction.mm	2022-02-15 21:51:11 UTC (rev 289850)
+++ trunk/Source/WebKit/UIProcess/API/Cocoa/_WKElementAction.mm	2022-02-15 21:57:03 UTC (rev 289851)
@@ -180,7 +180,7 @@
         break;
     case _WKElementActionTypeCopyCroppedImage:
 #if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
-        title = WEB_UI_STRING("Copy Cropped Image", "Title for Copy Cropped Image");
+        title = WebCore::contextMenuItemTagCopyCroppedImage();
         handler = ^(WKActionSheetAssistant *assistant, _WKActivatedElementInfo *actionInfo) {
             [assistant handleElementActionWithType:type element:actionInfo needsInteraction:YES];
         };

Modified: trunk/Source/WebKit/UIProcess/WebPageProxy.cpp (289850 => 289851)


--- trunk/Source/WebKit/UIProcess/WebPageProxy.cpp	2022-02-15 21:51:11 UTC (rev 289850)
+++ trunk/Source/WebKit/UIProcess/WebPageProxy.cpp	2022-02-15 21:57:03 UTC (rev 289851)
@@ -7113,6 +7113,13 @@
 #endif
         return;
 
+    case ContextMenuItemTagCopyCroppedImage:
+#if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
+        if (hitTestData.imageBitmap)
+            handleContextMenuCopyCroppedImage(*hitTestData.imageBitmap, hitTestData.sourceImageMIMEType);
+#endif
+        return;
+
     default:
         break;
     }

Modified: trunk/Source/WebKit/UIProcess/WebPageProxy.h (289850 => 289851)


--- trunk/Source/WebKit/UIProcess/WebPageProxy.h	2022-02-15 21:51:11 UTC (rev 289850)
+++ trunk/Source/WebKit/UIProcess/WebPageProxy.h	2022-02-15 21:57:03 UTC (rev 289851)
@@ -2009,10 +2009,14 @@
 
     void dispatchWheelEventWithoutScrolling(const WebWheelEvent&, CompletionHandler<void(bool)>&&);
 
-#if ENABLE(IMAGE_ANALYSIS) && ENABLE(CONTEXT_MENUS)
-    void handleContextMenuLookUpImage();
+#if ENABLE(CONTEXT_MENUS)
+#if ENABLE(IMAGE_ANALYSIS)
     void handleContextMenuQuickLookImage(QuickLookPreviewActivity);
 #endif
+#if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
+    void handleContextMenuCopyCroppedImage(ShareableBitmap&, const String& preferredMIMEType);
+#endif
+#endif // ENABLE(CONTEXT_MENUS)
 
 #if USE(APPKIT)
     void beginPreviewPanelControl(QLPreviewPanel *);

Modified: trunk/Source/WebKit/UIProcess/mac/WebPageProxyMac.mm (289850 => 289851)


--- trunk/Source/WebKit/UIProcess/mac/WebPageProxyMac.mm	2022-02-15 21:51:11 UTC (rev 289850)
+++ trunk/Source/WebKit/UIProcess/mac/WebPageProxyMac.mm	2022-02-15 21:57:03 UTC (rev 289851)
@@ -29,6 +29,7 @@
 #if PLATFORM(MAC)
 
 #import "APIUIClient.h"
+#import "CocoaImage.h"
 #import "Connection.h"
 #import "DataReference.h"
 #import "EditorState.h"
@@ -43,6 +44,7 @@
 #import "RemoteLayerTreeHost.h"
 #import "StringUtilities.h"
 #import "TextChecker.h"
+#import "TextRecognitionUtilities.h"
 #import "WKBrowsingContextControllerInternal.h"
 #import "WKQuickLookPreviewController.h"
 #import "WKSharingServicePickerDelegate.h"
@@ -773,6 +775,31 @@
 
 #endif // ENABLE(IMAGE_ANALYSIS)
 
+#if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
+
+void WebPageProxy::handleContextMenuCopyCroppedImage(ShareableBitmap& imageBitmap, const String& preferredMIMEType)
+{
+    auto originalImage = imageBitmap.makeCGImage();
+    if (!originalImage)
+        return;
+
+    auto changeCount = NSPasteboard.generalPasteboard.changeCount;
+    requestImageAnalysisMarkup(originalImage.get(), [changeCount, originalImage, preferredMIMEType](CGImageRef resultImage) {
+        auto pasteboard = NSPasteboard.generalPasteboard;
+        if (changeCount != pasteboard.changeCount)
+            return;
+
+        auto [data, type] = WebKit::transcodeWithPreferredMIMEType(resultImage ?: originalImage.get(), preferredMIMEType.createCFString().get(), (__bridge CFStringRef)UTTypeTIFF.identifier);
+        if (!data)
+            return;
+
+        [pasteboard declareTypes:@[(__bridge NSString *)type.get()] owner:nil];
+        [pasteboard setData:data.get() forType:(__bridge NSString *)type.get()];
+    });
+}
+
+#endif // ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
+
 } // namespace WebKit
 
 #endif // PLATFORM(MAC)

Modified: trunk/Source/WebKit/WebProcess/WebCoreSupport/WebContextMenuClient.h (289850 => 289851)


--- trunk/Source/WebKit/WebProcess/WebCoreSupport/WebContextMenuClient.h	2022-02-15 21:51:11 UTC (rev 289850)
+++ trunk/Source/WebKit/WebProcess/WebCoreSupport/WebContextMenuClient.h	2022-02-15 21:57:03 UTC (rev 289851)
@@ -56,6 +56,10 @@
     bool supportsLookUpInImages() final { return true; }
 #endif
 
+#if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
+    bool supportsCopyCroppedImage() final { return true; }
+#endif
+
 #if PLATFORM(COCOA)
     void searchWithSpotlight() override;
 #endif

Modified: trunk/Source/WebKitLegacy/mac/ChangeLog (289850 => 289851)


--- trunk/Source/WebKitLegacy/mac/ChangeLog	2022-02-15 21:51:11 UTC (rev 289850)
+++ trunk/Source/WebKitLegacy/mac/ChangeLog	2022-02-15 21:57:03 UTC (rev 289851)
@@ -1,3 +1,17 @@
+2022-02-15  Wenson Hsieh  <wenson_hs...@apple.com>
+
+        [macOS] Add a context menu item to "Copy Cropped Image"
+        https://bugs.webkit.org/show_bug.cgi?id=236602
+        rdar://88924479
+
+        Reviewed by Megan Gardner.
+
+        See WebCore and WebKit ChangeLogs for more details.
+
+        * WebCoreSupport/WebContextMenuClient.h:
+        * WebView/WebHTMLView.mm:
+        (toTag):
+
 2022-02-11  Chris Dumez  <cdu...@apple.com>
 
         Unreviewed, fix build with the latest iOS SDK.

Modified: trunk/Source/WebKitLegacy/mac/WebCoreSupport/WebContextMenuClient.h (289850 => 289851)


--- trunk/Source/WebKitLegacy/mac/WebCoreSupport/WebContextMenuClient.h	2022-02-15 21:51:11 UTC (rev 289850)
+++ trunk/Source/WebKitLegacy/mac/WebCoreSupport/WebContextMenuClient.h	2022-02-15 21:57:03 UTC (rev 289851)
@@ -64,6 +64,10 @@
     bool supportsLookUpInImages() final { return false; }
 #endif
 
+#if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
+    bool supportsCopyCroppedImage() final { return false; }
+#endif
+
 #if ENABLE(SERVICE_CONTROLS)
     // WebSharingServicePickerClient
     void sharingServicePickerWillBeDestroyed(WebSharingServicePickerController &) override;

Modified: trunk/Source/WebKitLegacy/mac/WebView/WebHTMLView.mm (289850 => 289851)


--- trunk/Source/WebKitLegacy/mac/WebView/WebHTMLView.mm	2022-02-15 21:51:11 UTC (rev 289850)
+++ trunk/Source/WebKitLegacy/mac/WebView/WebHTMLView.mm	2022-02-15 21:57:03 UTC (rev 289851)
@@ -600,6 +600,7 @@
     case ContextMenuItemTagTranslate:
         return WebMenuItemTagTranslate;
     case ContextMenuItemTagQuickLookImage:
+    case ContextMenuItemTagCopyCroppedImage:
         return std::nullopt;
 
     case ContextMenuItemBaseCustomTag ... ContextMenuItemLastCustomTag:

Modified: trunk/Tools/ChangeLog (289850 => 289851)


--- trunk/Tools/ChangeLog	2022-02-15 21:51:11 UTC (rev 289850)
+++ trunk/Tools/ChangeLog	2022-02-15 21:57:03 UTC (rev 289851)
@@ -1,3 +1,17 @@
+2022-02-15  Wenson Hsieh  <wenson_hs...@apple.com>
+
+        [macOS] Add a context menu item to "Copy Cropped Image"
+        https://bugs.webkit.org/show_bug.cgi?id=236602
+        rdar://88924479
+
+        Reviewed by Megan Gardner.
+
+        Adjust an existing API test to account for the fact that ContextMenuItemTagTranslate is no longer the last non-
+        custom context menu item tag.
+
+        * TestWebKitAPI/Tests/WebCore/ContextMenuAction.cpp:
+        (TestWebKitAPI::TEST):
+
 2022-02-15  Jonathan Bedard  <jbed...@apple.com>
 
         [EWS] Use EWS as commit author when rebasing

Modified: trunk/Tools/TestWebKitAPI/Tests/WebCore/ContextMenuAction.cpp (289850 => 289851)


--- trunk/Tools/TestWebKitAPI/Tests/WebCore/ContextMenuAction.cpp	2022-02-15 21:51:11 UTC (rev 289850)
+++ trunk/Tools/TestWebKitAPI/Tests/WebCore/ContextMenuAction.cpp	2022-02-15 21:57:03 UTC (rev 289851)
@@ -37,9 +37,9 @@
     EXPECT_TRUE(WTF::isValidEnum<WebCore::ContextMenuAction>(WebCore::ContextMenuAction::ContextMenuItemTagNoAction));
     EXPECT_TRUE(WTF::isValidEnum<WebCore::ContextMenuAction>(WebCore::ContextMenuAction::ContextMenuItemTagNoAction + 1));
 
-    EXPECT_TRUE(WTF::isValidEnum<WebCore::ContextMenuAction>(WebCore::ContextMenuAction::ContextMenuItemTagTranslate - 1));
-    EXPECT_TRUE(WTF::isValidEnum<WebCore::ContextMenuAction>(WebCore::ContextMenuAction::ContextMenuItemTagTranslate));
-    EXPECT_FALSE(WTF::isValidEnum<WebCore::ContextMenuAction>(WebCore::ContextMenuAction::ContextMenuItemTagTranslate + 1));
+    EXPECT_TRUE(WTF::isValidEnum<WebCore::ContextMenuAction>(WebCore::ContextMenuAction::ContextMenuItemLastNonCustomTag - 1));
+    EXPECT_TRUE(WTF::isValidEnum<WebCore::ContextMenuAction>(WebCore::ContextMenuAction::ContextMenuItemLastNonCustomTag));
+    EXPECT_FALSE(WTF::isValidEnum<WebCore::ContextMenuAction>(WebCore::ContextMenuAction::ContextMenuItemLastNonCustomTag + 1));
 
     EXPECT_FALSE(WTF::isValidEnum<WebCore::ContextMenuAction>(WebCore::ContextMenuAction::ContextMenuItemBaseCustomTag - 1));
     EXPECT_TRUE(WTF::isValidEnum<WebCore::ContextMenuAction>(WebCore::ContextMenuAction::ContextMenuItemBaseCustomTag));
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to