Implement Lookup transition from selection for Legacy WebKit
authortimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 2 Dec 2014 19:38:05 +0000 (19:38 +0000)
committertimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 2 Dec 2014 19:38:05 +0000 (19:38 +0000)
https://bugs.webkit.org/show_bug.cgi?id=139180
<rdar://problem/19067172>

Reviewed by Beth Dakin.

* WebView/DictionaryPopupInfo.h: Added.
Split DictionaryPopupInfo out into its own file.

* WebView/WebActionMenuController.mm:
(-[WebActionMenuController _lookupText:]):
Hand the DictionaryPopupInfo to WebView, which will show the TextIndicator and invoke Lookup.

(performDictionaryLookupForSelection):
(performDictionaryLookupForRange):
Plumb the transition style through these helpers.
Create a TextIndicator from the selection range.

* WebView/WebHTMLView.mm:
(-[WebHTMLView _lookUpInDictionaryFromMenu:]):
Make use of TextIndicator (and DictionaryPopupInfo, and the new
_showDictionaryLookupPopup on WebView) to add a transition from blue
when performing Lookup from the context menu.

(-[WebHTMLView quickLookWithEvent:]):
Remove our TextIndicator whenever a normal three-finger-tap event
comes through, as it will have its own indication.

* WebView/WebView.mm:
(-[WebView _commonInitializationWithFrameName:groupName:]):
(-[WebView _dictionaryLookupPopoverWillClose:]):
Register for the LUNotificationPopoverWillClose notification and remove
our TextIndicator when it is called, just like in WebKit2.
Deregistration is already handled in _close.

(-[WebView _showDictionaryLookupPopup:]):
Just like WebKit2, show our TextIndicator and tell Lookup not to show its own,
if the relevant API is available.

* WebView/WebViewInternal.h:

* WebKit.xcodeproj/project.pbxproj:
Add mac/WebView/DictionaryPopupInfo.h.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@176680 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebKit/ChangeLog
Source/WebKit/WebKit.xcodeproj/project.pbxproj
Source/WebKit/mac/ChangeLog
Source/WebKit/mac/WebView/DictionaryPopupInfo.h [new file with mode: 0644]
Source/WebKit/mac/WebView/WebActionMenuController.mm
Source/WebKit/mac/WebView/WebHTMLView.mm
Source/WebKit/mac/WebView/WebView.mm
Source/WebKit/mac/WebView/WebViewInternal.h

index 65d8b92c9348d45ed980f5dca5977526541114d9..d0d763f9e38ac4bf39054e482c3a266fdfdf8350 100644 (file)
@@ -1,3 +1,14 @@
+2014-12-02  Tim Horton  <timothy_horton@apple.com>
+
+        Implement Lookup transition from selection for Legacy WebKit
+        https://bugs.webkit.org/show_bug.cgi?id=139180
+        <rdar://problem/19067172>
+
+        Reviewed by Beth Dakin.
+
+        * WebKit.xcodeproj/project.pbxproj:
+        Add mac/WebView/DictionaryPopupInfo.h.
+
 2014-12-01  Anders Carlsson  <andersca@apple.com>
 
         Remove IWebCookieManager on Windows
index c25853aafdac243802b84035c3b5b696c6474f67..7f7f3fe58b6d36be3dbbec5ee0065e344e343013 100644 (file)
@@ -85,6 +85,7 @@
                2D25396718CE85C200270222 /* WebSharingServicePickerController.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2D25396518CE85C200270222 /* WebSharingServicePickerController.mm */; };
                2DD632C219E5D1F0002E9C7B /* WebSelectionServiceController.h in Headers */ = {isa = PBXBuildFile; fileRef = 2DD632C019E5D1F0002E9C7B /* WebSelectionServiceController.h */; };
                2DD632C319E5D1F0002E9C7B /* WebSelectionServiceController.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2DD632C119E5D1F0002E9C7B /* WebSelectionServiceController.mm */; };
