Title: [272710] trunk
Revision
272710
Author
wenson_hs...@apple.com
Date
2021-02-10 23:25:58 -0800 (Wed, 10 Feb 2021)

Log Message

[Webpage translation] Add support for the -_translate: action on WKContentView
https://bugs.webkit.org/show_bug.cgi?id=221713
Source/WebKit:

Reviewed by Tim Horton.

Add another way to trigger web page translation on iOS, via the `-_translate:` selector.

* Platform/spi/ios/UIKitSPI.h:

Add a new staged SPI declaration on `UIWKTextInteractionAssistant`. Additionally remove an old staging
declaration for a method in `UIWebGeolocationPolicyDecider`.

* UIProcess/API/ios/WKWebViewIOS.h:
* UIProcess/ios/WKContentViewInteraction.h:
* UIProcess/ios/WKContentViewInteraction.mm:
(-[WKContentView _translateForWebView:]):

Grab the currently selected text and invoke the new method on `UIWKTextInteractionAssistant`.

(-[WKContentView _addShortcutForWebView:]):

Drive-by fix: remove an extraneous `nil` check.

(-[WKContentView canPerformActionForWebView:withSender:]):

Return `YES` if the selection is ranged, and we're not inside a password input.

Tools:

<rdar://problem/73902020>

Reviewed by Tim Horton.

Add an API test to exercise `-canPerformAction:withSender:` with the new action.

* TestWebKitAPI/Tests/WebKitCocoa/WKWebViewEditActions.mm:

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (272709 => 272710)


--- trunk/Source/WebKit/ChangeLog	2021-02-11 07:24:24 UTC (rev 272709)
+++ trunk/Source/WebKit/ChangeLog	2021-02-11 07:25:58 UTC (rev 272710)
@@ -1,3 +1,32 @@
+2021-02-10  Wenson Hsieh  <wenson_hs...@apple.com>
+
+        [Webpage translation] Add support for the -_translate: action on WKContentView
+        https://bugs.webkit.org/show_bug.cgi?id=221713
+
+        Reviewed by Tim Horton.
+
+        Add another way to trigger web page translation on iOS, via the `-_translate:` selector.
+
+        * Platform/spi/ios/UIKitSPI.h:
+
+        Add a new staged SPI declaration on `UIWKTextInteractionAssistant`. Additionally remove an old staging
+        declaration for a method in `UIWebGeolocationPolicyDecider`.
+
+        * UIProcess/API/ios/WKWebViewIOS.h:
+        * UIProcess/ios/WKContentViewInteraction.h:
+        * UIProcess/ios/WKContentViewInteraction.mm:
+        (-[WKContentView _translateForWebView:]):
+
+        Grab the currently selected text and invoke the new method on `UIWKTextInteractionAssistant`.
+
+        (-[WKContentView _addShortcutForWebView:]):
+
+        Drive-by fix: remove an extraneous `nil` check.
+
+        (-[WKContentView canPerformActionForWebView:withSender:]):
+
+        Return `YES` if the selection is ranged, and we're not inside a password input.
+
 2021-02-10  Jer Noble  <jer.no...@apple.com>
 
         Move AudioHardwareListener into the GPU Process

Modified: trunk/Source/WebKit/Platform/spi/ios/UIKitSPI.h (272709 => 272710)


--- trunk/Source/WebKit/Platform/spi/ios/UIKitSPI.h	2021-02-11 07:24:24 UTC (rev 272709)
+++ trunk/Source/WebKit/Platform/spi/ios/UIKitSPI.h	2021-02-11 07:25:58 UTC (rev 272710)
@@ -803,6 +803,7 @@
 @interface UIWebGeolocationPolicyDecider ()
 + (instancetype)sharedPolicyDecider;
 - (void)decidePolicyForGeolocationRequestFromOrigin:(id)securityOrigin requestingURL:(NSURL *)requestingURL window:(UIWindow *)window listener:(id)listener;
+- (void)decidePolicyForGeolocationRequestFromOrigin:(id)securityOrigin requestingURL:(NSURL *)requestingURL view:(UIView *)view listener:(id)listener;
 @end
 
 typedef enum {
@@ -1316,8 +1317,8 @@
 @property (nonatomic, readonly) NSInteger _gsModifierFlags;
 @end
 
-@interface UIWebGeolocationPolicyDecider (Staging_25963823)
-- (void)decidePolicyForGeolocationRequestFromOrigin:(id)securityOrigin requestingURL:(NSURL *)requestingURL view:(UIView *)view listener:(id)listener;
+@interface UIWKTextInteractionAssistant (Staging_74209560)
+- (void)translate:(NSString *)text fromRect:(CGRect)presentationRect;
 @end
 
  @interface UIColor (IPI)

Modified: trunk/Source/WebKit/UIProcess/API/ios/WKWebViewIOS.h (272709 => 272710)


--- trunk/Source/WebKit/UIProcess/API/ios/WKWebViewIOS.h	2021-02-11 07:24:24 UTC (rev 272709)
+++ trunk/Source/WebKit/UIProcess/API/ios/WKWebViewIOS.h	2021-02-11 07:25:58 UTC (rev 272710)
@@ -113,6 +113,7 @@
 - (void)_promptForReplace:(id)sender;
 - (void)_transliterateChinese:(id)sender;
 - (void)replace:(id)sender;
+- (void)_translate:(id)sender;
 
 - (void)_nextAccessoryTab:(id)sender;
 - (void)_previousAccessoryTab:(id)sender;

Modified: trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.h (272709 => 272710)


--- trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.h	2021-02-11 07:24:24 UTC (rev 272709)
+++ trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.h	2021-02-11 07:25:58 UTC (rev 272710)
@@ -145,6 +145,7 @@
     M(_addShortcut) \
     M(_define) \
     M(_lookup) \
+    M(_translate) \
     M(_promptForReplace) \
     M(_share) \
     M(_showTextStyleOptions) \

Modified: trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm (272709 => 272710)


--- trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm	2021-02-11 07:24:24 UTC (rev 272709)
+++ trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm	2021-02-11 07:25:58 UTC (rev 272710)
@@ -3241,10 +3241,31 @@
     });
 }
 
