animation.cc revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
1// Copyright (c) 2011 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.
4
5#include "ui/gfx/animation/animation.h"
6
7#include "ui/gfx/animation/animation_container.h"
8#include "ui/gfx/animation/animation_delegate.h"
9#include "ui/gfx/animation/tween.h"
10#include "ui/gfx/rect.h"
11
12#if defined(OS_WIN)
13#include "base/win/windows_version.h"
14#endif
15
16namespace gfx {
17
18Animation::Animation(base::TimeDelta timer_interval)
19    : timer_interval_(timer_interval),
20      is_animating_(false),
21      delegate_(NULL) {
22}
23
24Animation::~Animation() {
25  // Don't send out notification from the destructor. Chances are the delegate
26  // owns us and is being deleted as well.
27  if (is_animating_)
28    container_->Stop(this);
29}
30
31void Animation::Start() {
32  if (is_animating_)
33    return;
34
35  if (!container_.get())
36    container_ = new AnimationContainer();
37
38  is_animating_ = true;
39
40  container_->Start(this);
41
42  AnimationStarted();
43}
44
45void Animation::Stop() {
46  if (!is_animating_)
47    return;
48
49  is_animating_ = false;
50
51  // Notify the container first as the delegate may delete us.
52  container_->Stop(this);
53
54  AnimationStopped();
55
56  if (delegate_) {
57    if (ShouldSendCanceledFromStop())
58      delegate_->AnimationCanceled(this);
59    else
60      delegate_->AnimationEnded(this);
61  }
62}
63
64double Animation::CurrentValueBetween(double start, double target) const {
65  return Tween::DoubleValueBetween(GetCurrentValue(), start, target);
66}
67
68int Animation::CurrentValueBetween(int start, int target) const {
69  return Tween::IntValueBetween(GetCurrentValue(), start, target);
70}
71
72gfx::Rect Animation::CurrentValueBetween(const gfx::Rect& start_bounds,
73                                         const gfx::Rect& target_bounds) const {
74  return Tween::RectValueBetween(
75      GetCurrentValue(), start_bounds, target_bounds);
76}
77
78void Animation::SetContainer(AnimationContainer* container) {
79  if (container == container_.get())
80    return;
81
82  if (is_animating_)
83    container_->Stop(this);
84
85  if (container)
86    container_ = container;
87  else
88    container_ = new AnimationContainer();
89
90  if (is_animating_)
91    container_->Start(this);
92}
93
94// static
95bool Animation::ShouldRenderRichAnimation() {
96#if defined(OS_WIN)
97  if (base::win::GetVersion() >= base::win::VERSION_VISTA) {
98    BOOL result;
99    // Get "Turn off all unnecessary animations" value.
100    if (::SystemParametersInfo(SPI_GETCLIENTAREAANIMATION, 0, &result, 0)) {
101      // There seems to be a typo in the MSDN document (as of May 2009):
102      //   http://msdn.microsoft.com/en-us/library/ms724947(VS.85).aspx
103      // The document states that the result is TRUE when animations are
104      // _disabled_, but in fact, it is TRUE when they are _enabled_.
105      return !!result;
106    }
107  }
108  return !::GetSystemMetrics(SM_REMOTESESSION);
109#endif
110  return true;
111}
112
113bool Animation::ShouldSendCanceledFromStop() {
114  return false;
115}
116
117void Animation::SetStartTime(base::TimeTicks start_time) {
118  start_time_ = start_time;
119}
120
121base::TimeDelta Animation::GetTimerInterval() const {
122  return timer_interval_;
123}
124
125}  // namespace gfx
126