| /* | |
| * Copyright (C) 2014-2019 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. | |
| */ | |
| #import "WebImmediateActionController.h" | |
| #if PLATFORM(MAC) | |
| #import "DOMElementInternal.h" | |
| #import "DOMNodeInternal.h" | |
| #import "DOMRangeInternal.h" | |
| #import "WebElementDictionary.h" | |
| #import "WebFrameInternal.h" | |
| #import "WebFrameView.h" | |
| #import "WebHTMLViewInternal.h" | |
| #import "WebUIDelegatePrivate.h" | |
| #import "WebViewInternal.h" | |
| #import <WebCore/DataDetection.h> | |
| #import <WebCore/DictionaryLookup.h> | |
| #import <WebCore/Document.h> | |
| #import <WebCore/DocumentPage.h> | |
| #import <WebCore/DocumentView.h> | |
| #import <WebCore/EditingHTMLConverter.h> | |
| #import <WebCore/Editor.h> | |
| #import <WebCore/EventHandler.h> | |
| #import <WebCore/FocusController.h> | |
| #import <WebCore/FontMetrics.h> | |
| #import <WebCore/FrameDestructionObserverInlines.h> | |
| #import <WebCore/GeometryUtilities.h> | |
| #import <WebCore/ImmediateActionStage.h> | |
| #import <WebCore/LocalFrame.h> | |
| #import <WebCore/LocalFrameInlines.h> | |
| #import <WebCore/LocalFrameView.h> | |
| #import <WebCore/NodeDocument.h> | |
| #import <WebCore/NodeRenderStyle.h> | |
| #import <WebCore/Page.h> | |
| #import <WebCore/Range.h> | |
| #import <WebCore/RenderElement.h> | |
| #import <WebCore/RenderObject.h> | |
| #import <WebCore/TextIndicator.h> | |
| #import <WebCore/TextIterator.h> | |
| #import <objc/objc-class.h> | |
| #import <objc/objc.h> | |
| #import <pal/spi/mac/LookupSPI.h> | |
| #import <pal/spi/mac/NSMenuSPI.h> | |
| #import <wtf/RuntimeApplicationChecks.h> | |
| #import <pal/mac/DataDetectorsSoftLink.h> | |
| #import <pal/mac/QuickLookUISoftLink.h> | |
| @interface WebImmediateActionController () <QLPreviewMenuItemDelegate> | |
| @end | |
| @interface WebAnimationController : NSObject <NSImmediateActionAnimationController> | |
| @end | |
| @implementation WebAnimationController | |
| @end | |
| @implementation WebImmediateActionController | |
| - (instancetype)initWithWebView:(WebView *)webView recognizer:(NSImmediateActionGestureRecognizer *)immediateActionRecognizer | |
| { | |
| if (!(self = [super init])) | |
| return nil; | |
| _webView = webView; | |
| _type = WebImmediateActionNone; | |
| _immediateActionRecognizer = immediateActionRecognizer; | |
| return self; | |
| } | |
| - (void)webViewClosed | |
| { | |
| _webView = nil; | |
| id animationController = [_immediateActionRecognizer animationController]; | |
| if (PAL::isQuickLookUIFrameworkAvailable() && [animationController isKindOfClass:PAL::getQLPreviewMenuItemClassSingleton()]) { | |
| RetainPtr menuItem = (QLPreviewMenuItem *)animationController; | |
| menuItem.get().delegate = nil; | |
| } | |
| _immediateActionRecognizer = nil; | |
| _currentActionContext = nil; | |
| } | |
| - (BOOL)isEnabled | |
| { | |
| return [_immediateActionRecognizer isEnabled]; | |
| } | |
| - (void)setEnabled:(BOOL)enabled | |
| { | |
| if (enabled == [_immediateActionRecognizer isEnabled]) | |
| return; | |
| [_immediateActionRecognizer setEnabled:enabled]; | |
| if (![_immediateActionRecognizer isEnabled]) | |
| [self _cancelImmediateAction]; | |
| } | |
| - (void)webView:(WebView *)webView didHandleScrollWheel:(NSEvent *)event | |
| { | |
| [_currentQLPreviewMenuItem close]; | |
| [self _clearImmediateActionState]; | |
| [protect(_webView) _clearTextIndicatorWithAnimation:WebCore::TextIndicatorDismissalAnimation::None]; | |
| } | |
| - (NSImmediateActionGestureRecognizer *)immediateActionRecognizer | |
| { | |
| return _immediateActionRecognizer.get(); | |
| } | |
| - (void)_cancelImmediateAction | |
| { | |
| // Reset the recognizer by turning it off and on again. | |
| if ([_immediateActionRecognizer isEnabled]) { | |
| [_immediateActionRecognizer setEnabled:NO]; | |
| [_immediateActionRecognizer setEnabled:YES]; | |
| } | |
| [self _clearImmediateActionState]; | |
| [protect(_webView) _clearTextIndicatorWithAnimation:WebCore::TextIndicatorDismissalAnimation::FadeOut]; | |
| } | |
| - (void)_clearImmediateActionState | |
| { | |
| if (!PAL::isDataDetectorsFrameworkAvailable()) | |
| return; | |
| DDActionsManager *actionsManager = [PAL::getDDActionsManagerClassSingleton() sharedManager]; | |
| [actionsManager requestBubbleClosureUnanchorOnFailure:YES]; | |
| if (_currentActionContext && _hasActivatedActionContext) { | |
| _hasActivatedActionContext = NO; | |
| [PAL::getDDActionsManagerClassSingleton() didUseActions]; | |
| } | |
| _type = WebImmediateActionNone; | |
| _currentActionContext = nil; | |
| _currentQLPreviewMenuItem = nil; | |
| _contentPreventsDefault = NO; | |
| } | |
| - (void)performHitTestAtPoint:(NSPoint)viewPoint | |
| { | |
| using namespace WebCore; | |
| RefPtr coreFrame = core([protect(_webView) _selectedOrMainFrame]); | |
| if (!coreFrame) | |
| return; | |
| RefPtr localMainFrame = dynamicDowncast<LocalFrame>(coreFrame->mainFrame()); | |
| if (!localMainFrame) | |
| return; | |
| constexpr OptionSet<HitTestRequest::Type> hitType { HitTestRequest::Type::ReadOnly, HitTestRequest::Type::Active, HitTestRequest::Type::DisallowUserAgentShadowContent, HitTestRequest::Type::AllowChildFrameContent }; | |
| _hitTestResult = coreFrame->eventHandler().hitTestResultAtPoint(WebCore::IntPoint(viewPoint), hitType); | |
| localMainFrame->eventHandler().setImmediateActionStage(WebCore::ImmediateActionStage::PerformedHitTest); | |
| if (RefPtr element = _hitTestResult.targetElement()) | |
| _contentPreventsDefault = element->dispatchMouseForceWillBegin(); | |
| } | |
| #pragma mark NSImmediateActionGestureRecognizerDelegate | |
| - (void)immediateActionRecognizerWillPrepare:(NSImmediateActionGestureRecognizer *)immediateActionRecognizer | |
| { | |
| if (!_webView) | |
| return; | |
| RetainPtr webView = _webView; | |
| NSView *documentView = [[[webView _selectedOrMainFrame] frameView] documentView]; | |
| if (![documentView isKindOfClass:[WebHTMLView class]]) { | |
| [self _cancelImmediateAction]; | |
| return; | |
| } | |
| if (immediateActionRecognizer != _immediateActionRecognizer) | |
| return; | |
| [webView _setMaintainsInactiveSelection:YES]; | |
| NSPoint locationInDocumentView = [immediateActionRecognizer locationInView:documentView]; | |
| [self performHitTestAtPoint:locationInDocumentView]; | |
| [self _updateImmediateActionItem]; | |
| if (![_immediateActionRecognizer animationController]) { | |
| // FIXME: We should be able to remove the dispatch_async when rdar://problem/19502927 is resolved. | |
| RunLoop::mainSingleton().dispatch([self, strongSelf = retainPtr(self)] { | |
| [self _cancelImmediateAction]; | |
| }); | |
| } | |
| } | |
| - (void)immediateActionRecognizerWillBeginAnimation:(NSImmediateActionGestureRecognizer *)immediateActionRecognizer | |
| { | |
| if (!PAL::isDataDetectorsFrameworkAvailable()) | |
| return; | |
| if (immediateActionRecognizer != _immediateActionRecognizer) | |
| return; | |
| if (_currentActionContext) { | |
| _hasActivatedActionContext = YES; | |
| if (![PAL::getDDActionsManagerClassSingleton() shouldUseActionsWithContext:_currentActionContext.get()]) | |
| [self _cancelImmediateAction]; | |
| } | |
| } | |
| - (void)immediateActionRecognizerDidUpdateAnimation:(NSImmediateActionGestureRecognizer *)immediateActionRecognizer | |
| { | |
| if (immediateActionRecognizer != _immediateActionRecognizer) | |
| return; | |
| RetainPtr webView = _webView; | |
| if (auto* coreFrame = [webView _mainCoreFrame]) | |
| coreFrame->eventHandler().setImmediateActionStage(WebCore::ImmediateActionStage::ActionUpdated); | |
| if (_contentPreventsDefault) | |
| return; | |
| [webView _setTextIndicatorAnimationProgress:[immediateActionRecognizer animationProgress]]; | |
| } | |
| - (void)immediateActionRecognizerDidCancelAnimation:(NSImmediateActionGestureRecognizer *)immediateActionRecognizer | |
| { | |
| if (immediateActionRecognizer != _immediateActionRecognizer) | |
| return; | |
| RetainPtr webView = _webView; | |
| if (auto* coreFrame = [webView _mainCoreFrame]) { | |
| WebCore::ImmediateActionStage lastStage = coreFrame->eventHandler().immediateActionStage(); | |
| if (lastStage == WebCore::ImmediateActionStage::ActionUpdated) | |
| coreFrame->eventHandler().setImmediateActionStage(WebCore::ImmediateActionStage::ActionCancelledAfterUpdate); | |
| else | |
| coreFrame->eventHandler().setImmediateActionStage(WebCore::ImmediateActionStage::ActionCancelledWithoutUpdate); | |
| } | |
| [webView _setTextIndicatorAnimationProgress:0]; | |
| [self _clearImmediateActionState]; | |
| [webView _clearTextIndicatorWithAnimation:WebCore::TextIndicatorDismissalAnimation::None]; | |
| [webView _setMaintainsInactiveSelection:NO]; | |
| } | |
| - (void)immediateActionRecognizerDidCompleteAnimation:(NSImmediateActionGestureRecognizer *)immediateActionRecognizer | |
| { | |
| if (immediateActionRecognizer != _immediateActionRecognizer) | |
| return; | |
| RetainPtr webView = _webView; | |
| if (auto* coreFrame = [webView _mainCoreFrame]) | |
| coreFrame->eventHandler().setImmediateActionStage(WebCore::ImmediateActionStage::ActionCompleted); | |
| [webView _setTextIndicatorAnimationProgress:1]; | |
| [webView _setMaintainsInactiveSelection:NO]; | |
| } | |
| #pragma mark Immediate actions | |
| - (id <NSImmediateActionAnimationController>)_defaultAnimationController | |
| { | |
| if (_contentPreventsDefault) | |
| return adoptNS([[WebAnimationController alloc] init]).autorelease(); | |
| RetainPtr url = _hitTestResult.absoluteLinkURL().createNSURL(); | |
| String absoluteURLString = [url absoluteString]; | |
| if (url && _hitTestResult.URLElement()) { | |
| if (WTF::protocolIs(absoluteURLString, "mailto"_s)) { | |
| _type = WebImmediateActionMailtoLink; | |
| return [self _animationControllerForDataDetectedLink]; | |
| } | |
| if (WTF::protocolIs(absoluteURLString, "tel"_s)) { | |
| _type = WebImmediateActionTelLink; | |
| return [self _animationControllerForDataDetectedLink]; | |
| } | |
| if (WTF::protocolIsInHTTPFamily(absoluteURLString)) { | |
| _type = WebImmediateActionLinkPreview; | |
| auto linkRange = makeRangeSelectingNodeContents(*protect(_hitTestResult.URLElement())); | |
| auto indicator = WebCore::TextIndicator::createWithRange(linkRange, { WebCore::TextIndicatorOption::UseBoundingRectAndPaintAllContentForComplexRanges }, WebCore::TextIndicatorPresentationTransition::FadeIn); | |
| if (indicator) | |
| [protect(_webView) _setTextIndicator:*indicator withLifetime:WebCore::TextIndicatorLifetime::Permanent]; | |
| QLPreviewMenuItem *item = [NSMenuItem standardQuickLookMenuItem]; | |
| item.previewStyle = QLPreviewStylePopover; | |
| item.delegate = self; | |
| _currentQLPreviewMenuItem = item; | |
| return (id <NSImmediateActionAnimationController>)item; | |
| } | |
| } | |
| RefPtr node = _hitTestResult.innerNode(); | |
| if ((node && node->isTextNode()) || _hitTestResult.isOverTextInsideFormControlElement()) { | |
| if (auto animationController = [self _animationControllerForDataDetectedText]) { | |
| _type = WebImmediateActionDataDetectedItem; | |
| return animationController; | |
| } | |
| if (auto animationController = [self _animationControllerForText]) { | |
| _type = WebImmediateActionText; | |
| return animationController; | |
| } | |
| } | |
| return nil; | |
| } | |
| - (void)_updateImmediateActionItem | |
| { | |
| RetainPtr webView = _webView; | |
| _type = WebImmediateActionNone; | |
| id <NSImmediateActionAnimationController> defaultAnimationController = [self _defaultAnimationController]; | |
| if (_contentPreventsDefault) { | |
| [_immediateActionRecognizer setAnimationController:defaultAnimationController]; | |
| return; | |
| } | |
| // Allow clients the opportunity to override the default immediate action. | |
| id customClientAnimationController = nil; | |
| if ([[webView UIDelegate] respondsToSelector:@selector(_webView:immediateActionAnimationControllerForHitTestResult:withType:)]) { | |
| RetainPtr<WebElementDictionary> webHitTestResult = adoptNS([[WebElementDictionary alloc] initWithHitTestResult:_hitTestResult]); | |
| customClientAnimationController = [(id)[webView UIDelegate] _webView:webView immediateActionAnimationControllerForHitTestResult:webHitTestResult.get() withType:_type]; | |
| } | |
| if (customClientAnimationController == [NSNull null]) { | |
| [self _cancelImmediateAction]; | |
| return; | |
| } | |
| if (customClientAnimationController && [customClientAnimationController conformsToProtocol:@protocol(NSImmediateActionAnimationController)]) | |
| [_immediateActionRecognizer setAnimationController:(id <NSImmediateActionAnimationController>)customClientAnimationController]; | |
| else | |
| [_immediateActionRecognizer setAnimationController:defaultAnimationController]; | |
| } | |
| #pragma mark QLPreviewMenuItemDelegate implementation | |
| - (NSView *)menuItem:(NSMenuItem *)menuItem viewAtScreenPoint:(NSPoint)screenPoint | |
| { | |
| return _webView; | |
| } | |
| - (id<QLPreviewItem>)menuItem:(NSMenuItem *)menuItem previewItemAtPoint:(NSPoint)point | |
| { | |
| if (!_webView) | |
| return nil; | |
| return _hitTestResult.absoluteLinkURL().createNSURL().autorelease(); | |
| } | |
| - (NSRectEdge)menuItem:(NSMenuItem *)menuItem preferredEdgeForPoint:(NSPoint)point | |
| { | |
| return NSMaxYEdge; | |
| } | |
| - (void)menuItemDidClose:(NSMenuItem *)menuItem | |
| { | |
| [self _clearImmediateActionState]; | |
| [protect(_webView) _clearTextIndicatorWithAnimation:WebCore::TextIndicatorDismissalAnimation::FadeOut]; | |
| } | |
| static WebCore::IntRect elementBoundingBoxInWindowCoordinatesFromNode(WebCore::Node* node) | |
| { | |
| if (!node) | |
| return { }; | |
| RefPtr frame = node->document().frame(); | |
| if (!frame) | |
| return { }; | |
| RefPtr view = frame->view(); | |
| if (!view) | |
| return { }; | |
| CheckedPtr renderer = node->renderer(); | |
| if (!renderer) | |
| return { }; | |
| return view->contentsToWindow(renderer->absoluteBoundingBoxRect()); | |
| } | |
| - (NSRect)menuItem:(NSMenuItem *)menuItem itemFrameForPoint:(NSPoint)point | |
| { | |
| if (!_webView) | |
| return NSZeroRect; | |
| RefPtr node = _hitTestResult.innerNode(); | |
| if (!node) | |
| return NSZeroRect; | |
| return elementBoundingBoxInWindowCoordinatesFromNode(node.get()); | |
| } | |
| - (NSSize)menuItem:(NSMenuItem *)menuItem maxSizeForPoint:(NSPoint)point | |
| { | |
| if (!_webView) | |
| return NSZeroSize; | |
| RetainPtr webView = _webView; | |
| NSSize screenSize = [webView window].screen.frame.size; | |
| WebCore::FloatRect largestRect = WebCore::largestRectWithAspectRatioInsideRect(screenSize.width / screenSize.height, [webView bounds]); | |
| return NSMakeSize(largestRect.width() * 0.75, largestRect.height() * 0.75); | |
| } | |
| #pragma mark Data Detectors actions | |
| - (id <NSImmediateActionAnimationController>)_animationControllerForDataDetectedText | |
| { | |
| RetainPtr webView = _webView; | |
| if (!PAL::isDataDetectorsFrameworkAvailable()) | |
| return nil; | |
| std::optional<WebCore::DetectedItem> detectedItem; | |
| if ([[webView UIDelegate] respondsToSelector:@selector(_webView:actionContextForHitTestResult:range:)]) { | |
| // This is an out-parameter which is set to an autoreleased object. | |
| SUPPRESS_UNRETAINED_LOCAL DOMRange *customDataDetectorsRange; | |
| RetainPtr actionContext = [(id)[webView UIDelegate] _webView:webView | |
| actionContextForHitTestResult:adoptNS([[WebElementDictionary alloc] initWithHitTestResult:_hitTestResult]).get() | |
| range:&customDataDetectorsRange]; | |
| if (actionContext && customDataDetectorsRange) { | |
| detectedItem = { { | |
| (WKDDActionContext *)actionContext.get(), | |
| { }, // FIXME: Seems like an empty rect isn't really OK. | |
| makeSimpleRange(*protect(core(customDataDetectorsRange))) | |
| } }; | |
| } | |
| } | |
| // If the client didn't give us an action context, try to scan around the hit point. | |
| if (!detectedItem) | |
| detectedItem = WebCore::DataDetection::detectItemAroundHitTestResult(_hitTestResult); | |
| if (!detectedItem) | |
| return nil; | |
| [detectedItem->actionContext setAltMode:YES]; | |
| [detectedItem->actionContext setImmediate:YES]; | |
| if (![[PAL::getDDActionsManagerClassSingleton() sharedManager] hasActionsForResult:protect([detectedItem->actionContext mainResult]) actionContext:detectedItem->actionContext.get()]) | |
| return nil; | |
| auto indicator = WebCore::TextIndicator::createWithRange(detectedItem->range, { }, WebCore::TextIndicatorPresentationTransition::FadeIn); | |
| _currentActionContext = [detectedItem->actionContext contextForView:webView altMode:YES interactionStartedHandler:^() { | |
| } interactionChangedHandler:^() { | |
| if (indicator) | |
| [webView _setTextIndicator:*indicator withLifetime:WebCore::TextIndicatorLifetime::Permanent]; | |
| } interactionStoppedHandler:^() { | |
| [webView _clearTextIndicatorWithAnimation:WebCore::TextIndicatorDismissalAnimation::FadeOut]; | |
| }]; | |
| [_currentActionContext setHighlightFrame:[[webView window] convertRectToScreen:detectedItem->boundingBox]]; | |
| RetainPtr menuItems = [[PAL::getDDActionsManagerClassSingleton() sharedManager] menuItemsForResult:protect([_currentActionContext mainResult]) actionContext:_currentActionContext.get()]; | |
| if (menuItems.get().count != 1) | |
| return nil; | |
| return menuItems.get().lastObject; | |
| } | |
| - (id <NSImmediateActionAnimationController>)_animationControllerForDataDetectedLink | |
| { | |
| RetainPtr webView = _webView; | |
| if (!PAL::isDataDetectorsFrameworkAvailable()) | |
| return nil; | |
| auto actionContext = adoptNS([PAL::allocWKDDActionContextInstance() init]); | |
| if (!actionContext) | |
| return nil; | |
| [actionContext setAltMode:YES]; | |
| [actionContext setImmediate:YES]; | |
| auto linkRange = makeRangeSelectingNodeContents(*protect(_hitTestResult.URLElement())); | |
| auto indicator = WebCore::TextIndicator::createWithRange(linkRange, { }, WebCore::TextIndicatorPresentationTransition::FadeIn); | |
| _currentActionContext = [actionContext contextForView:webView altMode:YES interactionStartedHandler:^() { | |
| } interactionChangedHandler:^() { | |
| if (indicator) | |
| [webView _setTextIndicator:*indicator withLifetime:WebCore::TextIndicatorLifetime::Permanent]; | |
| } interactionStoppedHandler:^() { | |
| [webView _clearTextIndicatorWithAnimation:WebCore::TextIndicatorDismissalAnimation::FadeOut]; | |
| }]; | |
| [_currentActionContext setHighlightFrame:[[webView window] convertRectToScreen:elementBoundingBoxInWindowCoordinatesFromNode(protect(_hitTestResult.URLElement()))]]; | |
| RetainPtr menuItems = [[PAL::getDDActionsManagerClassSingleton() sharedManager] menuItemsForTargetURL:_hitTestResult.absoluteLinkURL().string().createNSString().get() actionContext:_currentActionContext.get()]; | |
| if ([menuItems.get() count] != 1) | |
| return nil; | |
| return menuItems.get().lastObject; | |
| } | |
| #pragma mark Text action | |
| + (WebCore::DictionaryPopupInfo)_dictionaryPopupInfoForRange:(const WebCore::SimpleRange&)range inFrame:(WebCore::LocalFrame*)frame indicatorOptions:(OptionSet<WebCore::TextIndicatorOption>)indicatorOptions transition:(WebCore::TextIndicatorPresentationTransition)presentationTransition | |
| { | |
| Ref editor = frame->editor(); | |
| editor->setIsGettingDictionaryPopupInfo(true); | |
| // Dictionary API will accept a whitespace-only string and display UI as if it were real text, | |
| // so bail out early to avoid that. | |
| WebCore::DictionaryPopupInfo popupInfo; | |
| if (plainText(range).find(deprecatedIsNotSpaceOrNewline) == notFound) { | |
| editor->setIsGettingDictionaryPopupInfo(false); | |
| return popupInfo; | |
| } | |
| CheckedPtr style = range.start.container->renderStyle(); | |
| float scaledDescent = style ? style->metricsOfPrimaryFont().intDescent() * frame->page()->pageScaleFactor() : 0; | |
| auto quads = WebCore::RenderObject::absoluteTextQuads(range); | |
| if (quads.isEmpty()) { | |
| editor->setIsGettingDictionaryPopupInfo(false); | |
| return popupInfo; | |
| } | |
| auto rangeRect = protect(frame->view())->contentsToWindow(quads[0].enclosingBoundingBox()); | |
| popupInfo.origin = NSMakePoint(rangeRect.x(), rangeRect.y() + scaledDescent); | |
| auto attributedString = editingAttributedString(range, { }).nsAttributedString(); | |
| #if ENABLE(LEGACY_PDFKIT_PLUGIN) | |
| auto scaledAttributedString = adoptNS([[NSMutableAttributedString alloc] initWithString:[attributedString string]]); | |
| NSFontManager *fontManager = [NSFontManager sharedFontManager]; | |
| [attributedString enumerateAttributesInRange:NSMakeRange(0, [attributedString length]) options:0 usingBlock:^(NSDictionary *attributes, NSRange attributeRange, BOOL *stop) { | |
| RetainPtr<NSMutableDictionary> scaledAttributes = adoptNS([attributes mutableCopy]); | |
| NSFont *font = [scaledAttributes objectForKey:NSFontAttributeName]; | |
| if (font) | |
| font = [fontManager convertFont:font toSize:font.pointSize * frame->page()->pageScaleFactor()]; | |
| if (font) | |
| [scaledAttributes setObject:font forKey:NSFontAttributeName]; | |
| [scaledAttributedString addAttributes:scaledAttributes.get() range:attributeRange]; | |
| }]; | |
| popupInfo.platformData.attributedString = WebCore::AttributedString::fromNSAttributedString(scaledAttributedString.get()); | |
| #else | |
| popupInfo.text = [attributedString string]; | |
| #endif | |
| if (auto textIndicator = WebCore::TextIndicator::createWithRange(range, indicatorOptions, presentationTransition)) | |
| popupInfo.textIndicator = textIndicator; | |
| editor->setIsGettingDictionaryPopupInfo(false); | |
| return popupInfo; | |
| } | |
| - (id<NSImmediateActionAnimationController>)_animationControllerForText | |
| { | |
| if (!PAL::getLULookupDefinitionModuleClassSingleton()) | |
| return nil; | |
| RefPtr node = _hitTestResult.innerNode(); | |
| if (!node) | |
| return nil; | |
| RefPtr frame = node->document().frame(); | |
| if (!frame) | |
| return nil; | |
| auto range = WebCore::DictionaryLookup::rangeAtHitTestResult(_hitTestResult); | |
| if (!range) | |
| return nil; | |
| auto dictionaryRange = WTF::move(*range); | |
| auto dictionaryPopupInfo = [WebImmediateActionController _dictionaryPopupInfoForRange:dictionaryRange inFrame:frame.get() indicatorOptions: { } transition: WebCore::TextIndicatorPresentationTransition::FadeIn]; | |
| #if ENABLE(LEGACY_PDFKIT_PLUGIN) | |
| if (!dictionaryPopupInfo.platformData.attributedString.nsAttributedString()) | |
| return nil; | |
| #else | |
| if (!dictionaryPopupInfo.text) | |
| return nil; | |
| #endif | |
| return [protect(_webView) _animationControllerForDictionaryLookupPopupInfo:dictionaryPopupInfo]; | |
| } | |
| @end | |
| #endif // PLATFORM(MAC) |