1 // Copyright 2015 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.
6 #include "TopControls.h"
8 #include "core/frame/FrameHost.h"
9 #include "core/page/ChromeClient.h"
10 #include "platform/geometry/FloatSize.h"
11 #include <algorithm> // for std::min and std::max
15 TopControls::TopControls(const FrameHost
& frameHost
)
16 : m_frameHost(&frameHost
)
19 , m_baselineContentOffset(0)
20 , m_accumulatedScrollDelta(0)
21 , m_shrinkViewport(false)
22 , m_permittedState(WebTopControlsBoth
)
26 TopControls::~TopControls()
30 DEFINE_TRACE(TopControls
)
32 visitor
->trace(m_frameHost
);
35 void TopControls::scrollBegin()
40 FloatSize
TopControls::scrollBy(FloatSize pendingDelta
)
42 if ((m_permittedState
== WebTopControlsShown
&& pendingDelta
.height() < 0) || (m_permittedState
== WebTopControlsHidden
&& pendingDelta
.height() > 0))
48 float oldOffset
= contentOffset();
49 float pageScale
= m_frameHost
->visualViewport().scale();
51 // Update accumulated vertical scroll and apply it to top controls
52 // Compute scroll delta in viewport space by applying page scale
53 m_accumulatedScrollDelta
+= pendingDelta
.height() * pageScale
;
55 float newContentOffset
= m_baselineContentOffset
+ m_accumulatedScrollDelta
;
57 setShownRatio(newContentOffset
/ m_height
);
59 // Reset baseline when controls are fully visible
60 if (m_shownRatio
== 1)
63 // Clamp and use the expected content offset so that we don't return
64 // spurrious remaining scrolls due to the imprecision of the shownRatio.
65 newContentOffset
= std::min(newContentOffset
, m_height
);
66 newContentOffset
= std::max(newContentOffset
, 0.f
);
68 FloatSize
appliedDelta(0, (newContentOffset
- oldOffset
) / pageScale
);
69 return pendingDelta
- appliedDelta
;
72 void TopControls::resetBaseline()
74 m_accumulatedScrollDelta
= 0;
75 m_baselineContentOffset
= contentOffset();
78 float TopControls::layoutHeight()
80 return m_shrinkViewport
? m_height
: 0;
83 float TopControls::contentOffset()
85 return m_shownRatio
* m_height
;
88 void TopControls::setShownRatio(float shownRatio
)
90 shownRatio
= std::min(shownRatio
, 1.f
);
91 shownRatio
= std::max(shownRatio
, 0.f
);
93 if (m_shownRatio
== shownRatio
)
96 m_shownRatio
= shownRatio
;
97 m_frameHost
->chromeClient().didUpdateTopControls();
100 void TopControls::updateConstraints(WebTopControlsState constraints
)
102 m_permittedState
= constraints
;
105 void TopControls::setHeight(float height
, bool shrinkViewport
)
107 if (m_height
== height
&& m_shrinkViewport
== shrinkViewport
)
111 m_shrinkViewport
= shrinkViewport
;
112 m_frameHost
->chromeClient().didUpdateTopControls();