| OLD | NEW |
| (Empty) | |
| 1 /* |
| 2 * Copyright (C) 2015 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 |
| 6 * are met: |
| 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright |
| 11 * notice, this list of conditions and the following disclaimer in the |
| 12 * documentation and/or other materials provided with the distribution. |
| 13 * |
| 14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 */ |
| 25 |
| 26 #include "config.h" |
| 27 #include "TopControls.h" |
| 28 |
| 29 #include "platform/PlatformGestureEvent.h" |
| 30 #include "web/WebViewImpl.h" |
| 31 #include <algorithm> // for std::min and std::max |
| 32 |
| 33 namespace blink { |
| 34 |
| 35 TopControls::TopControls(WebViewImpl* webView) |
| 36 : m_webView(webView) |
| 37 , m_height(0) |
| 38 , m_shownRatio(0) |
| 39 , m_baselineContentOffset(0) |
| 40 , m_accumulatedScrollDelta(0) |
| 41 , m_shrinkViewport(false) |
| 42 , m_permittedState(WebTopControlsBoth) |
| 43 , m_pinchGestureActive(false) |
| 44 { |
| 45 } |
| 46 |
| 47 bool TopControls::handleGestureEvent(const PlatformGestureEvent& event, float* r
emainingDeltaY) |
| 48 { |
| 49 switch (event.type()) { |
| 50 case PlatformEvent::GestureScrollBegin: |
| 51 scrollBegin(); |
| 52 break; |
| 53 case PlatformEvent::GestureScrollUpdate: |
| 54 if (m_webView->shouldTopControlsConsumeScroll(event.deltaY())) { |
| 55 *remainingDeltaY = scrollBy(event.deltaY()); |
| 56 } else { |
| 57 *remainingDeltaY = event.deltaY(); |
| 58 } |
| 59 return *remainingDeltaY != event.deltaY(); |
| 60 case PlatformEvent::GesturePinchBegin: |
| 61 m_pinchGestureActive = true; |
| 62 break; |
| 63 case PlatformEvent::GesturePinchEnd: |
| 64 m_pinchGestureActive = false; |
| 65 break; |
| 66 default: |
| 67 break; |
| 68 } |
| 69 |
| 70 return false; |
| 71 } |
| 72 |
| 73 void TopControls::scrollBegin() |
| 74 { |
| 75 resetBaseline(); |
| 76 } |
| 77 |
| 78 float TopControls::scrollBy(float pendingDelta) |
| 79 { |
| 80 if (m_pinchGestureActive) |
| 81 return pendingDelta; |
| 82 |
| 83 if ((m_permittedState == WebTopControlsShown && pendingDelta < 0) || (m_perm
ittedState == WebTopControlsHidden && pendingDelta > 0)) |
| 84 return pendingDelta; |
| 85 |
| 86 if (m_height == 0.f) |
| 87 return pendingDelta; |
| 88 |
| 89 float oldOffset = contentOffset(); |
| 90 |
| 91 // Update accumulated vertical scroll and apply it to top controls |
| 92 m_accumulatedScrollDelta += pendingDelta; |
| 93 setShownRatio((m_baselineContentOffset + m_accumulatedScrollDelta) / m_heigh
t); |
| 94 |
| 95 // Reset baseline when controls are fully visible |
| 96 if (m_shownRatio == 1.f) |
| 97 resetBaseline(); |
| 98 |
| 99 float appliedDelta = contentOffset() - oldOffset; |
| 100 return pendingDelta - appliedDelta; |
| 101 } |
| 102 |
| 103 void TopControls::resetBaseline() |
| 104 { |
| 105 m_accumulatedScrollDelta = 0.f; |
| 106 m_baselineContentOffset = contentOffset(); |
| 107 } |
| 108 |
| 109 float TopControls::layoutHeight() |
| 110 { |
| 111 return m_shrinkViewport ? m_height : 0.f; |
| 112 } |
| 113 |
| 114 float TopControls::contentOffset() |
| 115 { |
| 116 return m_shownRatio * m_height; |
| 117 } |
| 118 |
| 119 void TopControls::setShownRatio(float offset) |
| 120 { |
| 121 // Clamp |
| 122 offset = std::min(offset, 1.f); |
| 123 offset = std::max(offset, 0.f); |
| 124 |
| 125 if (m_shownRatio == offset) |
| 126 return; |
| 127 |
| 128 m_shownRatio = offset; |
| 129 m_webView->didUpdateTopControls(); |
| 130 } |
| 131 |
| 132 void TopControls::updateShownRatio(float delta) |
| 133 { |
| 134 setShownRatio(m_shownRatio + delta); |
| 135 } |
| 136 |
| 137 void TopControls::updateState( |
| 138 WebTopControlsState constraints, |
| 139 WebTopControlsState current, |
| 140 bool animate) |
| 141 { |
| 142 // CC handles current state and animate so they are ignored here. |
| 143 m_permittedState = constraints; |
| 144 } |
| 145 |
| 146 void TopControls::setHeight(float height) |
| 147 { |
| 148 if (m_height == height) |
| 149 return; |
| 150 |
| 151 m_height = height; |
| 152 m_webView->didUpdateTopControls(); |
| 153 } |
| 154 |
| 155 void TopControls::setShrinkViewport(bool shrinkViewport) |
| 156 { |
| 157 if (m_shrinkViewport == shrinkViewport) |
| 158 return; |
| 159 |
| 160 m_shrinkViewport = shrinkViewport; |
| 161 m_webView->didUpdateTopControls(); |
| 162 } |
| 163 } // namespace blink |
| OLD | NEW |