Add support for recognizing data interaction gestures in WebKit2
[WebKit-https.git] / Source / WebKit2 / UIProcess / ios / WKContentViewInteraction.h
1 /*
2  * Copyright (C) 2012-2014 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #if PLATFORM(IOS)
27
28 #import "WKContentView.h"
29
30 #import "AssistedNodeInformation.h"
31 #import "EditorState.h"
32 #import "GestureTypes.h"
33 #import "InteractionInformationAtPosition.h"
34 #import "UIKitSPI.h"
35 #import "WKActionSheetAssistant.h"
36 #import "WKAirPlayRoutePicker.h"
37 #import "WKFileUploadPanel.h"
38 #import "WKFormPeripheral.h"
39 #import "WKSyntheticClickTapGestureRecognizer.h"
40 #import <UIKit/UIView.h>
41 #import <WebCore/Color.h>
42 #import <WebCore/FloatQuad.h>
43 #import <wtf/Forward.h>
44 #import <wtf/Vector.h>
45 #import <wtf/text/WTFString.h>
46
47 #if USE(APPLE_INTERNAL_SDK) && __has_include(<WebKitAdditions/WKContentViewInteractionAdditions.h>)
48 #import <WebKitAdditions/WKContentViewInteractionAdditions.h>
49 #endif
50
51 namespace API {
52 class OpenPanelParameters;
53 }
54
55 namespace WebCore {
56 class Color;
57 class FloatQuad;
58 class IntSize;
59 class TextStream;
60 }
61
62 namespace WebKit {
63 class NativeWebTouchEvent;
64 class SmartMagnificationController;
65 class WebOpenPanelResultListenerProxy;
66 class WebPageProxy;
67 }
68
69 @class WKActionSheetAssistant;
70 @class WKFormInputSession;
71 @class WKInspectorNodeSearchGestureRecognizer;
72 @class WebIOSEvent;
73 @class _UIHighlightView;
74 @class _UIWebHighlightLongPressGestureRecognizer;
75
76 typedef void (^UIWKAutocorrectionCompletionHandler)(UIWKAutocorrectionRects *rectsForInput);
77 typedef void (^UIWKAutocorrectionContextHandler)(UIWKAutocorrectionContext *autocorrectionContext);
78 typedef void (^UIWKDictationContextHandler)(NSString *selectedText, NSString *beforeText, NSString *afterText);
79 typedef void (^UIWKSelectionCompletionHandler)(void);
80 typedef void (^UIWKSelectionWithDirectionCompletionHandler)(BOOL selectionEndIsMoving);
81 typedef void (^UIWKKeyWebEventCompletionHandler)(WebIOSEvent *theEvent, BOOL wasHandled);
82
83 namespace WebKit {
84
85 struct WKSelectionDrawingInfo {
86     enum class SelectionType { None, Plugin, Range };
87     WKSelectionDrawingInfo();
88     explicit WKSelectionDrawingInfo(const EditorState&);
89     SelectionType type;
90     WebCore::IntRect caretRect;
91     Vector<WebCore::SelectionRect> selectionRects;
92 };
93
94 WebCore::TextStream& operator<<(WebCore::TextStream&, const WKSelectionDrawingInfo&);
95
96 struct WKAutoCorrectionData {
97     String fontName;
98     CGFloat fontSize;
99     uint64_t fontTraits;
100     CGRect textFirstRect;
101     CGRect textLastRect;
102     UIWKAutocorrectionCompletionHandler autocorrectionHandler;
103     UIWKAutocorrectionContextHandler autocorrectionContextHandler;
104 };
105
106 }
107
108 @interface WKContentView () {
109     RetainPtr<UIWebTouchEventsGestureRecognizer> _touchEventGestureRecognizer;
110
111     BOOL _canSendTouchEventsAsynchronously;
112
113     RetainPtr<WKSyntheticClickTapGestureRecognizer> _singleTapGestureRecognizer;
114     RetainPtr<_UIWebHighlightLongPressGestureRecognizer> _highlightLongPressGestureRecognizer;
115     RetainPtr<UILongPressGestureRecognizer> _longPressGestureRecognizer;
116     RetainPtr<UITapGestureRecognizer> _doubleTapGestureRecognizer;
117     RetainPtr<UITapGestureRecognizer> _nonBlockingDoubleTapGestureRecognizer;
118     RetainPtr<UITapGestureRecognizer> _twoFingerDoubleTapGestureRecognizer;
119     RetainPtr<UITapGestureRecognizer> _twoFingerSingleTapGestureRecognizer;
120     RetainPtr<WKInspectorNodeSearchGestureRecognizer> _inspectorNodeSearchGestureRecognizer;
121
122     RetainPtr<UIWKTextInteractionAssistant> _textSelectionAssistant;
123     RetainPtr<UIWKSelectionAssistant> _webSelectionAssistant;
124
125     RetainPtr<UITextInputTraits> _traits;
126     RetainPtr<UIWebFormAccessory> _formAccessoryView;
127     RetainPtr<_UIHighlightView> _highlightView;
128     RetainPtr<UIView> _interactionViewsContainerView;
129     RetainPtr<NSString> _markedText;
130     RetainPtr<WKActionSheetAssistant> _actionSheetAssistant;
131     RetainPtr<WKAirPlayRoutePicker> _airPlayRoutePicker;
132     RetainPtr<WKFormInputSession> _formInputSession;
133     RetainPtr<WKFileUploadPanel> _fileUploadPanel;
134     RetainPtr<UIGestureRecognizer> _previewGestureRecognizer;
135     RetainPtr<UIGestureRecognizer> _previewSecondaryGestureRecognizer;
136 #if HAVE(LINK_PREVIEW)
137     RetainPtr<UIPreviewItemController> _previewItemController;
138 #endif
139
140     std::unique_ptr<WebKit::SmartMagnificationController> _smartMagnificationController;
141
142     id <UITextInputDelegate> _inputDelegate;
143
144     uint64_t _latestTapID;
145     struct TapHighlightInformation {
146         WebCore::Color color;
147         Vector<WebCore::FloatQuad> quads;
148         WebCore::IntSize topLeftRadius;
149         WebCore::IntSize topRightRadius;
150         WebCore::IntSize bottomLeftRadius;
151         WebCore::IntSize bottomRightRadius;
152     };
153     TapHighlightInformation _tapHighlightInformation;
154
155     WebKit::WKAutoCorrectionData _autocorrectionData;
156     WebKit::InteractionInformationAtPosition _positionInformation;
157     WebKit::AssistedNodeInformation _assistedNodeInformation;
158     RetainPtr<NSObject<WKFormPeripheral>> _inputPeripheral;
159     RetainPtr<UIEvent> _uiEventBeingResent;
160     UIWKKeyWebEventCompletionHandler _keyWebEventHandler;
161
162     CGPoint _lastInteractionLocation;
163
164     WebKit::WKSelectionDrawingInfo _lastSelectionDrawingInfo;
165
166     std::optional<WebKit::InteractionInformationRequest> _outstandingPositionInformationRequest;
167
168     BOOL _isEditable;
169     BOOL _showingTextStyleOptions;
170     BOOL _hasValidPositionInformation;
171     BOOL _isTapHighlightIDValid;
172     BOOL _potentialTapInProgress;
173     BOOL _isDoubleTapPending;
174     BOOL _highlightLongPressCanClick;
175     BOOL _hasTapHighlightForPotentialTap;
176     BOOL _selectionNeedsUpdate;
177     BOOL _shouldRestoreSelection;
178     BOOL _usingGestureForSelection;
179     BOOL _inspectorNodeSearchEnabled;
180     BOOL _didAccessoryTabInitiateFocus;
181     BOOL _isExpectingFastSingleTapCommit;
182     BOOL _showDebugTapHighlightsForFastClicking;
183
184     BOOL _resigningFirstResponder;
185     BOOL _needsDeferredEndScrollingSelectionUpdate;
186
187 #if ENABLE(DATA_INTERACTION)
188     RetainPtr<UILongPressGestureRecognizer> _dataInteractionGestureRecognizer;
189     RetainPtr<UIImage> _currentDataInteractionImage;
190     CGPoint _currentDataInteractionOrigin;
191     BOOL _shouldHandleLongPressActionAfterDataInteraction;
192 #endif
193 }
194
195 @end
196
197 @interface WKContentView (WKInteraction) <UIGestureRecognizerDelegate, UIWebTouchEventsGestureRecognizerDelegate, UITextInputPrivate, UIWebFormAccessoryDelegate, UIWKInteractionViewProtocol, WKFileUploadPanelDelegate, WKActionSheetAssistantDelegate
198 #if ENABLE(DATA_INTERACTION)
199     , WKViewDataInteractionSourceDelegate, WKDataInteractionSessionDelegate, WKViewDataInteractionDestinationDelegate, WKDataInteractionItemVisualTarget
200 #endif
201 >
202
203 @property (nonatomic, readonly) CGPoint lastInteractionLocation;
204 @property (nonatomic, readonly) BOOL isEditable;
205 @property (nonatomic, readonly) BOOL shouldHideSelectionWhenScrolling;
206 @property (nonatomic, readonly) const WebKit::InteractionInformationAtPosition& positionInformation;
207 @property (nonatomic, readonly) const WebKit::WKAutoCorrectionData& autocorrectionData;
208 @property (nonatomic, readonly) const WebKit::AssistedNodeInformation& assistedNodeInformation;
209 @property (nonatomic, readonly) UIWebFormAccessory *formAccessoryView;
210
211 - (void)setupInteraction;
212 - (void)cleanupInteraction;
213
214 - (void)scrollViewWillStartPanOrPinchGesture;
215
216 #if ENABLE(TOUCH_EVENTS)
217 - (void)_webTouchEvent:(const WebKit::NativeWebTouchEvent&)touchEvent preventsNativeGestures:(BOOL)preventsDefault;
218 #endif
219 - (void)_commitPotentialTapFailed;
220 - (void)_didNotHandleTapAsClick:(const WebCore::IntPoint&)point;
221 - (void)_didGetTapHighlightForRequest:(uint64_t)requestID color:(const WebCore::Color&)color quads:(const Vector<WebCore::FloatQuad>&)highlightedQuads topLeftRadius:(const WebCore::IntSize&)topLeftRadius topRightRadius:(const WebCore::IntSize&)topRightRadius bottomLeftRadius:(const WebCore::IntSize&)bottomLeftRadius bottomRightRadius:(const WebCore::IntSize&)bottomRightRadius;
222
223 - (BOOL)_mayDisableDoubleTapGesturesDuringSingleTap;
224 - (void)_disableDoubleTapGesturesDuringTapIfNecessary:(uint64_t)requestID;
225 - (void)_startAssistingNode:(const WebKit::AssistedNodeInformation&)information userIsInteracting:(BOOL)userIsInteracting blurPreviousNode:(BOOL)blurPreviousNode userObject:(NSObject <NSSecureCoding> *)userObject;
226 - (void)_stopAssistingNode;
227 - (void)_selectionChanged;
228 - (void)_updateChangedSelection;
229 - (BOOL)_interpretKeyEvent:(WebIOSEvent *)theEvent isCharEvent:(BOOL)isCharEvent;
230 - (void)_positionInformationDidChange:(const WebKit::InteractionInformationAtPosition&)info;
231 - (void)_attemptClickAtLocation:(CGPoint)location;
232 - (void)_willStartScrollingOrZooming;
233 - (void)_didScroll;
234 - (void)_didEndScrollingOrZooming;
235 - (void)_overflowScrollingWillBegin;
236 - (void)_overflowScrollingDidEnd;
237 - (void)_didUpdateBlockSelectionWithTouch:(WebKit::SelectionTouch)touch withFlags:(WebKit::SelectionFlags)flags growThreshold:(CGFloat)growThreshold shrinkThreshold:(CGFloat)shrinkThreshold;
238 - (void)_showPlaybackTargetPicker:(BOOL)hasVideo fromRect:(const WebCore::IntRect&)elementRect;
239 - (void)_showRunOpenPanel:(API::OpenPanelParameters*)parameters resultListener:(WebKit::WebOpenPanelResultListenerProxy*)listener;
240 - (void)accessoryDone;
241 - (void)_didHandleKeyEvent:(WebIOSEvent *)event eventWasHandled:(BOOL)eventWasHandled;
242 - (Vector<WebKit::OptionItem>&) assistedNodeSelectOptions;
243 - (void)_enableInspectorNodeSearch;
244 - (void)_disableInspectorNodeSearch;
245 - (void)_becomeFirstResponderWithSelectionMovingForward:(BOOL)selectingForward completionHandler:(void (^)(BOOL didBecomeFirstResponder))completionHandler;
246 - (void)_setDoubleTapGesturesEnabled:(BOOL)enabled;
247 - (NSArray *)_dataDetectionResults;
248 - (NSArray<NSValue *> *)_uiTextSelectionRects;
249 - (void)accessibilityRetrieveSpeakSelectionContent;
250
251 #if ENABLE(DATA_INTERACTION)
252 - (void)_didPerformDataInteractionControllerOperation;
253 - (void)_startDataInteractionWithImage:(RetainPtr<CGImageRef>)image atClientPosition:(CGPoint)clientPosition isLink:(BOOL)isLink;
254 #endif
255
256 @end
257
258 @interface WKContentView (WKTesting)
259
260 - (void)selectFormAccessoryPickerRow:(NSInteger)rowIndex;
261 - (NSDictionary *)_contentsOfUserInterfaceItem:(NSString *)userInterfaceItem;
262
263 @end
264
265 #if HAVE(LINK_PREVIEW)
266 @interface WKContentView (WKInteractionPreview) <UIPreviewItemDelegate>
267
268 - (void)_registerPreview;
269 - (void)_unregisterPreview;
270 @end
271 #endif
272
273 #endif // PLATFORM(IOS)