summaryrefslogtreecommitdiffstats
path: root/Source/WebKit2/UIProcess/qt/QtViewportInteractionEngine.h
blob: b7595e56975213ea9a4a6e5324d98a5e983f1ae9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
 * Copyright (C) 2011 Benjamin Poulain <benjamin@webkit.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this program; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#ifndef QtViewportInteractionEngine_h
#define QtViewportInteractionEngine_h

#include <QtCore/QObject>
#include <QtCore/QRectF>
#include <QtCore/QVariant>
#include <QtCore/QVariantAnimation>
#include <WebCore/ViewportArguments.h>
#include <wtf/OwnPtr.h>

QT_BEGIN_NAMESPACE
class QPointF;
class QQuickWebPage;
class QQuickWebView;
class QWheelEvent;
QT_END_NAMESPACE

class QWebKitTest;

namespace WebKit {

class ViewportUpdateDeferrer;

class QtViewportInteractionEngine : public QObject {
    Q_OBJECT

public:
    QtViewportInteractionEngine(QQuickWebView*, QQuickWebPage*);
    ~QtViewportInteractionEngine();

    void reset();

    bool hadUserInteraction() const { return m_hadUserInteraction; }

    void setCSSScaleBounds(qreal minimum, qreal maximum);
    void setCSSScale(qreal);

    qreal currentCSSScale() const;

    void setAllowsUserScaling(bool allow) { m_allowsUserScaling = allow; }
    void setDevicePixelRatio(qreal ratio) { m_devicePixelRatio = ratio; }

    void setPageItemRectVisible(const QRectF&);
    void animatePageItemRectVisible(const QRectF&);

    QRectF nearestValidBounds() const;

    void pageContentPositionRequest(const QPoint& pos);

    void touchBegin();
    void touchEnd();

    bool scrollAnimationActive() const;
    void cancelScrollAnimation();

    bool panGestureActive() const;
    void panGestureStarted(const QPointF& position, qint64 eventTimestampMillis);
    void panGestureRequestUpdate(const QPointF& position, qint64 eventTimestampMillis);
    void panGestureEnded(const QPointF& position, qint64 eventTimestampMillis);
    void panGestureCancelled();

    bool scaleAnimationActive() const;
    void interruptScaleAnimation();

    bool pinchGestureActive() const;
    void pinchGestureStarted(const QPointF& pinchCenterInViewportCoordinates);
    void pinchGestureRequestUpdate(const QPointF& pinchCenterInViewportCoordinates, qreal totalScaleFactor);
    void pinchGestureEnded();
    void pinchGestureCancelled();

    void zoomToAreaGestureEnded(const QPointF& touchPoint, const QRectF& targetArea);
    void focusEditableArea(const QRectF& caretArea, const QRectF& targetArea);

    void viewportAttributesChanged(const WebCore::ViewportAttributes&);
    void pageContentsSizeChanged(const QSize& newSize, const QSize& viewportSize);

Q_SIGNALS:
    void contentSuspendRequested();
    void contentResumeRequested();

    void informVisibleContentChange(const QPointF& trajectory = QPointF());

private Q_SLOTS:
    // Respond to changes of content that are not driven by us, like the page resizing itself.
    void pageItemSizeChanged();
    void pageItemPositionChanged();

    void scaleAnimationStateChanged(QAbstractAnimation::State, QAbstractAnimation::State);
    void scaleAnimationValueChanged(QVariant value);

    void flickMoveStarted(); // Called when panning starts.
    void flickMoveEnded(); //   Called when panning (+ kinetic animation) ends.

private:
    friend class ViewportUpdateDeferrer;
    friend class ::QWebKitTest;

    QQuickWebView* const m_viewportItem;
    QQuickWebPage* const m_pageItem;

    qreal cssScaleFromItem(qreal) const;
    qreal itemScaleFromCSS(qreal) const;
    qreal itemCoordFromCSS(qreal) const;
    QRectF itemRectFromCSS(const QRectF&) const;

    qreal innerBoundedCSSScale(qreal) const;
    qreal outerBoundedCSSScale(qreal) const;

    QRectF computePosRangeForPageItemAtScale(qreal itemScale) const;
    void scaleContent(const QPointF& centerInCSSCoordinates, qreal cssScale);

    WebCore::ViewportAttributes m_rawAttributes;

    bool m_allowsUserScaling;
    qreal m_minimumScale;
    qreal m_maximumScale;
    qreal m_devicePixelRatio;

    QSize m_layoutSize;

    int m_suspendCount;
    bool m_hasSuspendedContent;

    OwnPtr<ViewportUpdateDeferrer> m_scaleUpdateDeferrer;
    OwnPtr<ViewportUpdateDeferrer> m_scrollUpdateDeferrer;
    OwnPtr<ViewportUpdateDeferrer> m_touchUpdateDeferrer;
    OwnPtr<ViewportUpdateDeferrer> m_animationUpdateDeferrer;

    bool m_hadUserInteraction;

    class ScaleAnimation : public QVariantAnimation {
    public:
        ScaleAnimation(QObject* parent = 0)
            : QVariantAnimation(parent)
        { }

        virtual void updateCurrentValue(const QVariant&) { }
    };

    struct ScaleStackItem {
        ScaleStackItem(qreal scale, qreal xPosition)
            : scale(scale)
            , xPosition(xPosition)
        { }

        qreal scale;
        qreal xPosition;
    };

    ScaleAnimation* m_scaleAnimation;
    QPointF m_lastPinchCenterInViewportCoordinates;
    QPointF m_lastScrollPosition;
    qreal m_pinchStartScale;
    qreal m_zoomOutScale;
    QList<ScaleStackItem> m_scaleStack;
};

} // namespace WebKit

#endif // QtViewportInteractionEngine_h