diff options
author | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-05 20:02:18 +0000 |
---|---|---|
committer | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-05 20:02:18 +0000 |
commit | d40c8492826ae9ac77b12c2cff7fbceb2075a772 (patch) | |
tree | a4ec160749f7f644654e9cb9165e8a1283b4574a /app | |
parent | 379f172c0f5573f49b03a72a22f917f3c2d3caae (diff) | |
download | chromium_src-d40c8492826ae9ac77b12c2cff7fbceb2075a772.zip chromium_src-d40c8492826ae9ac77b12c2cff7fbceb2075a772.tar.gz chromium_src-d40c8492826ae9ac77b12c2cff7fbceb2075a772.tar.bz2 |
Changes the mini tab title animation.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/1902005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@46485 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'app')
-rw-r--r-- | app/app.gyp | 1 | ||||
-rw-r--r-- | app/app_base.gypi | 2 | ||||
-rw-r--r-- | app/multi_animation.cc | 59 | ||||
-rw-r--r-- | app/multi_animation.h | 63 | ||||
-rw-r--r-- | app/multi_animation_unittest.cc | 36 |
5 files changed, 161 insertions, 0 deletions
diff --git a/app/app.gyp b/app/app.gyp index f03cc0a..6b7b706 100644 --- a/app/app.gyp +++ b/app/app.gyp @@ -43,6 +43,7 @@ 'clipboard/clipboard_unittest.cc', 'l10n_util_mac_unittest.mm', 'l10n_util_unittest.cc', + 'multi_animation_unittest.cc', 'os_exchange_data_win_unittest.cc', 'run_all_unittests.cc', 'slide_animation_unittest.cc', diff --git a/app/app_base.gypi b/app/app_base.gypi index b28bc2b..6991816 100644 --- a/app/app_base.gypi +++ b/app/app_base.gypi @@ -155,6 +155,8 @@ 'menus/simple_menu_model.cc', 'menus/simple_menu_model.h', 'message_box_flags.h', + 'multi_animation.cc', + 'multi_animation.h', 'os_exchange_data_provider_gtk.cc', 'os_exchange_data_provider_gtk.h', 'os_exchange_data_provider_win.cc', diff --git a/app/multi_animation.cc b/app/multi_animation.cc new file mode 100644 index 0000000..703ed85 --- /dev/null +++ b/app/multi_animation.cc @@ -0,0 +1,59 @@ +// Copyright (c) 2010 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 "app/multi_animation.h" + +// Default interval, in ms. +static const int kDefaultInterval = 20; + +static int TotalTime(const MultiAnimation::Parts& parts) { + int time_ms = 0; + for (size_t i = 0; i < parts.size(); ++i) + time_ms += parts[i].time_ms; + return time_ms; +} + +MultiAnimation::MultiAnimation(const Parts& parts) + : Animation(base::TimeDelta::FromMilliseconds(kDefaultInterval)), + parts_(parts), + cycle_time_ms_(TotalTime(parts)), + current_value_(0), + current_part_index_(0) { + DCHECK(!parts_.empty()); +} + +void MultiAnimation::Step(base::TimeTicks time_now) { + double last_value = current_value_; + size_t last_index = current_part_index_; + + int delta = static_cast<int>((time_now - start_time()).InMilliseconds() % + cycle_time_ms_); + const Part& part = GetPart(&delta, ¤t_part_index_); + double percent = static_cast<double>(delta) / + static_cast<double>(part.time_ms); + current_value_ = Tween::CalculateValue(part.type, percent); + + if ((current_value_ != last_value || current_part_index_ != last_index) && + delegate()) { + delegate()->AnimationProgressed(this); + } +} + +const MultiAnimation::Part& MultiAnimation::GetPart(int* time_ms, + size_t* part_index) { + DCHECK(*time_ms < cycle_time_ms_); + + for (size_t i = 0; i < parts_.size(); ++i) { + if (*time_ms < parts_[i].time_ms) { + *part_index = i; + return parts_[i]; + } + + *time_ms -= parts_[i].time_ms; + } + NOTREACHED(); + *time_ms = 0; + *part_index = 0; + return parts_[0]; +} diff --git a/app/multi_animation.h b/app/multi_animation.h new file mode 100644 index 0000000..ac0ab84 --- /dev/null +++ b/app/multi_animation.h @@ -0,0 +1,63 @@ +// Copyright (c) 2010 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. + +#ifndef APP_MULTI_ANIMATION_H_ +#define APP_MULTI_ANIMATION_H_ + +#include <vector> + +#include "app/animation.h" +#include "app/tween.h" + +// MultiAnimation is an animation that consists of a number of sub animations. +// To create a MultiAnimation pass in the parts, invoke Start() and the delegate +// is notified as the animation progresses. MultiAnimation runs until Stop is +// invoked. +class MultiAnimation : public Animation { + public: + struct Part { + Part() : time_ms(0), type(Tween::ZERO) {} + Part(int time_ms, Tween::Type type) : time_ms(time_ms), type(type) {} + + int time_ms; + Tween::Type type; + }; + + typedef std::vector<Part> Parts; + + explicit MultiAnimation(const Parts& parts); + + // Returns the current value. The current value for a MultiAnimation is + // determined from the tween type of the current part. + virtual double GetCurrentValue() const { return current_value_; } + + // Returns the index of the current part. + size_t current_part_index() const { return current_part_index_; } + + protected: + // Animation overrides. + virtual void Step(base::TimeTicks time_now); + + private: + // Returns the part containing the specified time. |time_ms| is reset to be + // relative to the part containing the time and |part_index| the index of the + // part. + const Part& GetPart(int* time_ms, size_t* part_index); + + // The parts that make up the animation. + const Parts parts_; + + // Total time of all the parts. + const int cycle_time_ms_; + + // Current value for the animation. + double current_value_; + + // Index of the current part. + size_t current_part_index_; + + DISALLOW_COPY_AND_ASSIGN(MultiAnimation); +}; + +#endif // APP_MULTI_ANIMATION_H_ diff --git a/app/multi_animation_unittest.cc b/app/multi_animation_unittest.cc new file mode 100644 index 0000000..c3990591 --- /dev/null +++ b/app/multi_animation_unittest.cc @@ -0,0 +1,36 @@ +// Copyright (c) 2010 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 "app/multi_animation.h" +#include "testing/gtest/include/gtest/gtest.h" + +typedef testing::Test MultiAnimationTest; + +TEST_F(MultiAnimationTest, Basic) { + // Create a MultiAnimation with two parts. + MultiAnimation::Parts parts; + parts.push_back(MultiAnimation::Part(100, Tween::LINEAR)); + parts.push_back(MultiAnimation::Part(100, Tween::EASE_OUT)); + + MultiAnimation animation(parts); + AnimationContainer::Element* as_element = + static_cast<AnimationContainer::Element*>(&animation); + as_element->SetStartTime(base::TimeTicks()); + + // Step to 50, which is half way through the first part. + as_element->Step(base::TimeTicks() + base::TimeDelta::FromMilliseconds(50)); + EXPECT_EQ(.5, animation.GetCurrentValue()); + + // Step to 120, which is 20% through the second part. + as_element->Step(base::TimeTicks() + + base::TimeDelta::FromMilliseconds(120)); + EXPECT_EQ(Tween::CalculateValue(Tween::EASE_OUT, .2), + animation.GetCurrentValue()); + + // Step to 320, which is 20% through the second part. + as_element->Step(base::TimeTicks() + + base::TimeDelta::FromMilliseconds(320)); + EXPECT_EQ(Tween::CalculateValue(Tween::EASE_OUT, .2), + animation.GetCurrentValue()); +} |