Skip to content
Permalink
Browse files
[iOS] Refactor some logic around tap gesture handling when VKCImageAn…
…alysisInteraction is active

https://bugs.webkit.org/show_bug.cgi?id=240891
rdar://93808100

Reviewed by Aditya Keerthi.

Currently, when handling a single tap in WKContentView, we check whether or not the tap occurred
over the Live Text button in the `VKCImageAnalysisInteraction`; if so, we manually toggle Live Text
interaction highlight state. This special treatment exists (instead of simply relying on the
UIButton's tap gesture) because `WKContentView` already has its own synthetic click gesture which
conflicts with the UIButton's tap gesture; without this logic, taps on the analysis button would be
completely ignored.

While this works for the Live Text button, it doesn't work for other actionable buttons in the image
analysis interaction. To address this, we install a `UIButtonConfigurationUpdateHandler` on the
image analysis interaction just to collect the set of `UIButton`s that are owned by the interaction.
When tapping on the content view, if we hit one of these VisionKitCore-owned buttons, then invoke
the button action on its behalf.

* Source/WebKit/UIProcess/ios/WKContentViewInteraction.h:
* Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm:
(-[WKContentView _singleTapIdentified:]):

Also, pull this VisionKit interception logic out into a new helper method below, named
`-_handleTapOverImageAnalysisInteractionButton:`. This returns whether or not the tap was handled by
VisionKit (in which case we avoid treating this as a synthetic click on the page).

(-[WKContentView _handleTapOverImageAnalysisInteractionButton:]):
(-[WKContentView installImageAnalysisInteraction:]):
(-[WKContentView uninstallImageAnalysisInteraction]):

Canonical link: https://commits.webkit.org/250968@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@294815 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
whsieh committed May 25, 2022
1 parent f9decae commit 555e4e329ac9474992ae5aafe18efe09a7e86cf6
Showing 2 changed files with 35 additions and 9 deletions.
@@ -537,6 +537,7 @@ using ImageAnalysisRequestIdentifier = ObjectIdentifier<ImageAnalysisRequestIden
uint32_t _fullscreenVideoExtractionRequestIdentifier;
#if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
RetainPtr<VKCImageAnalysisInteraction> _imageAnalysisInteraction;
RetainPtr<NSMutableSet<UIButton *>> _imageAnalysisActionButtons;
WebCore::FloatRect _imageAnalysisInteractionBounds;
std::optional<WebKit::ImageAnalysisMarkupData> _imageAnalysisMarkupData;
RetainPtr<CGImageRef> _croppedImageResult;
@@ -3226,17 +3226,11 @@ - (void)_endPotentialTapAndEnableDoubleTapGesturesIfNecessary
- (void)_singleTapIdentified:(UITapGestureRecognizer *)gestureRecognizer
{
auto position = [gestureRecognizer locationInView:self];

#if ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
if (UIButton *analysisButton = [_imageAnalysisInteraction analysisButton]) {
auto hitTestedView = dynamic_objc_cast<UIButton>([self hitTest:position withEvent:nil]);
// FIXME: Instead of a class check, this should be a straightforward equality check.
// However, rdar://91828384 currently prevents us from doing so. We can simplify this logic
// once the fix for that bug has landed.
if (hitTestedView.class == analysisButton.class)
[_imageAnalysisInteraction setHighlightSelectableItems:![_imageAnalysisInteraction highlightSelectableItems]];
if ([self _handleTapOverImageAnalysisInteractionButton:position])
return;
}
#endif // ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)
#endif

ASSERT(gestureRecognizer == _singleTapGestureRecognizer);
ASSERT(!_potentialTapInProgress);
@@ -4755,6 +4749,29 @@ - (void)doAfterComputingImageAnalysisResultsForMarkup:(CompletionHandler<void()>
});
}

- (BOOL)_handleTapOverImageAnalysisInteractionButton:(CGPoint)position
{
if (!_imageAnalysisInteraction)
return NO;

auto *hitButton = dynamic_objc_cast<UIButton>([self hitTest:position withEvent:nil]);
UIButton *analysisButton = [_imageAnalysisInteraction analysisButton];
// FIXME: Instead of a class check, this should be a straightforward equality check.
// However, rdar://91828384 currently prevents us from doing so. We can simplify this logic
// once the fix for that bug has landed.
if (analysisButton && hitButton.class == analysisButton.class) {
[_imageAnalysisInteraction setHighlightSelectableItems:![_imageAnalysisInteraction highlightSelectableItems]];
return YES;
}

if ([_imageAnalysisActionButtons containsObject:hitButton]) {
[hitButton sendActionsForControlEvents:UIControlEventTouchUpInside];
return YES;
}

return NO;
}

#endif // ENABLE(IMAGE_ANALYSIS_ENHANCEMENTS)

- (void)selectPositionAtPoint:(CGPoint)point completionHandler:(void (^)(void))completionHandler
@@ -10996,10 +11013,15 @@ - (void)actionSheetAssistant:(WKActionSheetAssistant *)assistant copyCroppedImag
- (void)installImageAnalysisInteraction:(VKCImageAnalysis *)analysis
{
if (!_imageAnalysisInteraction) {
_imageAnalysisActionButtons = adoptNS([[NSMutableSet alloc] initWithCapacity:1]);
_imageAnalysisInteraction = adoptNS([PAL::allocVKCImageAnalysisInteractionInstance() init]);
[_imageAnalysisInteraction setActiveInteractionTypes:VKImageAnalysisInteractionTypeTextSelection | VKImageAnalysisInteractionTypeDataDetectors];
[_imageAnalysisInteraction setDelegate:self];
[_imageAnalysisInteraction setWantsAutomaticContentsRectCalculation:NO];
[_imageAnalysisInteraction setQuickActionConfigurationUpdateHandler:[weakSelf = WeakObjCPtr<WKContentView>(self)] (UIButton *button) {
if (auto strongSelf = weakSelf.get())
[strongSelf->_imageAnalysisActionButtons addObject:button];
}];
WebKit::setUpAdditionalImageAnalysisBehaviors(_imageAnalysisInteraction.get());
[self addInteraction:_imageAnalysisInteraction.get()];
}
@@ -11014,7 +11036,10 @@ - (void)uninstallImageAnalysisInteraction
return;

[self removeInteraction:_imageAnalysisInteraction.get()];
[_imageAnalysisInteraction setDelegate:nil];
[_imageAnalysisInteraction setQuickActionConfigurationUpdateHandler:nil];
_imageAnalysisInteraction = nil;
_imageAnalysisActionButtons = nil;
[_imageAnalysisDeferringGestureRecognizer setEnabled:WebKit::isLiveTextAvailableAndEnabled()];
[_imageAnalysisGestureRecognizer setEnabled:WebKit::isLiveTextAvailableAndEnabled()];
}

0 comments on commit 555e4e3

Please sign in to comment.