ScrollAnimator.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2010, Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "config.h"
  31. #include "ScrollAnimator.h"
  32. #include "FloatPoint.h"
  33. #include "PlatformWheelEvent.h"
  34. #include "ScrollableArea.h"
  35. #include <algorithm>
  36. #include <wtf/PassOwnPtr.h>
  37. using namespace std;
  38. namespace WebCore {
  39. #if !ENABLE(SMOOTH_SCROLLING)
  40. PassOwnPtr<ScrollAnimator> ScrollAnimator::create(ScrollableArea* scrollableArea)
  41. {
  42. return adoptPtr(new ScrollAnimator(scrollableArea));
  43. }
  44. #endif
  45. ScrollAnimator::ScrollAnimator(ScrollableArea* scrollableArea)
  46. : m_scrollableArea(scrollableArea)
  47. , m_currentPosX(0)
  48. , m_currentPosY(0)
  49. {
  50. }
  51. ScrollAnimator::~ScrollAnimator()
  52. {
  53. }
  54. bool ScrollAnimator::scroll(ScrollbarOrientation orientation, ScrollGranularity, float step, float multiplier)
  55. {
  56. float* currentPos = (orientation == HorizontalScrollbar) ? &m_currentPosX : &m_currentPosY;
  57. float newPos = std::max(std::min(*currentPos + (step * multiplier), static_cast<float>(m_scrollableArea->scrollSize(orientation))), 0.0f);
  58. float delta = *currentPos - newPos;
  59. if (*currentPos == newPos)
  60. return false;
  61. *currentPos = newPos;
  62. notifyPositionChanged(orientation == HorizontalScrollbar ? FloatSize(delta, 0) : FloatSize(0, delta));
  63. return true;
  64. }
  65. void ScrollAnimator::scrollToOffsetWithoutAnimation(const FloatPoint& offset)
  66. {
  67. FloatSize delta = FloatSize(offset.x() - m_currentPosX, offset.y() - m_currentPosY);
  68. m_currentPosX = offset.x();
  69. m_currentPosY = offset.y();
  70. notifyPositionChanged(delta);
  71. }
  72. bool ScrollAnimator::handleWheelEvent(const PlatformWheelEvent& e)
  73. {
  74. Scrollbar* horizontalScrollbar = m_scrollableArea->horizontalScrollbar();
  75. Scrollbar* verticalScrollbar = m_scrollableArea->verticalScrollbar();
  76. // Accept the event if we have a scrollbar in that direction and can still
  77. // scroll any further.
  78. float deltaX = horizontalScrollbar ? e.deltaX() : 0;
  79. float deltaY = verticalScrollbar ? e.deltaY() : 0;
  80. bool handled = false;
  81. ScrollGranularity granularity = ScrollByPixel;
  82. IntSize maxForwardScrollDelta = m_scrollableArea->maximumScrollPosition() - m_scrollableArea->scrollPosition();
  83. IntSize maxBackwardScrollDelta = m_scrollableArea->scrollPosition() - m_scrollableArea->minimumScrollPosition();
  84. if ((deltaX < 0 && maxForwardScrollDelta.width() > 0)
  85. || (deltaX > 0 && maxBackwardScrollDelta.width() > 0)
  86. || (deltaY < 0 && maxForwardScrollDelta.height() > 0)
  87. || (deltaY > 0 && maxBackwardScrollDelta.height() > 0)) {
  88. handled = true;
  89. if (deltaY) {
  90. if (e.granularity() == ScrollByPageWheelEvent) {
  91. bool negative = deltaY < 0;
  92. deltaY = max(max(static_cast<float>(m_scrollableArea->visibleHeight()) * Scrollbar::minFractionToStepWhenPaging(), static_cast<float>(m_scrollableArea->visibleHeight() - Scrollbar::maxOverlapBetweenPages())), 1.0f);
  93. if (negative)
  94. deltaY = -deltaY;
  95. }
  96. scroll(VerticalScrollbar, granularity, verticalScrollbar->pixelStep(), -deltaY);
  97. }
  98. if (deltaX) {
  99. if (e.granularity() == ScrollByPageWheelEvent) {
  100. bool negative = deltaX < 0;
  101. deltaX = max(max(static_cast<float>(m_scrollableArea->visibleWidth()) * Scrollbar::minFractionToStepWhenPaging(), static_cast<float>(m_scrollableArea->visibleWidth() - Scrollbar::maxOverlapBetweenPages())), 1.0f);
  102. if (negative)
  103. deltaX = -deltaX;
  104. }
  105. scroll(HorizontalScrollbar, granularity, horizontalScrollbar->pixelStep(), -deltaX);
  106. }
  107. }
  108. return handled;
  109. }
  110. void ScrollAnimator::setCurrentPosition(const FloatPoint& position)
  111. {
  112. m_currentPosX = position.x();
  113. m_currentPosY = position.y();
  114. }
  115. FloatPoint ScrollAnimator::currentPosition() const
  116. {
  117. return FloatPoint(m_currentPosX, m_currentPosY);
  118. }
  119. void ScrollAnimator::notifyPositionChanged(const FloatSize& delta)
  120. {
  121. UNUSED_PARAM(delta);
  122. m_scrollableArea->setScrollOffsetFromAnimation(IntPoint(m_currentPosX, m_currentPosY));
  123. }
  124. } // namespace WebCore