Title: [276220] trunk/Source
Revision
276220
Author
[email protected]
Date
2021-04-17 21:24:59 -0700 (Sat, 17 Apr 2021)

Log Message

[macOS] Add some support for webpage translation in WebKitLegacy
https://bugs.webkit.org/show_bug.cgi?id=224683
<rdar://problem/75641882>

Reviewed by Darin Adler.

Source/WebCore:

Remove compile-time guards around `ContextMenuItemTagAddHighlightToCurrentGroup` and
`ContextMenuItemTagAddHighlightToNewGroup`, so that we can keep these internal WebCore context menu tags in sync
with the SPI-exposed enum values in `WebUIDelegatePrivate.h`. See WebKitLegacy ChangeLog for more details.

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

Source/WebKit:

Remove some more compile-time guards, now that the WebCore enum is not conditional on `ENABLE(APP_HIGHLIGHT)`.
See WebCore/ChangeLog for more information.

* Shared/API/c/WKSharedAPICast.h:
(WebKit::toAPI):
(WebKit::toImpl):
* UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::contextMenuItemSelected):
* UIProcess/mac/WebContextMenuProxyMac.mm:
(WebKit::menuItemIdentifier):

Source/WebKitLegacy/mac:

Add support for the webpage translation context menu item in WebKitLegacy. See below for more details.

* WebView/WebHTMLView.mm:
(toAction):
(toTag):

Introduce the `WebMenuItemTagTranslate` enum value, and use it as the tag value when creating an `NSMenuItem`
for the Translate action.

(createMenuItem):
* WebView/WebUIDelegatePrivate.h:

Unfortunately, in order to support a particular internal client of WebKitLegacy, we need to expose
`WebMenuItemTagTranslate` as an enum value in this existing enumeration of context menu item tags. This is
because the client's implementation of `-webView:contextMenuItemsForElement:defaultMenuItems:` rejects context
menu items by default, unless the item tag is within the set of item tags that they support.

This client will need to add the enum value of `WebMenuItemTagTranslate` into their allow-list.

* WebView/WebView.mm:
(+[WebView _canHandleContextMenuTranslation]):

Softlink against and check with `TranslationUIServices` to see if we should be showing the Translate menu item.

(-[WebView _handleContextMenuTranslation:selectionBounds:menuLocation:]):

Handle the menu action by creating and presenting a new `LTUITranslationViewController` using the given
information.

* WebView/WebViewInternal.h:

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (276219 => 276220)


--- trunk/Source/WebCore/ChangeLog	2021-04-18 03:49:07 UTC (rev 276219)
+++ trunk/Source/WebCore/ChangeLog	2021-04-18 04:24:59 UTC (rev 276220)
@@ -1,5 +1,24 @@
 2021-04-17  Wenson Hsieh  <[email protected]>
 
+        [macOS] Add some support for webpage translation in WebKitLegacy
+        https://bugs.webkit.org/show_bug.cgi?id=224683
+        <rdar://problem/75641882>
+
+        Reviewed by Darin Adler.
+
+        Remove compile-time guards around `ContextMenuItemTagAddHighlightToCurrentGroup` and
+        `ContextMenuItemTagAddHighlightToNewGroup`, so that we can keep these internal WebCore context menu tags in sync
+        with the SPI-exposed enum values in `WebUIDelegatePrivate.h`. See WebKitLegacy ChangeLog for more details.
+
+        * page/ContextMenuController.cpp:
+        (WebCore::ContextMenuController::contextMenuItemSelected):
+        (WebCore::ContextMenuController::checkOrEnableIfNeeded const):
+        * platform/ContextMenuItem.cpp:
+        (WebCore::isValidContextMenuAction):
+        * platform/ContextMenuItem.h:
+
+2021-04-17  Wenson Hsieh  <[email protected]>
+
         Remove PromisedAttachmentInfo::blobURL and adjacent code
         https://bugs.webkit.org/show_bug.cgi?id=224720
 

