summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-02-07 17:23:14 +0000
committersky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-02-07 17:23:14 +0000
commitdbba0c455f8b3f139fbfd9634d0bfba268085a33 (patch)
tree91c42ac484be649b5ed537ff1e12fc7edbe9f7fa
parent4c1b6f0b5e1ce512780f5c83c245fd62dca3f3d1 (diff)
downloadchromium_src-dbba0c455f8b3f139fbfd9634d0bfba268085a33.zip
chromium_src-dbba0c455f8b3f139fbfd9634d0bfba268085a33.tar.gz
chromium_src-dbba0c455f8b3f139fbfd9634d0bfba268085a33.tar.bz2
Adds Animation::CurrentValueBetween and gets rid of a bunch of
duplicate code. BUG=none TEST=none Review URL: http://codereview.chromium.org/570055 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@38330 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--app/animation.cc20
-rw-r--r--app/animation.h14
-rw-r--r--chrome/browser/gtk/tabs/tab_strip_gtk.cc32
-rw-r--r--chrome/browser/views/tabs/grid.cc21
-rw-r--r--chrome/browser/views/tabs/grid.h11
-rw-r--r--chrome/browser/views/tabs/tab_overview_controller.cc2
-rw-r--r--chrome/browser/views/tabs/tab_strip.cc74
7 files changed, 89 insertions, 85 deletions
diff --git a/app/animation.cc b/app/animation.cc
index 94c99c2..b341d2a 100644
--- a/app/animation.cc
+++ b/app/animation.cc
@@ -4,6 +4,7 @@
#include "app/animation.h"
+#include "base/gfx/rect.h"
#include "base/message_loop.h"
#if defined(OS_WIN)
@@ -47,6 +48,25 @@ double Animation::GetCurrentValue() const {
return state_;
}
+double Animation::CurrentValueBetween(double start, double target) const {
+ return start + (target - start) * GetCurrentValue();
+}
+
+int Animation::CurrentValueBetween(int start, int target) const {
+ return static_cast<int>(CurrentValueBetween(static_cast<double>(start),
+ static_cast<double>(target)));
+}
+
+gfx::Rect Animation::CurrentValueBetween(const gfx::Rect& start_bounds,
+ const gfx::Rect& target_bounds) const {
+ return gfx::Rect(CurrentValueBetween(start_bounds.x(), target_bounds.x()),
+ CurrentValueBetween(start_bounds.y(), target_bounds.y()),
+ CurrentValueBetween(start_bounds.width(),
+ target_bounds.width()),
+ CurrentValueBetween(start_bounds.height(),
+ target_bounds.height()));
+}
+
void Animation::Start() {
if (!animating_) {
start_time_ = Time::Now();
diff --git a/app/animation.h b/app/animation.h
index bd38fb4..bfa7fd0 100644
--- a/app/animation.h
+++ b/app/animation.h
@@ -11,6 +11,10 @@
class Animation;
+namespace gfx {
+class Rect;
+}
+
// AnimationDelegate
//
// Implement this interface when you want to receive notifications about the
@@ -77,6 +81,13 @@ class Animation {
// however subclasses can override this to provide others.
virtual double GetCurrentValue() const;
+ // Convenience for returning a value between |start| and |target| based on
+ // the current value. This is (target - start) * GetCurrentValue() + start.
+ double CurrentValueBetween(double start, double target) const;
+ int CurrentValueBetween(int start, int target) const;
+ gfx::Rect CurrentValueBetween(const gfx::Rect& start_bounds,
+ const gfx::Rect& target_bounds) const;
+
// Start the animation.
void Start();
@@ -98,6 +109,9 @@ class Animation {
// to give guidance for heavy animations such as "start download" arrow.
static bool ShouldRenderRichAnimation();
+ // Sets the delegate.
+ void set_delegate(AnimationDelegate* delegate) { delegate_ = delegate; }
+
protected:
// Overriddable, called by Run.
virtual void Step();
diff --git a/chrome/browser/gtk/tabs/tab_strip_gtk.cc b/chrome/browser/gtk/tabs/tab_strip_gtk.cc
index 344d935..1ac295ce 100644
--- a/chrome/browser/gtk/tabs/tab_strip_gtk.cc
+++ b/chrome/browser/gtk/tabs/tab_strip_gtk.cc
@@ -222,21 +222,6 @@ class TabStripGtk::TabAnimation : public AnimationDelegate {
&end_selected_width_);
}
- // Returns a value between |start| and |target| based on the current
- // animation.
- // TODO(sky): move this to animation.
- int AnimationPosition(int start, int target) const {
- return static_cast<int>(AnimationPosition(static_cast<double>(start),
- static_cast<double>(target)));
- }
-
- // Returns a value between |start| and |target| based on the current
- // animation.
- // TODO(sky): move this to animation.
- double AnimationPosition(double start, double target) const {
- return start + (target - start) * animation_.GetCurrentValue();
- }
-
TabStripGtk* tabstrip_;
SlideAnimation animation_;
@@ -355,8 +340,10 @@ class RemoveTabAnimation : public TabStripGtk::TabAnimation {
if (index == index_) {
// The tab(s) being removed are gradually shrunken depending on the state
// of the animation.
- if (tab->is_pinned())
- return AnimationPosition(TabGtk::GetPinnedWidth(), -kTabHOffset);
+ if (tab->is_pinned()) {
+ return animation_.CurrentValueBetween(TabGtk::GetPinnedWidth(),
+ -kTabHOffset);
+ }
// Removed animated Tabs are never selected.
double start_width = start_unselected_width_;
@@ -365,7 +352,7 @@ class RemoveTabAnimation : public TabStripGtk::TabAnimation {
double target_width =
std::max(abs(kTabHOffset),
TabGtk::GetMinimumUnselectedSize().width() + kTabHOffset);
- return AnimationPosition(start_width, target_width);
+ return animation_.CurrentValueBetween(start_width, target_width);
}
if (tab->is_pinned())
@@ -486,10 +473,13 @@ class ResizeLayoutAnimation : public TabStripGtk::TabAnimation {
if (tab->is_pinned())
return TabGtk::GetPinnedWidth();
- if (tab->IsSelected())
- return AnimationPosition(start_selected_width_, end_selected_width_);
+ if (tab->IsSelected()) {
+ return animation_.CurrentValueBetween(start_selected_width_,
+ end_selected_width_);
+ }
- return AnimationPosition(start_unselected_width_, end_unselected_width_);
+ return animation_.CurrentValueBetween(start_unselected_width_,
+ end_unselected_width_);
}
private:
diff --git a/chrome/browser/views/tabs/grid.cc b/chrome/browser/views/tabs/grid.cc
index 4924956..e6db1e7 100644
--- a/chrome/browser/views/tabs/grid.cc
+++ b/chrome/browser/views/tabs/grid.cc
@@ -64,21 +64,6 @@ void Grid::AnimateToTargetBounds() {
CalculateTargetBoundsAndStartAnimation();
}
-int Grid::AnimationPosition(int start, int target) {
- return start + static_cast<int>(
- static_cast<double>(target - start) * animation_.GetCurrentValue());
-}
-
-gfx::Rect Grid::AnimationPosition(const gfx::Rect& start_bounds,
- const gfx::Rect& target_bounds) {
- return gfx::Rect(AnimationPosition(start_bounds.x(), target_bounds.x()),
- AnimationPosition(start_bounds.y(), target_bounds.y()),
- AnimationPosition(start_bounds.width(),
- target_bounds.width()),
- AnimationPosition(start_bounds.height(),
- target_bounds.height()));
-}
-
void Grid::ViewHierarchyChanged(bool is_add, View* parent, View* child) {
if (modifying_children_ || parent != this)
return;
@@ -147,8 +132,10 @@ void Grid::AnimationProgressed(const Animation* animation) {
View* view = GetChildViewAt(i);
gfx::Rect start_bounds = start_bounds_[i];
gfx::Rect target_bounds = target_bounds_[i];
- if (static_cast<int>(i) != floating_index_)
- view->SetBounds(AnimationPosition(start_bounds, target_bounds));
+ if (static_cast<int>(i) != floating_index_) {
+ view->SetBounds(
+ animation_.CurrentValueBetween(start_bounds, target_bounds));
+ }
}
SchedulePaint();
}
diff --git a/chrome/browser/views/tabs/grid.h b/chrome/browser/views/tabs/grid.h
index 83653fb..c046737 100644
--- a/chrome/browser/views/tabs/grid.h
+++ b/chrome/browser/views/tabs/grid.h
@@ -65,15 +65,8 @@ class Grid : public views::View, public AnimationDelegate {
// Returns the bounds of the specified cell.
gfx::Rect CellBounds(int index);
- // Returns the value based on the current animation. |start| gives the
- // starting coordinate and |target| the target coordinate. The resulting
- // value is between |start| and |target| based on the current animation.
- int AnimationPosition(int start, int target);
-
- // Convenience for returning a rectangle between |start_bounds| and
- // |target_bounds| based on the current animation.
- gfx::Rect AnimationPosition(const gfx::Rect& start_bounds,
- const gfx::Rect& target_bounds);
+ // Returns the animation.
+ const SlideAnimation& animation() { return animation_; }
// View overrides.
virtual void ViewHierarchyChanged(bool is_add,
diff --git a/chrome/browser/views/tabs/tab_overview_controller.cc b/chrome/browser/views/tabs/tab_overview_controller.cc
index 4422d85..3321e6d 100644
--- a/chrome/browser/views/tabs/tab_overview_controller.cc
+++ b/chrome/browser/views/tabs/tab_overview_controller.cc
@@ -209,7 +209,7 @@ void TabOverviewController::GridAnimationProgressed() {
// of the view shrinking in size.
container_->SchedulePaint();
container_->SetBounds(
- grid_->AnimationPosition(start_bounds_, target_bounds_));
+ grid_->animation().CurrentValueBetween(start_bounds_, target_bounds_));
container_->SchedulePaint();
// Update the position of the dragged cell.
diff --git a/chrome/browser/views/tabs/tab_strip.cc b/chrome/browser/views/tabs/tab_strip.cc
index 6f6e0d0..6a275f2 100644
--- a/chrome/browser/views/tabs/tab_strip.cc
+++ b/chrome/browser/views/tabs/tab_strip.cc
@@ -241,21 +241,6 @@ class TabStrip::TabAnimation : public AnimationDelegate {
&end_selected_width_);
}
- // Returns a value between |start| and |target| based on the current
- // animation.
- // TODO(sky): move this to animation.
- int AnimationPosition(int start, int target) const {
- return static_cast<int>(AnimationPosition(static_cast<double>(start),
- static_cast<double>(target)));
- }
-
- // Returns a value between |start| and |target| based on the current
- // animation.
- // TODO(sky): move this to animation.
- double AnimationPosition(double start, double target) const {
- return start + (target - start) * animation_.GetCurrentValue();
- }
-
TabStrip* tabstrip_;
SlideAnimation animation_;
@@ -376,8 +361,10 @@ class TabStrip::RemoveTabAnimation : public TabStrip::TabAnimation {
// The tab(s) being removed are gradually shrunken depending on the state
// of the animation.
// Removed animated Tabs are never selected.
- if (tab->mini())
- return AnimationPosition(Tab::GetMiniWidth(), -kTabHOffset);
+ if (tab->mini()) {
+ return animation_.CurrentValueBetween(Tab::GetMiniWidth(),
+ -kTabHOffset);
+ }
double start_width = start_unselected_width_;
// Make sure target_width is at least abs(kTabHOffset), otherwise if
@@ -385,7 +372,7 @@ class TabStrip::RemoveTabAnimation : public TabStrip::TabAnimation {
double target_width =
std::max(abs(kTabHOffset),
Tab::GetMinimumUnselectedSize().width() + kTabHOffset);
- return AnimationPosition(start_width, target_width);
+ return animation_.CurrentValueBetween(start_width, target_width);
}
if (tab->mini())
@@ -534,10 +521,13 @@ class TabStrip::ResizeLayoutAnimation : public TabStrip::TabAnimation {
if (tab->mini())
return Tab::GetMiniWidth();
- if (tab->IsSelected())
- return AnimationPosition(start_selected_width_, end_selected_width_);
+ if (tab->IsSelected()) {
+ return animation_.CurrentValueBetween(start_selected_width_,
+ end_selected_width_);
+ }
- return AnimationPosition(start_unselected_width_, end_unselected_width_);
+ return animation_.CurrentValueBetween(start_unselected_width_,
+ end_unselected_width_);
}
private:
@@ -591,21 +581,25 @@ class TabStrip::MiniTabAnimation : public TabStrip::TabAnimation {
if (index == index_) {
if (tab->mini()) {
- return AnimationPosition(
+ return animation_.CurrentValueBetween(
start_selected_width_,
static_cast<double>(Tab::GetMiniWidth()));
} else {
- return AnimationPosition(static_cast<double>(Tab::GetMiniWidth()),
- end_selected_width_);
+ return animation_.CurrentValueBetween(
+ static_cast<double>(Tab::GetMiniWidth()),
+ end_selected_width_);
}
} else if (tab->mini()) {
return Tab::GetMiniWidth();
}
- if (tab->IsSelected())
- return AnimationPosition(start_selected_width_, end_selected_width_);
+ if (tab->IsSelected()) {
+ return animation_.CurrentValueBetween(start_selected_width_,
+ end_selected_width_);
+ }
- return AnimationPosition(start_unselected_width_, end_unselected_width_);
+ return animation_.CurrentValueBetween(start_unselected_width_,
+ end_unselected_width_);
}
private:
@@ -649,9 +643,10 @@ class TabStrip::MiniMoveAnimation : public TabStrip::TabAnimation {
TabAnimation::AnimationProgressed(animation);
// Then special case the position of the tab being moved.
- int x = AnimationPosition(start_bounds_.x(), target_bounds_.x());
- int width = AnimationPosition(start_bounds_.width(),
- target_bounds_.width());
+ int x = animation_.CurrentValueBetween(start_bounds_.x(),
+ target_bounds_.x());
+ int width = animation_.CurrentValueBetween(start_bounds_.width(),
+ target_bounds_.width());
gfx::Rect tab_bounds(x, start_bounds_.y(), width,
start_bounds_.height());
tab_->SetBounds(tab_bounds);
@@ -666,16 +661,18 @@ class TabStrip::MiniMoveAnimation : public TabStrip::TabAnimation {
if (to_index_ < from_index_) {
// The tab was mini.
if (index == to_index_) {
- double current_size = AnimationPosition(0, target_bounds_.width());
+ double current_size =
+ animation_.CurrentValueBetween(0, target_bounds_.width());
if (current_size < -kTabHOffset)
return -(current_size + kTabHOffset);
} else if (index == from_index_ + 1) {
- return AnimationPosition(start_bounds_.width(), 0);
+ return animation_.CurrentValueBetween(start_bounds_.width(), 0);
}
} else {
// The tab was made a normal tab.
if (index == from_index_) {
- return AnimationPosition(Tab::GetMiniWidth() + kTabHOffset, 0);
+ return animation_.CurrentValueBetween(Tab::GetMiniWidth() +
+ kTabHOffset, 0);
}
}
return 0;
@@ -689,15 +686,18 @@ class TabStrip::MiniMoveAnimation : public TabStrip::TabAnimation {
Tab* tab = tabstrip_->GetTabAt(index);
if (index == to_index_)
- return AnimationPosition(0, target_bounds_.width());
+ return animation_.CurrentValueBetween(0, target_bounds_.width());
if (tab->mini())
return Tab::GetMiniWidth();
- if (tab->IsSelected())
- return AnimationPosition(start_selected_width_, end_selected_width_);
+ if (tab->IsSelected()) {
+ return animation_.CurrentValueBetween(start_selected_width_,
+ end_selected_width_);
+ }
- return AnimationPosition(start_unselected_width_, end_unselected_width_);
+ return animation_.CurrentValueBetween(start_unselected_width_,
+ end_unselected_width_);
}
private: