WK1: Add initial support for immediate actions
[WebKit-https.git] / Source / WebKit / mac / WebView / WebImmediateActionController.mm
1 /*
2  * Copyright (C) 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 #import "WebImmediateActionController.h"
27
28 #if PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101000
29
30 #import "WebFrameInternal.h"
31 #import "WebHTMLView.h"
32 #import "WebHTMLViewInternal.h"
33 #import "WebUIDelegatePrivate.h"
34 #import "WebViewInternal.h"
35 #import <WebCore/EventHandler.h>
36 #import <WebCore/Frame.h>
37 #import <WebCore/NSImmediateActionGestureRecognizerSPI.h>
38 #import <objc/objc-class.h>
39 #import <objc/objc.h>
40
41 using namespace WebCore;
42
43 @implementation WebImmediateActionController
44
45 - (instancetype)initWithWebView:(WebView *)webView
46 {
47     if (!(self = [super init]))
48         return nil;
49
50     _webView = webView;
51     _type = WebImmediateActionNone;
52
53     return self;
54 }
55
56 - (void)webViewClosed
57 {
58     _webView = nil;
59 }
60
61 - (void)_clearImmediateActionState
62 {
63     _type = WebImmediateActionNone;
64 }
65
66 - (void)performHitTestAtPoint:(NSPoint)viewPoint
67 {
68     Frame* coreFrame = core([[[[_webView _selectedOrMainFrame] frameView] documentView] _frame]);
69     if (!coreFrame)
70         return;
71     _hitTestResult = coreFrame->eventHandler().hitTestResultAtPoint(IntPoint(viewPoint));
72 }
73
74 #pragma mark NSGestureRecognizerDelegate
75
76 - (void)immediateActionRecognizerWillPrepare:(NSImmediateActionGestureRecognizer *)immediateActionRecognizer
77 {
78     if (!_webView)
79         return;
80
81     if (immediateActionRecognizer.view != _webView)
82         return;
83
84     WebHTMLView *documentView = [[[_webView _selectedOrMainFrame] frameView] documentView];
85     NSPoint locationInDocumentView = [immediateActionRecognizer locationInView:documentView];
86     [self performHitTestAtPoint:locationInDocumentView];
87 }
88
89 - (void)immediateActionRecognizerWillBeginAnimation:(NSImmediateActionGestureRecognizer *)immediateActionRecognizer
90 {
91     if (immediateActionRecognizer.view != _webView)
92         return;
93
94     // FIXME: Add support for the types of functionality provided in Action menu's menuNeedsUpdate.
95 }
96
97 - (void)immediateActionRecognizerDidCancelAnimation:(NSImmediateActionGestureRecognizer *)immediateActionRecognizer
98 {
99     if (immediateActionRecognizer.view != _webView)
100         return;
101
102     [self _clearImmediateActionState];
103 }
104
105 - (void)immediateActionRecognizerDidCompleteAnimation:(NSImmediateActionGestureRecognizer *)immediateActionRecognizer
106 {
107     if (immediateActionRecognizer.view != _webView)
108         return;
109
110     // FIXME: Add support for the types of functionality provided in Action menu's willOpenMenu.
111 }
112
113 #pragma mark Immediate actions
114
115 - (void)_updateImmediateActionItem
116 {
117     // FIXME: Implement. Inspect _hitTestResult to determine if there is an immediate action to take.
118 }
119
120 @end
121
122 #endif // PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101000