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; | |
| 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 | |
| 95 // Calculates a Transform for a circle layer. The transform will be set up to | |
| 96 // translate the |drawn_center_point| to the origin, scale, and then translate | |
| 97 // to the target point defined by |target_center_x| and |target_center_y|. | |
| 98 gfx::Transform CalculateCircleTransform(const gfx::Point& drawn_center_point, | |
| 99 float scale, | |
| 100 float target_center_x, | |
| 101 float target_center_y) { | |
| 102 gfx::Transform transform; | |
| 103 transform.Translate(target_center_x, target_center_y); | |
| 104 transform.Scale(scale, scale); | |
| 105 transform.Translate(-drawn_center_point.x(), -drawn_center_point.y()); | |
| 106 return transform; | |
| 107 } | |
| 108 | |
| 109 // Calculates a Transform for a rectangle layer. The transform will be set up to | |
| 110 // translate the |drawn_center_point| to the origin and then scale by the | |
| 111 // |x_scale| and |y_scale| factors. | |
| 112 gfx::Transform CalculateRectTransform(const gfx::Point& drawn_center_point, | |
| 113 float x_scale, | |
| 114 float y_scale) { | |
| 115 gfx::Transform transform; | |
| 116 transform.Scale(x_scale, y_scale); | |
| 117 transform.Translate(-drawn_center_point.x(), -drawn_center_point.y()); | |
| 118 return transform; | |
| 119 } | |
| 120 | |
| 60 } // namespace | 121 } // namespace |
| 61 | 122 |
| 62 namespace views { | 123 namespace views { |
| 63 | 124 |
| 64 // An animation observer that should be set on animations of the provided | 125 // 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 | 126 // specific color. |
| 66 // upon completion of the current animation. | 127 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: | 128 public: |
| 73 // Will automatically start a hide animation of |layer| if |hide| is true. | 129 ~BasePaintedLayerDelegate() override; |
| 74 // Otherwise StartHideAnimation() or HideNowIfDoneOrOnceCompleted() must be | 130 |
| 75 // called. | 131 SkColor color() const { return color_; } |
| 76 AppearAnimationObserver(ui::Layer* layer, bool hide); | 132 |
| 77 ~AppearAnimationObserver() override; | 133 // ui::LayerDelegate: |
| 78 | 134 void OnPaintLayer(const ui::PaintContext& context) override; |
|
sadrul
2015/09/14 20:05:40
Should this be left as pure virtual?
bruthig
2015/09/15 19:47:09
Done.
| |
| 79 // Returns true during both the appearing animation, and the hiding animation. | 135 void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip) override; |
| 80 bool IsAnimationActive(); | 136 void OnDeviceScaleFactorChanged(float device_scale_factor) override; |
| 81 | 137 base::Closure PrepareForLayerBoundsChange() override; |
| 82 // Starts a hide animation, preempting any current animations on |layer_|. | 138 |
| 83 void StartHideAnimation(); | 139 protected: |
| 84 | 140 explicit BasePaintedLayerDelegate(SkColor color); |
| 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 | 141 |
| 93 private: | 142 private: |
| 94 // ui::ImplicitAnimationObserver: | 143 // The color to paint. |
| 95 void OnLayerAnimationEnded(ui::LayerAnimationSequence* sequence) override; | 144 SkColor color_; |
| 96 void OnLayerAnimationAborted(ui::LayerAnimationSequence* sequence) override; | 145 |
| 97 void OnLayerAnimationScheduled( | 146 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 }; | 147 }; |
| 115 | 148 |
| 116 AppearAnimationObserver::AppearAnimationObserver(ui::Layer* layer, bool hide) | 149 BasePaintedLayerDelegate::BasePaintedLayerDelegate(SkColor color) |
| 117 : layer_(layer), background_layer_(nullptr), hide_(hide) {} | 150 : color_(color) {} |
| 118 | 151 |
| 119 AppearAnimationObserver::~AppearAnimationObserver() { | 152 BasePaintedLayerDelegate::~BasePaintedLayerDelegate() {} |
| 120 StopObserving(); | 153 |
| 121 } | 154 void BasePaintedLayerDelegate::OnPaintLayer(const ui::PaintContext& context) {} |
| 122 | 155 |
| 123 bool AppearAnimationObserver::IsAnimationActive() { | 156 void BasePaintedLayerDelegate::OnDelegatedFrameDamage( |
| 124 // Initial animation ongoing | 157 const gfx::Rect& damage_rect_in_dip) {} |
| 125 if (!attached_sequences().empty()) | 158 |
| 126 return true; | 159 void BasePaintedLayerDelegate::OnDeviceScaleFactorChanged( |
| 127 // Maintain the animation until told to hide. | 160 float device_scale_factor) {} |
| 128 if (!hide_) | 161 |
| 129 return true; | 162 base::Closure BasePaintedLayerDelegate::PrepareForLayerBoundsChange() { |
| 130 | 163 return base::Closure(); |
| 131 // Check the state of the triggered hide animation | 164 } |
| 132 return layer_->GetAnimator()->IsAnimatingProperty( | 165 |
| 133 ui::LayerAnimationElement::OPACITY) && | 166 // A BasePaintedLayerDelegate that paints a circle of a specified color and |
| 134 layer_->GetTargetOpacity() == 0.0f && | 167 // radius. |
| 135 layer_->GetAnimator()->IsAnimatingProperty( | 168 class CircleLayerDelegate : public BasePaintedLayerDelegate { |
| 136 ui::LayerAnimationElement::VISIBILITY) && | 169 public: |
| 137 !layer_->GetTargetVisibility(); | 170 CircleLayerDelegate(SkColor color, int radius); |
| 138 } | 171 ~CircleLayerDelegate() override; |
| 139 | 172 |
| 140 void AppearAnimationObserver::StartHideAnimation() { | 173 int radius() const { return radius_; } |
| 141 if (background_layer_) | 174 |
| 142 background_layer_->SetVisible(false); | 175 // ui::LayerDelegate: |
| 143 if (!layer_->GetTargetVisibility()) | 176 void OnPaintLayer(const ui::PaintContext& context) override; |
| 177 | |
| 178 private: | |
| 179 // The radius of the circle. | |
| 180 int radius_; | |
| 181 | |
| 182 DISALLOW_COPY_AND_ASSIGN(CircleLayerDelegate); | |
| 183 }; | |
| 184 | |
| 185 CircleLayerDelegate::CircleLayerDelegate(SkColor color, int radius) | |
| 186 : BasePaintedLayerDelegate(color), radius_(radius) {} | |
| 187 | |
| 188 CircleLayerDelegate::~CircleLayerDelegate() {} | |
| 189 | |
| 190 void CircleLayerDelegate::OnPaintLayer(const ui::PaintContext& context) { | |
| 191 SkPaint paint; | |
| 192 paint.setColor(color()); | |
| 193 paint.setFlags(SkPaint::kAntiAlias_Flag); | |
| 194 paint.setStyle(SkPaint::kFill_Style); | |
| 195 | |
| 196 ui::PaintRecorder recorder(context, gfx::Size(radius_, radius_)); | |
| 197 gfx::Canvas* canvas = recorder.canvas(); | |
| 198 | |
| 199 gfx::Point center_point = gfx::Point(radius_, radius_); | |
| 200 canvas->DrawCircle(center_point, radius_, paint); | |
| 201 } | |
| 202 | |
| 203 // A BasePaintedLayerDelegate that paints a rectangle of a specified color and | |
| 204 // size. | |
| 205 class RectangleLayerDelegate : public BasePaintedLayerDelegate { | |
| 206 public: | |
| 207 RectangleLayerDelegate(SkColor color, gfx::Size size); | |
| 208 ~RectangleLayerDelegate() override; | |
| 209 | |
| 210 gfx::Size size() const { return size_; } | |
|
sadrul
2015/09/14 20:05:40
const gfx::Size& size()
bruthig
2015/09/15 19:47:09
Done.
| |
| 211 | |
| 212 // ui::LayerDelegate: | |
| 213 void OnPaintLayer(const ui::PaintContext& context) override; | |
| 214 | |
| 215 private: | |
| 216 // The size of the rectangle. | |
| 217 gfx::Size size_; | |
| 218 | |
| 219 DISALLOW_COPY_AND_ASSIGN(RectangleLayerDelegate); | |
| 220 }; | |
| 221 | |
| 222 RectangleLayerDelegate::RectangleLayerDelegate(SkColor color, gfx::Size size) | |
| 223 : BasePaintedLayerDelegate(color), size_(size) {} | |
| 224 | |
| 225 RectangleLayerDelegate::~RectangleLayerDelegate() {} | |
| 226 | |
| 227 void RectangleLayerDelegate::OnPaintLayer(const ui::PaintContext& context) { | |
| 228 SkPaint paint; | |
| 229 paint.setColor(color()); | |
| 230 paint.setFlags(SkPaint::kAntiAlias_Flag); | |
| 231 paint.setStyle(SkPaint::kFill_Style); | |
| 232 | |
| 233 ui::PaintRecorder recorder(context, size_); | |
| 234 gfx::Canvas* canvas = recorder.canvas(); | |
| 235 canvas->DrawRect(gfx::Rect(0, 0, size_.width(), size_.height()), paint); | |
|
sadrul
2015/09/14 20:05:40
gfx::Rect(size_)
bruthig
2015/09/15 19:47:09
Done.
| |
| 236 } | |
| 237 | |
| 238 InkDropAnimation::InkDropAnimation(const gfx::Size& large_size, | |
| 239 int large_corner_radius, | |
| 240 const gfx::Size& small_size, | |
| 241 int small_corner_radius) | |
| 242 : large_size_(large_size), | |
| 243 large_corner_radius_(large_corner_radius), | |
| 244 small_size_(small_size), | |
| 245 small_corner_radius_(small_corner_radius), | |
| 246 circle_layer_delegate_(new CircleLayerDelegate( | |
| 247 kInkDropColor, | |
| 248 std::min(large_size_.width(), large_size_.height()) / 2)), | |
| 249 rect_layer_delegate_( | |
| 250 new RectangleLayerDelegate(kInkDropColor, large_size_)), | |
| 251 root_layer_(new ui::Layer(ui::LAYER_NOT_DRAWN)), | |
| 252 ink_drop_state_(InkDropState::HIDDEN) { | |
| 253 for (int i = 0; i < PAINTED_SHAPE_COUNT; ++i) | |
| 254 AddPaintLayer(static_cast<PaintedShape>(i)); | |
| 255 | |
| 256 root_layer_->SetMasksToBounds(false); | |
| 257 root_layer_->SetBounds(gfx::Rect(large_size_)); | |
| 258 | |
| 259 ResetTransformsToMinSize(); | |
| 260 | |
| 261 SetOpacity(kHiddenOpacity); | |
| 262 } | |
| 263 | |
| 264 InkDropAnimation::~InkDropAnimation() {} | |
| 265 | |
| 266 void InkDropAnimation::AnimateToState(InkDropState ink_drop_state) { | |
| 267 if (ink_drop_state_ == ink_drop_state) | |
| 144 return; | 268 return; |
| 145 | 269 |
| 146 ui::ScopedLayerAnimationSettings animation(layer_->GetAnimator()); | 270 if (ink_drop_state_ == InkDropState::HIDDEN) { |
| 147 animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds( | 271 ResetTransformsToMinSize(); |
| 148 UseFastAnimations() ? kHideAnimationDurationFastMs | 272 SetOpacity(kVisibleOpacity); |
| 149 : kHideAnimationDurationSlowMs)); | 273 } |
| 150 animation.SetPreemptionStrategy( | 274 |
| 151 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | 275 InkDropTransforms transforms; |
| 152 layer_->SetOpacity(0.0f); | 276 |
| 153 layer_->SetVisible(false); | 277 // Must set the |ink_drop_state_| before handling the state change because |
| 154 } | 278 // some state changes make recursive calls to AnimateToState() and the last |
| 155 | 279 // call should 'win'. |
| 156 void AppearAnimationObserver::HideNowIfDoneOrOnceCompleted() { | 280 ink_drop_state_ = ink_drop_state; |
| 157 hide_ = true; | 281 |
| 158 if (attached_sequences().empty()) | 282 switch (ink_drop_state_) { |
| 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) { | |
| 213 case InkDropState::HIDDEN: | 283 case InkDropState::HIDDEN: |
| 214 AnimateHide(); | 284 GetCurrentTansforms(transforms); |
| 285 AnimateToTransforms(transforms, kHiddenOpacity, | |
| 286 GetAnimationDuration(InkDropState::HIDDEN), | |
| 287 ui::LayerAnimator::ENQUEUE_NEW_ANIMATION); | |
| 215 break; | 288 break; |
| 216 case InkDropState::ACTION_PENDING: | 289 case InkDropState::ACTION_PENDING: |
| 217 AnimateTapDown(); | 290 CalculateCircleTransforms(large_size_, transforms); |
| 291 AnimateToTransforms(transforms, kVisibleOpacity, | |
| 292 GetAnimationDuration(InkDropState::ACTION_PENDING), | |
| 293 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
| 218 break; | 294 break; |
| 219 case InkDropState::QUICK_ACTION: | 295 case InkDropState::QUICK_ACTION: |
| 220 AnimateTapDown(); | 296 CalculateCircleTransforms(large_size_, transforms); |
| 221 AnimateHide(); | 297 AnimateToTransforms(transforms, kHiddenOpacity, |
| 298 GetAnimationDuration(InkDropState::QUICK_ACTION), | |
| 299 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
| 300 AnimateToState(InkDropState::HIDDEN); | |
| 301 break; | |
| 302 case InkDropState::SLOW_ACTION_PENDING: | |
| 303 CalculateRectTransforms(small_size_, small_corner_radius_, transforms); | |
| 304 AnimateToTransforms( | |
| 305 transforms, kVisibleOpacity, | |
| 306 GetAnimationDuration(InkDropState::SLOW_ACTION_PENDING), | |
| 307 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
| 222 break; | 308 break; |
| 223 case InkDropState::SLOW_ACTION: | 309 case InkDropState::SLOW_ACTION: |
| 224 AnimateLongPress(); | 310 CalculateRectTransforms(large_size_, large_corner_radius_, transforms); |
| 311 AnimateToTransforms(transforms, kHiddenOpacity, | |
| 312 GetAnimationDuration(InkDropState::SLOW_ACTION), | |
| 313 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
| 314 AnimateToState(InkDropState::HIDDEN); | |
| 225 break; | 315 break; |
| 226 case InkDropState::ACTIVATED: | 316 case InkDropState::ACTIVATED: |
| 227 AnimateLongPress(); | 317 CalculateRectTransforms(small_size_, small_corner_radius_, transforms); |
| 318 AnimateToTransforms(transforms, kVisibleOpacity, | |
| 319 GetAnimationDuration(InkDropState::ACTIVATED), | |
| 320 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
| 321 break; | |
| 322 case InkDropState::DEACTIVATED: | |
| 323 CalculateRectTransforms(large_size_, large_corner_radius_, transforms); | |
| 324 AnimateToTransforms(transforms, kHiddenOpacity, | |
| 325 GetAnimationDuration(InkDropState::DEACTIVATED), | |
| 326 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
| 327 AnimateToState(InkDropState::HIDDEN); | |
| 228 break; | 328 break; |
| 229 } | 329 } |
| 230 } | 330 } |
| 231 | 331 |
| 232 void InkDropAnimation::SetInkDropSize(const gfx::Size& size) { | 332 void InkDropAnimation::AnimateToTransforms( |
| 233 SetInkDropBounds(gfx::Rect(ink_drop_bounds_.origin(), size)); | 333 InkDropTransforms transforms, |
| 234 } | 334 float opacity, |
| 235 | 335 base::TimeDelta duration, |
| 236 gfx::Rect InkDropAnimation::GetInkDropBounds() const { | 336 ui::LayerAnimator::PreemptionStrategy preemption_strategy) { |
| 237 return ink_drop_bounds_; | 337 ui::LayerAnimator* root_animator = root_layer_->GetAnimator(); |
| 238 } | 338 ui::ScopedLayerAnimationSettings root_animation(root_animator); |
| 239 | 339 root_animation.SetPreemptionStrategy(preemption_strategy); |
| 240 void InkDropAnimation::SetInkDropBounds(const gfx::Rect& bounds) { | 340 ui::LayerAnimationElement* root_element = |
| 241 ink_drop_bounds_ = bounds; | 341 ui::LayerAnimationElement::CreateOpacityElement(opacity, duration); |
| 242 SetLayerBounds(ink_drop_layer_.get()); | 342 ui::LayerAnimationSequence* root_sequence = |
| 243 SetLayerBounds(long_press_layer_.get()); | 343 new ui::LayerAnimationSequence(root_element); |
| 244 } | 344 root_animator->StartAnimation(root_sequence); |
| 245 | 345 |
| 246 void InkDropAnimation::AnimateTapDown() { | 346 for (int i = 0; i < PAINTED_SHAPE_COUNT; ++i) { |
| 247 if ((appear_animation_observer_ && | 347 ui::LayerAnimator* animator = painted_layers_[i]->GetAnimator(); |
| 248 appear_animation_observer_->IsAnimationActive()) || | 348 ui::ScopedLayerAnimationSettings animation(animator); |
| 249 (long_press_animation_observer_ && | 349 animation.SetPreemptionStrategy(preemption_strategy); |
| 250 long_press_animation_observer_->IsAnimationActive())) { | 350 ui::LayerAnimationElement* element = |
| 251 // Only one animation at a time. Subsequent tap downs are ignored until the | 351 ui::LayerAnimationElement::CreateTransformElement(transforms[i], |
| 252 // current animation completes. | 352 duration); |
| 253 return; | 353 ui::LayerAnimationSequence* sequence = |
| 354 new ui::LayerAnimationSequence(element); | |
| 355 animator->StartAnimation(sequence); | |
| 254 } | 356 } |
| 255 appear_animation_observer_.reset( | 357 } |
| 256 new AppearAnimationObserver(ink_drop_layer_.get(), false)); | 358 |
| 257 AnimateShow(ink_drop_layer_.get(), appear_animation_observer_.get(), | 359 void InkDropAnimation::ResetTransformsToMinSize() { |
| 258 base::TimeDelta::FromMilliseconds( | 360 InkDropTransforms transforms; |
| 259 (UseFastAnimations() ? kShowInkDropAnimationDurationFastMs | 361 // Using a size of 0x0 creates visual anomalies. |
| 260 : kShowInkDropAnimationDurationSlowMs))); | 362 CalculateCircleTransforms(gfx::Size(1, 1), transforms); |
|
sadrul
2015/09/14 20:05:40
Explain why size(1,1) is used here (instead of, sa
bruthig
2015/09/15 19:47:09
Discussed offline. This should make more sense on
| |
| 261 } | 363 SetTransforms(transforms); |
| 262 | 364 } |
| 263 void InkDropAnimation::AnimateHide() { | 365 |
| 264 if (appear_animation_observer_ && | 366 void InkDropAnimation::SetTransforms(InkDropTransforms transforms) { |
| 265 appear_animation_observer_->IsAnimationActive()) { | 367 for (int i = 0; i < PAINTED_SHAPE_COUNT; ++i) |
| 266 appear_animation_observer_->HideNowIfDoneOrOnceCompleted(); | 368 painted_layers_[i]->SetTransform(transforms[i]); |
|
sadrul
2015/09/14 20:05:40
Can you clarify why you need to set transforms on
bruthig
2015/09/15 19:47:09
(I'm assuming this is not a request for added docu
| |
| 267 } else if (long_press_animation_observer_) { | 369 } |
| 268 long_press_animation_observer_->HideNowIfDoneOrOnceCompleted(); | 370 |
| 371 void InkDropAnimation::SetOpacity(float opacity) { | |
| 372 root_layer_->SetOpacity(opacity); | |
| 373 } | |
| 374 | |
| 375 void InkDropAnimation::CalculateCircleTransforms( | |
| 376 const gfx::SizeF size, | |
| 377 InkDropTransforms transforms) const { | |
| 378 CalculateRectTransforms(size, std::min(size.width(), size.height()) / 2.0f, | |
| 379 transforms); | |
| 380 } | |
| 381 | |
| 382 void InkDropAnimation::CalculateRectTransforms( | |
| 383 const gfx::SizeF size, | |
| 384 float corner_radius, | |
| 385 InkDropTransforms transforms) const { | |
| 386 CHECK_GE(size.width() / 2.0f, corner_radius) | |
| 387 << "The circle's diameter should not be greater than the total width."; | |
| 388 CHECK_GE(size.height() / 2.0f, corner_radius) | |
| 389 << "The circle's diameter should not be greater than the total height."; | |
|
sadrul
2015/09/14 20:05:40
DCHECK*
bruthig
2015/09/15 19:47:09
Done.
| |
| 390 | |
| 391 // The shapes are drawn such that their center points are not at the origin. | |
| 392 // Thus we use the CalculateCircleTransform() and CalculateRectTransform() | |
| 393 // methods to calculate the complex Transforms. | |
| 394 | |
| 395 const float circle_scale = std::max( | |
| 396 kMinimumCircleScale, | |
| 397 corner_radius / static_cast<float>(circle_layer_delegate_->radius())); | |
| 398 | |
| 399 const float circle_target_x_offset = size.width() / 2.0f - corner_radius; | |
| 400 const float circle_target_y_offset = size.height() / 2.0f - corner_radius; | |
| 401 | |
| 402 transforms[TOP_LEFT_CIRCLE] = CalculateCircleTransform( | |
| 403 painted_layers_[TOP_LEFT_CIRCLE]->bounds().CenterPoint(), circle_scale, | |
| 404 -circle_target_x_offset, -circle_target_y_offset); | |
| 405 | |
| 406 transforms[TOP_RIGHT_CIRCLE] = CalculateCircleTransform( | |
| 407 painted_layers_[TOP_RIGHT_CIRCLE]->bounds().CenterPoint(), circle_scale, | |
| 408 circle_target_x_offset, -circle_target_y_offset); | |
| 409 | |
| 410 transforms[BOTTOM_RIGHT_CIRCLE] = CalculateCircleTransform( | |
| 411 painted_layers_[BOTTOM_RIGHT_CIRCLE]->bounds().CenterPoint(), | |
| 412 circle_scale, circle_target_x_offset, circle_target_y_offset); | |
| 413 | |
| 414 transforms[BOTTOM_LEFT_CIRCLE] = CalculateCircleTransform( | |
| 415 painted_layers_[BOTTOM_LEFT_CIRCLE]->bounds().CenterPoint(), circle_scale, | |
| 416 -circle_target_x_offset, circle_target_y_offset); | |
| 417 | |
| 418 const float rect_delegate_width = | |
| 419 static_cast<float>(rect_layer_delegate_->size().width()); | |
| 420 const float rect_delegate_height = | |
| 421 static_cast<float>(rect_layer_delegate_->size().height()); | |
| 422 | |
| 423 transforms[HORIZONTAL_RECT] = CalculateRectTransform( | |
| 424 painted_layers_[HORIZONTAL_RECT]->bounds().CenterPoint(), | |
| 425 std::max(kMinimumRectScale, size.width() / rect_delegate_width), | |
| 426 std::max(kMinimumRectScale, | |
| 427 (size.height() - 2.0f * corner_radius) / rect_delegate_height)); | |
| 428 | |
| 429 transforms[VERTICAL_RECT] = CalculateRectTransform( | |
| 430 painted_layers_[VERTICAL_RECT]->bounds().CenterPoint(), | |
| 431 std::max(kMinimumRectScale, | |
| 432 (size.width() - 2.0f * corner_radius) / rect_delegate_width), | |
| 433 std::max(kMinimumRectScale, size.height() / rect_delegate_height)); | |
| 434 } | |
| 435 | |
| 436 void InkDropAnimation::GetCurrentTansforms(InkDropTransforms transforms) const { | |
| 437 for (int i = 0; i < PAINTED_SHAPE_COUNT; ++i) | |
| 438 transforms[i] = painted_layers_[i]->GetTargetTransform(); | |
| 439 } | |
| 440 | |
| 441 void InkDropAnimation::SetCenterPoint(const gfx::Point& center_point) { | |
| 442 gfx::Transform transform; | |
| 443 transform.Translate(center_point.x(), center_point.y()); | |
| 444 root_layer_->SetTransform(transform); | |
| 445 } | |
| 446 | |
| 447 void InkDropAnimation::AddPaintLayer(PaintedShape painted_shape) { | |
| 448 ui::LayerDelegate* delegate = nullptr; | |
| 449 switch (painted_shape) { | |
| 450 case TOP_LEFT_CIRCLE: | |
| 451 case TOP_RIGHT_CIRCLE: | |
| 452 case BOTTOM_RIGHT_CIRCLE: | |
| 453 case BOTTOM_LEFT_CIRCLE: | |
| 454 delegate = circle_layer_delegate_.get(); | |
| 455 break; | |
| 456 case HORIZONTAL_RECT: | |
| 457 case VERTICAL_RECT: | |
| 458 delegate = rect_layer_delegate_.get(); | |
| 459 break; | |
| 460 case PAINTED_SHAPE_COUNT: | |
| 461 NOTREACHED() << "PAINTED_SHAPE_COUNT is not an actual shape type."; | |
| 462 break; | |
| 269 } | 463 } |
| 270 } | 464 |
| 271 | 465 ui::Layer* layer = new ui::Layer(); |
| 272 void InkDropAnimation::AnimateLongPress() { | 466 root_layer_->Add(layer); |
| 273 // Only one animation at a time. Subsequent long presses are ignored until the | 467 |
| 274 // current animation completes. | 468 layer->SetBounds(gfx::Rect(large_size_)); |
| 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(); | |
| 306 ui::ScopedLayerAnimationSettings animation(animator); | |
| 307 animation.SetPreemptionStrategy( | |
| 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 = | |
| 313 ui::LayerAnimationElement::CreateTransformElement(target_transform, | |
| 314 duration); | |
| 315 ui::LayerAnimationSequence* sequence = | |
| 316 new ui::LayerAnimationSequence(element); | |
| 317 sequence->AddObserver(observer); | |
| 318 animator->StartAnimation(sequence); | |
| 319 } | |
| 320 | |
| 321 void InkDropAnimation::SetLayerBounds(ui::Layer* layer) { | |
| 322 bool circle = UseCircularFeedback(); | |
| 323 gfx::Size size = ink_drop_bounds_.size(); | |
| 324 float circle_width = circle ? 2.0f * kCircleRadius : size.width(); | |
| 325 float circle_height = circle ? 2.0f * kCircleRadius : size.height(); | |
| 326 float circle_x = circle ? (size.width() - circle_width) * 0.5f : 0; | |
| 327 float circle_y = circle ? (size.height() - circle_height) * 0.5f : 0; | |
| 328 layer->SetBounds(gfx::Rect(circle_x, circle_y, circle_width, circle_height)); | |
| 329 } | |
| 330 | |
| 331 void InkDropAnimation::SetupAnimationLayer(ui::Layer* layer, | |
| 332 InkDropDelegate* delegate) { | |
| 333 layer->SetFillsBoundsOpaquely(false); | 469 layer->SetFillsBoundsOpaquely(false); |
| 334 layer->set_delegate(delegate); | 470 layer->set_delegate(delegate); |
| 335 layer->SetVisible(false); | 471 layer->SetVisible(true); |
| 336 layer->SetBounds(gfx::Rect()); | 472 layer->SetOpacity(1.0); |
| 337 delegate->set_should_render_circle(UseCircularFeedback()); | 473 layer->SetMasksToBounds(false); |
| 474 | |
| 475 painted_layers_[painted_shape].reset(layer); | |
|
sadrul
2015/09/14 20:05:40
Looks like you are creating 6 layers at startup. C
sadrul
2015/09/14 20:06:25
We discussed this offline. Please make sure there
| |
| 338 } | 476 } |
| 339 | 477 |
| 340 } // namespace views | 478 } // namespace views |
| OLD | NEW |