Title: [277196] trunk/Source
Revision
277196
Author
wenson_hs...@apple.com
Date
2021-05-07 13:28:04 -0700 (Fri, 07 May 2021)

Log Message

[macOS] Set the -isSourceEditable property when presenting webpage translation popup
https://bugs.webkit.org/show_bug.cgi?id=225515
<rdar://problem/77658040>

Reviewed by Tim Horton.

Source/WebCore:

Set `-[LTUITranslationViewController isSourceEditable]` to `YES` when showing the context menu for editable
content. Instead of plumbing another boolean flag through `ContextMenuClient` and into the client layers through
`handleTranslation()`, we roll all of the webpage translation context menu parameters into a single object
(`TranslationContextMenuInfo`), including a new flag indicating whether or not the translation controller should
be shown in "editable" mode.

* Headers.cmake:
* WebCore.xcodeproj/project.pbxproj:
* loader/EmptyClients.cpp:
* page/ContextMenuClient.h:
* page/ContextMenuController.cpp:
(WebCore::ContextMenuController::contextMenuItemSelected):
* page/TranslationContextMenuInfo.h: Added.

Pull the translated text string, selection bounds, menu location, and the new mode flag into a separate struct.

(WebCore::TranslationContextMenuInfo::encode const):
(WebCore::TranslationContextMenuInfo::decode):

Source/WebKit:

See WebCore/ChangeLog for more details.

* UIProcess/Cocoa/WebPageProxyCocoa.mm:
(WebKit::WebPageProxy::handleContextMenuTranslation):
* UIProcess/Cocoa/WebViewImpl.h:
* UIProcess/Cocoa/WebViewImpl.mm:
(WebKit::WebViewImpl::handleContextMenuTranslation):
* UIProcess/PageClient.h:
* UIProcess/WebPageProxy.h:
* UIProcess/WebPageProxy.messages.in:
* UIProcess/mac/PageClientImplMac.h:
* UIProcess/mac/PageClientImplMac.mm:
(WebKit::PageClientImpl::handleContextMenuTranslation):
* WebProcess/WebCoreSupport/WebContextMenuClient.h:
* WebProcess/WebCoreSupport/mac/WebContextMenuClientMac.mm:
(WebKit::WebContextMenuClient::handleTranslation):
* WebProcess/WebPage/WebPage.cpp:
(WebKit::WebPage::handleContextMenuTranslation):
* WebProcess/WebPage/WebPage.h:

Source/WebKitLegacy/mac:

See WebCore/ChangeLog for more details.

* WebCoreSupport/WebContextMenuClient.h:
* WebCoreSupport/WebContextMenuClient.mm:
(WebContextMenuClient::handleTranslation):
* WebView/WebView.mm:
(-[WebView _handleContextMenuTranslation:]):
(-[WebView _handleContextMenuTranslation:selectionBounds:menuLocation:]): Deleted.
* WebView/WebViewInternal.h:

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (277195 => 277196)


--- trunk/Source/WebCore/ChangeLog	2021-05-07 20:09:01 UTC (rev 277195)
+++ trunk/Source/WebCore/ChangeLog	2021-05-07 20:28:04 UTC (rev 277196)
@@ -1,3 +1,30 @@
+2021-05-07  Wenson Hsieh  <wenson_hs...@apple.com>
+
+        [macOS] Set the -isSourceEditable property when presenting webpage translation popup
+        https://bugs.webkit.org/show_bug.cgi?id=225515
+        <rdar://problem/77658040>
+
+        Reviewed by Tim Horton.
+
+        Set `-[LTUITranslationViewController isSourceEditable]` to `YES` when showing the context menu for editable
+        content. Instead of plumbing another boolean flag through `ContextMenuClient` and into the client layers through
+        `handleTranslation()`, we roll all of the webpage translation context menu parameters into a single object
+        (`TranslationContextMenuInfo`), including a new flag indicating whether or not the translation controller should
+        be shown in "editable" mode.
+
+        * Headers.cmake:
+        * WebCore.xcodeproj/project.pbxproj:
+        * loader/EmptyClients.cpp:
+        * page/ContextMenuClient.h:
+        * page/ContextMenuController.cpp:
+        (WebCore::ContextMenuController::contextMenuItemSelected):
+        * page/TranslationContextMenuInfo.h: Added.
+
+        Pull the translated text string, selection bounds, menu location, and the new mode flag into a separate struct.
+
+        (WebCore::TranslationContextMenuInfo::encode const):
+        (WebCore::TranslationContextMenuInfo::decode):
+
 2021-05-07  Chris Dumez  <cdu...@apple.com>
 
         AudioWorkletProcessor which does not extend base class crashes Safari

Modified: trunk/Source/WebCore/Headers.cmake (277195 => 277196)


--- trunk/Source/WebCore/Headers.cmake	2021-05-07 20:09:01 UTC (rev 277195)
+++ trunk/Source/WebCore/Headers.cmake	2021-05-07 20:28:04 UTC (rev 277196)
@@ -945,6 +945,7 @@
     page/SuspendableTimer.h
     page/TextDirectionSubmenuInclusionBehavior.h
     page/TextIndicator.h
+    page/TranslationContextMenuInfo.h
     page/UserContentController.h
     page/UserContentProvider.h
     page/UserContentTypes.h

Modified: trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj (277195 => 277196)


--- trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2021-05-07 20:09:01 UTC (rev 277195)
+++ trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2021-05-07 20:28:04 UTC (rev 277196)
@@ -5338,6 +5338,7 @@
 		F48223111E3869B80066FC79 /* WebItemProviderPasteboard.h in Headers */ = {isa = PBXBuildFile; fileRef = F482230F1E3869B80066FC79 /* WebItemProviderPasteboard.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		F48223131E386E240066FC79 /* AbstractPasteboard.h in Headers */ = {isa = PBXBuildFile; fileRef = F48223121E386E240066FC79 /* AbstractPasteboard.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		F482434B260C33060022497C /* ImageOverlayController.h in Headers */ = {isa = PBXBuildFile; fileRef = F4824348260C32F10022497C /* ImageOverlayController.h */; };
+		F48570A52644C76D00C05F71 /* TranslationContextMenuInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = F48570A42644C76D00C05F71 /* TranslationContextMenuInfo.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		F48B7D5325C341E6009E75DD /* ImageExtractionResult.h in Headers */ = {isa = PBXBuildFile; fileRef = F48B7D5225C341E6009E75DD /* ImageExtractionResult.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		F48D2A6C215623B400C6752B /* FontShadow.h in Headers */ = {isa = PBXBuildFile; fileRef = F48D2A6A215623B400C6752B /* FontShadow.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		F48D2A7E2157182600C6752B /* FontAttributes.h in Headers */ = {isa = PBXBuildFile; fileRef = F48D2A712156DC0A00C6752B /* FontAttributes.h */; settings = {ATTRIBUTES = (Private, ); }; };
@@ -16796,6 +16797,7 @@
 		F482434A260C32F10022497C /* ImageOverlayController.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ImageOverlayController.cpp; sourceTree = "<group>"; };
 		F48389831E1DDF2B0076B7EA /* DumpEditingHistory.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode._javascript_; name = DumpEditingHistory.js; path = Scripts/DumpEditingHistory.js; sourceTree = "<group>"; };
 		F48389841E1DDF2B0076B7EA /* EditingHistoryUtil.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode._javascript_; name = EditingHistoryUtil.js; path = Scripts/EditingHistoryUtil.js; sourceTree = "<group>"; };
+		F48570A42644C76D00C05F71 /* TranslationContextMenuInfo.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TranslationContextMenuInfo.h; sourceTree = "<group>"; };
 		F48B7D5225C341E6009E75DD /* ImageExtractionResult.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ImageExtractionResult.h; sourceTree = "<group>"; };
 		F48D2A6A215623B400C6752B /* FontShadow.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FontShadow.h; sourceTree = "<group>"; };
 		F48D2A712156DC0A00C6752B /* FontAttributes.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FontAttributes.h; sourceTree = "<group>"; };
@@ -22517,6 +22519,7 @@
 				BC4A5324256055590028C592 /* TextDirectionSubmenuInclusionBehavior.h */,
 				2D4F96F11A1ECC240098BF88 /* TextIndicator.cpp */,
 				2D4F96F21A1ECC240098BF88 /* TextIndicator.h */,
+				F48570A42644C76D00C05F71 /* TranslationContextMenuInfo.h */,
 				F4E1965F21F26E4E00285078 /* UndoItem.cpp */,
 				2ECDBAD521D8906300F00ECD /* UndoItem.h */,
 				2ECDBAD721D8906300F00ECD /* UndoItem.idl */,
@@ -31426,7 +31429,6 @@
 				31A795C71888BCB200382F90 /* ANGLEInstancedArrays.h in Headers */,
 				49E912AB0EFAC906009D0CAF /* Animation.h in Headers */,
 				71EFCEDC202B38A900D7C411 /* AnimationEffect.h in Headers */,
-				449D864F2640C9C5000122DE /* HighlightVisibility.h in Headers */,
 				71E2C42621C935280024F8C8 /* AnimationEffectPhase.h in Headers */,
 				319848011A1D817B00A13318 /* AnimationEvent.h in Headers */,
 				711AD126236D86E5006FF37C /* AnimationEventBase.h in Headers */,
@@ -32645,6 +32647,7 @@
 				4430D00B2575A83E0046D401 /* Highlight.h in Headers */,
 				CDCFABBD18C0AF78006F8450 /* HighlightData.h in Headers */,
 				4430D00D2575A8A50046D401 /* HighlightRegister.h in Headers */,
+				449D864F2640C9C5000122DE /* HighlightVisibility.h in Headers */,
 				BC94D1540C275C8B006BC617 /* History.h in Headers */,
 				97DCE20210807C750057D394 /* HistoryController.h in Headers */,
 				51741D110B07259A00ED442C /* HistoryItem.h in Headers */,
@@ -34883,7 +34886,6 @@
 				E46B41F91CB24E70008F11DE /* ScriptDisallowedScope.h in Headers */,
 				4998AED213FB224D0090B1AA /* ScriptedAnimationController.h in Headers */,
 				08A484780E5272C500C3FE76 /* ScriptElement.h in Headers */,
-				E4D86E6D2640394C00B62425 /* StyleScopeOrdinal.h in Headers */,
 				E3E4E2A81E3B17100023BB8A /* ScriptElementCachedScriptFetcher.h in Headers */,
 				E11C9D9B0EB3681200E409DB /* ScriptExecutionContext.h in Headers */,
 				411223BA260244FE00B0A0B6 /* ScriptExecutionContextIdentifier.h in Headers */,
@@ -35199,6 +35201,7 @@
 				E4946EAF156E64DD00D3297F /* StyleRuleImport.h in Headers */,
 				0F94F37E23661626003AA5C7 /* StyleRuleType.h in Headers */,
 				E461D65F1BB0C80D00CB5645 /* StyleScope.h in Headers */,
+				E4D86E6D2640394C00B62425 /* StyleScopeOrdinal.h in Headers */,
 				4A4F48AA16B0DFC000EDBB29 /* StyleScopeRuleSets.h in Headers */,
 				F47A5E3E195B8C8A00483100 /* StyleScrollSnapPoints.h in Headers */,
 				9D6380101AF173220031A15C /* StyleSelfAlignmentData.h in Headers */,
@@ -35537,6 +35540,7 @@
 				416E6FE81BBD12DF000A3F74 /* TransformStreamInternalsBuiltins.h in Headers */,
 				E17B491616A9B094001C8839 /* TransitionEvent.h in Headers */,
 				49E911D20EF86D47009D0CAF /* TranslateTransformOperation.h in Headers */,
+				F48570A52644C76D00C05F71 /* TranslationContextMenuInfo.h in Headers */,
 				854FE7370A2297BE0058D7AD /* Traversal.h in Headers */,
 				37FD4298118368460093C029 /* TreeDepthLimit.h in Headers */,
 				14D64B5D134A5B6B00E58FDA /* TreeScope.h in Headers */,

Modified: trunk/Source/WebCore/loader/EmptyClients.cpp (277195 => 277196)


--- trunk/Source/WebCore/loader/EmptyClients.cpp	2021-05-07 20:09:01 UTC (rev 277195)
+++ trunk/Source/WebCore/loader/EmptyClients.cpp	2021-05-07 20:28:04 UTC (rev 277196)
@@ -120,7 +120,7 @@
 #endif
 
 #if HAVE(TRANSLATION_UI_SERVICES)
-    void handleTranslation(const String&, const IntRect&, const IntPoint&) final { }
+    void handleTranslation(const TranslationContextMenuInfo&) final { }
 #endif
 
 #if PLATFORM(GTK)

Modified: trunk/Source/WebCore/page/ContextMenuClient.h (277195 => 277196)


--- trunk/Source/WebCore/page/ContextMenuClient.h	2021-05-07 20:09:01 UTC (rev 277195)
+++ trunk/Source/WebCore/page/ContextMenuClient.h	2021-05-07 20:28:04 UTC (rev 277196)
@@ -35,6 +35,10 @@
 class IntPoint;
 class IntRect;
 
+#if HAVE(TRANSLATION_UI_SERVICES)
+struct TranslationContextMenuInfo;
+#endif
+
 class ContextMenuClient {
 public:
     virtual ~ContextMenuClient() = default;
@@ -48,7 +52,7 @@
     virtual void stopSpeaking() = 0;
 
 #if HAVE(TRANSLATION_UI_SERVICES)
-    virtual void handleTranslation(const String&, const IntRect&, const IntPoint&) = 0;
+    virtual void handleTranslation(const TranslationContextMenuInfo&) = 0;
 #endif
 
 #if PLATFORM(COCOA)

Modified: trunk/Source/WebCore/page/ContextMenuController.cpp (277195 => 277196)


--- trunk/Source/WebCore/page/ContextMenuController.cpp	2021-05-07 20:09:01 UTC (rev 277195)
+++ trunk/Source/WebCore/page/ContextMenuController.cpp	2021-05-07 20:28:04 UTC (rev 277196)
@@ -63,6 +63,7 @@
 #include "ResourceRequest.h"
 #include "Settings.h"
 #include "TextIterator.h"
+#include "TranslationContextMenuInfo.h"
 #include "TypingCommand.h"
 #include "UserTypingGestureIndicator.h"
 #include "WindowFeatures.h"
@@ -527,9 +528,12 @@
     case ContextMenuItemTagTranslate:
 #if HAVE(TRANSLATION_UI_SERVICES)
         if (auto view = makeRefPtr(frame->view())) {
-            auto selectionBounds = view->contentsToRootView(enclosingIntRect(frame->selection().selectionBounds()));
-            auto location = view->contentsToRootView(m_context.hitTestResult().roundedPointInInnerNodeFrame());
-            m_client.handleTranslation(m_context.hitTestResult().selectedText(), selectionBounds, location);
+            m_client.handleTranslation({
+                m_context.hitTestResult().selectedText(),
+                view->contentsToRootView(enclosingIntRect(frame->selection().selectionBounds())),
+                view->contentsToRootView(m_context.hitTestResult().roundedPointInInnerNodeFrame()),
+                m_context.hitTestResult().isContentEditable() ? TranslationContextMenuMode::Editable : TranslationContextMenuMode::NonEditable,
+            });
         }
 #endif
         break;

Added: trunk/Source/WebCore/page/TranslationContextMenuInfo.h (0 => 277196)


--- trunk/Source/WebCore/page/TranslationContextMenuInfo.h	                        (rev 0)
+++ trunk/Source/WebCore/page/TranslationContextMenuInfo.h	2021-05-07 20:28:04 UTC (rev 277196)
@@ -0,0 +1,96 @@
+/*
+ * Copyright (C) 2021 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#if HAVE(TRANSLATION_UI_SERVICES) && ENABLE(CONTEXT_MENUS)
+
+#include "IntPoint.h"
+#include "IntRect.h"
+#include <wtf/EnumTraits.h>
+#include <wtf/text/WTFString.h>
+
+namespace WebCore {
+
+enum class TranslationContextMenuMode : bool { NonEditable, Editable };
+
+struct TranslationContextMenuInfo {
+    String text;
+    IntRect selectionBoundsInRootView;
+    IntPoint locationInRootView;
+    TranslationContextMenuMode mode { TranslationContextMenuMode::NonEditable };
+
+    template<class Encoder> void encode(Encoder&) const;
+    template<class Decoder> static Optional<TranslationContextMenuInfo> decode(Decoder&);
+};
+
+template<class Encoder> void TranslationContextMenuInfo::encode(Encoder& encoder) const
+{
+    encoder << text;
+    encoder << selectionBoundsInRootView;
+    encoder << locationInRootView;
+    encoder << mode;
+}
+
+template<class Decoder> Optional<TranslationContextMenuInfo> TranslationContextMenuInfo::decode(Decoder& decoder)
+{
+    Optional<String> text;
+    decoder >> text;
+    if (!text)
+        return WTF::nullopt;
+
+    Optional<IntRect> selectionBoundsInRootView;
+    decoder >> selectionBoundsInRootView;
+    if (!selectionBoundsInRootView)
+        return WTF::nullopt;
+
+    Optional<IntPoint> locationInRootView;
+    decoder >> locationInRootView;
+    if (!locationInRootView)
+        return WTF::nullopt;
+
+    Optional<TranslationContextMenuMode> mode;
+    decoder >> mode;
+    if (!mode)
+        return WTF::nullopt;
+
+    return {{ WTFMove(*text), WTFMove(*selectionBoundsInRootView), WTFMove(*locationInRootView), *mode }};
+}
+
+} // namespace WebCore
+
+namespace WTF {
+
+template<> struct EnumTraits<WebCore::TranslationContextMenuMode> {
+    using values = EnumValues<
+        WebCore::TranslationContextMenuMode,
+        WebCore::TranslationContextMenuMode::NonEditable,
+        WebCore::TranslationContextMenuMode::Editable
+    >;
+};
+
+} // namespace WTF
+
+#endif // HAVE(TRANSLATION_UI_SERVICES) && ENABLE(CONTEXT_MENUS)

Modified: trunk/Source/WebKit/ChangeLog (277195 => 277196)


--- trunk/Source/WebKit/ChangeLog	2021-05-07 20:09:01 UTC (rev 277195)
+++ trunk/Source/WebKit/ChangeLog	2021-05-07 20:28:04 UTC (rev 277196)
@@ -1,3 +1,31 @@
+2021-05-07  Wenson Hsieh  <wenson_hs...@apple.com>
+
+        [macOS] Set the -isSourceEditable property when presenting webpage translation popup
+        https://bugs.webkit.org/show_bug.cgi?id=225515
+        <rdar://problem/77658040>
+
+        Reviewed by Tim Horton.
+
+        See WebCore/ChangeLog for more details.
+
+        * UIProcess/Cocoa/WebPageProxyCocoa.mm:
+        (WebKit::WebPageProxy::handleContextMenuTranslation):
+        * UIProcess/Cocoa/WebViewImpl.h:
+        * UIProcess/Cocoa/WebViewImpl.mm:
+        (WebKit::WebViewImpl::handleContextMenuTranslation):
+        * UIProcess/PageClient.h:
+        * UIProcess/WebPageProxy.h:
+        * UIProcess/WebPageProxy.messages.in:
+        * UIProcess/mac/PageClientImplMac.h:
+        * UIProcess/mac/PageClientImplMac.mm:
+        (WebKit::PageClientImpl::handleContextMenuTranslation):
+        * WebProcess/WebCoreSupport/WebContextMenuClient.h:
+        * WebProcess/WebCoreSupport/mac/WebContextMenuClientMac.mm:
+        (WebKit::WebContextMenuClient::handleTranslation):
+        * WebProcess/WebPage/WebPage.cpp:
+        (WebKit::WebPage::handleContextMenuTranslation):
+        * WebProcess/WebPage/WebPage.h:
+
 2021-05-07  Carlos Garnacho  <carl...@gnome.org>
 
         [GTK4] Rewrite GTK gesture support to work for both 3.x and 4.x.

Modified: trunk/Source/WebKit/UIProcess/Cocoa/WebPageProxyCocoa.mm (277195 => 277196)


--- trunk/Source/WebKit/UIProcess/Cocoa/WebPageProxyCocoa.mm	2021-05-07 20:09:01 UTC (rev 277195)
+++ trunk/Source/WebKit/UIProcess/Cocoa/WebPageProxyCocoa.mm	2021-05-07 20:28:04 UTC (rev 277196)
@@ -626,9 +626,9 @@
     return pageClient().canHandleContextMenuTranslation();
 }
 
-void WebPageProxy::handleContextMenuTranslation(const String& text, const WebCore::IntRect& boundsInView, const WebCore::IntPoint& locationInView)
+void WebPageProxy::handleContextMenuTranslation(const TranslationContextMenuInfo& info)
 {
-    return pageClient().handleContextMenuTranslation(text, boundsInView, locationInView);
+    return pageClient().handleContextMenuTranslation(info);
 }
 
 #endif // HAVE(TRANSLATION_UI_SERVICES)

Modified: trunk/Source/WebKit/UIProcess/Cocoa/WebViewImpl.h (277195 => 277196)


--- trunk/Source/WebKit/UIProcess/Cocoa/WebViewImpl.h	2021-05-07 20:09:01 UTC (rev 277195)
+++ trunk/Source/WebKit/UIProcess/Cocoa/WebViewImpl.h	2021-05-07 20:28:04 UTC (rev 277196)
@@ -96,6 +96,10 @@
 namespace WebCore {
 struct ShareDataWithParsedURL;
 
+#if HAVE(TRANSLATION_UI_SERVICES) && ENABLE(CONTEXT_MENUS)
+struct TranslationContextMenuInfo;
+#endif
+
 #if ENABLE(IMAGE_EXTRACTION)
 struct ImageExtractionResult;
 #endif
@@ -648,7 +652,7 @@
 
 #if HAVE(TRANSLATION_UI_SERVICES) && ENABLE(CONTEXT_MENUS)
     bool canHandleContextMenuTranslation() const;
-    void handleContextMenuTranslation(const String& text, const WebCore::IntRect& boundsInView, const WebCore::IntPoint& menuLocation);
+    void handleContextMenuTranslation(const WebCore::TranslationContextMenuInfo&);
 #endif
 
 #if ENABLE(MEDIA_SESSION_COORDINATOR)

Modified: trunk/Source/WebKit/UIProcess/Cocoa/WebViewImpl.mm (277195 => 277196)


--- trunk/Source/WebKit/UIProcess/Cocoa/WebViewImpl.mm	2021-05-07 20:09:01 UTC (rev 277195)
+++ trunk/Source/WebKit/UIProcess/Cocoa/WebViewImpl.mm	2021-05-07 20:28:04 UTC (rev 277196)
@@ -107,6 +107,7 @@
 #import <WebCore/PromisedAttachmentInfo.h>
 #import <WebCore/TextAlternativeWithRange.h>
 #import <WebCore/TextUndoInsertionMarkupMac.h>
+#import <WebCore/TranslationContextMenuInfo.h>
 #import <WebCore/WebActionDisablingCALayerDelegate.h>
 #import <WebCore/WebCoreCALayerExtras.h>
 #import <WebCore/WebCoreFullScreenPlaceholderView.h>
@@ -145,6 +146,10 @@
 #if HAVE(TRANSLATION_UI_SERVICES)
 #import <TranslationUIServices/LTUITranslationViewController.h>
 
+@interface LTUITranslationViewController (Staging_77660675)
+@property (nonatomic, copy) void(^replacementHandler)(NSAttributedString *);
+@end
+
 SOFT_LINK_PRIVATE_FRAMEWORK_OPTIONAL(TranslationUIServices)
 SOFT_LINK_CLASS_OPTIONAL(TranslationUIServices, LTUITranslationViewController)
 #endif
@@ -5586,7 +5591,7 @@
     return TranslationUIServicesLibrary() && [getLTUITranslationViewControllerClass() isAvailable];
 }
 
-void WebViewImpl::handleContextMenuTranslation(const String& text, const IntRect& boundsInView, const IntPoint& menuLocation)
+void WebViewImpl::handleContextMenuTranslation(const TranslationContextMenuInfo& info)
 {
     if (!canHandleContextMenuTranslation()) {
         ASSERT_NOT_REACHED();
@@ -5595,8 +5600,15 @@
 
     auto view = m_view.get();
     auto translationViewController = adoptNS([allocLTUITranslationViewControllerInstance() init]);
-    auto textToTranslate = adoptNS([[NSAttributedString alloc] initWithString:text]);
-    [translationViewController setText:textToTranslate.get()];
+    [translationViewController setText:adoptNS([[NSAttributedString alloc] initWithString:info.text]).get()];
+    if (info.mode == WebCore::TranslationContextMenuMode::Editable && [translationViewController respondsToSelector:@selector(setReplacementHandler:)]) {
+        [translationViewController setIsSourceEditable:YES];
+        [translationViewController setReplacementHandler:[this, weakThis = makeWeakPtr(*this)](NSAttributedString *string) {
+            if (weakThis)
+                insertText(string.string);
+        }];
+    }
+
     if (NSEqualSizes([translationViewController preferredContentSize], NSZeroSize))
         [translationViewController setPreferredContentSize:NSMakeSize(400, 400)];
 
@@ -5608,14 +5620,14 @@
     [popover setContentSize:[translationViewController preferredContentSize]];
 
     NSRectEdge preferredEdge;
-    auto aim = menuLocation.x();
-    auto highlight = boundsInView.center().x();
+    auto aim = info.locationInRootView.x();
+    auto highlight = info.selectionBoundsInRootView.center().x();
     if (aim == highlight)
         preferredEdge = [view userInterfaceLayoutDirection] == NSUserInterfaceLayoutDirectionRightToLeft ? NSRectEdgeMinX : NSRectEdgeMaxX;
     else
         preferredEdge = aim > highlight ? NSRectEdgeMaxX : NSRectEdgeMinX;
 
-    [popover showRelativeToRect:boundsInView ofView:view.get() preferredEdge:preferredEdge];
+    [popover showRelativeToRect:info.selectionBoundsInRootView ofView:view.get() preferredEdge:preferredEdge];
 }
 
 #endif // HAVE(TRANSLATION_UI_SERVICES) && ENABLE(CONTEXT_MENUS)

Modified: trunk/Source/WebKit/UIProcess/PageClient.h (277195 => 277196)


--- trunk/Source/WebKit/UIProcess/PageClient.h	2021-05-07 20:09:01 UTC (rev 277195)
+++ trunk/Source/WebKit/UIProcess/PageClient.h	2021-05-07 20:28:04 UTC (rev 277196)
@@ -126,6 +126,10 @@
 #if ENABLE(ATTACHMENT_ELEMENT)
 struct PromisedAttachmentInfo;
 #endif
+
+#if HAVE(TRANSLATION_UI_SERVICES) && ENABLE(CONTEXT_MENUS)
+struct TranslationContextMenuInfo;
+#endif
 }
 
 namespace WebKit {
@@ -617,7 +621,7 @@
 
 #if HAVE(TRANSLATION_UI_SERVICES) && ENABLE(CONTEXT_MENUS)
     virtual bool canHandleContextMenuTranslation() const = 0;
-    virtual void handleContextMenuTranslation(const String& text, const WebCore::IntRect& boundsInView, const WebCore::IntPoint& menuLocation) = 0;
+    virtual void handleContextMenuTranslation(const WebCore::TranslationContextMenuInfo&) = 0;
 #endif
 };
 

Modified: trunk/Source/WebKit/UIProcess/WebPageProxy.h (277195 => 277196)


--- trunk/Source/WebKit/UIProcess/WebPageProxy.h	2021-05-07 20:09:01 UTC (rev 277195)
+++ trunk/Source/WebKit/UIProcess/WebPageProxy.h	2021-05-07 20:28:04 UTC (rev 277196)
@@ -108,6 +108,7 @@
 #include <WebCore/TextChecking.h>
 #include <WebCore/TextGranularity.h>
 #include <WebCore/TextManipulationController.h>
+#include <WebCore/TranslationContextMenuInfo.h>
 #include <WebCore/UserInterfaceLayoutDirection.h>
 #include <WebCore/ViewportArguments.h>
 #include <memory>
@@ -1911,7 +1912,7 @@
 
 #if HAVE(TRANSLATION_UI_SERVICES) && ENABLE(CONTEXT_MENUS)
     bool canHandleContextMenuTranslation() const;
-    void handleContextMenuTranslation(const String& text, const WebCore::IntRect& boundsInView, const WebCore::IntPoint& locationInView);
+    void handleContextMenuTranslation(const WebCore::TranslationContextMenuInfo&);
 #endif
 
 #if ENABLE(MEDIA_SESSION_COORDINATOR)

Modified: trunk/Source/WebKit/UIProcess/WebPageProxy.messages.in (277195 => 277196)


--- trunk/Source/WebKit/UIProcess/WebPageProxy.messages.in	2021-05-07 20:09:01 UTC (rev 277195)
+++ trunk/Source/WebKit/UIProcess/WebPageProxy.messages.in	2021-05-07 20:28:04 UTC (rev 277196)
@@ -228,7 +228,7 @@
 #endif
 
 #if HAVE(TRANSLATION_UI_SERVICES) && ENABLE(CONTEXT_MENUS)
-    HandleContextMenuTranslation(String text, WebCore::IntRect boundsInView, WebCore::IntPoint locationInView)
+    HandleContextMenuTranslation(struct WebCore::TranslationContextMenuInfo info)
 #endif
 
 #if ENABLE(MEDIA_CONTROLS_CONTEXT_MENUS) && USE(UICONTEXTMENU)

Modified: trunk/Source/WebKit/UIProcess/mac/PageClientImplMac.h (277195 => 277196)


--- trunk/Source/WebKit/UIProcess/mac/PageClientImplMac.h	2021-05-07 20:09:01 UTC (rev 277195)
+++ trunk/Source/WebKit/UIProcess/mac/PageClientImplMac.h	2021-05-07 20:28:04 UTC (rev 277196)
@@ -283,7 +283,7 @@
 
 #if HAVE(TRANSLATION_UI_SERVICES) && ENABLE(CONTEXT_MENUS)
     bool canHandleContextMenuTranslation() const override;
-    void handleContextMenuTranslation(const String&, const WebCore::IntRect&, const WebCore::IntPoint&) override;
+    void handleContextMenuTranslation(const WebCore::TranslationContextMenuInfo&) override;
 #endif
 
     NSView *m_view;

Modified: trunk/Source/WebKit/UIProcess/mac/PageClientImplMac.mm (277195 => 277196)


--- trunk/Source/WebKit/UIProcess/mac/PageClientImplMac.mm	2021-05-07 20:09:01 UTC (rev 277195)
+++ trunk/Source/WebKit/UIProcess/mac/PageClientImplMac.mm	2021-05-07 20:28:04 UTC (rev 277196)
@@ -1022,9 +1022,9 @@
     return m_impl->canHandleContextMenuTranslation();
 }
 
-void PageClientImpl::handleContextMenuTranslation(const String& text, const IntRect& boundsInView, const WebCore::IntPoint& menuLocation)
+void PageClientImpl::handleContextMenuTranslation(const TranslationContextMenuInfo& info)
 {
-    m_impl->handleContextMenuTranslation(text, boundsInView, menuLocation);
+    m_impl->handleContextMenuTranslation(info);
 }
 
 #endif // HAVE(TRANSLATION_UI_SERVICES) && ENABLE(CONTEXT_MENUS)

Modified: trunk/Source/WebKit/WebProcess/WebCoreSupport/WebContextMenuClient.h (277195 => 277196)


--- trunk/Source/WebKit/WebProcess/WebCoreSupport/WebContextMenuClient.h	2021-05-07 20:09:01 UTC (rev 277195)
+++ trunk/Source/WebKit/WebProcess/WebCoreSupport/WebContextMenuClient.h	2021-05-07 20:28:04 UTC (rev 277196)
@@ -57,7 +57,7 @@
 #endif
 
 #if HAVE(TRANSLATION_UI_SERVICES)
-    void handleTranslation(const String&, const WebCore::IntRect& selectionBoundsInRootView, const WebCore::IntPoint& locationInRootView) final;
+    void handleTranslation(const WebCore::TranslationContextMenuInfo&) final;
 #endif
 
 #if PLATFORM(GTK)

Modified: trunk/Source/WebKit/WebProcess/WebCoreSupport/mac/WebContextMenuClientMac.mm (277195 => 277196)


--- trunk/Source/WebKit/WebProcess/WebCoreSupport/mac/WebContextMenuClientMac.mm	2021-05-07 20:09:01 UTC (rev 277195)
+++ trunk/Source/WebKit/WebProcess/WebCoreSupport/mac/WebContextMenuClientMac.mm	2021-05-07 20:28:04 UTC (rev 277196)
@@ -37,6 +37,7 @@
 #import <WebCore/FrameView.h>
 #import <WebCore/Page.h>
 #import <WebCore/TextIndicator.h>
+#import <WebCore/TranslationContextMenuInfo.h>
 #import <wtf/text/WTFString.h>
 
 namespace WebKit {
@@ -96,9 +97,9 @@
 
 #if HAVE(TRANSLATION_UI_SERVICES)
 
-void WebContextMenuClient::handleTranslation(const String& text, const IntRect& bounds, const IntPoint& location)
+void WebContextMenuClient::handleTranslation(const WebCore::TranslationContextMenuInfo& info)
 {
-    m_page->send(Messages::WebPageProxy::HandleContextMenuTranslation(text, bounds, location));
+    m_page->send(Messages::WebPageProxy::HandleContextMenuTranslation(info));
 }
 
 #endif // HAVE(TRANSLATION_UI_SERVICES)

Modified: trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp (277195 => 277196)


--- trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp	2021-05-07 20:09:01 UTC (rev 277195)
+++ trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp	2021-05-07 20:28:04 UTC (rev 277196)
@@ -244,6 +244,7 @@
 #include <WebCore/SubframeLoader.h>
 #include <WebCore/SubstituteData.h>
 #include <WebCore/TextIterator.h>
+#include <WebCore/TranslationContextMenuInfo.h>
 #include <WebCore/UserContentURLPattern.h>
 #include <WebCore/UserGestureIndicator.h>
 #include <WebCore/UserInputBridge.h>
@@ -7571,9 +7572,9 @@
 
 #if HAVE(TRANSLATION_UI_SERVICES) && ENABLE(CONTEXT_MENUS)
 
-void WebPage::handleContextMenuTranslation(const String& text, const IntRect& boundsInView, const IntPoint& locationInView)
+void WebPage::handleContextMenuTranslation(const TranslationContextMenuInfo& info)
 {
-    send(Messages::WebPageProxy::HandleContextMenuTranslation(text, boundsInView, locationInView));
+    send(Messages::WebPageProxy::HandleContextMenuTranslation(info));
 }
 
 #endif

Modified: trunk/Source/WebKit/WebProcess/WebPage/WebPage.h (277195 => 277196)


--- trunk/Source/WebKit/WebProcess/WebPage/WebPage.h	2021-05-07 20:09:01 UTC (rev 277195)
+++ trunk/Source/WebKit/WebProcess/WebPage/WebPage.h	2021-05-07 20:28:04 UTC (rev 277196)
@@ -247,6 +247,10 @@
 #if ENABLE(ATTACHMENT_ELEMENT)
 class HTMLAttachmentElement;
 #endif
+
+#if HAVE(TRANSLATION_UI_SERVICES) && ENABLE(CONTEXT_MENUS)
+struct TranslationContextMenuInfo;
+#endif
 }
 
 namespace WebKit {
@@ -1396,7 +1400,7 @@
 #endif
 
 #if HAVE(TRANSLATION_UI_SERVICES) && ENABLE(CONTEXT_MENUS)
-    void handleContextMenuTranslation(const String& text, const WebCore::IntRect& selectionBoundsInView, const WebCore::IntPoint& menuLocationInView);
+    void handleContextMenuTranslation(const WebCore::TranslationContextMenuInfo&);
 #endif
 
 #if ENABLE(MEDIA_CONTROLS_CONTEXT_MENUS) && USE(UICONTEXTMENU)

Modified: trunk/Source/WebKitLegacy/mac/ChangeLog (277195 => 277196)


--- trunk/Source/WebKitLegacy/mac/ChangeLog	2021-05-07 20:09:01 UTC (rev 277195)
+++ trunk/Source/WebKitLegacy/mac/ChangeLog	2021-05-07 20:28:04 UTC (rev 277196)
@@ -1,3 +1,21 @@
+2021-05-07  Wenson Hsieh  <wenson_hs...@apple.com>
+
+        [macOS] Set the -isSourceEditable property when presenting webpage translation popup
+        https://bugs.webkit.org/show_bug.cgi?id=225515
+        <rdar://problem/77658040>
+
+        Reviewed by Tim Horton.
+
+        See WebCore/ChangeLog for more details.
+
+        * WebCoreSupport/WebContextMenuClient.h:
+        * WebCoreSupport/WebContextMenuClient.mm:
+        (WebContextMenuClient::handleTranslation):
+        * WebView/WebView.mm:
+        (-[WebView _handleContextMenuTranslation:]):
+        (-[WebView _handleContextMenuTranslation:selectionBounds:menuLocation:]): Deleted.
+        * WebView/WebViewInternal.h:
+
 2021-05-04  Alex Christensen  <achristen...@webkit.org>
 
         Remove unused references to PageGroup from WebKitLegacy

Modified: trunk/Source/WebKitLegacy/mac/WebCoreSupport/WebContextMenuClient.h (277195 => 277196)


--- trunk/Source/WebKitLegacy/mac/WebCoreSupport/WebContextMenuClient.h	2021-05-07 20:09:01 UTC (rev 277195)
+++ trunk/Source/WebKitLegacy/mac/WebCoreSupport/WebContextMenuClient.h	2021-05-07 20:28:04 UTC (rev 277196)
@@ -68,7 +68,7 @@
 #endif
 
 #if HAVE(TRANSLATION_UI_SERVICES)
-    void handleTranslation(const String&, const WebCore::IntRect& selectionBoundsInRootView, const WebCore::IntPoint& locationInRootView) final;
+    void handleTranslation(const WebCore::TranslationContextMenuInfo&) final;
 #endif
 
 private:

Modified: trunk/Source/WebKitLegacy/mac/WebCoreSupport/WebContextMenuClient.mm (277195 => 277196)


--- trunk/Source/WebKitLegacy/mac/WebCoreSupport/WebContextMenuClient.mm	2021-05-07 20:09:01 UTC (rev 277195)
+++ trunk/Source/WebKitLegacy/mac/WebCoreSupport/WebContextMenuClient.mm	2021-05-07 20:28:04 UTC (rev 277196)
@@ -149,9 +149,9 @@
 
 #if HAVE(TRANSLATION_UI_SERVICES)
 
-void WebContextMenuClient::handleTranslation(const String& text, const IntRect& selectionBoundsInRootView, const IntPoint& locationInRootView)
+void WebContextMenuClient::handleTranslation(const TranslationContextMenuInfo& info)
 {
-    [m_webView _handleContextMenuTranslation:text selectionBounds:selectionBoundsInRootView menuLocation:locationInRootView];
+    [m_webView _handleContextMenuTranslation:info];
 }
 
 #endif

Modified: trunk/Source/WebKitLegacy/mac/WebView/WebView.mm (277195 => 277196)


--- trunk/Source/WebKitLegacy/mac/WebView/WebView.mm	2021-05-07 20:09:01 UTC (rev 277195)
+++ trunk/Source/WebKitLegacy/mac/WebView/WebView.mm	2021-05-07 20:28:04 UTC (rev 277196)
@@ -215,6 +215,7 @@
 #import <WebCore/StyleProperties.h>
 #import <WebCore/TextResourceDecoder.h>
 #import <WebCore/ThreadCheck.h>
+#import <WebCore/TranslationContextMenuInfo.h>
 #import <WebCore/UTIRegistry.h>
 #import <WebCore/UserAgent.h>
 #import <WebCore/UserContentController.h>
@@ -352,6 +353,10 @@
 #if HAVE(TRANSLATION_UI_SERVICES)
 #import <TranslationUIServices/LTUITranslationViewController.h>
 
+@interface LTUITranslationViewController (Staging_77660675)
+@property (nonatomic, copy) void(^replacementHandler)(NSAttributedString *);
+@end
+
 SOFT_LINK_PRIVATE_FRAMEWORK_OPTIONAL(TranslationUIServices)
 SOFT_LINK_CLASS_OPTIONAL(TranslationUIServices, LTUITranslationViewController)
 #endif
@@ -9638,7 +9643,7 @@
     return TranslationUIServicesLibrary() && [getLTUITranslationViewControllerClass() isAvailable];
 }
 
-- (void)_handleContextMenuTranslation:(const String&)text selectionBounds:(const WebCore::IntRect&)selectionBoundsInRootView menuLocation:(const WebCore::IntPoint&)locationInRootView
+- (void)_handleContextMenuTranslation:(const WebCore::TranslationContextMenuInfo&)info
 {
     if (!WebView._canHandleContextMenuTranslation) {
         ASSERT_NOT_REACHED();
@@ -9646,10 +9651,17 @@
     }
 
     auto translationViewController = adoptNS([allocLTUITranslationViewControllerInstance() init]);
-    [translationViewController setText:adoptNS([[NSAttributedString alloc] initWithString:text]).get()];
+    [translationViewController setText:adoptNS([[NSAttributedString alloc] initWithString:info.text]).get()];
+    if (info.mode == WebCore::TranslationContextMenuMode::Editable && [translationViewController respondsToSelector:@selector(setReplacementHandler:)]) {
+        [translationViewController setIsSourceEditable:YES];
+        [translationViewController setReplacementHandler:[weakSelf = WeakObjCPtr<WebView>(self)](NSAttributedString *string) {
+            auto strongSelf = weakSelf.get();
+            [strongSelf insertText:string.string];
+        }];
+    }
 
-    auto convertedSelectionBounds = [self _convertRectFromRootView:selectionBoundsInRootView];
-    auto convertedMenuLocation = [self _convertPointFromRootView:locationInRootView];
+    auto convertedSelectionBounds = [self _convertRectFromRootView:info.selectionBoundsInRootView];
+    auto convertedMenuLocation = [self _convertPointFromRootView:info.locationInRootView];
 
     auto popover = adoptNS([[NSPopover alloc] init]);
     [popover setBehavior:NSPopoverBehaviorTransient];

Modified: trunk/Source/WebKitLegacy/mac/WebView/WebViewInternal.h (277195 => 277196)


--- trunk/Source/WebKitLegacy/mac/WebView/WebViewInternal.h	2021-05-07 20:09:01 UTC (rev 277195)
+++ trunk/Source/WebKitLegacy/mac/WebView/WebViewInternal.h	2021-05-07 20:28:04 UTC (rev 277196)
@@ -65,6 +65,10 @@
 class TextIndicator;
 struct DictationAlternative;
 struct DictionaryPopupInfo;
+
+#if HAVE(TRANSLATION_UI_SERVICES) && ENABLE(CONTEXT_MENUS)
+struct TranslationContextMenuInfo;
+#endif
 }
 
 #if PLATFORM(IOS_FAMILY) && ENABLE(DRAG_SUPPORT)
@@ -159,7 +163,7 @@
 
 #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;
+- (void)_handleContextMenuTranslation:(const WebCore::TranslationContextMenuInfo&)info;
 #endif
 
 - (void)_windowVisibilityChanged:(NSNotification *)notification;
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to