OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/page/scrolling/ViewportScrollCallback.h" | 5 #include "core/page/scrolling/ViewportScrollCallback.h" |
6 | 6 |
| 7 #include "core/frame/FrameHost.h" |
| 8 #include "core/frame/FrameView.h" |
| 9 #include "core/frame/RootFrameViewport.h" |
| 10 #include "core/frame/Settings.h" |
| 11 #include "core/frame/TopControls.h" |
| 12 #include "core/page/scrolling/OverscrollController.h" |
7 #include "core/page/scrolling/ScrollState.h" | 13 #include "core/page/scrolling/ScrollState.h" |
| 14 #include "platform/geometry/FloatSize.h" |
8 #include "platform/scroll/ScrollableArea.h" | 15 #include "platform/scroll/ScrollableArea.h" |
9 | 16 |
10 namespace blink { | 17 namespace blink { |
11 | 18 |
12 ScrollResult ViewportScrollCallback::performNativeScroll( | 19 ViewportScrollCallback::ViewportScrollCallback( |
13 ScrollState& state, ScrollableArea& scroller) | 20 TopControls* topControls, |
| 21 OverscrollController* overscrollController, |
| 22 RootFrameViewport& rootFrameViewport) |
| 23 : m_topControls(topControls) |
| 24 , m_overscrollController(overscrollController) |
| 25 , m_rootFrameViewport(&rootFrameViewport) |
14 { | 26 { |
| 27 } |
| 28 |
| 29 ViewportScrollCallback::~ViewportScrollCallback() |
| 30 { |
| 31 } |
| 32 |
| 33 DEFINE_TRACE(ViewportScrollCallback) |
| 34 { |
| 35 visitor->trace(m_topControls); |
| 36 visitor->trace(m_overscrollController); |
| 37 visitor->trace(m_rootFrameViewport); |
| 38 ScrollStateCallback::trace(visitor); |
| 39 } |
| 40 |
| 41 bool ViewportScrollCallback::shouldScrollTopControls(const FloatSize& delta, |
| 42 ScrollGranularity granularity) const |
| 43 { |
| 44 if (granularity != ScrollByPixel && granularity != ScrollByPrecisePixel) |
| 45 return false; |
| 46 |
| 47 if (!m_rootFrameViewport) |
| 48 return false; |
| 49 |
| 50 DoublePoint maxScroll = m_rootFrameViewport->maximumScrollPositionDouble(); |
| 51 DoublePoint scrollPosition = m_rootFrameViewport->scrollPositionDouble(); |
| 52 |
| 53 // Always give the delta to the top controls if the scroll is in |
| 54 // the direction to show the top controls. If it's in the |
| 55 // direction to hide the top controls, only give the delta to the |
| 56 // top controls when the frame can scroll. |
| 57 return delta.height() < 0 || scrollPosition.y() < maxScroll.y(); |
| 58 } |
| 59 |
| 60 bool ViewportScrollCallback::scrollTopControls(ScrollState& state) |
| 61 { |
| 62 // Scroll top controls. |
| 63 if (m_topControls) { |
| 64 if (state.isBeginning()) |
| 65 m_topControls->scrollBegin(); |
| 66 |
| 67 FloatSize delta(state.deltaX(), state.deltaY()); |
| 68 ScrollGranularity granularity = |
| 69 ScrollGranularity(static_cast<int>(state.deltaGranularity())); |
| 70 if (shouldScrollTopControls(delta, granularity)) { |
| 71 FloatSize remainingDelta = m_topControls->scrollBy(delta); |
| 72 FloatSize consumed = delta - remainingDelta; |
| 73 state.consumeDeltaNative(consumed.width(), consumed.height()); |
| 74 return !consumed.isZero(); |
| 75 } |
| 76 } |
| 77 |
| 78 return false; |
| 79 } |
| 80 |
| 81 void ViewportScrollCallback::handleEvent(ScrollState* state) |
| 82 { |
| 83 DCHECK(state); |
| 84 if (!m_rootFrameViewport) |
| 85 return; |
| 86 |
| 87 bool topControlsDidScroll = scrollTopControls(*state); |
| 88 |
| 89 ScrollResult result = performNativeScroll(*state); |
| 90 |
| 91 // We consider top controls movement to be scrolling. |
| 92 result.didScrollY |= topControlsDidScroll; |
| 93 |
| 94 // Handle Overscroll. |
| 95 if (m_overscrollController) { |
| 96 FloatPoint position(state->positionX(), state->positionY()); |
| 97 FloatSize velocity(state->velocityX(), state->velocityY()); |
| 98 m_overscrollController->handleOverscroll(result, position, velocity); |
| 99 } |
| 100 } |
| 101 |
| 102 void ViewportScrollCallback::setScroller(ScrollableArea* scroller) |
| 103 { |
| 104 DCHECK(scroller); |
| 105 m_rootFrameViewport->setLayoutViewport(*scroller); |
| 106 } |
| 107 |
| 108 ScrollResult ViewportScrollCallback::performNativeScroll(ScrollState& state) |
| 109 { |
| 110 DCHECK(m_rootFrameViewport); |
| 111 |
15 FloatSize delta(state.deltaX(), state.deltaY()); | 112 FloatSize delta(state.deltaX(), state.deltaY()); |
16 ScrollGranularity granularity = | 113 ScrollGranularity granularity = |
17 ScrollGranularity(static_cast<int>(state.deltaGranularity())); | 114 ScrollGranularity(static_cast<int>(state.deltaGranularity())); |
18 | 115 |
19 ScrollResult result = scroller.userScroll(granularity, delta); | 116 ScrollResult result = m_rootFrameViewport->userScroll(granularity, delta); |
20 | 117 |
21 // The viewport consumes everything. | 118 // The viewport consumes everything. |
22 // TODO(bokan): This isn't actually consuming everything but doing so breaks | 119 // TODO(bokan): This isn't actually consuming everything but doing so breaks |
23 // the main thread pull-to-refresh action. crbug.com/607210. | 120 // the main thread pull-to-refresh action. crbug.com/607210. |
24 state.consumeDeltaNative( | 121 state.consumeDeltaNative( |
25 delta.width() - result.unusedScrollDeltaX, | 122 delta.width() - result.unusedScrollDeltaX, |
26 delta.height() - result.unusedScrollDeltaY); | 123 delta.height() - result.unusedScrollDeltaY); |
27 | 124 |
28 return result; | 125 return result; |
29 } | 126 } |
30 | 127 |
31 } // namespace blink | 128 } // namespace blink |
OLD | NEW |