ScrollableLayerAndroid.cpp revision 86ba073431c8ddf2e9d1f2d5d4f89157dd32ec33
1#define LOG_TAG "ScrollableLayerAndroid"
2#define LOG_NDEBUG 1
3
4#include "config.h"
5#include "ScrollableLayerAndroid.h"
6
7#include "GLWebViewState.h"
8
9#if USE(ACCELERATED_COMPOSITING)
10
11#include "AndroidLog.h"
12
13namespace WebCore {
14
15bool ScrollableLayerAndroid::scrollTo(int x, int y)
16{
17    IntRect scrollBounds;
18    getScrollBounds(&scrollBounds);
19    if (!scrollBounds.width() && !scrollBounds.height())
20        return false;
21    SkScalar newX = SkScalarPin(x, scrollBounds.x(), scrollBounds.width());
22    SkScalar newY = SkScalarPin(y, scrollBounds.y(), scrollBounds.height());
23    // Check for no change.
24    if (newX == getScrollOffset().x() && newY == getScrollOffset().y())
25        return false;
26    setScrollOffset(IntPoint(newX, newY));
27    return true;
28}
29
30void ScrollableLayerAndroid::getScrollBounds(IntRect* out) const
31{
32    out->setX(m_scrollLimits.fLeft);
33    out->setY(m_scrollLimits.fTop);
34    out->setWidth(m_scrollLimits.width());
35    out->setHeight(m_scrollLimits.height());
36}
37
38void ScrollableLayerAndroid::getScrollRect(SkIRect* out) const
39{
40    out->fLeft = getScrollOffset().x();
41    out->fTop = getScrollOffset().y();
42
43    out->fRight = m_scrollLimits.width();
44    out->fBottom = m_scrollLimits.height();
45}
46
47void ScrollableLayerAndroid::setScrollLimits(float minX, float minY,
48                                             float maxX, float maxY)
49{
50    if (minX < 0) minX = 0;
51    if (minY < 0) minY = 0;
52    if (maxX < 0) maxX = 0;
53    if (maxY < 0) maxY = 0;
54    if (minX > maxX) minX = maxX;
55    if (minY > maxY) minY = maxY;
56    m_scrollLimits.set(minX, minY, minX + maxX, minY + maxY);
57}
58
59bool ScrollableLayerAndroid::scrollRectIntoView(const SkIRect& rect)
60{
61    // Apply the local transform to the rect to get it relative to the parent
62    // layer.
63    SkMatrix localTransform;
64    getLocalTransform(&localTransform);
65    SkRect transformedRect;
66    transformedRect.set(rect);
67    localTransform.mapRect(&transformedRect);
68
69    // Test left before right to prioritize left alignment if transformedRect is wider than
70    // visible area.
71    int x = m_scrollLimits.fLeft;
72    if (transformedRect.fLeft < m_scrollLimits.fLeft)
73        x = transformedRect.fLeft;
74    else if (transformedRect.fRight > m_scrollLimits.fRight)
75        x = transformedRect.fRight - std::max(m_scrollLimits.width(), transformedRect.width());
76
77    // Test top before bottom to prioritize top alignment if transformedRect is taller than
78    // visible area.
79    int y = m_scrollLimits.fTop;
80    if (transformedRect.fTop < m_scrollLimits.fTop)
81        y = transformedRect.fTop;
82    else if (transformedRect.fBottom > m_scrollLimits.fBottom)
83        y = transformedRect.fBottom - std::max(m_scrollLimits.height(), transformedRect.height());
84
85    return scrollTo(x - getPosition().fX, y - getPosition().fY);
86}
87
88} // namespace WebCore
89
90#endif // USE(ACCELERATED_COMPOSITING)
91