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

Unified Diff: Source/platform/scroll/ScrollbarStateTransitionAnimator.cpp

Issue 246293006: Blink Support for Overlay Scrollbar Animation Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: fix try server compile error Created 6 years, 6 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/platform/scroll/ScrollbarStateTransitionAnimator.cpp
diff --git a/Source/platform/scroll/ScrollbarStateTransitionAnimator.cpp b/Source/platform/scroll/ScrollbarStateTransitionAnimator.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..bb05a2b1ab034f29a458f0172b7d1a6df065efaa
--- /dev/null
+++ b/Source/platform/scroll/ScrollbarStateTransitionAnimator.cpp
@@ -0,0 +1,230 @@
+/*
+ * Copyright (c) 2011, 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:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * 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.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 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 THE COPYRIGHT
+ * OWNER OR 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 "platform/scroll/ScrollbarStateTransitionAnimator.h"
+
+#include "platform/TraceEvent.h"
+#include "platform/scroll/ScrollableArea.h"
+#include "wtf/CurrentTime.h"
+#include "wtf/PassOwnPtr.h"
+#include <algorithm>
+
+using namespace std;
+
+namespace {
+// This number is calculated from scrollbar animation controller thinning. It is supposed to last 300 milliseconds.
+const double kDefaultStateTransitionTime = 0.3;
+
+} // namespace
+
+namespace WebCore {
+
+PassOwnPtr<ScrollbarStateTransitionAnimator> ScrollbarStateTransitionAnimator::create(ScrollableArea* scrollableArea)
+{
+ if (scrollableArea->hasOverlayScrollbars())
+ return adoptPtr(new ScrollbarStateTransitionAnimator(scrollableArea));
+ return PassOwnPtr<ScrollbarStateTransitionAnimator>();
+}
+
+ScrollbarStateTransitionAnimator::StateTransitionData::StateTransitionData(ScrollbarStateTransitionAnimator* parent)
+{
+ reset();
+}
+
+void ScrollbarStateTransitionAnimator::StateTransitionData::reset()
+{
+ m_startTime = 0;
+ m_animationTime = kDefaultStateTransitionTime;
+ m_lastAnimatedTime = 0;
+ m_progress = 0;
+ m_startState = blink::WebThemeEngine::StateDisabled;
+ m_endState = blink::WebThemeEngine::StateDisabled;
+}
+
+void ScrollbarStateTransitionAnimator::StateTransitionData::startStateTransitionAnimation(double currentTime, blink::WebThemeEngine::State startState, blink::WebThemeEngine::State endState, const Parameters parameters)
+{
+ if (m_startTime) {
+ // Animation is going on. This is probably because we need to reverse animation state.
+ if (startState == m_endState || endState == m_startState) {
+ // Since one set of the state could be either hover/pressed, check whether the other
+ // state is the same. (normal state)
+ double progressedTime = currentTime - m_startTime;
+ // startTime is used to calculate progress, we are faking a startTime.
+ m_startTime = currentTime - (m_animationTime - progressedTime);
+ m_progress = 1 - m_progress;
+ }
+ } else {
+ m_startTime = currentTime;
+ m_progress = 0;
+ }
+ m_startState = startState;
+ m_endState = endState;
+}
+
+bool ScrollbarStateTransitionAnimator::StateTransitionData::animate(double currentTime)
+{
+ if (!duringAnimation(currentTime)) {
+ // Animation should end.
+ reset();
+ m_lastAnimatedTime = currentTime;
+ return false;
+ }
+
+ m_progress = (currentTime - m_startTime) / m_animationTime;
+
+ // Store progress of size/opacity so ScrollbarTheme can read.
+ return true;
+}
+
+bool ScrollbarStateTransitionAnimator::StateTransitionData::duringAnimation(double currentTime) const
+{
+ return m_startTime && m_startTime <= currentTime && currentTime <= m_startTime + m_animationTime;
+}
+
+ScrollbarStateTransitionAnimator::ScrollbarStateTransitionAnimator(ScrollableArea* scrollableArea)
+ : m_scrollableArea(scrollableArea)
+ , m_data(this)
+ , m_animationActive(false)
+{
+ m_parameters.m_isEnabled = scrollableArea->hasOverlayScrollbars();
+ m_parameters.m_animationTime = kDefaultStateTransitionTime;
+}
+
+ScrollbarStateTransitionAnimator::~ScrollbarStateTransitionAnimator()
+{
+ stopAnimationTimerIfNeeded();
+}
+
+void ScrollbarStateTransitionAnimator::cancelAnimations()
+{
+ m_animationActive = false;
+}
+
+void ScrollbarStateTransitionAnimator::serviceAnimations()
+{
+ if (m_animationActive)
+ animationTimerFired();
+}
+
+void ScrollbarStateTransitionAnimator::mouseEnteredScrollbar(Scrollbar* scrollbar)
+{
+ if (!m_parameters.m_isEnabled)
+ return;
+
+ // Start transition from normal to hover/pressed.
+ double currentTime = this->currentTime();
+ blink::WebThemeEngine::State startState = blink::WebThemeEngine::StateNormal;
+ blink::WebThemeEngine::State endState = blink::WebThemeEngine::StateHover;
+
+ if (scrollbar) {
+ ScrollbarPart pressedPart = scrollbar->pressedPart();
+ if (pressedPart == ThumbPart)
+ endState = blink::WebThemeEngine::StatePressed;
+ }
+
+ m_data.startStateTransitionAnimation(currentTime, startState, endState, m_parameters);
+ animationWillStart();
+ animationTimerFired();
+}
+
+void ScrollbarStateTransitionAnimator::mouseExitedScrollbar(Scrollbar* scrollbar)
+{
+ if (!m_parameters.m_isEnabled)
+ return;
+
+ // Start transition from hover/pressed to normal.
+ double currentTime = this->currentTime();
+ blink::WebThemeEngine::State endState = blink::WebThemeEngine::StateNormal;
+ blink::WebThemeEngine::State startState = blink::WebThemeEngine::StateHover;
+
+ if (scrollbar) {
+ ScrollbarPart pressedPart = scrollbar->pressedPart();
+ if (pressedPart == ThumbPart)
+ endState = blink::WebThemeEngine::StatePressed;
+ }
+
+ m_data.startStateTransitionAnimation(currentTime, startState, endState, m_parameters);
+ animationWillStart();
+ animationTimerFired();
+}
+
+bool ScrollbarStateTransitionAnimator::isDuringStateTransitionAnimation() const
+{
+ double currentTime = this->currentTime();
+ return m_animationActive && m_data.duringAnimation(currentTime);
+}
+
+void ScrollbarStateTransitionAnimator::notifyStateTransitionInProgress() const
+{
+ m_scrollableArea->stateTransitionInProgress();
+}
+
+void ScrollbarStateTransitionAnimator::animationTimerFired()
+{
+ TRACE_EVENT0("webkit", "ScrollbarStateTransitionAnimator::animationTimerFired");
+
+ double currentTime = this->currentTime();
+
+ if (m_data.animate(currentTime)) {
+ startNextTimer();
+ } else {
+ m_animationActive = false;
+ animationDidFinish();
+ }
+
+ notifyStateTransitionInProgress();
+}
+
+void ScrollbarStateTransitionAnimator::startNextTimer()
+{
+ if (scrollableArea()->scheduleAnimation())
+ m_animationActive = true;
+}
+
+bool ScrollbarStateTransitionAnimator::animationTimerActive()
+{
+ return m_animationActive;
+}
+
+void ScrollbarStateTransitionAnimator::stopAnimationTimerIfNeeded()
+{
+ if (animationTimerActive()) {
+ m_animationActive = false;
+ }
+}
+
+double ScrollbarStateTransitionAnimator::currentTime() const
+{
+ return WTF::monotonicallyIncreasingTime();
+}
+
+} // namespace WebCore

Powered by Google App Engine
This is Rietveld 408576698