Modified: trunk/Source/WebCore/page/ContextMenuController.cpp (276219 => 276220)


--- trunk/Source/WebCore/page/ContextMenuController.cpp	2021-04-18 03:49:07 UTC (rev 276219)
+++ trunk/Source/WebCore/page/ContextMenuController.cpp	2021-04-18 04:24:59 UTC (rev 276220)
@@ -440,7 +440,6 @@
     case ContextMenuItemTagTextDirectionRightToLeft:
         frame->editor().command("MakeTextWritingDirectionRightToLeft").execute();
         break;
-#if ENABLE(APP_HIGHLIGHTS)
     case ContextMenuItemTagAddHighlightToCurrentGroup:
         // FIXME: Add Highlight Logic
         break;
@@ -447,7 +446,6 @@
     case ContextMenuItemTagAddHighlightToNewGroup:
         // FIXME: Add Highlight Logic
         break;
-#endif
 #if PLATFORM(COCOA)
     case ContextMenuItemTagSearchInSpotlight:
         m_client.searchWithSpotlight();
@@ -1314,7 +1312,6 @@
         case ContextMenuItemTagCheckSpellingWhileTyping:
             shouldCheck = frame->editor().isContinuousSpellCheckingEnabled();
             break;
-#if ENABLE(APP_HIGHLIGHTS)
         case ContextMenuItemTagAddHighlightToCurrentGroup:
             shouldEnable = frame->selection().isRange();
             break;
@@ -1321,7 +1318,6 @@
         case ContextMenuItemTagAddHighlightToNewGroup:
             shouldEnable = frame->selection().isRange();
             break;
-#endif
 #if PLATFORM(COCOA)
         case ContextMenuItemTagSubstitutionsMenu:
         case ContextMenuItemTagTransformationsMenu:

Modified: trunk/Source/WebCore/platform/ContextMenuItem.cpp (276219 => 276220)


--- trunk/Source/WebCore/platform/ContextMenuItem.cpp	2021-04-18 03:49:07 UTC (rev 276219)
+++ trunk/Source/WebCore/platform/ContextMenuItem.cpp	2021-04-18 04:24:59 UTC (rev 276220)
@@ -229,10 +229,8 @@
     case ContextMenuAction::ContextMenuItemTagTextDirectionDefault:
     case ContextMenuAction::ContextMenuItemTagTextDirectionLeftToRight:
     case ContextMenuAction::ContextMenuItemTagTextDirectionRightToLeft:
-#if ENABLE(APP_HIGHLIGHTS)
     case ContextMenuAction::ContextMenuItemTagAddHighlightToCurrentGroup:
     case ContextMenuAction::ContextMenuItemTagAddHighlightToNewGroup:
-#endif
 #if PLATFORM(COCOA)
     case ContextMenuAction::ContextMenuItemTagCorrectSpellingAutomatically:
     case ContextMenuAction::ContextMenuItemTagSubstitutionsMenu:

Modified: trunk/Source/WebCore/platform/ContextMenuItem.h (276219 => 276220)


--- trunk/Source/WebCore/platform/ContextMenuItem.h	2021-04-18 03:49:07 UTC (rev 276219)
+++ trunk/Source/WebCore/platform/ContextMenuItem.h	2021-04-18 04:24:59 UTC (rev 276220)
@@ -145,10 +145,8 @@
     ContextMenuItemTagToggleVideoFullscreen,
     ContextMenuItemTagShareMenu,
     ContextMenuItemTagToggleVideoEnhancedFullscreen,
-#if ENABLE(APP_HIGHLIGHTS)
     ContextMenuItemTagAddHighlightToCurrentGroup,
     ContextMenuItemTagAddHighlightToNewGroup,
-#endif
     ContextMenuItemTagRevealImage,
     ContextMenuItemTagTranslate,
     ContextMenuItemBaseCustomTag = 5000,

Modified: trunk/Source/WebKit/ChangeLog (276219 => 276220)


