summaryrefslogtreecommitdiffstats
path: root/views/animator.cc
blob: 177b0e150e9c50eaf91d7ef51d6022e72ecf41b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.

#include "views/animator.h"

#include <algorithm>

#include "app/slide_animation.h"
#include "views/view.h"

namespace views {

////////////////////////////////////////////////////////////////////////////////
// Animator, public:

Animator::Animator(View* host)
    : host_(host),
      delegate_(NULL),
      direction_(ANIMATE_NONE) {
  InitAnimation();
}

Animator::Animator(View* host, AnimatorDelegate* delegate)
    : host_(host),
      animation_(NULL),
      delegate_(delegate),
      direction_(ANIMATE_NONE) {
  InitAnimation();
}

Animator::~Animator() {
  // Explicitly NULL the delegate so we don't call back through to the delegate
  // when the animation is stopped. The Animator is designed to be owned by a
  // View and at this point the View is dust.
  delegate_ = NULL;
  animation_->Stop();
}

bool Animator::IsAnimating() const {
  return animation_->IsAnimating();
}

void Animator::AnimateToBounds(const gfx::Rect& bounds, int direction) {
  direction_ = direction;
  start_bounds_ = host_->bounds();
  target_bounds_ = bounds;

  // Stop any running animation before we have a chance to return.
  animation_->Stop();

  if (bounds == host_->bounds())
    return;

  if (direction_ == ANIMATE_NONE) {
    host_->SetBounds(bounds);
    return;
  }

  if (direction_ & ANIMATE_X) {
    if (direction_ & ANIMATE_CLAMP)
      start_bounds_.set_x(GetClampedX());
  } else {
    start_bounds_.set_x(target_bounds_.x());
  }

  if (direction_ & ANIMATE_Y) {
    if (direction_ & ANIMATE_CLAMP)
      start_bounds_.set_y(GetClampedY());
  } else {
    start_bounds_.set_y(target_bounds_.y());
  }

  if (!(direction_ & ANIMATE_WIDTH))
    start_bounds_.set_width(target_bounds_.width());
  if (!(direction_ & ANIMATE_HEIGHT))
    start_bounds_.set_height(target_bounds_.height());

  // Make sure the host view has the start bounds to avoid a flicker.
  host_->SetBounds(start_bounds_);

  // Start the animation from the beginning.
  animation_->Reset(0.0);
  animation_->Show();
}

void Animator::AnimateToBounds(int x, int y, int width, int height,
                               int direction) {
  AnimateToBounds(gfx::Rect(x, y, std::max(0, width), std::max(0, height)),
                  direction);
}

////////////////////////////////////////////////////////////////////////////////
// Animator, AnimationDelegate:

void Animator::AnimationEnded(const Animation* animation) {
  // |delegate_| could be NULL if we're called back from the destructor.
  if (delegate_)
    delegate_->AnimationCompletedForHost(host_);
}

void Animator::AnimationProgressed(const Animation* animation) {
  int delta_x = target_bounds_.x() - start_bounds_.x();
  int delta_y = target_bounds_.y() - start_bounds_.y();
  int delta_width =  target_bounds_.width() - start_bounds_.width();
  int delta_height = target_bounds_.height() - start_bounds_.height();

  // The current frame's position and size is the animation's progress percent
  // multiplied by the delta between the start and target position/size...
  double cv = animation_->GetCurrentValue();
  int frame_x = start_bounds_.x() + static_cast<int>(delta_x * cv);
  int frame_y = start_bounds_.y() + static_cast<int>(delta_y * cv);
  // ... except for clamped positions, which remain clamped at the right/bottom
  // edge of the previous view in the layout flow.
  if (direction_ & ANIMATE_CLAMP && direction_ & ANIMATE_X)
    frame_x = GetClampedX();
  if (direction_ & ANIMATE_CLAMP && direction_ & ANIMATE_Y)
    frame_y = GetClampedY();
  int frame_width = start_bounds_.width() + static_cast<int>(delta_width * cv);
  int frame_height =
      start_bounds_.height() + static_cast<int>(delta_height * cv);
  host_->SetBounds(frame_x, frame_y, frame_width, frame_height);
  host_->GetParent()->SchedulePaint();
}

void Animator::AnimationCanceled(const Animation* animation) {
  AnimationEnded(animation);
}

////////////////////////////////////////////////////////////////////////////////
// Animator, private:

void Animator::InitAnimation() {
  animation_.reset(new SlideAnimation(this));
  animation_->SetSlideDuration(150);
  animation_->SetTweenType(SlideAnimation::EASE_OUT);
}

int Animator::GetClampedX() const {
  if (delegate_ && direction_ & ANIMATE_CLAMP && direction_ & ANIMATE_X) {
    View* previous_view = delegate_->GetClampedView(host_);
    if (previous_view)
      return previous_view->bounds().right();
  }
  return host_->x();
}

int Animator::GetClampedY() const {
  if (delegate_ && direction_ & ANIMATE_CLAMP && direction_ & ANIMATE_Y) {
    View* previous_view = delegate_->GetClampedView(host_);
    if (previous_view)
      return previous_view->bounds().bottom();
  }
  return host_->y();
}

}  // namespace views