Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Unified Diff: Source/web/TopControls.cpp

Issue 893683003: Implement top controls show/hide functionality for main thread scrolling (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Fix hiding at the page bottom issue Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/web/TopControls.cpp
diff --git a/Source/web/TopControls.cpp b/Source/web/TopControls.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..3e8af70af41497a3370615526e2d35a89b073969
--- /dev/null
+++ b/Source/web/TopControls.cpp
@@ -0,0 +1,163 @@
+/*
+ * Copyright (C) 2015 Google Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "TopControls.h"
+
+#include "platform/PlatformGestureEvent.h"
+#include "web/WebViewImpl.h"
+#include <algorithm> // for std::min and std::max
+
+namespace blink {
+
+TopControls::TopControls(WebViewImpl* webView)
+ : m_webView(webView)
+ , m_height(0)
+ , m_shownRatio(0)
+ , m_baselineContentOffset(0)
+ , m_accumulatedScrollDelta(0)
+ , m_shrinkViewport(false)
+ , m_permittedState(WebTopControlsBoth)
+ , m_pinchGestureActive(false)
+{
+}
+
+bool TopControls::handleGestureEvent(const PlatformGestureEvent& event, float* remainingDeltaY)
+{
+ switch (event.type()) {
+ case PlatformEvent::GestureScrollBegin:
+ scrollBegin();
+ break;
+ case PlatformEvent::GestureScrollUpdate:
+ if (m_webView->shouldTopControlsConsumeScroll(event.deltaY())) {
+ *remainingDeltaY = scrollBy(event.deltaY());
+ } else {
+ *remainingDeltaY = event.deltaY();
+ }
+ return *remainingDeltaY != event.deltaY();
+ case PlatformEvent::GesturePinchBegin:
+ m_pinchGestureActive = true;
+ break;
+ case PlatformEvent::GesturePinchEnd:
+ m_pinchGestureActive = false;
+ break;
+ default:
+ break;
+ }
+
+ return false;
+}
+
+void TopControls::scrollBegin()
+{
+ resetBaseline();
+}
+
+float TopControls::scrollBy(float pendingDelta)
+{
+ if (m_pinchGestureActive)
+ return pendingDelta;
+
+ if ((m_permittedState == WebTopControlsShown && pendingDelta < 0) || (m_permittedState == WebTopControlsHidden && pendingDelta > 0))
+ return pendingDelta;
+
+ if (m_height == 0.f)
+ return pendingDelta;
+
+ float oldOffset = contentOffset();
+
+ // Update accumulated vertical scroll and apply it to top controls
+ m_accumulatedScrollDelta += pendingDelta;
+ setShownRatio((m_baselineContentOffset + m_accumulatedScrollDelta) / m_height);
+
+ // Reset baseline when controls are fully visible
+ if (m_shownRatio == 1.f)
+ resetBaseline();
+
+ float appliedDelta = contentOffset() - oldOffset;
+ return pendingDelta - appliedDelta;
+}
+
+void TopControls::resetBaseline()
+{
+ m_accumulatedScrollDelta = 0.f;
+ m_baselineContentOffset = contentOffset();
+}
+
+float TopControls::layoutHeight()
+{
+ return m_shrinkViewport ? m_height : 0.f;
+}
+
+float TopControls::contentOffset()
+{
+ return m_shownRatio * m_height;
+}
+
+void TopControls::setShownRatio(float offset)
+{
+ // Clamp
+ offset = std::min(offset, 1.f);
+ offset = std::max(offset, 0.f);
+
+ if (m_shownRatio == offset)
+ return;
+
+ m_shownRatio = offset;
+ m_webView->didUpdateTopControls();
+}
+
+void TopControls::updateShownRatio(float delta)
+{
+ setShownRatio(m_shownRatio + delta);
+}
+
+void TopControls::updateState(
+ WebTopControlsState constraints,
+ WebTopControlsState current,
+ bool animate)
+{
+ // CC handles current state and animate so they are ignored here.
+ m_permittedState = constraints;
+}
+
+void TopControls::setHeight(float height)
+{
+ if (m_height == height)
+ return;
+
+ m_height = height;
+ m_webView->didUpdateTopControls();
+}
+
+void TopControls::setShrinkViewport(bool shrinkViewport)
+{
+ if (m_shrinkViewport == shrinkViewport)
+ return;
+
+ m_shrinkViewport = shrinkViewport;
+ m_webView->didUpdateTopControls();
+}
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698