Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/animation/ink_drop_animation.h" | 5 #include "ui/views/animation/ink_drop_animation.h" |
| 6 | 6 |
| 7 #include <algorithm> | |
| 8 | |
| 7 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/logging.h" | |
| 11 #include "third_party/skia/include/core/SkColor.h" | |
| 12 #include "third_party/skia/include/core/SkPaint.h" | |
| 8 #include "ui/base/ui_base_switches.h" | 13 #include "ui/base/ui_base_switches.h" |
| 9 #include "ui/compositor/layer.h" | 14 #include "ui/compositor/layer.h" |
| 10 #include "ui/compositor/layer_animation_observer.h" | 15 #include "ui/compositor/layer_animation_observer.h" |
| 11 #include "ui/compositor/layer_animation_sequence.h" | 16 #include "ui/compositor/layer_animation_sequence.h" |
| 12 #include "ui/compositor/paint_recorder.h" | 17 #include "ui/compositor/paint_recorder.h" |
| 13 #include "ui/compositor/scoped_layer_animation_settings.h" | 18 #include "ui/compositor/scoped_layer_animation_settings.h" |
| 14 #include "ui/gfx/canvas.h" | 19 #include "ui/gfx/canvas.h" |
| 15 #include "ui/gfx/geometry/size.h" | 20 #include "ui/gfx/transform_util.h" |
| 16 #include "ui/views/animation/ink_drop_delegate.h" | |
| 17 #include "ui/views/view.h" | 21 #include "ui/views/view.h" |
| 18 | 22 |
| 19 namespace { | 23 namespace { |
| 20 | 24 |
| 21 // Animation constants | 25 // The minimum scale factor to use when scaling rectangle layers. Smaller values |
| 22 const float kMinimumScale = 0.1f; | 26 // were causing visual anomalies. |
| 23 const float kMinimumScaleCenteringOffset = 0.5f - kMinimumScale / 2.0f; | 27 const float kMinimumRectScale = 0.0001f; |
| 24 | 28 |
| 25 const int kHideAnimationDurationFastMs = 100; | 29 // The minimum scale factor to use when scaling circle layers. Smaller values |
| 26 const int kHideAnimationDurationSlowMs = 1000; | 30 // were causing visual anomalies. |
| 31 const float kMinimumCircleScale = 0.001f; | |
| 27 | 32 |
| 28 const int kShowInkDropAnimationDurationFastMs = 250; | 33 // The ink drop color. |
| 29 const int kShowInkDropAnimationDurationSlowMs = 750; | 34 const SkColor kInkDropColor = SK_ColorBLACK; |
| 30 | 35 |
| 31 const int kShowLongPressAnimationDurationFastMs = 250; | 36 // The opacity of the ink drop when it is visible. |
| 32 const int kShowLongPressAnimationDurationSlowMs = 2500; | 37 const float kVisibleOpacity = 0.12f; |
| 33 | 38 |
| 34 const int kRoundedRectCorners = 5; | 39 // The opacity of the ink drop when it is not visible. |
| 35 const int kCircleRadius = 30; | 40 const float kHiddenOpacity = 0.0f; |
| 36 | 41 |
| 37 const SkColor kInkDropColor = SK_ColorLTGRAY; | 42 // Durations for the different InkDropState animations in milliseconds. |
| 38 const SkColor kLongPressColor = SkColorSetRGB(182, 182, 182); | 43 const int kHiddenStateAnimationDurationMs = 1; |
| 44 const int kActionPendingStateAnimationDurationMs = 500; | |
| 45 const int kQuickActionStateAnimationDurationMs = 250; | |
| 46 const int kSlowActionPendingStateAnimationDurationMs = 500; | |
| 47 const int kSlowActionStateAnimationDurationMs = 250; | |
| 48 const int kActivatedStateAnimationDurationMs = 250; | |
| 49 const int kDeactivatedStateAnimationDurationMs = 250; | |
|
jonross
2015/08/21 15:14:49
I think you should still use an array. However hav
bruthig
2015/09/03 21:21:04
Discussed offline, will leave it as-is and see if
| |
| 39 | 50 |
| 40 // Checks CommandLine switches to determine if the visual feedback should be | 51 // A multiplicative factor used to slow down InkDropState animations. |
| 41 // circular. | 52 const int kSlowAnimationDurationFactor = 3; |
| 42 bool UseCircularFeedback() { | |
| 43 static bool circular = | |
| 44 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 45 (::switches::kMaterialDesignInkDrop)) != | |
| 46 ::switches::kMaterialDesignInkDropSquare; | |
| 47 return circular; | |
| 48 } | |
| 49 | 53 |
| 50 // Checks CommandLine switches to determine if the visual feedback should have | 54 // Checks CommandLine switches to determine if the visual feedback should have |
| 51 // a fast animations speed. | 55 // a fast animations speed. |
| 52 bool UseFastAnimations() { | 56 bool UseFastAnimations() { |
| 53 static bool fast = | 57 static bool fast = |
| 54 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 58 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 55 (::switches::kMaterialDesignInkDropAnimationSpeed)) != | 59 (::switches::kMaterialDesignInkDropAnimationSpeed)) != |
| 56 ::switches::kMaterialDesignInkDropAnimationSpeedSlow; | 60 ::switches::kMaterialDesignInkDropAnimationSpeedSlow; |
| 57 return fast; | 61 return fast; |
| 58 } | 62 } |
| 59 | 63 |
| 64 // Returns the InkDropState animation duration for the given |state|. | |
| 65 base::TimeDelta GetAnimationDuration(views::InkDropState state) { | |
| 66 int duration = 0; | |
| 67 switch (state) { | |
| 68 case views::InkDropState::HIDDEN: | |
| 69 duration = kHiddenStateAnimationDurationMs; | |
| 70 break; | |
| 71 case views::InkDropState::ACTION_PENDING: | |
| 72 duration = kActionPendingStateAnimationDurationMs; | |
| 73 break; | |
| 74 case views::InkDropState::QUICK_ACTION: | |
| 75 duration = kQuickActionStateAnimationDurationMs; | |
| 76 break; | |
| 77 case views::InkDropState::SLOW_ACTION_PENDING: | |
| 78 duration = kSlowActionPendingStateAnimationDurationMs; | |
| 79 break; | |
| 80 case views::InkDropState::SLOW_ACTION: | |
| 81 duration = kSlowActionStateAnimationDurationMs; | |
| 82 break; | |
| 83 case views::InkDropState::ACTIVATED: | |
| 84 duration = kActivatedStateAnimationDurationMs; | |
| 85 break; | |
| 86 case views::InkDropState::DEACTIVATED: | |
| 87 duration = kDeactivatedStateAnimationDurationMs; | |
| 88 break; | |
| 89 } | |
| 90 | |
| 91 return base::TimeDelta::FromMilliseconds( | |
| 92 (UseFastAnimations() ? 1 : kSlowAnimationDurationFactor) * duration); | |
| 93 } | |
| 94 | |
| 60 } // namespace | 95 } // namespace |
| 61 | 96 |
| 62 namespace views { | 97 namespace views { |
| 63 | 98 |
| 64 // An animation observer that should be set on animations of the provided | 99 // Base ui::LayerDelegate stub that can be extended to paint shapes of a |
| 65 // ui::Layer. Can be used to either start a hide animation, or to trigger one | 100 // specific color. |
| 66 // upon completion of the current animation. | 101 class BasePaintedLayerDelegate : public ui::LayerDelegate { |
| 67 // | |
| 68 // Sequential animations with PreemptionStrategy::ENQUEUE_NEW_ANIMATION cannot | |
| 69 // be used as the observed animation can complete before user input is received | |
| 70 // which determines if the hide animation should run. | |
| 71 class AppearAnimationObserver : public ui::LayerAnimationObserver { | |
| 72 public: | 102 public: |
| 73 // Will automatically start a hide animation of |layer| if |hide| is true. | 103 explicit BasePaintedLayerDelegate(SkColor color); |
| 74 // Otherwise StartHideAnimation() or HideNowIfDoneOrOnceCompleted() must be | 104 ~BasePaintedLayerDelegate() override; |
| 75 // called. | 105 |
| 76 AppearAnimationObserver(ui::Layer* layer, bool hide); | 106 SkColor color() const { return color_; } |
| 77 ~AppearAnimationObserver() override; | 107 |
| 78 | 108 // ui::LayerDelegate: |
| 79 // Returns true during both the appearing animation, and the hiding animation. | 109 void OnPaintLayer(const ui::PaintContext& context) override; |
| 80 bool IsAnimationActive(); | 110 void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip) override; |
| 81 | 111 void OnDeviceScaleFactorChanged(float device_scale_factor) override; |
| 82 // Starts a hide animation, preempting any current animations on |layer_|. | 112 base::Closure PrepareForLayerBoundsChange() override; |
| 83 void StartHideAnimation(); | |
| 84 | |
| 85 // Starts a hide animation if |layer_| is no longer animating. Otherwise the | |
| 86 // hide animation will be started once the current animation is completed. | |
| 87 void HideNowIfDoneOrOnceCompleted(); | |
| 88 | |
| 89 // Hides |background_layer| (without animation) after the current animation | |
| 90 // completes. | |
| 91 void SetBackgroundToHide(ui::Layer* background_layer); | |
| 92 | 113 |
| 93 private: | 114 private: |
| 94 // ui::ImplicitAnimationObserver: | 115 // The color to paint. |
| 95 void OnLayerAnimationEnded(ui::LayerAnimationSequence* sequence) override; | 116 SkColor color_; |
| 96 void OnLayerAnimationAborted(ui::LayerAnimationSequence* sequence) override; | 117 |
| 97 void OnLayerAnimationScheduled( | 118 DISALLOW_COPY_AND_ASSIGN(BasePaintedLayerDelegate); |
| 98 ui::LayerAnimationSequence* sequence) override {} | |
| 99 | |
| 100 bool RequiresNotificationWhenAnimatorDestroyed() const override; | |
| 101 | |
| 102 // The ui::Layer being observed, which hide animations will be set on. | |
| 103 ui::Layer* layer_; | |
| 104 | |
| 105 // Optional ui::Layer which will be hidden upon the completion of animating | |
| 106 // |layer_| | |
| 107 ui::Layer* background_layer_; | |
| 108 | |
| 109 // If true the hide animation will immediately be scheduled upon completion of | |
| 110 // the observed animation. | |
| 111 bool hide_; | |
| 112 | |
| 113 DISALLOW_COPY_AND_ASSIGN(AppearAnimationObserver); | |
| 114 }; | 119 }; |
| 115 | 120 |
| 116 AppearAnimationObserver::AppearAnimationObserver(ui::Layer* layer, bool hide) | 121 BasePaintedLayerDelegate::BasePaintedLayerDelegate(SkColor color) |
| 117 : layer_(layer), background_layer_(nullptr), hide_(hide) {} | 122 : color_(color) {} |
| 118 | 123 |
| 119 AppearAnimationObserver::~AppearAnimationObserver() { | 124 BasePaintedLayerDelegate::~BasePaintedLayerDelegate() {} |
| 120 StopObserving(); | 125 |
| 121 } | 126 void BasePaintedLayerDelegate::OnPaintLayer(const ui::PaintContext& context) {} |
| 122 | 127 |
| 123 bool AppearAnimationObserver::IsAnimationActive() { | 128 void BasePaintedLayerDelegate::OnDelegatedFrameDamage( |
| 124 // Initial animation ongoing | 129 const gfx::Rect& damage_rect_in_dip) {} |
| 125 if (!attached_sequences().empty()) | 130 |
| 126 return true; | 131 void BasePaintedLayerDelegate::OnDeviceScaleFactorChanged( |
| 127 // Maintain the animation until told to hide. | 132 float device_scale_factor) {} |
| 128 if (!hide_) | 133 |
| 129 return true; | 134 base::Closure BasePaintedLayerDelegate::PrepareForLayerBoundsChange() { |
| 130 | 135 return base::Closure(); |
| 131 // Check the state of the triggered hide animation | 136 } |
| 132 return layer_->GetAnimator()->IsAnimatingProperty( | 137 |
| 133 ui::LayerAnimationElement::OPACITY) && | 138 // A BasePaintedLayerDelegate that paints a circle of the specified color and |
| 134 layer_->GetTargetOpacity() == 0.0f && | 139 // radius. |
| 135 layer_->GetAnimator()->IsAnimatingProperty( | 140 class CircleLayerDelegate : public BasePaintedLayerDelegate { |
| 136 ui::LayerAnimationElement::VISIBILITY) && | 141 public: |
| 137 !layer_->GetTargetVisibility(); | 142 CircleLayerDelegate(SkColor color, int radius); |
| 138 } | 143 ~CircleLayerDelegate() override; |
| 139 | 144 |
| 140 void AppearAnimationObserver::StartHideAnimation() { | 145 int radius() const { return radius_; } |
| 141 if (background_layer_) | 146 |
| 142 background_layer_->SetVisible(false); | 147 // ui::LayerDelegate: |
| 143 if (!layer_->GetTargetVisibility()) | 148 void OnPaintLayer(const ui::PaintContext& context) override; |
| 149 | |
| 150 private: | |
| 151 // The radius of the circle. | |
| 152 int radius_; | |
| 153 | |
| 154 DISALLOW_COPY_AND_ASSIGN(CircleLayerDelegate); | |
| 155 }; | |
| 156 | |
| 157 CircleLayerDelegate::CircleLayerDelegate(SkColor color, int radius) | |
| 158 : BasePaintedLayerDelegate(color), radius_(radius) {} | |
| 159 | |
| 160 CircleLayerDelegate::~CircleLayerDelegate() {} | |
| 161 | |
| 162 void CircleLayerDelegate::OnPaintLayer(const ui::PaintContext& context) { | |
| 163 SkPaint paint; | |
| 164 paint.setColor(color()); | |
| 165 paint.setFlags(SkPaint::kAntiAlias_Flag); | |
| 166 paint.setStyle(SkPaint::kFill_Style); | |
| 167 | |
| 168 ui::PaintRecorder recorder(context, gfx::Size(radius_, radius_)); | |
| 169 gfx::Canvas* canvas = recorder.canvas(); | |
| 170 | |
| 171 gfx::Point center_point = gfx::Point(radius_, radius_); | |
| 172 canvas->DrawCircle(center_point, radius_, paint); | |
| 173 } | |
| 174 | |
| 175 // A BasePaintedLayerDelegate that paints a rectangle of the specified color and | |
| 176 // size. | |
| 177 class RectangleLayerDelegate : public BasePaintedLayerDelegate { | |
| 178 public: | |
| 179 RectangleLayerDelegate(SkColor color, gfx::Size size); | |
| 180 ~RectangleLayerDelegate() override; | |
| 181 | |
| 182 gfx::Size size() const { return size_; } | |
| 183 | |
| 184 // ui::LayerDelegate: | |
| 185 void OnPaintLayer(const ui::PaintContext& context) override; | |
| 186 | |
| 187 private: | |
| 188 // The size of the rectangle. | |
| 189 gfx::Size size_; | |
| 190 | |
| 191 DISALLOW_COPY_AND_ASSIGN(RectangleLayerDelegate); | |
| 192 }; | |
| 193 | |
| 194 RectangleLayerDelegate::RectangleLayerDelegate(SkColor color, gfx::Size size) | |
| 195 : BasePaintedLayerDelegate(color), size_(size) {} | |
| 196 | |
| 197 RectangleLayerDelegate::~RectangleLayerDelegate() {} | |
| 198 | |
| 199 void RectangleLayerDelegate::OnPaintLayer(const ui::PaintContext& context) { | |
| 200 SkPaint paint; | |
| 201 paint.setColor(color()); | |
| 202 paint.setFlags(SkPaint::kAntiAlias_Flag); | |
| 203 paint.setStyle(SkPaint::kFill_Style); | |
| 204 | |
| 205 ui::PaintRecorder recorder(context, size_); | |
| 206 gfx::Canvas* canvas = recorder.canvas(); | |
| 207 canvas->DrawRect(gfx::Rect(0, 0, size_.width(), size_.height()), paint); | |
| 208 } | |
| 209 | |
| 210 InkDropAnimation::InkDropAnimation(const gfx::Size& large_size, | |
| 211 int large_corner_radius, | |
| 212 const gfx::Size& small_size, | |
| 213 int small_corner_radius) | |
| 214 : large_size_(large_size), | |
| 215 large_corner_radius_(large_corner_radius), | |
| 216 small_size_(small_size), | |
| 217 small_corner_radius_(small_corner_radius), | |
| 218 circle_layer_delegate_(new CircleLayerDelegate( | |
| 219 kInkDropColor, | |
| 220 std::min(large_size_.width(), large_size_.height()) / 2)), | |
| 221 rect_layer_delegate_( | |
| 222 new RectangleLayerDelegate(kInkDropColor, large_size_)), | |
| 223 root_layer_(new ui::Layer(ui::LAYER_NOT_DRAWN)), | |
| 224 ink_drop_state_(InkDropState::HIDDEN) { | |
| 225 for (int i = 0; i < PAINTED_SHAPE_COUNT; ++i) | |
| 226 AddPaintLayer(static_cast<PaintedShape>(i)); | |
| 227 | |
| 228 root_layer_->SetMasksToBounds(false); | |
| 229 root_layer_->SetBounds(gfx::Rect(large_size_)); | |
| 230 | |
| 231 ResetTransformsToMinSize(); | |
| 232 | |
| 233 SetOpacity(kHiddenOpacity); | |
| 234 } | |
| 235 | |
| 236 InkDropAnimation::~InkDropAnimation() {} | |
| 237 | |
| 238 void InkDropAnimation::AnimateToState(InkDropState state) { | |
| 239 if (ink_drop_state_ == state) | |
| 144 return; | 240 return; |
| 145 | 241 |
| 146 ui::ScopedLayerAnimationSettings animation(layer_->GetAnimator()); | 242 if (ink_drop_state_ == InkDropState::HIDDEN && |
| 147 animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds( | 243 state != InkDropState::HIDDEN) { |
| 148 UseFastAnimations() ? kHideAnimationDurationFastMs | 244 ResetTransformsToMinSize(); |
| 149 : kHideAnimationDurationSlowMs)); | 245 SetOpacity(kVisibleOpacity); |
| 150 animation.SetPreemptionStrategy( | 246 } |
| 151 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | 247 |
| 152 layer_->SetOpacity(0.0f); | 248 InkDropTransforms transforms; |
| 153 layer_->SetVisible(false); | 249 |
| 154 } | |
| 155 | |
| 156 void AppearAnimationObserver::HideNowIfDoneOrOnceCompleted() { | |
| 157 hide_ = true; | |
| 158 if (attached_sequences().empty()) | |
| 159 StartHideAnimation(); | |
| 160 } | |
| 161 | |
| 162 void AppearAnimationObserver::SetBackgroundToHide(ui::Layer* background_layer) { | |
| 163 background_layer_ = background_layer; | |
| 164 } | |
| 165 | |
| 166 void AppearAnimationObserver::OnLayerAnimationEnded( | |
| 167 ui::LayerAnimationSequence* sequence) { | |
| 168 if (hide_) | |
| 169 StartHideAnimation(); | |
| 170 } | |
| 171 | |
| 172 void AppearAnimationObserver::OnLayerAnimationAborted( | |
| 173 ui::LayerAnimationSequence* sequence) { | |
| 174 if (hide_) | |
| 175 StartHideAnimation(); | |
| 176 } | |
| 177 | |
| 178 bool AppearAnimationObserver::RequiresNotificationWhenAnimatorDestroyed() | |
| 179 const { | |
| 180 // Ensures that OnImplicitAnimationsCompleted is called even if the observed | |
| 181 // animation is deleted. Allows for setting the proper state on |layer_|. | |
| 182 return true; | |
| 183 } | |
| 184 | |
| 185 InkDropAnimation::InkDropAnimation() | |
| 186 : root_layer_(new ui::Layer(ui::LAYER_NOT_DRAWN)), | |
| 187 ink_drop_layer_(new ui::Layer()), | |
| 188 appear_animation_observer_(nullptr), | |
| 189 long_press_layer_(new ui::Layer()), | |
| 190 long_press_animation_observer_(nullptr), | |
| 191 ink_drop_bounds_(0, 0, 0, 0) { | |
| 192 ink_drop_delegate_.reset(new InkDropDelegate(ink_drop_layer_.get(), | |
| 193 kInkDropColor, kCircleRadius, | |
| 194 kRoundedRectCorners)); | |
| 195 long_press_delegate_.reset(new InkDropDelegate(long_press_layer_.get(), | |
| 196 kLongPressColor, kCircleRadius, | |
| 197 kRoundedRectCorners)); | |
| 198 | |
| 199 SetupAnimationLayer(long_press_layer_.get(), long_press_delegate_.get()); | |
| 200 SetupAnimationLayer(ink_drop_layer_.get(), ink_drop_delegate_.get()); | |
| 201 | |
| 202 root_layer_->Add(ink_drop_layer_.get()); | |
| 203 root_layer_->Add(long_press_layer_.get()); | |
| 204 } | |
| 205 | |
| 206 InkDropAnimation::~InkDropAnimation() {} | |
| 207 | |
| 208 void InkDropAnimation::AnimateToState(InkDropState state) { | |
| 209 // TODO(bruthig): Do not transition if we are already in |state| and restrict | |
| 210 // any state transition that don't make sense or wouldn't look visually | |
| 211 // appealing. | |
| 212 switch (state) { | 250 switch (state) { |
| 213 case InkDropState::HIDDEN: | 251 case InkDropState::HIDDEN: |
| 214 AnimateHide(); | 252 ink_drop_state_ = state; |
| 253 GetCurrentTansforms(transforms); | |
| 254 AnimateToTransforms(transforms, kHiddenOpacity, | |
| 255 GetAnimationDuration(InkDropState::HIDDEN), | |
| 256 ui::LayerAnimator::ENQUEUE_NEW_ANIMATION); | |
| 215 break; | 257 break; |
| 216 case InkDropState::ACTION_PENDING: | 258 case InkDropState::ACTION_PENDING: |
| 217 AnimateTapDown(); | 259 ink_drop_state_ = state; |
| 260 CalculateCircleTransforms(large_size_, transforms); | |
| 261 AnimateToTransforms(transforms, kVisibleOpacity, | |
| 262 GetAnimationDuration(InkDropState::ACTION_PENDING), | |
| 263 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
| 218 break; | 264 break; |
| 219 case InkDropState::QUICK_ACTION: | 265 case InkDropState::QUICK_ACTION: |
| 220 AnimateTapDown(); | 266 ink_drop_state_ = state; |
| 221 AnimateHide(); | 267 CalculateCircleTransforms(large_size_, transforms); |
| 268 AnimateToTransforms(transforms, kHiddenOpacity, | |
| 269 GetAnimationDuration(InkDropState::QUICK_ACTION), | |
| 270 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
| 271 AnimateToState(InkDropState::HIDDEN); | |
| 272 break; | |
| 273 case InkDropState::SLOW_ACTION_PENDING: | |
| 274 ink_drop_state_ = state; | |
| 275 CalculateRectTransforms(small_size_, small_corner_radius_, transforms); | |
| 276 AnimateToTransforms( | |
| 277 transforms, kVisibleOpacity, | |
| 278 GetAnimationDuration(InkDropState::SLOW_ACTION_PENDING), | |
| 279 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
| 222 break; | 280 break; |
| 223 case InkDropState::SLOW_ACTION: | 281 case InkDropState::SLOW_ACTION: |
| 224 AnimateLongPress(); | 282 ink_drop_state_ = state; |
| 283 CalculateRectTransforms(large_size_, large_corner_radius_, transforms); | |
| 284 AnimateToTransforms(transforms, kHiddenOpacity, | |
| 285 GetAnimationDuration(InkDropState::SLOW_ACTION), | |
| 286 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
| 287 AnimateToState(InkDropState::HIDDEN); | |
| 225 break; | 288 break; |
| 226 case InkDropState::ACTIVATED: | 289 case InkDropState::ACTIVATED: |
| 227 AnimateLongPress(); | 290 ink_drop_state_ = state; |
| 291 CalculateRectTransforms(small_size_, small_corner_radius_, transforms); | |
| 292 AnimateToTransforms(transforms, kVisibleOpacity, | |
| 293 GetAnimationDuration(InkDropState::ACTIVATED), | |
| 294 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
| 295 break; | |
| 296 case InkDropState::DEACTIVATED: | |
| 297 ink_drop_state_ = state; | |
| 298 CalculateRectTransforms(large_size_, large_corner_radius_, transforms); | |
| 299 AnimateToTransforms(transforms, kHiddenOpacity, | |
| 300 GetAnimationDuration(InkDropState::DEACTIVATED), | |
| 301 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
| 302 AnimateToState(InkDropState::HIDDEN); | |
| 228 break; | 303 break; |
| 229 } | 304 } |
| 230 } | 305 } |
| 231 | 306 |
| 232 void InkDropAnimation::SetInkDropSize(const gfx::Size& size) { | 307 void InkDropAnimation::AnimateToTransforms( |
| 233 SetInkDropBounds(gfx::Rect(ink_drop_bounds_.origin(), size)); | 308 InkDropTransforms transforms, |
| 234 } | 309 float opacity, |
| 235 | 310 base::TimeDelta duration, |
| 236 gfx::Rect InkDropAnimation::GetInkDropBounds() const { | 311 ui::LayerAnimator::PreemptionStrategy preemption_strategy) { |
| 237 return ink_drop_bounds_; | 312 ui::LayerAnimator* root_animator = root_layer_->GetAnimator(); |
| 238 } | 313 ui::ScopedLayerAnimationSettings root_animation(root_animator); |
| 239 | 314 root_animation.SetPreemptionStrategy(preemption_strategy); |
| 240 void InkDropAnimation::SetInkDropBounds(const gfx::Rect& bounds) { | 315 ui::LayerAnimationElement* root_element = |
| 241 ink_drop_bounds_ = bounds; | 316 ui::LayerAnimationElement::CreateOpacityElement(opacity, duration); |
| 242 SetLayerBounds(ink_drop_layer_.get()); | 317 ui::LayerAnimationSequence* root_sequence = |
| 243 SetLayerBounds(long_press_layer_.get()); | 318 new ui::LayerAnimationSequence(root_element); |
| 244 } | 319 root_animator->StartAnimation(root_sequence); |
| 245 | 320 |
| 246 void InkDropAnimation::AnimateTapDown() { | 321 for (int i = 0; i < PAINTED_SHAPE_COUNT; ++i) { |
| 247 if ((appear_animation_observer_ && | 322 AnimateToTransform(painted_layers_[i].get(), transforms[i], duration, |
| 248 appear_animation_observer_->IsAnimationActive()) || | 323 preemption_strategy); |
| 249 (long_press_animation_observer_ && | |
| 250 long_press_animation_observer_->IsAnimationActive())) { | |
| 251 // Only one animation at a time. Subsequent tap downs are ignored until the | |
| 252 // current animation completes. | |
| 253 return; | |
| 254 } | 324 } |
| 255 appear_animation_observer_.reset( | 325 } |
| 256 new AppearAnimationObserver(ink_drop_layer_.get(), false)); | 326 |
| 257 AnimateShow(ink_drop_layer_.get(), appear_animation_observer_.get(), | 327 void InkDropAnimation::AnimateToTransform( |
| 258 base::TimeDelta::FromMilliseconds( | 328 ui::Layer* layer, |
| 259 (UseFastAnimations() ? kShowInkDropAnimationDurationFastMs | 329 const gfx::Transform& target_transform, |
| 260 : kShowInkDropAnimationDurationSlowMs))); | 330 base::TimeDelta duration, |
| 261 } | 331 ui::LayerAnimator::PreemptionStrategy preemption_strategy) { |
| 262 | |
| 263 void InkDropAnimation::AnimateHide() { | |
| 264 if (appear_animation_observer_ && | |
| 265 appear_animation_observer_->IsAnimationActive()) { | |
| 266 appear_animation_observer_->HideNowIfDoneOrOnceCompleted(); | |
| 267 } else if (long_press_animation_observer_) { | |
| 268 long_press_animation_observer_->HideNowIfDoneOrOnceCompleted(); | |
| 269 } | |
| 270 } | |
| 271 | |
| 272 void InkDropAnimation::AnimateLongPress() { | |
| 273 // Only one animation at a time. Subsequent long presses are ignored until the | |
| 274 // current animation completes. | |
| 275 if (long_press_animation_observer_ && | |
| 276 long_press_animation_observer_->IsAnimationActive()) { | |
| 277 return; | |
| 278 } | |
| 279 appear_animation_observer_.reset(); | |
| 280 long_press_animation_observer_.reset( | |
| 281 new AppearAnimationObserver(long_press_layer_.get(), false)); | |
| 282 long_press_animation_observer_->SetBackgroundToHide(ink_drop_layer_.get()); | |
| 283 AnimateShow(long_press_layer_.get(), long_press_animation_observer_.get(), | |
| 284 base::TimeDelta::FromMilliseconds( | |
| 285 UseFastAnimations() ? kShowLongPressAnimationDurationFastMs | |
| 286 : kShowLongPressAnimationDurationSlowMs)); | |
| 287 } | |
| 288 | |
| 289 void InkDropAnimation::AnimateShow(ui::Layer* layer, | |
| 290 AppearAnimationObserver* observer, | |
| 291 base::TimeDelta duration) { | |
| 292 layer->SetVisible(true); | |
| 293 layer->SetOpacity(1.0f); | |
| 294 | |
| 295 float start_x = ink_drop_bounds_.x() + | |
| 296 layer->bounds().width() * kMinimumScaleCenteringOffset; | |
| 297 float start_y = ink_drop_bounds_.y() + | |
| 298 layer->bounds().height() * kMinimumScaleCenteringOffset; | |
| 299 | |
| 300 gfx::Transform initial_transform; | |
| 301 initial_transform.Translate(start_x, start_y); | |
| 302 initial_transform.Scale(kMinimumScale, kMinimumScale); | |
| 303 layer->SetTransform(initial_transform); | |
| 304 | |
| 305 ui::LayerAnimator* animator = layer->GetAnimator(); | 332 ui::LayerAnimator* animator = layer->GetAnimator(); |
| 306 ui::ScopedLayerAnimationSettings animation(animator); | 333 ui::ScopedLayerAnimationSettings animation(animator); |
| 307 animation.SetPreemptionStrategy( | 334 animation.SetPreemptionStrategy(preemption_strategy); |
| 308 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
| 309 | |
| 310 gfx::Transform target_transform; | |
| 311 target_transform.Translate(ink_drop_bounds_.x(), ink_drop_bounds_.y()); | |
| 312 ui::LayerAnimationElement* element = | 335 ui::LayerAnimationElement* element = |
| 313 ui::LayerAnimationElement::CreateTransformElement(target_transform, | 336 ui::LayerAnimationElement::CreateTransformElement(target_transform, |
| 314 duration); | 337 duration); |
| 315 ui::LayerAnimationSequence* sequence = | 338 ui::LayerAnimationSequence* sequence = |
| 316 new ui::LayerAnimationSequence(element); | 339 new ui::LayerAnimationSequence(element); |
| 317 sequence->AddObserver(observer); | |
| 318 animator->StartAnimation(sequence); | 340 animator->StartAnimation(sequence); |
| 319 } | 341 } |
| 320 | 342 |
| 321 void InkDropAnimation::SetLayerBounds(ui::Layer* layer) { | 343 void InkDropAnimation::ResetTransformsToMinSize() { |
| 322 bool circle = UseCircularFeedback(); | 344 InkDropTransforms transforms; |
| 323 gfx::Size size = ink_drop_bounds_.size(); | 345 // Using a size of 0x0 creates visual anomalies. |
| 324 float circle_width = circle ? 2.0f * kCircleRadius : size.width(); | 346 CalculateCircleTransforms(gfx::Size(1, 1), transforms); |
| 325 float circle_height = circle ? 2.0f * kCircleRadius : size.height(); | 347 SetTransforms(transforms); |
| 326 float circle_x = circle ? (size.width() - circle_width) * 0.5f : 0; | 348 } |
| 327 float circle_y = circle ? (size.height() - circle_height) * 0.5f : 0; | 349 |
| 328 layer->SetBounds(gfx::Rect(circle_x, circle_y, circle_width, circle_height)); | 350 void InkDropAnimation::SetTransforms(InkDropTransforms transforms) { |
| 329 } | 351 for (int i = 0; i < PAINTED_SHAPE_COUNT; ++i) |
| 330 | 352 painted_layers_[i]->SetTransform(transforms[i]); |
| 331 void InkDropAnimation::SetupAnimationLayer(ui::Layer* layer, | 353 } |
| 332 InkDropDelegate* delegate) { | 354 |
| 355 void InkDropAnimation::SetOpacity(float opacity) { | |
| 356 root_layer_->SetOpacity(opacity); | |
| 357 } | |
| 358 | |
| 359 void InkDropAnimation::CalculateCircleTransforms( | |
| 360 const gfx::SizeF size, | |
| 361 InkDropTransforms transforms) const { | |
| 362 CalculateRectTransforms(size, std::min(size.width(), size.height()) / 2.0f, | |
| 363 transforms); | |
| 364 } | |
| 365 | |
| 366 void InkDropAnimation::CalculateRectTransforms( | |
| 367 const gfx::SizeF size, | |
| 368 float corner_radius, | |
| 369 InkDropTransforms transforms) const { | |
| 370 // TODO(bruthig): Add some documentation. | |
| 371 const float circle_radius = circle_layer_delegate_->radius(); | |
| 372 const float corner_scale = | |
| 373 std::max(kMinimumCircleScale, corner_radius / circle_radius); | |
| 374 const float corner_circle_x_offset = size.width() / 2.0f - corner_radius; | |
| 375 const float corner_circle_y_offset = size.height() / 2.0f - corner_radius; | |
| 376 | |
| 377 transforms[TOP_LEFT_CIRCLE].Translate(circle_radius - corner_circle_x_offset, | |
| 378 circle_radius - corner_circle_y_offset); | |
| 379 transforms[TOP_LEFT_CIRCLE].Scale(corner_scale, corner_scale); | |
| 380 transforms[TOP_LEFT_CIRCLE].Translate(-circle_radius, -circle_radius); | |
| 381 | |
| 382 transforms[TOP_RIGHT_CIRCLE].Translate( | |
| 383 circle_radius + corner_circle_x_offset, | |
| 384 circle_radius - corner_circle_y_offset); | |
| 385 transforms[TOP_RIGHT_CIRCLE].Scale(corner_scale, corner_scale); | |
| 386 transforms[TOP_RIGHT_CIRCLE].Translate(-circle_radius, -circle_radius); | |
| 387 | |
| 388 transforms[BOTTOM_RIGHT_CIRCLE].Translate( | |
| 389 circle_radius + corner_circle_x_offset, | |
| 390 circle_radius + corner_circle_y_offset); | |
| 391 transforms[BOTTOM_RIGHT_CIRCLE].Scale(corner_scale, corner_scale); | |
| 392 transforms[BOTTOM_RIGHT_CIRCLE].Translate(-circle_radius, -circle_radius); | |
| 393 | |
| 394 transforms[BOTTOM_LEFT_CIRCLE].Translate( | |
| 395 circle_radius - corner_circle_x_offset, | |
| 396 circle_radius + corner_circle_y_offset); | |
| 397 transforms[BOTTOM_LEFT_CIRCLE].Scale(corner_scale, corner_scale); | |
| 398 transforms[BOTTOM_LEFT_CIRCLE].Translate(-circle_radius, -circle_radius); | |
| 399 | |
| 400 const float rect_delegate_width = | |
| 401 static_cast<float>(rect_layer_delegate_->size().width()); | |
| 402 const float rect_delegate_height = | |
| 403 static_cast<float>(rect_layer_delegate_->size().height()); | |
| 404 | |
| 405 const float horizontal_rect_x_scale = | |
| 406 std::max(kMinimumRectScale, size.width() / rect_delegate_width); | |
| 407 const float horizontal_rect_y_scale = | |
| 408 std::max(kMinimumRectScale, | |
| 409 (size.height() - 2.0f * corner_radius) / rect_delegate_height); | |
| 410 | |
| 411 transforms[HORIZONTAL_RECT].Scale(horizontal_rect_x_scale, | |
| 412 horizontal_rect_y_scale); | |
| 413 transforms[HORIZONTAL_RECT] = TransformAboutPivot( | |
| 414 painted_layers_[HORIZONTAL_RECT]->bounds().CenterPoint(), | |
| 415 transforms[HORIZONTAL_RECT]); | |
| 416 | |
| 417 const float vertical_rect_x_scale = | |
| 418 std::max(kMinimumRectScale, | |
| 419 (size.width() - 2.0f * corner_radius) / rect_delegate_width); | |
| 420 const float vertical_rect_y_scale = | |
| 421 std::max(kMinimumRectScale, size.height() / rect_delegate_height); | |
| 422 | |
| 423 transforms[VERTICAL_RECT].Scale(vertical_rect_x_scale, vertical_rect_y_scale); | |
| 424 transforms[VERTICAL_RECT] = TransformAboutPivot( | |
| 425 painted_layers_[VERTICAL_RECT]->bounds().CenterPoint(), | |
| 426 transforms[VERTICAL_RECT]); | |
| 427 } | |
| 428 | |
| 429 void InkDropAnimation::GetCurrentTansforms(InkDropTransforms transforms) const { | |
| 430 for (int i = 0; i < PAINTED_SHAPE_COUNT; ++i) | |
| 431 transforms[i] = painted_layers_[i]->GetTargetTransform(); | |
| 432 } | |
| 433 | |
| 434 void InkDropAnimation::SetOrigin(const gfx::Point& origin) { | |
| 435 gfx::Rect bounds = root_layer_->bounds(); | |
| 436 bounds.set_origin(origin); | |
| 437 root_layer_->SetBounds(bounds); | |
| 438 } | |
| 439 | |
| 440 void InkDropAnimation::AddPaintLayer(PaintedShape painted_shape) { | |
| 441 ui::LayerDelegate* delegate = nullptr; | |
| 442 switch (painted_shape) { | |
| 443 case TOP_LEFT_CIRCLE: | |
| 444 case TOP_RIGHT_CIRCLE: | |
| 445 case BOTTOM_RIGHT_CIRCLE: | |
| 446 case BOTTOM_LEFT_CIRCLE: | |
| 447 delegate = circle_layer_delegate_.get(); | |
| 448 break; | |
| 449 case HORIZONTAL_RECT: | |
| 450 case VERTICAL_RECT: | |
| 451 delegate = rect_layer_delegate_.get(); | |
| 452 break; | |
| 453 case PAINTED_SHAPE_COUNT: | |
| 454 NOTREACHED() << "PAINTED_SHAPE_COUNT is not an actual shape type."; | |
| 455 break; | |
| 456 } | |
| 457 | |
| 458 ui::Layer* layer = new ui::Layer(); | |
| 459 root_layer_->Add(layer); | |
| 460 | |
| 461 layer->SetBounds(gfx::Rect(large_size_)); | |
| 333 layer->SetFillsBoundsOpaquely(false); | 462 layer->SetFillsBoundsOpaquely(false); |
| 334 layer->set_delegate(delegate); | 463 layer->set_delegate(delegate); |
| 335 layer->SetVisible(false); | 464 layer->SetVisible(true); |
| 336 layer->SetBounds(gfx::Rect()); | 465 layer->SetOpacity(1.0); |
| 337 delegate->set_should_render_circle(UseCircularFeedback()); | 466 layer->SetMasksToBounds(false); |
| 467 | |
| 468 painted_layers_[painted_shape].reset(layer); | |
| 338 } | 469 } |
| 339 | 470 |
| 340 } // namespace views | 471 } // namespace views |
| OLD | NEW |