OLD | NEW |
1 // Copyright 2015 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/frame/TopControls.h" | 5 #include "core/frame/BrowserControls.h" |
6 | 6 |
7 #include "core/frame/FrameHost.h" | 7 #include "core/frame/FrameHost.h" |
8 #include "core/frame/VisualViewport.h" | 8 #include "core/frame/VisualViewport.h" |
9 #include "core/page/ChromeClient.h" | 9 #include "core/page/ChromeClient.h" |
10 #include "platform/geometry/FloatSize.h" | 10 #include "platform/geometry/FloatSize.h" |
11 #include <algorithm> // for std::min and std::max | 11 #include <algorithm> // for std::min and std::max |
12 | 12 |
13 namespace blink { | 13 namespace blink { |
14 | 14 |
15 TopControls::TopControls(const FrameHost& frameHost) | 15 BrowserControls::BrowserControls(const FrameHost& frameHost) |
16 : m_frameHost(&frameHost), | 16 : m_frameHost(&frameHost), |
17 m_height(0), | 17 m_height(0), |
18 m_shownRatio(0), | 18 m_shownRatio(0), |
19 m_baselineContentOffset(0), | 19 m_baselineContentOffset(0), |
20 m_accumulatedScrollDelta(0), | 20 m_accumulatedScrollDelta(0), |
21 m_shrinkViewport(false), | 21 m_shrinkViewport(false), |
22 m_permittedState(WebTopControlsBoth) {} | 22 m_permittedState(WebBrowserControlsBoth) {} |
23 | 23 |
24 DEFINE_TRACE(TopControls) { | 24 DEFINE_TRACE(BrowserControls) { |
25 visitor->trace(m_frameHost); | 25 visitor->trace(m_frameHost); |
26 } | 26 } |
27 | 27 |
28 void TopControls::scrollBegin() { | 28 void BrowserControls::scrollBegin() { |
29 resetBaseline(); | 29 resetBaseline(); |
30 } | 30 } |
31 | 31 |
32 FloatSize TopControls::scrollBy(FloatSize pendingDelta) { | 32 FloatSize BrowserControls::scrollBy(FloatSize pendingDelta) { |
33 if ((m_permittedState == WebTopControlsShown && pendingDelta.height() > 0) || | 33 if ((m_permittedState == WebBrowserControlsShown && |
34 (m_permittedState == WebTopControlsHidden && pendingDelta.height() < 0)) | 34 pendingDelta.height() > 0) || |
| 35 (m_permittedState == WebBrowserControlsHidden && |
| 36 pendingDelta.height() < 0)) |
35 return pendingDelta; | 37 return pendingDelta; |
36 | 38 |
37 if (m_height == 0) | 39 if (m_height == 0) |
38 return pendingDelta; | 40 return pendingDelta; |
39 | 41 |
40 float oldOffset = contentOffset(); | 42 float oldOffset = contentOffset(); |
41 float pageScale = m_frameHost->visualViewport().scale(); | 43 float pageScale = m_frameHost->visualViewport().scale(); |
42 | 44 |
43 // Update accumulated vertical scroll and apply it to top controls | 45 // Update accumulated vertical scroll and apply it to browser controls |
44 // Compute scroll delta in viewport space by applying page scale | 46 // Compute scroll delta in viewport space by applying page scale |
45 m_accumulatedScrollDelta += pendingDelta.height() * pageScale; | 47 m_accumulatedScrollDelta += pendingDelta.height() * pageScale; |
46 | 48 |
47 float newContentOffset = m_baselineContentOffset - m_accumulatedScrollDelta; | 49 float newContentOffset = m_baselineContentOffset - m_accumulatedScrollDelta; |
48 | 50 |
49 setShownRatio(newContentOffset / m_height); | 51 setShownRatio(newContentOffset / m_height); |
50 | 52 |
51 // Reset baseline when controls are fully visible | 53 // Reset baseline when controls are fully visible |
52 if (m_shownRatio == 1) | 54 if (m_shownRatio == 1) |
53 resetBaseline(); | 55 resetBaseline(); |
54 | 56 |
55 // Clamp and use the expected content offset so that we don't return | 57 // Clamp and use the expected content offset so that we don't return |
56 // spurrious remaining scrolls due to the imprecision of the shownRatio. | 58 // spurrious remaining scrolls due to the imprecision of the shownRatio. |
57 newContentOffset = std::min(newContentOffset, m_height); | 59 newContentOffset = std::min(newContentOffset, m_height); |
58 newContentOffset = std::max(newContentOffset, 0.f); | 60 newContentOffset = std::max(newContentOffset, 0.f); |
59 | 61 |
60 // We negate the difference because scrolling down (positive delta) causes | 62 // We negate the difference because scrolling down (positive delta) causes |
61 // top controls to hide (negative offset difference). | 63 // browser controls to hide (negative offset difference). |
62 FloatSize appliedDelta(0, (oldOffset - newContentOffset) / pageScale); | 64 FloatSize appliedDelta(0, (oldOffset - newContentOffset) / pageScale); |
63 return pendingDelta - appliedDelta; | 65 return pendingDelta - appliedDelta; |
64 } | 66 } |
65 | 67 |
66 void TopControls::resetBaseline() { | 68 void BrowserControls::resetBaseline() { |
67 m_accumulatedScrollDelta = 0; | 69 m_accumulatedScrollDelta = 0; |
68 m_baselineContentOffset = contentOffset(); | 70 m_baselineContentOffset = contentOffset(); |
69 } | 71 } |
70 | 72 |
71 float TopControls::layoutHeight() { | 73 float BrowserControls::layoutHeight() { |
72 return m_shrinkViewport ? m_height : 0; | 74 return m_shrinkViewport ? m_height : 0; |
73 } | 75 } |
74 | 76 |
75 float TopControls::contentOffset() { | 77 float BrowserControls::contentOffset() { |
76 return m_shownRatio * m_height; | 78 return m_shownRatio * m_height; |
77 } | 79 } |
78 | 80 |
79 void TopControls::setShownRatio(float shownRatio) { | 81 void BrowserControls::setShownRatio(float shownRatio) { |
80 shownRatio = std::min(shownRatio, 1.f); | 82 shownRatio = std::min(shownRatio, 1.f); |
81 shownRatio = std::max(shownRatio, 0.f); | 83 shownRatio = std::max(shownRatio, 0.f); |
82 | 84 |
83 if (m_shownRatio == shownRatio) | 85 if (m_shownRatio == shownRatio) |
84 return; | 86 return; |
85 | 87 |
86 m_shownRatio = shownRatio; | 88 m_shownRatio = shownRatio; |
87 m_frameHost->chromeClient().didUpdateTopControls(); | 89 m_frameHost->chromeClient().didUpdateBrowserControls(); |
88 } | 90 } |
89 | 91 |
90 void TopControls::updateConstraintsAndState(WebTopControlsState constraints, | 92 void BrowserControls::updateConstraintsAndState( |
91 WebTopControlsState current, | 93 WebBrowserControlsState constraints, |
92 bool animate) { | 94 WebBrowserControlsState current, |
| 95 bool animate) { |
93 m_permittedState = constraints; | 96 m_permittedState = constraints; |
94 | 97 |
95 DCHECK( | 98 DCHECK(!(constraints == WebBrowserControlsShown && |
96 !(constraints == WebTopControlsShown && current == WebTopControlsHidden)); | 99 current == WebBrowserControlsHidden)); |
97 DCHECK( | 100 DCHECK(!(constraints == WebBrowserControlsHidden && |
98 !(constraints == WebTopControlsHidden && current == WebTopControlsShown)); | 101 current == WebBrowserControlsShown)); |
99 | 102 |
100 // If the change should be animated, let the impl thread drive the change. | 103 // If the change should be animated, let the impl thread drive the change. |
101 // Otherwise, immediately set the shown ratio so we don't have to wait for | 104 // Otherwise, immediately set the shown ratio so we don't have to wait for |
102 // a commit from the impl thread. | 105 // a commit from the impl thread. |
103 if (animate) | 106 if (animate) |
104 return; | 107 return; |
105 | 108 |
106 if (constraints == WebTopControlsBoth && current == WebTopControlsBoth) | 109 if (constraints == WebBrowserControlsBoth && |
| 110 current == WebBrowserControlsBoth) |
107 return; | 111 return; |
108 | 112 |
109 if (constraints == WebTopControlsHidden || current == WebTopControlsHidden) | 113 if (constraints == WebBrowserControlsHidden || |
| 114 current == WebBrowserControlsHidden) |
110 setShownRatio(0.f); | 115 setShownRatio(0.f); |
111 else | 116 else |
112 setShownRatio(1.f); | 117 setShownRatio(1.f); |
113 } | 118 } |
114 | 119 |
115 void TopControls::setHeight(float height, bool shrinkViewport) { | 120 void BrowserControls::setHeight(float height, bool shrinkViewport) { |
116 if (m_height == height && m_shrinkViewport == shrinkViewport) | 121 if (m_height == height && m_shrinkViewport == shrinkViewport) |
117 return; | 122 return; |
118 | 123 |
119 m_height = height; | 124 m_height = height; |
120 m_shrinkViewport = shrinkViewport; | 125 m_shrinkViewport = shrinkViewport; |
121 m_frameHost->chromeClient().didUpdateTopControls(); | 126 m_frameHost->chromeClient().didUpdateBrowserControls(); |
122 } | 127 } |
123 | 128 |
124 } // namespace blink | 129 } // namespace blink |
OLD | NEW |