diff options
author | vollick@google.com <vollick@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-10 14:26:24 +0000 |
---|---|---|
committer | vollick@google.com <vollick@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-10 14:26:24 +0000 |
commit | a67935f837cf7d8a1bf0a22ef5b0b1f5d92b66aa (patch) | |
tree | 8563fa6d4d7f09f272c01e586c9c1072312af0e5 /ui | |
parent | 7df588fbddab01beecf3f996b41e917f4e81b5a9 (diff) | |
download | chromium_src-a67935f837cf7d8a1bf0a22ef5b0b1f5d92b66aa.zip chromium_src-a67935f837cf7d8a1bf0a22ef5b0b1f5d92b66aa.tar.gz chromium_src-a67935f837cf7d8a1bf0a22ef5b0b1f5d92b66aa.tar.bz2 |
Make visibility an animatable layer property.
Now that windows can fade in or out, it makes sense to be able to ask a layer what its target visibility is. To facilitate this, visibility can now be animated like any other layer property.
BUG=110487
TEST=compositor_unittests
Review URL: https://chromiumcodereview.appspot.com/9253014
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@121447 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui')
-rw-r--r-- | ui/gfx/compositor/layer.cc | 38 | ||||
-rw-r--r-- | ui/gfx/compositor/layer.h | 7 | ||||
-rw-r--r-- | ui/gfx/compositor/layer_animation_delegate.h | 4 | ||||
-rw-r--r-- | ui/gfx/compositor/layer_animation_element.cc | 76 | ||||
-rw-r--r-- | ui/gfx/compositor/layer_animation_element.h | 12 | ||||
-rw-r--r-- | ui/gfx/compositor/layer_animation_element_unittest.cc | 29 | ||||
-rw-r--r-- | ui/gfx/compositor/layer_animator.cc | 15 | ||||
-rw-r--r-- | ui/gfx/compositor/layer_animator.h | 4 | ||||
-rw-r--r-- | ui/gfx/compositor/test/test_layer_animation_delegate.cc | 17 | ||||
-rw-r--r-- | ui/gfx/compositor/test/test_layer_animation_delegate.h | 5 |
10 files changed, 176 insertions, 31 deletions
diff --git a/ui/gfx/compositor/layer.cc b/ui/gfx/compositor/layer.cc index 1a3d984..9795409 100644 --- a/ui/gfx/compositor/layer.cc +++ b/ui/gfx/compositor/layer.cc @@ -150,7 +150,8 @@ void Layer::SetTransform(const ui::Transform& transform) { } Transform Layer::GetTargetTransform() const { - if (animator_.get() && animator_->is_animating()) + if (animator_.get() && animator_->IsAnimatingProperty( + LayerAnimationElement::TRANSFORM)) return animator_->GetTargetTransform(); return transform_; } @@ -160,7 +161,8 @@ void Layer::SetBounds(const gfx::Rect& bounds) { } gfx::Rect Layer::GetTargetBounds() const { - if (animator_.get() && animator_->is_animating()) + if (animator_.get() && animator_->IsAnimatingProperty( + LayerAnimationElement::BOUNDS)) return animator_->GetTargetBounds(); return bounds_; } @@ -170,18 +172,21 @@ void Layer::SetOpacity(float opacity) { } float Layer::GetTargetOpacity() const { - if (animator_.get() && animator_->is_animating()) + if (animator_.get() && animator_->IsAnimatingProperty( + LayerAnimationElement::OPACITY)) return animator_->GetTargetOpacity(); return opacity_; } void Layer::SetVisible(bool visible) { - if (visible_ == visible) - return; + GetAnimator()->SetVisibility(visible); +} - visible_ = visible; - // TODO(piman): Expose a visibility flag on WebLayer. - web_layer_.setOpacity(visible_ ? opacity_ : 0.f); +bool Layer::GetTargetVisibility() const { + if (animator_.get() && animator_->IsAnimatingProperty( + LayerAnimationElement::VISIBILITY)) + return animator_->GetTargetVisibility(); + return visible_; } bool Layer::IsDrawn() const { @@ -417,6 +422,15 @@ void Layer::SetOpacityImmediately(float opacity) { ScheduleDraw(); } +void Layer::SetVisibilityImmediately(bool visible) { + if (visible_ == visible) + return; + + visible_ = visible; + // TODO(piman): Expose a visibility flag on WebLayer. + web_layer_.setOpacity(visible_ ? opacity_ : 0.f); +} + void Layer::SetBoundsFromAnimation(const gfx::Rect& bounds) { SetBoundsImmediately(bounds); } @@ -429,6 +443,10 @@ void Layer::SetOpacityFromAnimation(float opacity) { SetOpacityImmediately(opacity); } +void Layer::SetVisibilityFromAnimation(bool visibility) { + SetVisibilityImmediately(visibility); +} + void Layer::ScheduleDrawForAnimation() { ScheduleDraw(); } @@ -445,6 +463,10 @@ float Layer::GetOpacityForAnimation() const { return opacity(); } +bool Layer::GetVisibilityForAnimation() const { + return visible(); +} + void Layer::CreateWebLayer() { if (type_ == LAYER_SOLID_COLOR) web_layer_ = WebKit::WebSolidColorLayer::create(); diff --git a/ui/gfx/compositor/layer.h b/ui/gfx/compositor/layer.h index b3adc3b..e6c7051 100644 --- a/ui/gfx/compositor/layer.h +++ b/ui/gfx/compositor/layer.h @@ -141,6 +141,10 @@ class COMPOSITOR_EXPORT Layer : void SetVisible(bool visible); bool visible() const { return visible_; } + // Returns the target visibility if the animator is running. Otherwise, it + // returns the current visibility. + bool GetTargetVisibility() const; + // Returns true if this Layer is drawn. A Layer is drawn only if all ancestors // are visible. bool IsDrawn() const; @@ -229,15 +233,18 @@ class COMPOSITOR_EXPORT Layer : void SetBoundsImmediately(const gfx::Rect& bounds); void SetTransformImmediately(const ui::Transform& transform); void SetOpacityImmediately(float opacity); + void SetVisibilityImmediately(bool visibility); // Implementation of LayerAnimatorDelegate virtual void SetBoundsFromAnimation(const gfx::Rect& bounds) OVERRIDE; virtual void SetTransformFromAnimation(const Transform& transform) OVERRIDE; virtual void SetOpacityFromAnimation(float opacity) OVERRIDE; + virtual void SetVisibilityFromAnimation(bool visibility) OVERRIDE; virtual void ScheduleDrawForAnimation() OVERRIDE; virtual const gfx::Rect& GetBoundsForAnimation() const OVERRIDE; virtual const Transform& GetTransformForAnimation() const OVERRIDE; virtual float GetOpacityForAnimation() const OVERRIDE; + virtual bool GetVisibilityForAnimation() const OVERRIDE; void CreateWebLayer(); void RecomputeTransform(); diff --git a/ui/gfx/compositor/layer_animation_delegate.h b/ui/gfx/compositor/layer_animation_delegate.h index 0f9ea4b..8470348 100644 --- a/ui/gfx/compositor/layer_animation_delegate.h +++ b/ui/gfx/compositor/layer_animation_delegate.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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. @@ -18,10 +18,12 @@ class COMPOSITOR_EXPORT LayerAnimationDelegate { virtual void SetBoundsFromAnimation(const gfx::Rect& bounds) = 0; virtual void SetTransformFromAnimation(const Transform& transform) = 0; virtual void SetOpacityFromAnimation(float opacity) = 0; + virtual void SetVisibilityFromAnimation(bool visibility) = 0; virtual void ScheduleDrawForAnimation() = 0; virtual const gfx::Rect& GetBoundsForAnimation() const = 0; virtual const Transform& GetTransformForAnimation() const = 0; virtual float GetOpacityForAnimation() const = 0; + virtual bool GetVisibilityForAnimation() const = 0; protected: virtual ~LayerAnimationDelegate() {} diff --git a/ui/gfx/compositor/layer_animation_element.cc b/ui/gfx/compositor/layer_animation_element.cc index 66ac054..330e7e8 100644 --- a/ui/gfx/compositor/layer_animation_element.cc +++ b/ui/gfx/compositor/layer_animation_element.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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. @@ -57,10 +57,9 @@ class TransformTransition : public LayerAnimationElement { virtual void OnAbort() OVERRIDE {} private: - static const AnimatableProperties& GetProperties() { - static AnimatableProperties properties; - if (properties.size() == 0) - properties.insert(LayerAnimationElement::TRANSFORM); + static AnimatableProperties GetProperties() { + AnimatableProperties properties; + properties.insert(LayerAnimationElement::TRANSFORM); return properties; } @@ -96,10 +95,9 @@ class BoundsTransition : public LayerAnimationElement { virtual void OnAbort() OVERRIDE {} private: - static const AnimatableProperties& GetProperties() { - static AnimatableProperties properties; - if (properties.size() == 0) - properties.insert(LayerAnimationElement::BOUNDS); + static AnimatableProperties GetProperties() { + AnimatableProperties properties; + properties.insert(LayerAnimationElement::BOUNDS); return properties; } @@ -109,6 +107,8 @@ class BoundsTransition : public LayerAnimationElement { DISALLOW_COPY_AND_ASSIGN(BoundsTransition); }; +// OpacityTransition ----------------------------------------------------------- + class OpacityTransition : public LayerAnimationElement { public: OpacityTransition(float target, base::TimeDelta duration) @@ -134,10 +134,9 @@ class OpacityTransition : public LayerAnimationElement { virtual void OnAbort() OVERRIDE {} private: - static const AnimatableProperties& GetProperties() { - static AnimatableProperties properties; - if (properties.size() == 0) - properties.insert(LayerAnimationElement::OPACITY); + static AnimatableProperties GetProperties() { + AnimatableProperties properties; + properties.insert(LayerAnimationElement::OPACITY); return properties; } @@ -147,19 +146,60 @@ class OpacityTransition : public LayerAnimationElement { DISALLOW_COPY_AND_ASSIGN(OpacityTransition); }; +// VisibilityTransition -------------------------------------------------------- + +class VisibilityTransition : public LayerAnimationElement { + public: + VisibilityTransition(bool target, base::TimeDelta duration) + : LayerAnimationElement(GetProperties(), duration), + start_(false), + target_(target) { + } + virtual ~VisibilityTransition() {} + + protected: + virtual void OnStart(LayerAnimationDelegate* delegate) OVERRIDE { + start_ = delegate->GetVisibilityForAnimation(); + } + + virtual void OnProgress(double t, LayerAnimationDelegate* delegate) OVERRIDE { + delegate->SetVisibilityFromAnimation(t == 1.0 ? target_ : start_); + } + + virtual void OnGetTarget(TargetValue* target) const OVERRIDE { + target->visibility = target_; + } + + virtual void OnAbort() OVERRIDE {} + + private: + static AnimatableProperties GetProperties() { + AnimatableProperties properties; + properties.insert(LayerAnimationElement::VISIBILITY); + return properties; + } + + bool start_; + const bool target_; + + DISALLOW_COPY_AND_ASSIGN(VisibilityTransition); +}; + } // namespace // LayerAnimationElement::TargetValue ------------------------------------------ LayerAnimationElement::TargetValue::TargetValue() - : opacity(0.0f) { + : opacity(0.0f), + visibility(false) { } LayerAnimationElement::TargetValue::TargetValue( const LayerAnimationDelegate* delegate) : bounds(delegate ? delegate->GetBoundsForAnimation() : gfx::Rect()), transform(delegate ? delegate->GetTransformForAnimation() : Transform()), - opacity(delegate ? delegate->GetOpacityForAnimation() : 0.0f) { + opacity(delegate ? delegate->GetOpacityForAnimation() : 0.0f), + visibility(delegate ? delegate->GetVisibilityForAnimation() : false) { } // LayerAnimationElement ------------------------------------------------------- @@ -212,6 +252,12 @@ LayerAnimationElement* LayerAnimationElement::CreateOpacityElement( } // static +LayerAnimationElement* LayerAnimationElement::CreateVisibilityElement( + bool visibility, base::TimeDelta duration) { + return new VisibilityTransition(visibility, duration); +} + +// static LayerAnimationElement* LayerAnimationElement::CreatePauseElement( const AnimatableProperties& properties, base::TimeDelta duration) { return new Pause(properties, duration); diff --git a/ui/gfx/compositor/layer_animation_element.h b/ui/gfx/compositor/layer_animation_element.h index 2dabd90..2867dee 100644 --- a/ui/gfx/compositor/layer_animation_element.h +++ b/ui/gfx/compositor/layer_animation_element.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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. @@ -26,7 +26,8 @@ class COMPOSITOR_EXPORT LayerAnimationElement { enum AnimatableProperty { TRANSFORM = 0, BOUNDS, - OPACITY + OPACITY, + VISIBILITY }; struct COMPOSITOR_EXPORT TargetValue { @@ -37,6 +38,7 @@ class COMPOSITOR_EXPORT LayerAnimationElement { gfx::Rect bounds; Transform transform; float opacity; + bool visibility; }; typedef std::set<AnimatableProperty> AnimatableProperties; @@ -63,6 +65,12 @@ class COMPOSITOR_EXPORT LayerAnimationElement { float opacity, base::TimeDelta duration); + // Creates an element that sets visibily following a delay. The caller owns + // the return value. + static LayerAnimationElement* CreateVisibilityElement( + bool visibility, + base::TimeDelta duration); + // Creates an element that pauses the given properties. The caller owns the // return value. static LayerAnimationElement* CreatePauseElement( diff --git a/ui/gfx/compositor/layer_animation_element_unittest.cc b/ui/gfx/compositor/layer_animation_element_unittest.cc index 13d549c..d6287b0 100644 --- a/ui/gfx/compositor/layer_animation_element_unittest.cc +++ b/ui/gfx/compositor/layer_animation_element_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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. @@ -109,6 +109,33 @@ TEST(LayerAnimationElementTest, OpacityElement) { EXPECT_EQ(delta, element->duration()); } +// Check that the visibility element progresses the delegate as expected and +// that the element can be reused after it completes. +TEST(LayerAnimationElementTest, VisibilityElement) { + TestLayerAnimationDelegate delegate; + bool start = true; + bool target = false; + base::TimeDelta delta = base::TimeDelta::FromSeconds(1); + scoped_ptr<LayerAnimationElement> element( + LayerAnimationElement::CreateVisibilityElement(target, delta)); + + for (int i = 0; i < 2; ++i) { + delegate.SetVisibilityFromAnimation(start); + element->Progress(0.0, &delegate); + EXPECT_TRUE(delegate.GetVisibilityForAnimation()); + element->Progress(0.5, &delegate); + EXPECT_TRUE(delegate.GetVisibilityForAnimation()); + element->Progress(1.0, &delegate); + EXPECT_FALSE(delegate.GetVisibilityForAnimation()); + } + + LayerAnimationElement::TargetValue target_value(&delegate); + element->GetTargetValue(&target_value); + EXPECT_FALSE(target_value.visibility); + + EXPECT_EQ(delta, element->duration()); +} + // Check that the pause element progresses the delegate as expected and // that the element can be reused after it completes. TEST(LayerAnimationElementTest, PauseElement) { diff --git a/ui/gfx/compositor/layer_animator.cc b/ui/gfx/compositor/layer_animator.cc index a200881..9d6fa14 100644 --- a/ui/gfx/compositor/layer_animator.cc +++ b/ui/gfx/compositor/layer_animator.cc @@ -102,6 +102,21 @@ float LayerAnimator::GetTargetOpacity() const { return target.opacity; } +void LayerAnimator::SetVisibility(bool visibility) { + base::TimeDelta duration = transition_duration_; + if (disable_animations_for_test_) + duration = base::TimeDelta(); + StartAnimation(new LayerAnimationSequence( + LayerAnimationElement::CreateVisibilityElement( + visibility, duration))); +} + +bool LayerAnimator::GetTargetVisibility() const { + LayerAnimationElement::TargetValue target(delegate()); + GetTargetValue(&target); + return target.visibility; +} + void LayerAnimator::SetDelegate(LayerAnimationDelegate* delegate) { DCHECK(delegate); delegate_ = delegate; diff --git a/ui/gfx/compositor/layer_animator.h b/ui/gfx/compositor/layer_animator.h index 2231f57..f40786d 100644 --- a/ui/gfx/compositor/layer_animator.h +++ b/ui/gfx/compositor/layer_animator.h @@ -66,6 +66,10 @@ class COMPOSITOR_EXPORT LayerAnimator : public AnimationContainerElement { virtual void SetOpacity(float opacity); float GetTargetOpacity() const; + // Sets the visibility of the delegate. May cause an implicit animation. + virtual void SetVisibility(bool visibility); + bool GetTargetVisibility() const; + // Sets the layer animation delegate the animator is associated with. The // animator does not own the delegate. void SetDelegate(LayerAnimationDelegate* delegate); diff --git a/ui/gfx/compositor/test/test_layer_animation_delegate.cc b/ui/gfx/compositor/test/test_layer_animation_delegate.cc index 8b40830..c079b45 100644 --- a/ui/gfx/compositor/test/test_layer_animation_delegate.cc +++ b/ui/gfx/compositor/test/test_layer_animation_delegate.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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. @@ -6,14 +6,17 @@ namespace ui { -TestLayerAnimationDelegate::TestLayerAnimationDelegate() : opacity_(1.0f) { +TestLayerAnimationDelegate::TestLayerAnimationDelegate() + : opacity_(1.0f), + visibility_(true) { } TestLayerAnimationDelegate::TestLayerAnimationDelegate( const LayerAnimationDelegate& other) : bounds_(other.GetBoundsForAnimation()), transform_(other.GetTransformForAnimation()), - opacity_(other.GetOpacityForAnimation()) { + opacity_(other.GetOpacityForAnimation()), + visibility_(other.GetVisibilityForAnimation()) { } TestLayerAnimationDelegate::~TestLayerAnimationDelegate() { @@ -33,6 +36,10 @@ void TestLayerAnimationDelegate::SetOpacityFromAnimation(float opacity) { opacity_ = opacity; } +void TestLayerAnimationDelegate::SetVisibilityFromAnimation(bool visibility) { + visibility_ = visibility; +} + void TestLayerAnimationDelegate::ScheduleDrawForAnimation() { } @@ -48,4 +55,8 @@ float TestLayerAnimationDelegate::GetOpacityForAnimation() const { return opacity_; } +bool TestLayerAnimationDelegate::GetVisibilityForAnimation() const { + return visibility_; +} + } // namespace ui diff --git a/ui/gfx/compositor/test/test_layer_animation_delegate.h b/ui/gfx/compositor/test/test_layer_animation_delegate.h index 37d3c82..8625445 100644 --- a/ui/gfx/compositor/test/test_layer_animation_delegate.h +++ b/ui/gfx/compositor/test/test_layer_animation_delegate.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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. @@ -23,15 +23,18 @@ class TestLayerAnimationDelegate : public LayerAnimationDelegate { virtual void SetBoundsFromAnimation(const gfx::Rect& bounds) OVERRIDE; virtual void SetTransformFromAnimation(const Transform& transform) OVERRIDE; virtual void SetOpacityFromAnimation(float opacity) OVERRIDE; + virtual void SetVisibilityFromAnimation(bool visibility) OVERRIDE; virtual void ScheduleDrawForAnimation() OVERRIDE; virtual const gfx::Rect& GetBoundsForAnimation() const OVERRIDE; virtual const Transform& GetTransformForAnimation() const OVERRIDE; virtual float GetOpacityForAnimation() const OVERRIDE; + virtual bool GetVisibilityForAnimation() const OVERRIDE; private: gfx::Rect bounds_; Transform transform_; float opacity_; + bool visibility_; // Allow copy and assign. }; |