+               2DF064A91A2DD53C00DBB354 /* DictionaryPopupInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = 2DF064A81A2DD53C00DBB354 /* DictionaryPopupInfo.h */; };
                312E2FE514E48182007CCA18 /* WebNotification.h in Headers */ = {isa = PBXBuildFile; fileRef = 312E2FE314E48182007CCA18 /* WebNotification.h */; settings = {ATTRIBUTES = (Private, ); }; };
                312E2FE614E48182007CCA18 /* WebNotification.mm in Sources */ = {isa = PBXBuildFile; fileRef = 312E2FE414E48182007CCA18 /* WebNotification.mm */; };
                312E2FE914E48215007CCA18 /* WebNotificationInternal.h in Headers */ = {isa = PBXBuildFile; fileRef = 312E2FE814E48215007CCA18 /* WebNotificationInternal.h */; };
                2D81DAB303EB0B2D00A80166 /* WebFormDelegate.m */ = {isa = PBXFileReference; fileEncoding = 30; indentWidth = 4; lastKnownFileType = sourcecode.c.objc; path = WebFormDelegate.m; sourceTree = "<group>"; tabWidth = 8; usesTabs = 0; };
                2DD632C019E5D1F0002E9C7B /* WebSelectionServiceController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebSelectionServiceController.h; sourceTree = "<group>"; };
                2DD632C119E5D1F0002E9C7B /* WebSelectionServiceController.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WebSelectionServiceController.mm; sourceTree = "<group>"; };
+               2DF064A81A2DD53C00DBB354 /* DictionaryPopupInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DictionaryPopupInfo.h; sourceTree = "<group>"; };
                312E2FE314E48182007CCA18 /* WebNotification.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebNotification.h; sourceTree = "<group>"; };
                312E2FE414E48182007CCA18 /* WebNotification.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WebNotification.mm; sourceTree = "<group>"; };
                312E2FE814E48215007CCA18 /* WebNotificationInternal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebNotificationInternal.h; sourceTree = "<group>"; };
                                F52CA6BD02DF9D0F018635CA /* HTML */,
                                51E94C0706C02CA300A9B09E /* PDF */,
                                A10C1D79182030190036883A /* ios */,
+                               2DF064A81A2DD53C00DBB354 /* DictionaryPopupInfo.h */,
                                935D44E219F9AFF000FB5306 /* WebActionMenuController.h */,
                                935D44E319F9AFF000FB5306 /* WebActionMenuController.mm */,
                                8373435A0624EE0D00F3B289 /* WebArchive.h */,
                                A10C1D8C1820305E0036883A /* WebPDFViewIPhone.h in Headers */,
                                9398106D0824BF01008DF038 /* WebKitErrorsPrivate.h in Headers */,
                                CD8BFCE815531224005AFB25 /* WebKitFullScreenListener.h in Headers */,
+                               2DF064A91A2DD53C00DBB354 /* DictionaryPopupInfo.h in Headers */,
                                9398101D0824BF01008DF038 /* WebKitLogging.h in Headers */,
                                9398101E0824BF01008DF038 /* WebKitNSStringExtras.h in Headers */,
                                1AAF58940EDCCF15008D883D /* WebKitPluginAgent.defs in Headers */,
index fceb88d6c3fdab1e67fa5f7880f752e8b4647b20..4c89a6c70b1febf3c436309c73a59376885463e2 100644 (file)
@@ -1,3 +1,46 @@
+2014-12-02  Tim Horton  <timothy_horton@apple.com>
+
+        Implement Lookup transition from selection for Legacy WebKit
+        https://bugs.webkit.org/show_bug.cgi?id=139180
+        <rdar://problem/19067172>
+
+        Reviewed by Beth Dakin.
+
+        * WebView/DictionaryPopupInfo.h: Added.
+        Split DictionaryPopupInfo out into its own file.
+
+        * WebView/WebActionMenuController.mm:
+        (-[WebActionMenuController _lookupText:]):
+        Hand the DictionaryPopupInfo to WebView, which will show the TextIndicator and invoke Lookup.
+
+        (performDictionaryLookupForSelection):
+        (performDictionaryLookupForRange):
+        Plumb the transition style through these helpers.
+        Create a TextIndicator from the selection range.
+
+        * WebView/WebHTMLView.mm:
+        (-[WebHTMLView _lookUpInDictionaryFromMenu:]):
+        Make use of TextIndicator (and DictionaryPopupInfo, and the new
+        _showDictionaryLookupPopup on WebView) to add a transition from blue
+        when performing Lookup from the context menu.
+
+        (-[WebHTMLView quickLookWithEvent:]):
+        Remove our TextIndicator whenever a normal three-finger-tap event
+        comes through, as it will have its own indication.
+
+        * WebView/WebView.mm:
+        (-[WebView _commonInitializationWithFrameName:groupName:]):
+        (-[WebView _dictionaryLookupPopoverWillClose:]):
+        Register for the LUNotificationPopoverWillClose notification and remove
+        our TextIndicator when it is called, just like in WebKit2.
+        Deregistration is already handled in _close.
+
+        (-[WebView _showDictionaryLookupPopup:]):
+        Just like WebKit2, show our TextIndicator and tell Lookup not to show its own,
+        if the relevant API is available.
+
+        * WebView/WebViewInternal.h:
+
 2014-12-02  Anders Carlsson  <andersca@apple.com>
 
         Remove visited link handling from PageGroup
