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