818143a8c86191bffeaf4841313dacdb51f8fee9
[WebKit-https.git] / Source / WebKit / UIProcess / ios / WKContentViewInteraction.h
1 /*
2  * Copyright (C) 2012-2017 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 "DragDropInteractionState.h"
32 #import "EditorState.h"
33 #import "GestureTypes.h"
34 #import "InteractionInformationAtPosition.h"
35 #import "UIKitSPI.h"
36 #import "WKActionSheetAssistant.h"
37 #import "WKAirPlayRoutePicker.h"
38 #import "WKFileUploadPanel.h"
39 #import "WKFormPeripheral.h"
40 #import "WKSyntheticClickTapGestureRecognizer.h"
41 #import <UIKit/UIView.h>
42 #import <WebCore/Color.h>
43 #import <WebCore/FloatQuad.h>
44 #import <wtf/BlockPtr.h>
45 #import <wtf/Forward.h>
46 #import <wtf/Vector.h>
47 #import <wtf/text/WTFString.h>
48
49 namespace API {
50 class OpenPanelParameters;
51 }
52
53 namespace WTF {
54 class TextStream;
55 }
56
57 namespace WebCore {
58 class Color;
59 class FloatQuad;
60 class IntSize;
61 class SelectionRect;
62 struct PromisedBlobInfo;
63 }
64
65 #if ENABLE(DRAG_SUPPORT)
66 namespace WebCore {
67 struct DragItem;
68 }
69 #endif
70
71 namespace WebKit {
72 class InputViewUpdateDeferrer;
73 class NativeWebTouchEvent;
74 class SmartMagnificationController;
75 class WebOpenPanelResultListenerProxy;
76 class WebPageProxy;
77 }
78
79 @class WKActionSheetAssistant;
80 @class WKFormInputSession;
81 @class WKInspectorNodeSearchGestureRecognizer;
82 @class WebEvent;
83 @class _UIHighlightView;
84 @class _UIWebHighlightLongPressGestureRecognizer;
85
86 #if ENABLE(EXTRA_ZOOM_MODE)
87 @class WKFocusedFormControlViewController;
88 @class WKTextInputViewController;
89 #endif
90
91 typedef void (^UIWKAutocorrectionCompletionHandler)(UIWKAutocorrectionRects *rectsForInput);
92 typedef void (^UIWKAutocorrectionContextHandler)(UIWKAutocorrectionContext *autocorrectionContext);
93 typedef void (^UIWKDictationContextHandler)(NSString *selectedText, NSString *beforeText, NSString *afterText);
94 typedef void (^UIWKSelectionCompletionHandler)(void);
95 typedef void (^UIWKSelectionWithDirectionCompletionHandler)(BOOL selectionEndIsMoving);
96 typedef void (^UIWKKeyWebEventCompletionHandler)(::WebEvent *theEvent, BOOL wasHandled);
97
98 typedef BlockPtr<void(WebKit::InteractionInformationAtPosition)> InteractionInformationCallback;
99 typedef std::pair<WebKit::InteractionInformationRequest, InteractionInformationCallback> InteractionInformationRequestAndCallback;
100
101 #define FOR_EACH_WKCONTENTVIEW_ACTION(M) \
102     M(_addShortcut) \
103     M(_arrowKey) \
104     M(_define) \
105     M(_lookup) \
106     M(_promptForReplace) \
107     M(_reanalyze) \
108     M(_share) \
109     M(_showTextStyleOptions) \
110     M(_transliterateChinese) \
111     M(copy) \
112     M(cut) \
113     M(paste) \
114     M(replace) \
115     M(select) \
116     M(selectAll) \
117     M(toggleBoldface) \
118     M(toggleItalics) \
119     M(toggleUnderline)
120
121 namespace WebKit {
122
123 struct WKSelectionDrawingInfo {
124     enum class SelectionType { None, Plugin, Range };
125     WKSelectionDrawingInfo();
126     explicit WKSelectionDrawingInfo(const EditorState&);
127     SelectionType type;
128     WebCore::IntRect caretRect;
129     Vector<WebCore::SelectionRect> selectionRects;
130 };
131
132 WTF::TextStream& operator<<(WTF::TextStream&, const WKSelectionDrawingInfo&);
133
134 struct WKAutoCorrectionData {
135     String fontName;
136     CGFloat fontSize;
137     uint64_t fontTraits;
138     CGRect textFirstRect;
139     CGRect textLastRect;
140     UIWKAutocorrectionCompletionHandler autocorrectionHandler;
141     UIWKAutocorrectionContextHandler autocorrectionContextHandler;
142 };
143
144 }
145
146 @interface WKContentView () {
147     RetainPtr<UIWebTouchEventsGestureRecognizer> _touchEventGestureRecognizer;
148
149     BOOL _canSendTouchEventsAsynchronously;
150
151     RetainPtr<WKSyntheticClickTapGestureRecognizer> _singleTapGestureRecognizer;
152     RetainPtr<_UIWebHighlightLongPressGestureRecognizer> _highlightLongPressGestureRecognizer;
153     RetainPtr<UILongPressGestureRecognizer> _longPressGestureRecognizer;
154     RetainPtr<UITapGestureRecognizer> _doubleTapGestureRecognizer;
155     RetainPtr<UITapGestureRecognizer> _nonBlockingDoubleTapGestureRecognizer;
156     RetainPtr<UITapGestureRecognizer> _twoFingerDoubleTapGestureRecognizer;
157     RetainPtr<UITapGestureRecognizer> _twoFingerSingleTapGestureRecognizer;
158     RetainPtr<WKInspectorNodeSearchGestureRecognizer> _inspectorNodeSearchGestureRecognizer;
159
160     RetainPtr<UIWKTextInteractionAssistant> _textSelectionAssistant;
161     RetainPtr<UIWKSelectionAssistant> _webSelectionAssistant;
162     BOOL _suppressAssistantSelectionView;
163
164     RetainPtr<UITextInputTraits> _traits;
165     RetainPtr<UIWebFormAccessory> _formAccessoryView;
166     RetainPtr<_UIHighlightView> _highlightView;
167     RetainPtr<UIView> _interactionViewsContainerView;
168     RetainPtr<NSString> _markedText;
169     RetainPtr<WKActionSheetAssistant> _actionSheetAssistant;
170     RetainPtr<WKAirPlayRoutePicker> _airPlayRoutePicker;
171     RetainPtr<WKFormInputSession> _formInputSession;
172     RetainPtr<WKFileUploadPanel> _fileUploadPanel;
173     RetainPtr<UIGestureRecognizer> _previewGestureRecognizer;
174     RetainPtr<UIGestureRecognizer> _previewSecondaryGestureRecognizer;
175     Vector<bool> _focusStateStack;
176 #if HAVE(LINK_PREVIEW)
177     RetainPtr<UIPreviewItemController> _previewItemController;
178 #endif
179
180     std::unique_ptr<WebKit::SmartMagnificationController> _smartMagnificationController;
181
182     id <UITextInputDelegate> _inputDelegate;
183
184     uint64_t _latestTapID;
185     struct TapHighlightInformation {
186         WebCore::Color color;
187         Vector<WebCore::FloatQuad> quads;
188         WebCore::IntSize topLeftRadius;
189         WebCore::IntSize topRightRadius;
190         WebCore::IntSize bottomLeftRadius;
191         WebCore::IntSize bottomRightRadius;
192     };
193     TapHighlightInformation _tapHighlightInformation;
194
195     WebKit::WKAutoCorrectionData _autocorrectionData;
196     WebKit::InteractionInformationAtPosition _positionInformation;
197     WebKit::AssistedNodeInformation _assistedNodeInformation;
198     RetainPtr<NSObject<WKFormPeripheral>> _inputPeripheral;
199     RetainPtr<UIEvent> _uiEventBeingResent;
200     UIWKKeyWebEventCompletionHandler _keyWebEventHandler;
201
202     CGPoint _lastInteractionLocation;
203     uint64_t _layerTreeTransactionIdAtLastTouchStart;
204
205     WebKit::WKSelectionDrawingInfo _lastSelectionDrawingInfo;
206
207     std::optional<WebKit::InteractionInformationRequest> _outstandingPositionInformationRequest;
208
209     uint64_t _positionInformationCallbackDepth;
210     Vector<std::optional<InteractionInformationRequestAndCallback>> _pendingPositionInformationHandlers;
211     
212     std::unique_ptr<WebKit::InputViewUpdateDeferrer> _inputViewUpdateDeferrer;
213
214     BOOL _isEditable;
215     BOOL _showingTextStyleOptions;
216     BOOL _hasValidPositionInformation;
217     BOOL _isTapHighlightIDValid;
218     BOOL _potentialTapInProgress;
219     BOOL _isDoubleTapPending;
220     BOOL _highlightLongPressCanClick;
221     BOOL _hasTapHighlightForPotentialTap;
222     BOOL _selectionNeedsUpdate;
223     BOOL _shouldRestoreSelection;
224     BOOL _usingGestureForSelection;
225     BOOL _inspectorNodeSearchEnabled;
226     BOOL _didAccessoryTabInitiateFocus;
227     BOOL _isExpectingFastSingleTapCommit;
228     BOOL _showDebugTapHighlightsForFastClicking;
229
230     BOOL _becomingFirstResponder;
231     BOOL _resigningFirstResponder;
232     BOOL _needsDeferredEndScrollingSelectionUpdate;
233     BOOL _isChangingFocus;
234
235 #if ENABLE(DATA_INTERACTION)
236     WebKit::DragDropInteractionState _dragDropInteractionState;
237     RetainPtr<UIDragInteraction> _dragInteraction;
238     RetainPtr<UIDropInteraction> _dropInteraction;
239     BOOL _shouldRestoreCalloutBarAfterDrop;
240     BOOL _isAnimatingConcludeEditDrag;
241     RetainPtr<UIView> _visibleContentViewSnapshot;
242     RetainPtr<_UITextDragCaretView> _editDropCaretView;
243 #endif
244
245 #if ENABLE(EXTRA_ZOOM_MODE)
246     RetainPtr<WKTextInputViewController> _textInputViewController;
247     RetainPtr<WKFocusedFormControlViewController> _focusedFormControlViewController;
248 #endif
249 }
250
251 @end
252
253 @interface WKContentView (WKInteraction) <UIGestureRecognizerDelegate, UITextAutoscrolling, UITextInputMultiDocument, UITextInputPrivate, UIWebFormAccessoryDelegate, UIWebTouchEventsGestureRecognizerDelegate, UIWKInteractionViewProtocol, WKActionSheetAssistantDelegate, WKFileUploadPanelDelegate
254 #if ENABLE(DATA_INTERACTION)
255     , UIDragInteractionDelegate, UIDropInteractionDelegate
256 #endif
257 >
258
259 @property (nonatomic, readonly) CGPoint lastInteractionLocation;
260 @property (nonatomic, readonly) BOOL isEditable;
261 @property (nonatomic, readonly) BOOL shouldHideSelectionWhenScrolling;
262 @property (nonatomic, readonly) const WebKit::InteractionInformationAtPosition& positionInformation;
263 @property (nonatomic, readonly) const WebKit::WKAutoCorrectionData& autocorrectionData;
264 @property (nonatomic, readonly) const WebKit::AssistedNodeInformation& assistedNodeInformation;
265 @property (nonatomic, readonly) UIWebFormAccessory *formAccessoryView;
266 @property (nonatomic) BOOL suppressAssistantSelectionView;
267
268 - (void)setupInteraction;
269 - (void)cleanupInteraction;
270
271 - (void)scrollViewWillStartPanOrPinchGesture;
272
273 - (BOOL)canBecomeFirstResponderForWebView;
274 - (BOOL)becomeFirstResponderForWebView;
275 - (BOOL)resignFirstResponderForWebView;
276 - (BOOL)canPerformActionForWebView:(SEL)action withSender:(id)sender;
277 - (id)targetForActionForWebView:(SEL)action withSender:(id)sender;
278
279 #define DECLARE_WKCONTENTVIEW_ACTION_FOR_WEB_VIEW(_action) \
280     - (void)_action ## ForWebView:(id)sender;
281 FOR_EACH_WKCONTENTVIEW_ACTION(DECLARE_WKCONTENTVIEW_ACTION_FOR_WEB_VIEW)
282 #undef DECLARE_WKCONTENTVIEW_ACTION_FOR_WEB_VIEW
283
284 #if ENABLE(TOUCH_EVENTS)
285 - (void)_webTouchEvent:(const WebKit::NativeWebTouchEvent&)touchEvent preventsNativeGestures:(BOOL)preventsDefault;
286 #endif
287 - (void)_commitPotentialTapFailed;
288 - (void)_didNotHandleTapAsClick:(const WebCore::IntPoint&)point;
289 - (void)_didCompleteSyntheticClick;
290 - (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;
291
292 - (BOOL)_mayDisableDoubleTapGesturesDuringSingleTap;
293 - (void)_disableDoubleTapGesturesDuringTapIfNecessary:(uint64_t)requestID;
294 - (void)_startAssistingNode:(const WebKit::AssistedNodeInformation&)information userIsInteracting:(BOOL)userIsInteracting blurPreviousNode:(BOOL)blurPreviousNode changingActivityState:(BOOL)changingActivityState userObject:(NSObject <NSSecureCoding> *)userObject;
295 - (void)_stopAssistingNode;
296 - (void)_selectionChanged;
297 - (void)_updateChangedSelection;
298 - (BOOL)_interpretKeyEvent:(::WebEvent *)theEvent isCharEvent:(BOOL)isCharEvent;
299 - (void)_positionInformationDidChange:(const WebKit::InteractionInformationAtPosition&)info;
300 - (void)_attemptClickAtLocation:(CGPoint)location;
301 - (void)_willStartScrollingOrZooming;
302 - (void)_didScroll;
303 - (void)_didEndScrollingOrZooming;
304 - (void)_overflowScrollingWillBegin;
305 - (void)_overflowScrollingDidEnd;
306 #if __IPHONE_OS_VERSION_MAX_ALLOWED < 120000
307 - (void)_didUpdateBlockSelectionWithTouch:(WebKit::SelectionTouch)touch withFlags:(WebKit::SelectionFlags)flags growThreshold:(CGFloat)growThreshold shrinkThreshold:(CGFloat)shrinkThreshold;
308 #endif
309 - (void)_showPlaybackTargetPicker:(BOOL)hasVideo fromRect:(const WebCore::IntRect&)elementRect;
310 - (void)_showRunOpenPanel:(API::OpenPanelParameters*)parameters resultListener:(WebKit::WebOpenPanelResultListenerProxy*)listener;
311 - (void)accessoryDone;
312 - (void)_didHandleKeyEvent:(::WebEvent *)event eventWasHandled:(BOOL)eventWasHandled;
313 - (Vector<WebKit::OptionItem>&) assistedNodeSelectOptions;
314 - (void)_enableInspectorNodeSearch;
315 - (void)_disableInspectorNodeSearch;
316 - (void)_becomeFirstResponderWithSelectionMovingForward:(BOOL)selectingForward completionHandler:(void (^)(BOOL didBecomeFirstResponder))completionHandler;
317 - (void)_setDoubleTapGesturesEnabled:(BOOL)enabled;
318 #if ENABLE(DATA_DETECTION)
319 - (NSArray *)_dataDetectionResults;
320 #endif
321 - (NSArray<NSValue *> *)_uiTextSelectionRects;
322 - (void)accessibilityRetrieveSpeakSelectionContent;
323 - (void)_accessibilityRetrieveRectsEnclosingSelectionOffset:(NSInteger)offset withGranularity:(UITextGranularity)granularity;
324 - (void)_accessibilityRetrieveRectsAtSelectionOffset:(NSInteger)offset withText:(NSString *)text completionHandler:(void (^)(const Vector<WebCore::SelectionRect>& rects))completionHandler;
325 - (void)_accessibilityRetrieveRectsAtSelectionOffset:(NSInteger)offset withText:(NSString *)text;
326 - (void)_accessibilityStoreSelection;
327 - (void)_accessibilityClearSelection;
328
329 @property (nonatomic, readonly) WebKit::InteractionInformationAtPosition currentPositionInformation;
330 - (void)doAfterPositionInformationUpdate:(void (^)(WebKit::InteractionInformationAtPosition))action forRequest:(WebKit::InteractionInformationRequest)request;
331 - (BOOL)ensurePositionInformationIsUpToDate:(WebKit::InteractionInformationRequest)request;
332
333 #if ENABLE(DATA_INTERACTION)
334 - (void)_didChangeDragInteractionPolicy;
335 - (void)_didPerformDataInteractionControllerOperation:(BOOL)handled;
336 - (void)_didHandleStartDataInteractionRequest:(BOOL)started;
337 - (void)_didHandleAdditionalDragItemsRequest:(BOOL)added;
338 - (void)_startDrag:(RetainPtr<CGImageRef>)image item:(const WebCore::DragItem&)item;
339 - (void)_didConcludeEditDataInteraction:(std::optional<WebCore::TextIndicatorData>)data;
340 - (void)_didChangeDataInteractionCaretRect:(CGRect)previousRect currentRect:(CGRect)rect;
341 #endif
342
343 @end
344
345 @interface WKContentView (WKTesting)
346
347 - (void)_simulateLongPressActionAtLocation:(CGPoint)location;
348 - (void)selectFormAccessoryPickerRow:(NSInteger)rowIndex;
349 - (NSDictionary *)_contentsOfUserInterfaceItem:(NSString *)userInterfaceItem;
350
351 @end
352
353 #if HAVE(LINK_PREVIEW)
354 @interface WKContentView (WKInteractionPreview) <UIPreviewItemDelegate>
355
356 - (void)_registerPreview;
357 - (void)_unregisterPreview;
358 @end
359 #endif
360
361 #endif // PLATFORM(IOS)