+- (void)_translateForWebView:(id)sender
+{
+    _page->getSelectionOrContentsAsString([weakSelf = WeakObjCPtr<WKContentView>(self)] (const String& string, WebKit::CallbackBase::Error error) {
+        if (!weakSelf)
+            return;
+
+        if (error != WebKit::CallbackBase::Error::None || string.isEmpty())
+            return;
+
+        auto strongSelf = weakSelf.get();
+        if (strongSelf->_page->editorState().isMissingPostLayoutData)
+            return;
+
+        auto& selectionRects = strongSelf->_page->editorState().postLayoutData().selectionRects;
+        if (selectionRects.isEmpty())
+            return;
+
+        if ([strongSelf->_textInteractionAssistant respondsToSelector:@selector(translate:fromRect:)])
+            [strongSelf->_textInteractionAssistant translate:string fromRect:selectionRects.first().rect()];
+    });
+}
+
 - (void)_addShortcutForWebView:(id)sender
 {
-    if (_textInteractionAssistant)
-        [_textInteractionAssistant showTextServiceFor:[self selectedText] fromRect:_page->editorState().postLayoutData().selectionRects[0].rect()];
+    [_textInteractionAssistant showTextServiceFor:[self selectedText] fromRect:_page->editorState().postLayoutData().selectionRects[0].rect()];
 }
 
 - (NSString *)selectedText
@@ -3636,6 +3657,9 @@
         return UIKeyboardEnabledInputModesAllowChineseTransliterationForText([self selectedText]);
     }
 
+    if (action == @selector(_translate:))
+        return !editorState.isInPasswordField && editorState.selectionIsRange;
+
     if (action == @selector(select:)) {
         // Disable select in password fields so that you can't see word boundaries.
         return !editorState.isInPasswordField && !editorState.selectionIsRange && self.hasContent;

Modified: trunk/Tools/ChangeLog (272709 => 272710)


--- trunk/Tools/ChangeLog	2021-02-11 07:24:24 UTC (rev 272709)
+++ trunk/Tools/ChangeLog	2021-02-11 07:25:58 UTC (rev 272710)
@@ -1,3 +1,15 @@
+2021-02-10  Wenson Hsieh  <wenson_hs...@apple.com>
+
+        [Webpage translation] Add support for the -_translate: action on WKContentView
+        https://bugs.webkit.org/show_bug.cgi?id=221713
+        <rdar://problem/73902020>
+
+        Reviewed by Tim Horton.
+
+        Add an API test to exercise `-canPerformAction:withSender:` with the new action.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/WKWebViewEditActions.mm:
+
 2021-02-10  Rob Buis  <rb...@igalia.com>
 
         Use event loop to set title

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebViewEditActions.mm (272709 => 272710)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebViewEditActions.mm	2021-02-11 07:24:24 UTC (rev 272709)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebViewEditActions.mm	2021-02-11 07:25:58 UTC (rev 272710)
@@ -70,6 +70,14 @@
 
 @end
 
+#if PLATFORM(IOS_FAMILY)
+
+@interface WKWebView (InternalIOS)
+- (void)_translate:(id)sender;
+@end
+
+#endif // PLATFORM(IOS_FAMILY)
+
 namespace TestWebKitAPI {
 
 static RetainPtr<TestWKWebView> webViewForEditActionTesting(NSString *markup)
@@ -343,6 +351,21 @@
     EXPECT_WK_STREQ("italic", [webView stylePropertyAtSelectionStart:@"font-style"]);
 }
 
+TEST(WebKit, CanInvokeTranslateWithTextSelection)
+{
+    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)]);
+    [webView synchronouslyLoadTestPageNamed:@"simple"];
+    EXPECT_FALSE([webView canPerformAction:@selector(_translate:) withSender:nil]);
+
+    [webView selectAll:nil];
+    [webView waitForNextPresentationUpdate];
+    EXPECT_TRUE([webView canPerformAction:@selector(_translate:) withSender:nil]);
+
+    [webView collapseToEnd];
+    [webView waitForNextPresentationUpdate];
+    EXPECT_FALSE([webView canPerformAction:@selector(_translate:) withSender:nil]);
+}
+
 #else
 
 TEST(WKWebViewEditActions, ModifyTextWritingDirection)
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to