--- trunk/Source/WebKit/ChangeLog	2021-04-18 03:49:07 UTC (rev 276219)
+++ trunk/Source/WebKit/ChangeLog	2021-04-18 04:24:59 UTC (rev 276220)
@@ -1,5 +1,24 @@
 2021-04-17  Wenson Hsieh  <[email protected]>
 
+        [macOS] Add some support for webpage translation in WebKitLegacy
+        https://bugs.webkit.org/show_bug.cgi?id=224683
+        <rdar://problem/75641882>
+
+        Reviewed by Darin Adler.
+
+        Remove some more compile-time guards, now that the WebCore enum is not conditional on `ENABLE(APP_HIGHLIGHT)`.
+        See WebCore/ChangeLog for more information.
+
+        * Shared/API/c/WKSharedAPICast.h:
+        (WebKit::toAPI):
+        (WebKit::toImpl):
+        * UIProcess/WebPageProxy.cpp:
+        (WebKit::WebPageProxy::contextMenuItemSelected):
+        * UIProcess/mac/WebContextMenuProxyMac.mm:
+        (WebKit::menuItemIdentifier):
+
+2021-04-17  Wenson Hsieh  <[email protected]>
+
         Remove PromisedAttachmentInfo::blobURL and adjacent code
         https://bugs.webkit.org/show_bug.cgi?id=224720
 

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


--- trunk/Source/WebKit/Shared/API/c/WKSharedAPICast.h	2021-04-18 03:49:07 UTC (rev 276219)
+++ trunk/Source/WebKit/Shared/API/c/WKSharedAPICast.h	2021-04-18 04:24:59 UTC (rev 276220)
@@ -509,12 +509,10 @@
         return kWKContextMenuItemTagMediaPlayPause;
     case WebCore::ContextMenuItemTagMediaMute:
         return kWKContextMenuItemTagMediaMute;
-#if ENABLE(APP_HIGHLIGHTS)
     case WebCore::ContextMenuItemTagAddHighlightToCurrentGroup:
         return kWKContextMenuItemTagAddHighlightToCurrentGroup;
     case WebCore::ContextMenuItemTagAddHighlightToNewGroup:
         return kWKContextMenuItemTagAddHighlightToNewGroup;
-#endif
 #if PLATFORM(COCOA)
     case WebCore::ContextMenuItemTagCorrectSpellingAutomatically:
         return kWKContextMenuItemTagCorrectSpellingAutomatically;
@@ -713,12 +711,10 @@
         return WebCore::ContextMenuItemTagMediaPlayPause;
     case kWKContextMenuItemTagMediaMute:
         return WebCore::ContextMenuItemTagMediaMute;
-#if ENABLE(APP_HIGHLIGHT)
     case kWKContextMenuItemTagAddHighlightToCurrentGroup:
         return WebCore::ContextMenuItemTagAddHighlightToCurrentGroup;
     case kWKContextMenuItemTagAddHighlightToNewGroup:
         return WebCore::ContextMenuItemTagAddHighlightToNewGroup;
-#endif
 #if PLATFORM(COCOA)
     case kWKContextMenuItemTagCorrectSpellingAutomatically:
         return WebCore::ContextMenuItemTagCorrectSpellingAutomatically;

Modified: trunk/Source/WebKit/UIProcess/WebPageProxy.cpp (276219 => 276220)


--- trunk/Source/WebKit/UIProcess/WebPageProxy.cpp	2021-04-18 03:49:07 UTC (rev 276219)
+++ trunk/Source/WebKit/UIProcess/WebPageProxy.cpp	2021-04-18 04:24:59 UTC (rev 276220)
@@ -6725,15 +6725,17 @@
         TextChecker::toggleSpellingUIIsShowing();
         return;
 
+    case ContextMenuItemTagAddHighlightToNewGroup:
 #if ENABLE(APP_HIGHLIGHTS)
-    case ContextMenuItemTagAddHighlightToNewGroup:
         createAppHighlightInSelectedRange(CreateNewGroupForHighlight::Yes, HighlightRequestOriginatedInApp::No);