diff --git a/Source/WebKit/mac/WebView/DictionaryPopupInfo.h b/Source/WebKit/mac/WebView/DictionaryPopupInfo.h
new file mode 100644 (file)
index 0000000..de58c62
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2014 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.
+ */
+
+#if PLATFORM(MAC)
+
+#import <WebCore/FloatPoint.h>
+#import <wtf/RetainPtr.h>
+
+@class NSAttributedString;
+@class NSDictionary;
+
+namespace WebCore {
+class TextIndicator;
+};
+
+struct DictionaryPopupInfo {
+    NSPoint origin;
+    RetainPtr<NSDictionary> options;
+    RetainPtr<NSAttributedString> attributedString;
+    RefPtr<WebCore::TextIndicator> textIndicator;
+};
+
+#endif // PLATFORM(MAC)
index 4ea89d305408596ecaf5a2940182b5d5ee714c17..5c98824c55bc54ed3a71c98cc44ace91e613a5a1 100644 (file)
@@ -30,6 +30,7 @@
 #import "DOMElementInternal.h"
 #import "DOMNodeInternal.h"
 #import "DOMRangeInternal.h"
+#import "DictionaryPopupInfo.h"
 #import "WebDocumentInternal.h"
 #import "WebElementDictionary.h"
 #import "WebFrameInternal.h"
@@ -86,12 +87,6 @@ SOFT_LINK_CLASS(ImageKit, IKSlideshow)
 
 using namespace WebCore;
 
-struct DictionaryPopupInfo {
-    NSPoint origin;
-    RetainPtr<NSDictionary> options;
-    RetainPtr<NSAttributedString> attributedString;
-};
-
 @implementation WebActionMenuController
 
 - (id)initWithWebView:(WebView *)webView
@@ -633,15 +628,7 @@ static NSString *pathToPhotoOnDisk(NSString *suggestedFilename)
         return;
 
     DictionaryPopupInfo popupInfo = performDictionaryLookupForSelection(frame, frame->selection().selection());
-    if (!popupInfo.attributedString)
-        return;
-
-    NSPoint textBaselineOrigin = popupInfo.origin;
-
-    // Convert to screen coordinates.
-    textBaselineOrigin = [_webView.window convertRectToScreen:NSMakeRect(textBaselineOrigin.x, textBaselineOrigin.y, 0, 0)].origin;
-
-    [getLULookupDefinitionModuleClass() showDefinitionForTerm:popupInfo.attributedString.get() atLocation:textBaselineOrigin options:popupInfo.options.get()];
+    [_webView _showDictionaryLookupPopup:popupInfo];
 }
 
 - (void)_paste:(id)sender
@@ -681,11 +668,11 @@ static DictionaryPopupInfo performDictionaryLookupForSelection(Frame* frame, con
     DictionaryPopupInfo popupInfo;
     RefPtr<Range> selectedRange = rangeForDictionaryLookupForSelection(selection, &options);
     if (selectedRange)
-        popupInfo = performDictionaryLookupForRange(frame, *selectedRange, options);
+        popupInfo = performDictionaryLookupForRange(frame, *selectedRange, options, TextIndicatorPresentationTransition::BounceAndCrossfade);
     return popupInfo;
 }
 
-static DictionaryPopupInfo performDictionaryLookupForRange(Frame* frame, Range& range, NSDictionary *options)
+static DictionaryPopupInfo performDictionaryLookupForRange(Frame* frame, Range& range, NSDictionary *options, TextIndicatorPresentationTransition presentationTransition)
 {
     DictionaryPopupInfo popupInfo;
     if (range.text().stripWhiteSpace().isEmpty())
@@ -721,6 +708,7 @@ static DictionaryPopupInfo performDictionaryLookupForRange(Frame* frame, Range&
     }];
 
     popupInfo.attributedString = scaledNSAttributedString.get();
