blob: 7d594dd843bae6dc6ae0e7410fa0bfabd41f21a8 [file] [log] [blame]
[email protected]31a82452012-07-13 00:54:551// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]ffb15d12013-09-15 17:29:305#include "ui/gfx/animation/slide_animation.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]a5b94a92008-08-12 23:25:437#include <math.h>
8
Peter Kastingd67d6bd82019-05-03 02:31:029#include "base/numerics/ranges.h"
sangwoo.ko2e7bf392019-01-19 05:14:5310#include "ui/gfx/animation/animation_delegate.h"
11
[email protected]ffb15d12013-09-15 17:29:3012namespace gfx {
[email protected]f67678062011-01-07 17:33:3913
initial.commit09911bf2008-07-26 23:55:2914SlideAnimation::SlideAnimation(AnimationDelegate* target)
Peter Kastingd67d6bd82019-05-03 02:31:0215 : LinearAnimation(target), target_(target) {}
initial.commit09911bf2008-07-26 23:55:2916
Peter Kastingd67d6bd82019-05-03 02:31:0217SlideAnimation::~SlideAnimation() = default;
initial.commit09911bf2008-07-26 23:55:2918
19void SlideAnimation::Reset(double value) {
Peter Kastingd67d6bd82019-05-03 02:31:0220 showing_ = value == 1;
initial.commit09911bf2008-07-26 23:55:2921 value_current_ = value;
Peter Kastingd67d6bd82019-05-03 02:31:0222 Stop();
initial.commit09911bf2008-07-26 23:55:2923}
24
25void SlideAnimation::Show() {
Peter Kastingd67d6bd82019-05-03 02:31:0226 BeginAnimating(true);
initial.commit09911bf2008-07-26 23:55:2927}
28
29void SlideAnimation::Hide() {
Peter Kastingd67d6bd82019-05-03 02:31:0230 BeginAnimating(false);
initial.commit09911bf2008-07-26 23:55:2931}
32
[email protected]7cf40912010-12-09 18:25:0333void SlideAnimation::SetSlideDuration(int duration) {
34 slide_duration_ = duration;
35}
36
Alex Newcomer71197762017-10-17 18:42:5937void SlideAnimation::SetDampeningValue(double dampening_value) {
38 dampening_value_ = dampening_value;
39}
40
[email protected]7cf40912010-12-09 18:25:0341double SlideAnimation::GetCurrentValue() const {
42 return value_current_;
43}
44
Alex Newcomer71197762017-10-17 18:42:5945base::TimeDelta SlideAnimation::GetDuration() {
46 const double current_progress =
47 showing_ ? value_current_ : 1.0 - value_current_;
48
49 return base::TimeDelta::FromMillisecondsD(
50 slide_duration_ * (1 - pow(current_progress, dampening_value_)));
51}
52
Peter Kastingd67d6bd82019-05-03 02:31:0253void SlideAnimation::BeginAnimating(bool showing) {
54 if (showing_ == showing)
55 return;
56
57 showing_ = showing;
58 value_start_ = value_current_;
59 value_end_ = showing ? 1.0 : 0.0;
60
61 // Make sure we actually have something to do.
62 if (slide_duration_ == 0) {
63 AnimateToState(1.0); // Skip to the end of the animation.
64 if (delegate()) {
65 delegate()->AnimationProgressed(this);
66 delegate()->AnimationEnded(this);
67 }
68 } else if (value_current_ != value_end_) {
69 // This will also reset the currently-occurring animation.
70 SetDuration(GetDuration());
71 Start();
72 }
73}
74
initial.commit09911bf2008-07-26 23:55:2975void SlideAnimation::AnimateToState(double state) {
Peter Kastingd67d6bd82019-05-03 02:31:0276 state =
77 Tween::CalculateValue(tween_type_, base::ClampToRange(state, 0.0, 1.0));
initial.commit09911bf2008-07-26 23:55:2978 value_current_ = value_start_ + (value_end_ - value_start_) * state;
79
80 // Implement snapping.
[email protected]4ce7e152010-05-05 03:43:5581 if (tween_type_ == Tween::EASE_OUT_SNAP &&
82 fabs(value_current_ - value_end_) <= 0.06)
initial.commit09911bf2008-07-26 23:55:2983 value_current_ = value_end_;
84
85 // Correct for any overshoot (while state may be capped at 1.0, let's not
86 // take any rounding error chances.
Peter Kastingd67d6bd82019-05-03 02:31:0287 if ((value_end_ >= value_start_) ? (value_current_ > value_end_)
88 : (value_current_ < value_end_)) {
initial.commit09911bf2008-07-26 23:55:2989 value_current_ = value_end_;
90 }
91}
[email protected]f67678062011-01-07 17:33:3992
[email protected]ffb15d12013-09-15 17:29:3093} // namespace gfx