+#endif
         return;
 
     case ContextMenuItemTagAddHighlightToCurrentGroup:
+#if ENABLE(APP_HIGHLIGHTS)
         createAppHighlightInSelectedRange(CreateNewGroupForHighlight::No, HighlightRequestOriginatedInApp::No);
+#endif
         return;
-#endif
 
     case ContextMenuItemTagLearnSpelling:
     case ContextMenuItemTagIgnoreSpelling:

Modified: trunk/Source/WebKit/UIProcess/mac/WebContextMenuProxyMac.mm (276219 => 276220)


--- trunk/Source/WebKit/UIProcess/mac/WebContextMenuProxyMac.mm	2021-04-18 03:49:07 UTC (rev 276219)
+++ trunk/Source/WebKit/UIProcess/mac/WebContextMenuProxyMac.mm	2021-04-18 04:24:59 UTC (rev 276220)
@@ -408,13 +408,13 @@
 
     case ContextMenuItemTagLookUpInDictionary:
         return _WKMenuItemIdentifierLookUp;
-#if ENABLE(APP_HIGHLIGHTS)
+
     case ContextMenuItemTagAddHighlightToCurrentGroup:
         return _WKMenuItemIdentifierAddHighlightToCurrentGroup;
         
     case ContextMenuItemTagAddHighlightToNewGroup:
         return _WKMenuItemIdentifierAddHighlightToNewGroup;
-#endif
+
     case ContextMenuItemTagOpenFrameInNewWindow:
         return _WKMenuItemIdentifierOpenFrameInNewWindow;
 

Modified: trunk/Source/WebKitLegacy/mac/ChangeLog (276219 => 276220)


--- trunk/Source/WebKitLegacy/mac/ChangeLog	2021-04-18 03:49:07 UTC (rev 276219)
+++ trunk/Source/WebKitLegacy/mac/ChangeLog	2021-04-18 04:24:59 UTC (rev 276220)
@@ -1,3 +1,42 @@
+2021-04-17  Wenson Hsieh  <[email protected]>
+
+        [macOS] Add some support for webpage translation in WebKitLegacy
+        https://bugs.webkit.org/show_bug.cgi?id=224683
+        <rdar://problem/75641882>
+
+        Reviewed by Darin Adler.
+
+        Add support for the webpage translation context menu item in WebKitLegacy. See below for more details.
+
+        * WebView/WebHTMLView.mm:
+        (toAction):
+        (toTag):
+
+        Introduce the `WebMenuItemTagTranslate` enum value, and use it as the tag value when creating an `NSMenuItem`
+        for the Translate action.
+
+        (createMenuItem):
+        * WebView/WebUIDelegatePrivate.h:
+
+        Unfortunately, in order to support a particular internal client of WebKitLegacy, we need to expose
+        `WebMenuItemTagTranslate` as an enum value in this existing enumeration of context menu item tags. This is
+        because the client's implementation of `-webView:contextMenuItemsForElement:defaultMenuItems:` rejects context
+        menu items by default, unless the item tag is within the set of item tags that they support.
+
+        This client will need to add the enum value of `WebMenuItemTagTranslate` into their allow-list.
+
+        * WebView/WebView.mm:
+        (+[WebView _canHandleContextMenuTranslation]):
+
+        Softlink against and check with `TranslationUIServices` to see if we should be showing the Translate menu item.
+
+        (-[WebView _handleContextMenuTranslation:selectionBounds:menuLocation:]):
+
+        Handle the menu action by creating and presenting a new `LTUITranslationViewController` using the given
+        information.
+
+        * WebView/WebViewInternal.h:
+
 2021-04-17  Tyler Wilcock  <[email protected]>
 
         Consider making CSSStyleSheet::rules() just an alias of CSSStyleSheet::cssRules().

Modified: trunk/Source/WebKitLegacy/mac/WebView/WebHTMLView.mm (276219 => 276220)