+    popupInfo.textIndicator = TextIndicator::createWithRange(range, presentationTransition);
     return popupInfo;
 }
 
index 6f87258157328819772f2e8ae0d75a379ed050de..102e0f21e0754e35b0a5c9a38926cd8fe4b5185b 100644 (file)
@@ -34,6 +34,7 @@
 #import "DOMDocumentInternal.h"
 #import "DOMNodeInternal.h"
 #import "DOMRangeInternal.h"
+#import "DictionaryPopupInfo.h"
 #import "WebActionMenuController.h"
 #import "WebArchive.h"
 #import "WebClipView.h"
 #import <WebCore/StyleProperties.h>
 #import <WebCore/Text.h>
 #import <WebCore/TextAlternativeWithRange.h>
+#import <WebCore/TextIndicator.h>
 #import <WebCore/TextUndoInsertionMarkupMac.h>
 #import <WebCore/WebCoreObjCExtras.h>
 #import <WebCore/WebNSAttributedStringExtras.h>
@@ -5634,12 +5636,22 @@ static BOOL writingDirectionKeyBindingsEnabled()
 
     NSRect rect = coreFrame->selection().selectionBounds();
 
-    NSDictionary *attributes = [attrString fontAttributesInRange:NSMakeRange(0,1)];
+    NSDictionary *attributes = [attrString fontAttributesInRange:NSMakeRange(0, 1)];
     NSFont *font = [attributes objectForKey:NSFontAttributeName];
     if (font)
-        rect.origin.y += [font ascender];
+        rect.origin.y += [font descender];
 
-    [self showDefinitionForAttributedString:attrString atPoint:rect.origin];
+    DictionaryPopupInfo info;
+    info.attributedString = attrString;
+    info.origin = coreFrame->view()->contentsToWindow(enclosingIntRect(rect)).location();
+    info.textIndicator = TextIndicator::createWithSelectionInFrame(*coreFrame, TextIndicatorPresentationTransition::BounceAndCrossfade);
+    [[self _webView] _showDictionaryLookupPopup:info];
+}
+
+- (void)quickLookWithEvent:(NSEvent *)event
+{
+    [[self _webView] _setTextIndicator:nullptr fadeOut:NO animationCompletionHandler:[] { }];
+    [super quickLookWithEvent:event];
 }
 #endif // !PLATFORM(IOS)
 
index 2cc92171a029a5e4caf73fdb832a4532adadecfb..2e5887e848d5785207d5a7a6c7e3726e2617f9f0 100644 (file)
@@ -35,6 +35,7 @@
 #import "DOMDocumentInternal.h"
 #import "DOMNodeInternal.h"
 #import "DOMRangeInternal.h"
+#import "DictionaryPopupInfo.h"
 #import "WebAlternativeTextClient.h"
 #import "WebApplicationCache.h"
 #import "WebBackForwardListInternal.h"
 #import "WebNSPasteboardExtras.h"
 #import "WebNSPrintOperationExtras.h"
 #import "WebPDFView.h"
+#import <WebCore/LookupSPI.h>
 #import <WebCore/NSViewSPI.h>
+#import <WebCore/SoftLinking.h>
 #import <WebCore/TextIndicator.h>
 #import <WebCore/TextIndicatorWindow.h>
 #import <WebCore/WebVideoFullscreenController.h>
 #import <WebCore/HIDGamepadProvider.h>
 #endif
 
+#if PLATFORM(MAC)
+SOFT_LINK_CONSTANT_MAY_FAIL(Lookup, LUNotificationPopoverWillClose, NSString *)
+SOFT_LINK_CONSTANT_MAY_FAIL(Lookup, LUTermOptionDisableSearchTermIndicator, NSString *)
+#endif
+
 #if !PLATFORM(IOS)
 @interface NSSpellChecker (WebNSSpellCheckerDetails)
 - (void)_preflightChosenSpellServer;
@@ -1021,6 +1029,9 @@ static void WebKitInitializeGamepadProviderIfNecessary()
 
 #if !PLATFORM(IOS)
     [self _registerDraggedTypes];
+
+    if (canLoadLUNotificationPopoverWillClose())
+        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_dictionaryLookupPopoverWillClose:) name:getLUNotificationPopoverWillClose() object:nil];
 #endif
 
     [self _setIsVisible:[self _isViewVisible]];
