OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "core/page/scrolling/ViewportScrollCallback.h" |
| 6 |
| 7 #include "core/dom/Document.h" |
| 8 #include "core/frame/FrameHost.h" |
| 9 #include "core/frame/FrameView.h" |
| 10 #include "core/frame/Settings.h" |
| 11 #include "core/frame/TopControls.h" |
| 12 #include "core/frame/VisualViewport.h" |
| 13 #include "core/input/EventHandler.h" |
| 14 #include "core/layout/LayoutView.h" |
| 15 #include "core/page/scrolling/ScrollState.h" |
| 16 #include "platform/geometry/FloatSize.h" |
| 17 #include "platform/scroll/ScrollableArea.h" |
| 18 |
| 19 namespace blink { |
| 20 |
| 21 ViewportScrollCallback::ViewportScrollCallback(Document& document, |
| 22 FrameHost& frameHost) |
| 23 : m_document(&document) |
| 24 , m_frameHost(&frameHost) |
| 25 { |
| 26 // Only the root document can have a viewport scroll callback for now. |
| 27 ASSERT(!document.ownerElement()); |
| 28 } |
| 29 |
| 30 ViewportScrollCallback::~ViewportScrollCallback() |
| 31 { |
| 32 } |
| 33 |
| 34 DEFINE_TRACE(ViewportScrollCallback) |
| 35 { |
| 36 visitor->trace(m_frameHost); |
| 37 visitor->trace(m_document); |
| 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 ScrollableArea* rootFrameViewport = getRootFrameViewport(); |
| 48 if (!rootFrameViewport) |
| 49 return false; |
| 50 |
| 51 DoublePoint maxScroll = rootFrameViewport->maximumScrollPositionDouble(); |
| 52 DoublePoint scrollPosition = rootFrameViewport->scrollPositionDouble(); |
| 53 |
| 54 // Always give the delta to the top controls if the scroll is in |
| 55 // the direction to show the top controls. If it's in the |
| 56 // direction to hide the top controls, only give the delta to the |
| 57 // top controls when the frame can scroll. |
| 58 return delta.height() < 0 || scrollPosition.y() < maxScroll.y(); |
| 59 } |
| 60 |
| 61 void ViewportScrollCallback::handleEvent(ScrollState* state) |
| 62 { |
| 63 if (!m_frameHost || !m_document) |
| 64 return; |
| 65 |
| 66 TopControls& topControls = m_frameHost->topControls(); |
| 67 |
| 68 // Scroll top controls. |
| 69 if (state->isBeginning()) |
| 70 topControls.scrollBegin(); |
| 71 |
| 72 FloatSize delta(state->deltaX(), state->deltaY()); |
| 73 ScrollGranularity granularity = |
| 74 ScrollGranularity(static_cast<int>(state->deltaGranularity())); |
| 75 FloatSize remainingDelta = delta; |
| 76 |
| 77 if (shouldScrollTopControls(delta, granularity)) |
| 78 remainingDelta = topControls.scrollBy(delta); |
| 79 |
| 80 bool topControlsConsumedScroll = remainingDelta.height() != delta.height(); |
| 81 |
| 82 // Do the native scroll. |
| 83 ScrollableArea* rootFrameViewport = getRootFrameViewport(); |
| 84 if (!rootFrameViewport) |
| 85 return; |
| 86 |
| 87 ScrollResult result = |
| 88 rootFrameViewport->userScroll(granularity, remainingDelta); |
| 89 |
| 90 // We consider top controls movement to be scrolling. |
| 91 result.didScrollY |= topControlsConsumedScroll; |
| 92 |
| 93 // Handle Overscroll. |
| 94 FloatPoint position(state->positionX(), state->positionY()); |
| 95 FloatSize velocity(state->velocityX(), state->velocityY()); |
| 96 |
| 97 m_document->frame()->eventHandler().handleOverscroll( |
| 98 result, position, velocity); |
| 99 |
| 100 // The viewport consumes everything. |
| 101 state->consumeDeltaNative( |
| 102 state->deltaX() - result.unusedScrollDeltaX, |
| 103 state->deltaY() - result.unusedScrollDeltaY); |
| 104 } |
| 105 |
| 106 ScrollableArea* ViewportScrollCallback::getRootFrameViewport() const |
| 107 { |
| 108 if (!m_document->layoutView()) |
| 109 return nullptr; |
| 110 |
| 111 FrameView* frameView = m_document->layoutView()->frameView(); |
| 112 if (!frameView) |
| 113 return nullptr; |
| 114 |
| 115 ScrollableArea* rootFrameViewport = frameView->getScrollableArea(); |
| 116 ASSERT(rootFrameViewport); |
| 117 |
| 118 return rootFrameViewport; |
| 119 } |
| 120 |
| 121 } // namespace blink |
OLD | NEW |