--- trunk/Source/WebKitLegacy/mac/WebView/WebHTMLView.mm	2021-04-18 03:49:07 UTC (rev 276219)
+++ trunk/Source/WebKitLegacy/mac/WebView/WebHTMLView.mm	2021-04-18 04:24:59 UTC (rev 276220)
@@ -426,6 +426,8 @@
         return ContextMenuItemTagMediaMute;
     case WebMenuItemTagDictationAlternative:
         return ContextMenuItemTagDictationAlternative;
+    case WebMenuItemTagTranslate:
+        return ContextMenuItemTagTranslate;
     }
     return WTF::nullopt;
 }
@@ -605,17 +607,16 @@
         return WebMenuItemTagDictationAlternative;
     case ContextMenuItemTagToggleVideoFullscreen:
         return WebMenuItemTagToggleVideoFullscreen;
-#if ENABLE(APP_HIGHLIGHTS)
     case ContextMenuItemTagAddHighlightToCurrentGroup:
     case ContextMenuItemTagAddHighlightToNewGroup:
         return WTF::nullopt;
-#endif
     case ContextMenuItemTagShareMenu:
         return WebMenuItemTagShareMenu;
     case ContextMenuItemTagToggleVideoEnhancedFullscreen:
         return WebMenuItemTagToggleVideoEnhancedFullscreen;
+    case ContextMenuItemTagTranslate:
+        return WebMenuItemTagTranslate;
     case ContextMenuItemTagRevealImage:
-    case ContextMenuItemTagTranslate:
         return WTF::nullopt;
 
     case ContextMenuItemBaseCustomTag ... ContextMenuItemLastCustomTag:
@@ -3632,6 +3633,11 @@
 
 static RetainPtr<NSMenuItem> createMenuItem(const WebCore::HitTestResult& hitTestResult, const WebCore::ContextMenuItem& item)
 {
+#if HAVE(TRANSLATION_UI_SERVICES)
+    if (item.action() == WebCore::ContextMenuItemTagTranslate && !WebView._canHandleContextMenuTranslation)
+        return nil;
+#endif
+
     if (item.action() == WebCore::ContextMenuItemTagShareMenu)
         return createShareMenuItem(hitTestResult);
 

Modified: trunk/Source/WebKitLegacy/mac/WebView/WebUIDelegatePrivate.h (276219 => 276220)


--- trunk/Source/WebKitLegacy/mac/WebView/WebUIDelegatePrivate.h	2021-04-18 03:49:07 UTC (rev 276219)
+++ trunk/Source/WebKitLegacy/mac/WebView/WebUIDelegatePrivate.h	2021-04-18 04:24:59 UTC (rev 276220)
@@ -107,6 +107,10 @@
     WebMenuItemTagToggleVideoFullscreen,
     WebMenuItemTagShareMenu,
     WebMenuItemTagToggleVideoEnhancedFullscreen,
+    WebMenuItemTagAddHighlightToCurrentGroup,
+    WebMenuItemTagAddHighlightToNewGroup,
+    WebMenuItemTagRevealImage,
+    WebMenuItemTagTranslate,
 };
 
 // Deprecated; remove when there are no more clients.

Modified: trunk/Source/WebKitLegacy/mac/WebView/WebView.mm (276219 => 276220)


--- trunk/Source/WebKitLegacy/mac/WebView/WebView.mm	2021-04-18 03:49:07 UTC (rev 276219)
+++ trunk/Source/WebKitLegacy/mac/WebView/WebView.mm	2021-04-18 04:24:59 UTC (rev 276220)
@@ -250,6 +250,7 @@
 #import <wtf/HashTraits.h>
 #import <wtf/Language.h>
 #import <wtf/MainThread.h>
+#import <wtf/MathExtras.h>
 #import <wtf/ProcessPrivilege.h>
 #import <wtf/RAMSize.h>
 #import <wtf/RefCountedLeakCounter.h>