@@ -8550,7 +8561,8 @@ static void glibContextIterationCallback(CFRunLoopObserverRef, CFRunLoopActivity
     return NSMakeRect(rect.origin.x, [self bounds].size.height - rect.origin.y - rect.size.height, rect.size.width, rect.size.height);
 }
 
-#if PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101000
+#if PLATFORM(MAC)
+#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101000
 - (void)prepareForMenu:(NSMenu *)menu withEvent:(NSEvent *)event
 {
     if (menu != self.actionMenu)
@@ -8575,6 +8587,12 @@ static void glibContextIterationCallback(CFRunLoopObserverRef, CFRunLoopActivity
     [_private->actionMenuController didCloseMenu:menu withEvent:event];
 }
 
+- (WebActionMenuController *)_actionMenuController
+{
+    return _private->actionMenuController;
+}
+#endif // __MAC_OS_X_VERSION_MIN_REQUIRED >= 101000
+
 - (void)_setTextIndicator:(TextIndicator *)textIndicator fadeOut:(BOOL)fadeOut animationCompletionHandler:(std::function<void ()>)completionHandler
 {
     if (!textIndicator) {
@@ -8593,11 +8611,33 @@ static void glibContextIterationCallback(CFRunLoopObserverRef, CFRunLoopActivity
     [self _setTextIndicator:nullptr fadeOut:NO animationCompletionHandler:^ { }];
 }
 
-- (WebActionMenuController *)_actionMenuController
+- (void)_showDictionaryLookupPopup:(const DictionaryPopupInfo&)dictionaryPopupInfo
 {
-    return _private->actionMenuController;
+    if (!dictionaryPopupInfo.attributedString)
+        return;
+
+    NSPoint textBaselineOrigin = dictionaryPopupInfo.origin;
+
+    // Convert to screen coordinates.
+    textBaselineOrigin = [self.window convertRectToScreen:NSMakeRect(textBaselineOrigin.x, textBaselineOrigin.y, 0, 0)].origin;
+
+    if (canLoadLUTermOptionDisableSearchTermIndicator()) {
+        RetainPtr<NSMutableDictionary> mutableOptions = adoptNS([dictionaryPopupInfo.options mutableCopy]);
+        if (!mutableOptions)
+            mutableOptions = adoptNS([[NSMutableDictionary alloc] init]);
+        [mutableOptions setObject:@YES forKey:getLUTermOptionDisableSearchTermIndicator()];
+        [self _setTextIndicator:dictionaryPopupInfo.textIndicator.get() fadeOut:NO animationCompletionHandler:[dictionaryPopupInfo, textBaselineOrigin, mutableOptions] {
+            [getLULookupDefinitionModuleClass() showDefinitionForTerm:dictionaryPopupInfo.attributedString.get() atLocation:textBaselineOrigin options:mutableOptions.get()];
+        }];
+    } else
+        [getLULookupDefinitionModuleClass() showDefinitionForTerm:dictionaryPopupInfo.attributedString.get() atLocation:textBaselineOrigin options:dictionaryPopupInfo.options.get()];
 }
-#endif // PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101000
+
+- (void)_dictionaryLookupPopoverWillClose:(NSNotification *)notification
+{
+    [self _setTextIndicator:nullptr fadeOut:NO animationCompletionHandler:[] { }];
+}
+#endif // PLATFORM(MAC)
 
 @end
 
index 5888bf9dbc8cdab0c88aafb044934e9cd2d78c0d..dbd42463022d8255068dfc1fb0824f313c3bc955 100644 (file)
@@ -59,6 +59,7 @@ class URL;
 struct DictationAlternative;
 }
 
+struct DictionaryPopupInfo;
 class WebSelectionServiceController;
 #endif
 
@@ -259,6 +260,7 @@ OBJC_CLASS NSTextAlternatives;
 #if PLATFORM(MAC) && defined(__cplusplus)
 - (void)_setTextIndicator:(WebCore::TextIndicator*)textIndicator fadeOut:(BOOL)fadeOut animationCompletionHandler:(std::function<void ()>)completionHandler;
 - (void)_clearTextIndicator;
+- (void)_showDictionaryLookupPopup:(const DictionaryPopupInfo&)dictionaryPopupInfo;
 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101000
 - (WebActionMenuController *)_actionMenuController;
 #endif