⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 276220 in webkit


Ignore:
Timestamp:
Apr 17, 2021, 9:24:59 PM (5 years ago)
Author:
Wenson Hsieh
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:
Location:
trunk/Source
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r276219 r276220  
     12021-04-17  Wenson Hsieh  <wenson_hsieh@apple.com>
     2
     3        [macOS] Add some support for webpage translation in WebKitLegacy
     4        https://bugs.webkit.org/show_bug.cgi?id=224683
     5        <rdar://problem/75641882>
     6
     7        Reviewed by Darin Adler.
     8
     9        Remove compile-time guards around `ContextMenuItemTagAddHighlightToCurrentGroup` and
     10        `ContextMenuItemTagAddHighlightToNewGroup`, so that we can keep these internal WebCore context menu tags in sync
     11        with the SPI-exposed enum values in `WebUIDelegatePrivate.h`. See WebKitLegacy ChangeLog for more details.
     12
     13        * page/ContextMenuController.cpp:
     14        (WebCore::ContextMenuController::contextMenuItemSelected):
     15        (WebCore::ContextMenuController::checkOrEnableIfNeeded const):
     16        * platform/ContextMenuItem.cpp:
     17        (WebCore::isValidContextMenuAction):
     18        * platform/ContextMenuItem.h:
     19
    1202021-04-17  Wenson Hsieh  <wenson_hsieh@apple.com>
    221
  • trunk/Source/WebCore/page/ContextMenuController.cpp

    r276181 r276220  
    441441        frame->editor().command("MakeTextWritingDirectionRightToLeft").execute();
    442442        break;
    443 #if ENABLE(APP_HIGHLIGHTS)
    444443    case ContextMenuItemTagAddHighlightToCurrentGroup:
    445444        // FIXME: Add Highlight Logic
     
    448447        // FIXME: Add Highlight Logic
    449448        break;
    450 #endif
    451449#if PLATFORM(COCOA)
    452450    case ContextMenuItemTagSearchInSpotlight:
     
    13151313            shouldCheck = frame->editor().isContinuousSpellCheckingEnabled();
    13161314            break;
    1317 #if ENABLE(APP_HIGHLIGHTS)
    13181315        case ContextMenuItemTagAddHighlightToCurrentGroup:
    13191316            shouldEnable = frame->selection().isRange();
     
    13221319            shouldEnable = frame->selection().isRange();
    13231320            break;
    1324 #endif
    13251321#if PLATFORM(COCOA)
    13261322        case ContextMenuItemTagSubstitutionsMenu:
  • trunk/Source/WebCore/platform/ContextMenuItem.cpp

    r274521 r276220  
    230230    case ContextMenuAction::ContextMenuItemTagTextDirectionLeftToRight:
    231231    case ContextMenuAction::ContextMenuItemTagTextDirectionRightToLeft:
    232 #if ENABLE(APP_HIGHLIGHTS)
    233232    case ContextMenuAction::ContextMenuItemTagAddHighlightToCurrentGroup:
    234233    case ContextMenuAction::ContextMenuItemTagAddHighlightToNewGroup:
    235 #endif
    236234#if PLATFORM(COCOA)
    237235    case ContextMenuAction::ContextMenuItemTagCorrectSpellingAutomatically:
  • trunk/Source/WebCore/platform/ContextMenuItem.h

    r274521 r276220  
    146146    ContextMenuItemTagShareMenu,
    147147    ContextMenuItemTagToggleVideoEnhancedFullscreen,
    148 #if ENABLE(APP_HIGHLIGHTS)
    149148    ContextMenuItemTagAddHighlightToCurrentGroup,
    150149    ContextMenuItemTagAddHighlightToNewGroup,
    151 #endif
    152150    ContextMenuItemTagRevealImage,
    153151    ContextMenuItemTagTranslate,
  • trunk/Source/WebKit/ChangeLog

    r276219 r276220  
     12021-04-17  Wenson Hsieh  <wenson_hsieh@apple.com>
     2
     3        [macOS] Add some support for webpage translation in WebKitLegacy
     4        https://bugs.webkit.org/show_bug.cgi?id=224683
     5        <rdar://problem/75641882>
     6
     7        Reviewed by Darin Adler.
     8
     9        Remove some more compile-time guards, now that the WebCore enum is not conditional on `ENABLE(APP_HIGHLIGHT)`.
     10        See WebCore/ChangeLog for more information.
     11
     12        * Shared/API/c/WKSharedAPICast.h:
     13        (WebKit::toAPI):
     14        (WebKit::toImpl):
     15        * UIProcess/WebPageProxy.cpp:
     16        (WebKit::WebPageProxy::contextMenuItemSelected):
     17        * UIProcess/mac/WebContextMenuProxyMac.mm:
     18        (WebKit::menuItemIdentifier):
     19
    1202021-04-17  Wenson Hsieh  <wenson_hsieh@apple.com>
    221
  • trunk/Source/WebKit/Shared/API/c/WKSharedAPICast.h

    r274148 r276220  
    510510    case WebCore::ContextMenuItemTagMediaMute:
    511511        return kWKContextMenuItemTagMediaMute;
    512 #if ENABLE(APP_HIGHLIGHTS)
    513512    case WebCore::ContextMenuItemTagAddHighlightToCurrentGroup:
    514513        return kWKContextMenuItemTagAddHighlightToCurrentGroup;
    515514    case WebCore::ContextMenuItemTagAddHighlightToNewGroup:
    516515        return kWKContextMenuItemTagAddHighlightToNewGroup;
    517 #endif
    518516#if PLATFORM(COCOA)
    519517    case WebCore::ContextMenuItemTagCorrectSpellingAutomatically:
     
    714712    case kWKContextMenuItemTagMediaMute:
    715713        return WebCore::ContextMenuItemTagMediaMute;
    716 #if ENABLE(APP_HIGHLIGHT)
    717714    case kWKContextMenuItemTagAddHighlightToCurrentGroup:
    718715        return WebCore::ContextMenuItemTagAddHighlightToCurrentGroup;
    719716    case kWKContextMenuItemTagAddHighlightToNewGroup:
    720717        return WebCore::ContextMenuItemTagAddHighlightToNewGroup;
    721 #endif
    722718#if PLATFORM(COCOA)
    723719    case kWKContextMenuItemTagCorrectSpellingAutomatically:
  • trunk/Source/WebKit/UIProcess/WebPageProxy.cpp

    r276181 r276220  
    67266726        return;
    67276727
     6728    case ContextMenuItemTagAddHighlightToNewGroup:
    67286729#if ENABLE(APP_HIGHLIGHTS)
    6729     case ContextMenuItemTagAddHighlightToNewGroup:
    67306730        createAppHighlightInSelectedRange(CreateNewGroupForHighlight::Yes, HighlightRequestOriginatedInApp::No);
     6731#endif
    67316732        return;
    67326733
    67336734    case ContextMenuItemTagAddHighlightToCurrentGroup:
     6735#if ENABLE(APP_HIGHLIGHTS)
    67346736        createAppHighlightInSelectedRange(CreateNewGroupForHighlight::No, HighlightRequestOriginatedInApp::No);
    6735         return;
    6736 #endif
     6737#endif
     6738        return;
    67376739
    67386740    case ContextMenuItemTagLearnSpelling:
  • trunk/Source/WebKit/UIProcess/mac/WebContextMenuProxyMac.mm

    r275572 r276220  
    409409    case ContextMenuItemTagLookUpInDictionary:
    410410        return _WKMenuItemIdentifierLookUp;
    411 #if ENABLE(APP_HIGHLIGHTS)
     411
    412412    case ContextMenuItemTagAddHighlightToCurrentGroup:
    413413        return _WKMenuItemIdentifierAddHighlightToCurrentGroup;
     
    415415    case ContextMenuItemTagAddHighlightToNewGroup:
    416416        return _WKMenuItemIdentifierAddHighlightToNewGroup;
    417 #endif
     417
    418418    case ContextMenuItemTagOpenFrameInNewWindow:
    419419        return _WKMenuItemIdentifierOpenFrameInNewWindow;
  • trunk/Source/WebKitLegacy/mac/ChangeLog

    r276209 r276220  
     12021-04-17  Wenson Hsieh  <wenson_hsieh@apple.com>
     2
     3        [macOS] Add some support for webpage translation in WebKitLegacy
     4        https://bugs.webkit.org/show_bug.cgi?id=224683
     5        <rdar://problem/75641882>
     6
     7        Reviewed by Darin Adler.
     8
     9        Add support for the webpage translation context menu item in WebKitLegacy. See below for more details.
     10
     11        * WebView/WebHTMLView.mm:
     12        (toAction):
     13        (toTag):
     14
     15        Introduce the `WebMenuItemTagTranslate` enum value, and use it as the tag value when creating an `NSMenuItem`
     16        for the Translate action.
     17
     18        (createMenuItem):
     19        * WebView/WebUIDelegatePrivate.h:
     20
     21        Unfortunately, in order to support a particular internal client of WebKitLegacy, we need to expose
     22        `WebMenuItemTagTranslate` as an enum value in this existing enumeration of context menu item tags. This is
     23        because the client's implementation of `-webView:contextMenuItemsForElement:defaultMenuItems:` rejects context
     24        menu items by default, unless the item tag is within the set of item tags that they support.
     25
     26        This client will need to add the enum value of `WebMenuItemTagTranslate` into their allow-list.
     27
     28        * WebView/WebView.mm:
     29        (+[WebView _canHandleContextMenuTranslation]):
     30
     31        Softlink against and check with `TranslationUIServices` to see if we should be showing the Translate menu item.
     32
     33        (-[WebView _handleContextMenuTranslation:selectionBounds:menuLocation:]):
     34
     35        Handle the menu action by creating and presenting a new `LTUITranslationViewController` using the given
     36        information.
     37
     38        * WebView/WebViewInternal.h:
     39
    1402021-04-17  Tyler Wilcock  <twilco.o@protonmail.com>
    241
  • trunk/Source/WebKitLegacy/mac/WebView/WebHTMLView.mm

    r276191 r276220  
    427427    case WebMenuItemTagDictationAlternative:
    428428        return ContextMenuItemTagDictationAlternative;
     429    case WebMenuItemTagTranslate:
     430        return ContextMenuItemTagTranslate;
    429431    }
    430432    return WTF::nullopt;
     
    606608    case ContextMenuItemTagToggleVideoFullscreen:
    607609        return WebMenuItemTagToggleVideoFullscreen;
    608 #if ENABLE(APP_HIGHLIGHTS)
    609610    case ContextMenuItemTagAddHighlightToCurrentGroup:
    610611    case ContextMenuItemTagAddHighlightToNewGroup:
    611612        return WTF::nullopt;
    612 #endif
    613613    case ContextMenuItemTagShareMenu:
    614614        return WebMenuItemTagShareMenu;
    615615    case ContextMenuItemTagToggleVideoEnhancedFullscreen:
    616616        return WebMenuItemTagToggleVideoEnhancedFullscreen;
     617    case ContextMenuItemTagTranslate:
     618        return WebMenuItemTagTranslate;
    617619    case ContextMenuItemTagRevealImage:
    618     case ContextMenuItemTagTranslate:
    619620        return WTF::nullopt;
    620621
     
    36333634static RetainPtr<NSMenuItem> createMenuItem(const WebCore::HitTestResult& hitTestResult, const WebCore::ContextMenuItem& item)
    36343635{
     3636#if HAVE(TRANSLATION_UI_SERVICES)
     3637    if (item.action() == WebCore::ContextMenuItemTagTranslate && !WebView._canHandleContextMenuTranslation)
     3638        return nil;
     3639#endif
     3640
    36353641    if (item.action() == WebCore::ContextMenuItemTagShareMenu)
    36363642        return createShareMenuItem(hitTestResult);
  • trunk/Source/WebKitLegacy/mac/WebView/WebUIDelegatePrivate.h

    r261138 r276220  
    108108    WebMenuItemTagShareMenu,
    109109    WebMenuItemTagToggleVideoEnhancedFullscreen,
     110    WebMenuItemTagAddHighlightToCurrentGroup,
     111    WebMenuItemTagAddHighlightToNewGroup,
     112    WebMenuItemTagRevealImage,
     113    WebMenuItemTagTranslate,
    110114};
    111115
  • trunk/Source/WebKitLegacy/mac/WebView/WebView.mm

    r276191 r276220  
    251251#import <wtf/Language.h>
    252252#import <wtf/MainThread.h>
     253#import <wtf/MathExtras.h>
    253254#import <wtf/ProcessPrivilege.h>
    254255#import <wtf/RAMSize.h>
     
    348349#import <WebCore/PlaybackSessionInterfaceMac.h>
    349350#import <WebCore/PlaybackSessionModelMediaElement.h>
     351#endif
     352
     353#if HAVE(TRANSLATION_UI_SERVICES)
     354#import <TranslationUIServices/LTUITranslationViewController.h>
     355
     356SOFT_LINK_PRIVATE_FRAMEWORK_OPTIONAL(TranslationUIServices)
     357SOFT_LINK_CLASS_OPTIONAL(TranslationUIServices, LTUITranslationViewController)
    350358#endif
    351359
     
    96309638#if HAVE(TRANSLATION_UI_SERVICES) && ENABLE(CONTEXT_MENUS)
    96319639
     9640+ (BOOL)_canHandleContextMenuTranslation
     9641{
     9642    return TranslationUIServicesLibrary() && [getLTUITranslationViewControllerClass() isAvailable];
     9643}
     9644
    96329645- (void)_handleContextMenuTranslation:(const String&)text selectionBounds:(const WebCore::IntRect&)selectionBoundsInRootView menuLocation:(const WebCore::IntPoint&)locationInRootView
    96339646{
    9634     // FIXME (224683): Not implemented yet.
    9635     UNUSED_PARAM(text);
    9636     UNUSED_PARAM(selectionBoundsInRootView);
    9637     UNUSED_PARAM(locationInRootView);
     9647    if (!WebView._canHandleContextMenuTranslation) {
     9648        ASSERT_NOT_REACHED();
     9649        return;
     9650    }
     9651
     9652    auto translationViewController = adoptNS([allocLTUITranslationViewControllerInstance() init]);
     9653    [translationViewController setText:adoptNS([[NSAttributedString alloc] initWithString:text]).get()];
     9654
     9655    auto convertedSelectionBounds = [self _convertRectFromRootView:selectionBoundsInRootView];
     9656    auto convertedMenuLocation = [self _convertPointFromRootView:locationInRootView];
     9657
     9658    auto popover = adoptNS([[NSPopover alloc] init]);
     9659    [popover setBehavior:NSPopoverBehaviorTransient];
     9660    [popover setAppearance:self.effectiveAppearance];
     9661    [popover setAnimates:YES];
     9662    [popover setContentViewController:translationViewController.get()];
     9663    [popover setContentSize:[translationViewController preferredContentSize]];
     9664
     9665    NSRectEdge preferredEdge;
     9666    auto aim = convertedMenuLocation.x;
     9667    auto highlight = NSMidX(convertedSelectionBounds);
     9668    if (WTF::areEssentiallyEqual<CGFloat>(aim, highlight))
     9669        preferredEdge = self.userInterfaceLayoutDirection == NSUserInterfaceLayoutDirectionRightToLeft ? NSRectEdgeMinX : NSRectEdgeMaxX;
     9670    else
     9671        preferredEdge = aim > highlight ? NSRectEdgeMaxX : NSRectEdgeMinX;
     9672
     9673    [popover showRelativeToRect:convertedSelectionBounds ofView:self preferredEdge:preferredEdge];
    96389674}
    96399675
  • trunk/Source/WebKitLegacy/mac/WebView/WebViewInternal.h

    r276181 r276220  
    159159
    160160#if HAVE(TRANSLATION_UI_SERVICES) && ENABLE(CONTEXT_MENUS)
     161@property (class, nonatomic, readonly) BOOL _canHandleContextMenuTranslation;
    161162- (void)_handleContextMenuTranslation:(const String&)text selectionBounds:(const WebCore::IntRect&)boundsInView menuLocation:(const WebCore::IntPoint&)menuLocation;
    162163#endif
Note: See TracChangeset for help on using the changeset viewer.