Home | History | Annotate | Download | only in android
      1 #include "config.h"
      2 #include "ScrollableLayerAndroid.h"
      3 
      4 #if USE(ACCELERATED_COMPOSITING)
      5 
      6 namespace WebCore {
      7 
      8 bool ScrollableLayerAndroid::scrollTo(int x, int y)
      9 {
     10     SkIRect scrollBounds;
     11     getScrollRect(&scrollBounds);
     12     if (!scrollBounds.fRight && !scrollBounds.fBottom)
     13         return false;
     14 
     15     SkScalar newX = SkScalarPin(x, 0, scrollBounds.fRight);
     16     SkScalar newY = SkScalarPin(y, 0, scrollBounds.fBottom);
     17     // Check for no change.
     18     if (newX == scrollBounds.fLeft && newY == scrollBounds.fTop)
     19         return false;
     20 
     21     setPosition(m_scrollLimits.fLeft - newX, m_scrollLimits.fTop - newY);
     22     return true;
     23 }
     24 
     25 void ScrollableLayerAndroid::getScrollRect(SkIRect* out) const
     26 {
     27     const SkPoint& pos = getPosition();
     28     out->fLeft = m_scrollLimits.fLeft - pos.fX;
     29     out->fTop = m_scrollLimits.fTop - pos.fY;
     30     out->fRight = getSize().width() - m_scrollLimits.width();
     31     out->fBottom = getSize().height() - m_scrollLimits.height();
     32 }
     33 
     34 bool ScrollableLayerAndroid::scrollRectIntoView(const SkIRect& rect)
     35 {
     36     // Apply the local transform to the rect to get it relative to the parent
     37     // layer.
     38     SkMatrix localTransform;
     39     getLocalTransform(&localTransform);
     40     SkRect transformedRect;
     41     transformedRect.set(rect);
     42     localTransform.mapRect(&transformedRect);
     43 
     44     // Test left before right to prioritize left alignment if transformedRect is wider than
     45     // visible area.
     46     int x = m_scrollLimits.fLeft;
     47     if (transformedRect.fLeft < m_scrollLimits.fLeft)
     48         x = transformedRect.fLeft;
     49     else if (transformedRect.fRight > m_scrollLimits.fRight)
     50         x = transformedRect.fRight - std::max(m_scrollLimits.width(), transformedRect.width());
     51 
     52     // Test top before bottom to prioritize top alignment if transformedRect is taller than
     53     // visible area.
     54     int y = m_scrollLimits.fTop;
     55     if (transformedRect.fTop < m_scrollLimits.fTop)
     56         y = transformedRect.fTop;
     57     else if (transformedRect.fBottom > m_scrollLimits.fBottom)
     58         y = transformedRect.fBottom - std::max(m_scrollLimits.height(), transformedRect.height());
     59 
     60     return scrollTo(x - getPosition().fX, y - getPosition().fY);
     61 }
     62 
     63 } // namespace WebCore
     64 
     65 #endif // USE(ACCELERATED_COMPOSITING)
     66