Tizen 2.1 base
[framework/web/webkit-efl.git] / Source / WebKit2 / UIProcess / API / efl / tizen / Flick.cpp
1 /*
2  * Copyright (C) 2012 Samsung Electronics
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this program; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include "config.h"
21 #include "Flick.h"
22
23 #include "EasingUtilities.h"
24 #include "PageClientImpl.h"
25 #include "WKAPICast.h"
26 #include "WKPageTizen.h"
27 #include "ewk_view_private.h"
28
29 using namespace WebCore;
30 using namespace WebKit;
31
32 Flick::Flick(Evas_Object* viewWidget)
33     : m_viewWidget(viewWidget)
34     , m_velocity()
35     , m_flickIndex(0)
36     , m_flickAnimator(0)
37 {
38 }
39
40 Flick::~Flick()
41 {
42     stop();
43 }
44
45 void Flick::start(IntPoint velocity)
46 {
47     stop();
48
49     if (!velocity.x() && !velocity.y())
50         return;
51
52     m_velocity = velocity;
53     m_flickIndex = 0;
54     m_flickDuration = 1 / ecore_animator_frametime_get();
55     m_flickAnimator = ecore_animator_add(flickAnimatorCallback, this);
56     m_remainder = IntPoint();
57 }
58
59 void Flick::stop()
60 {
61     if (m_flickAnimator) {
62         ecore_animator_del(m_flickAnimator);
63         m_flickAnimator = 0;
64     }
65 }
66
67 Eina_Bool Flick::flickAnimatorCallback(void* data)
68 {
69     return static_cast<Flick*>(data)->process();
70 }
71
72 bool Flick::process()
73 {
74     double valueOfEaseInOutCubic = easeInOutQuad(m_flickDuration, m_flickIndex);
75     double decimalDeltaX = -1 * m_velocity.x() / m_flickDuration * valueOfEaseInOutCubic + m_remainder.x();
76     double decimalDeltaY = -1 * m_velocity.y() / m_flickDuration * valueOfEaseInOutCubic + m_remainder.y();
77     int deltaX = static_cast<int>(decimalDeltaX);
78     int deltaY = static_cast<int>(decimalDeltaY);
79
80     m_remainder.setX(decimalDeltaX - deltaX);
81     if (m_remainder.x() >= 0.5) {
82         deltaX += 1;
83         m_remainder.setX(m_remainder.x() - 0.5);
84     } else if (m_remainder.x() <= -0.5) {
85         deltaX -= 1;
86         m_remainder.setX(m_remainder.x() + 0.5);
87     }
88
89     m_remainder.setY(decimalDeltaY - deltaY);
90     if (m_remainder.y() >= 0.5) {
91         deltaY += 1;
92         m_remainder.setY(m_remainder.y() - 0.5);
93     } else if (m_remainder.y() <= -0.5) {
94         deltaY -= 1;
95         m_remainder.setY(m_remainder.y() + 0.5);
96     }
97
98     m_flickIndex++;
99
100     if (m_flickIndex > m_flickDuration) {
101         m_flickAnimator = 0;
102 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
103         PageClientImpl* pageClientImpl = ewkViewGetPageClient(m_viewWidget);
104         EINA_SAFETY_ON_NULL_RETURN_VAL(pageClientImpl, false);
105         pageClientImpl->updateTextSelectionHandlesAndContextMenu(true);
106 #endif
107         return false;
108
109     } else {
110         if (!deltaX && !deltaY)
111             return true;
112
113         PageClientImpl* pageClientImpl = ewkViewGetPageClient(m_viewWidget);
114         EINA_SAFETY_ON_NULL_RETURN_VAL(pageClientImpl, true);
115         ewkViewSendScrollEvent(m_viewWidget, deltaX, deltaY);
116         IntPoint scrollPosition = pageClientImpl->scrollPosition();
117         WKPageScrollBy(toAPI(pageClientImpl->page()), toAPI(IntSize(deltaX, deltaY)));
118         ewkViewSendEdgeEvent(m_viewWidget, scrollPosition, deltaX, deltaY);
119 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
120         pageClientImpl->updateTextSelectionHandlesAndContextMenu(false);
121 #endif
122
123         // Stop flick if we reach the end of contents.
124         int scrollX;
125         int scrollY;
126         int scrollWidth;
127         int scrollHeight;
128         ewk_view_scroll_pos_get(m_viewWidget, &scrollX, &scrollY);
129         ewk_view_scroll_size_get(m_viewWidget, &scrollWidth, &scrollHeight);
130         if ((!deltaX || (deltaX < 0 && scrollX == 0) || (deltaX > 0 && scrollX >= scrollWidth))
131             && (!deltaY || (deltaY < 0 && scrollY == 0) || (deltaY > 0 && scrollY >= scrollHeight))) {
132 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION)
133             // Enable flick in the overflow scroll area even if we reach the end of contents
134             if (pageClientImpl->drawingArea()
135                 && pageClientImpl->drawingArea()->layerTreeCoordinatorProxy()
136                 && pageClientImpl->drawingArea()->layerTreeCoordinatorProxy()->hasOverflowLayer())
137                 return true;
138 #endif
139             m_flickAnimator = 0;
140             return false;
141         }
142
143         return true;
144     }
145 }