Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / core / frame / TopControls.cpp
blob8f44999fb40f14b743a4294c3dbe044915b72351
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.
5 #include "config.h"
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
13 namespace blink {
15 TopControls::TopControls(const FrameHost& frameHost)
16 : m_frameHost(&frameHost)
17 , m_height(0)
18 , m_shownRatio(0)
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()
37 resetBaseline();
40 FloatSize TopControls::scrollBy(FloatSize pendingDelta)
42 if ((m_permittedState == WebTopControlsShown && pendingDelta.height() < 0) || (m_permittedState == WebTopControlsHidden && pendingDelta.height() > 0))
43 return pendingDelta;
45 if (m_height == 0)
46 return pendingDelta;
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)
61 resetBaseline();
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)
94 return;
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)
108 return;
110 m_height = height;
111 m_shrinkViewport = shrinkViewport;
112 m_frameHost->chromeClient().didUpdateTopControls();
115 } // namespace blink