@@ -349,6 +350,13 @@
 #import <WebCore/PlaybackSessionModelMediaElement.h>
 #endif
 
+#if HAVE(TRANSLATION_UI_SERVICES)
+#import <TranslationUIServices/LTUITranslationViewController.h>
+
+SOFT_LINK_PRIVATE_FRAMEWORK_OPTIONAL(TranslationUIServices)
+SOFT_LINK_CLASS_OPTIONAL(TranslationUIServices, LTUITranslationViewController)
+#endif
+
 #if PLATFORM(IOS_FAMILY)
 #import <UIKit/UIColor.h>
 #import <UIKit/UIImage.h>
@@ -9629,12 +9637,40 @@
 
 #if HAVE(TRANSLATION_UI_SERVICES) && ENABLE(CONTEXT_MENUS)
 
++ (BOOL)_canHandleContextMenuTranslation
+{
+    return TranslationUIServicesLibrary() && [getLTUITranslationViewControllerClass() isAvailable];
+}
+
 - (void)_handleContextMenuTranslation:(const String&)text selectionBounds:(const WebCore::IntRect&)selectionBoundsInRootView menuLocation:(const WebCore::IntPoint&)locationInRootView
 {
-    // FIXME (224683): Not implemented yet.
-    UNUSED_PARAM(text);
-    UNUSED_PARAM(selectionBoundsInRootView);
-    UNUSED_PARAM(locationInRootView);
+    if (!WebView._canHandleContextMenuTranslation) {
+        ASSERT_NOT_REACHED();
+        return;
+    }
+
+    auto translationViewController = adoptNS([allocLTUITranslationViewControllerInstance() init]);
+    [translationViewController setText:adoptNS([[NSAttributedString alloc] initWithString:text]).get()];
+
+    auto convertedSelectionBounds = [self _convertRectFromRootView:selectionBoundsInRootView];
+    auto convertedMenuLocation = [self _convertPointFromRootView:locationInRootView];
+
+    auto popover = adoptNS([[NSPopover alloc] init]);
+    [popover setBehavior:NSPopoverBehaviorTransient];
+    [popover setAppearance:self.effectiveAppearance];
+    [popover setAnimates:YES];
+    [popover setContentViewController:translationViewController.get()];
+    [popover setContentSize:[translationViewController preferredContentSize]];
+
+    NSRectEdge preferredEdge;
+    auto aim = convertedMenuLocation.x;
+    auto highlight = NSMidX(convertedSelectionBounds);
+    if (WTF::areEssentiallyEqual<CGFloat>(aim, highlight))
+        preferredEdge = self.userInterfaceLayoutDirection == NSUserInterfaceLayoutDirectionRightToLeft ? NSRectEdgeMinX : NSRectEdgeMaxX;
+    else
+        preferredEdge = aim > highlight ? NSRectEdgeMaxX : NSRectEdgeMinX;
+
+    [popover showRelativeToRect:convertedSelectionBounds ofView:self preferredEdge:preferredEdge];
 }
 
 #endif // HAVE(TRANSLATION_UI_SERVICES) && ENABLE(CONTEXT_MENUS)

Modified: trunk/Source/WebKitLegacy/mac/WebView/WebViewInternal.h (276219 => 276220)


--- trunk/Source/WebKitLegacy/mac/WebView/WebViewInternal.h	2021-04-18 03:49:07 UTC (rev 276219)
+++ trunk/Source/WebKitLegacy/mac/WebView/WebViewInternal.h	2021-04-18 04:24:59 UTC (rev 276220)
@@ -158,6 +158,7 @@
 #endif
 
 #if HAVE(TRANSLATION_UI_SERVICES) && ENABLE(CONTEXT_MENUS)
+@property (class, nonatomic, readonly) BOOL _canHandleContextMenuTranslation;
 - (void)_handleContextMenuTranslation:(const String&)text selectionBounds:(const WebCore::IntRect&)boundsInView menuLocation:(const WebCore::IntPoint&)menuLocation;
 #endif
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to