This source file includes following definitions.
- m_currentPosY
- scroll
- scrollToOffsetWithoutAnimation
- handleWheelEvent
- setCurrentPosition
- currentPosition
- notifyPositionChanged
- clampScrollPosition
#include "config.h"
#include "platform/scroll/ScrollAnimator.h"
#include "platform/geometry/FloatPoint.h"
#include "platform/scroll/ScrollableArea.h"
#include "wtf/PassOwnPtr.h"
#include <algorithm>
using namespace std;
namespace WebCore {
ScrollAnimator::ScrollAnimator(ScrollableArea* scrollableArea)
: m_scrollableArea(scrollableArea)
, m_currentPosX(0)
, m_currentPosY(0)
{
}
ScrollAnimator::~ScrollAnimator()
{
}
bool ScrollAnimator::scroll(ScrollbarOrientation orientation, ScrollGranularity, float step, float delta)
{
float& currentPos = (orientation == HorizontalScrollbar) ? m_currentPosX : m_currentPosY;
float newPos = clampScrollPosition(orientation, currentPos + step * delta);
if (currentPos == newPos)
return false;
currentPos = newPos;
notifyPositionChanged();
return true;
}
void ScrollAnimator::scrollToOffsetWithoutAnimation(const FloatPoint& offset)
{
m_currentPosX = offset.x();
m_currentPosY = offset.y();
notifyPositionChanged();
}
bool ScrollAnimator::handleWheelEvent(const PlatformWheelEvent& e)
{
bool canScrollX = m_scrollableArea->userInputScrollable(HorizontalScrollbar);
bool canScrollY = m_scrollableArea->userInputScrollable(VerticalScrollbar);
float deltaX = canScrollX ? e.deltaX() : 0;
float deltaY = canScrollY ? e.deltaY() : 0;
bool handled = false;
#if !OS(MACOSX)
ScrollGranularity granularity = e.hasPreciseScrollingDeltas() ? ScrollByPrecisePixel : ScrollByPixel;
#else
ScrollGranularity granularity = ScrollByPixel;
#endif
IntSize maxForwardScrollDelta = m_scrollableArea->maximumScrollPosition() - m_scrollableArea->scrollPosition();
IntSize maxBackwardScrollDelta = m_scrollableArea->scrollPosition() - m_scrollableArea->minimumScrollPosition();
if ((deltaX < 0 && maxForwardScrollDelta.width() > 0)
|| (deltaX > 0 && maxBackwardScrollDelta.width() > 0)
|| (deltaY < 0 && maxForwardScrollDelta.height() > 0)
|| (deltaY > 0 && maxBackwardScrollDelta.height() > 0)) {
handled = true;
if (deltaY) {
if (e.granularity() == ScrollByPageWheelEvent) {
bool negative = deltaY < 0;
deltaY = m_scrollableArea->pageStep(VerticalScrollbar);
if (negative)
deltaY = -deltaY;
}
scroll(VerticalScrollbar, granularity, m_scrollableArea->pixelStep(VerticalScrollbar), -deltaY);
}
if (deltaX) {
if (e.granularity() == ScrollByPageWheelEvent) {
bool negative = deltaX < 0;
deltaX = m_scrollableArea->pageStep(HorizontalScrollbar);
if (negative)
deltaX = -deltaX;
}
scroll(HorizontalScrollbar, granularity, m_scrollableArea->pixelStep(HorizontalScrollbar), -deltaX);
}
}
return handled;
}
void ScrollAnimator::setCurrentPosition(const FloatPoint& position)
{
m_currentPosX = position.x();
m_currentPosY = position.y();
}
FloatPoint ScrollAnimator::currentPosition() const
{
return FloatPoint(m_currentPosX, m_currentPosY);
}
void ScrollAnimator::notifyPositionChanged()
{
m_scrollableArea->setScrollOffsetFromAnimation(IntPoint(m_currentPosX, m_currentPosY));
}
float ScrollAnimator::clampScrollPosition(ScrollbarOrientation orientation, float pos)
{
float maxScrollPos = m_scrollableArea->maximumScrollPosition(orientation);
float minScrollPos = m_scrollableArea->minimumScrollPosition(orientation);
return std::max(std::min(pos, maxScrollPos), minScrollPos);
}
}