Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(99)

Unified Diff: ui/views/animation/ink_drop_animation_controller.cc

Issue 1280953003: Enhance the material design ripple API so the ripple's state can be controlled by it's owning View. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed some comments from patch set 1. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/views/animation/ink_drop_animation_controller.cc
diff --git a/ui/views/animation/ink_drop_animation_controller.cc b/ui/views/animation/ink_drop_animation_controller.cc
index bfe459dd8fa1e60cc39ca51b63158023407b5902..5baa9f8f325ebf39ba602e63dc1addae642e443a 100644
--- a/ui/views/animation/ink_drop_animation_controller.cc
+++ b/ui/views/animation/ink_drop_animation_controller.cc
@@ -13,6 +13,9 @@
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/events/event.h"
#include "ui/gfx/canvas.h"
+#include "ui/gfx/geometry/size.h"
+#include "ui/views/animation/ink_drop_delegate.h"
+#include "ui/views/animation/ink_drop_host.h"
#include "ui/views/view.h"
namespace {
@@ -182,95 +185,68 @@ bool AppearAnimationObserver::RequiresNotificationWhenAnimatorDestroyed()
return true;
}
-// Renders the visual feedback. Will render a circle if |circle_| is true,
-// otherwise renders a rounded rectangle.
-class InkDropDelegate : public ui::LayerDelegate {
- public:
- InkDropDelegate(ui::Layer* layer, SkColor color);
- ~InkDropDelegate() override;
-
- // Sets the visual style of the feedback.
- void set_should_render_circle(bool should_render_circle) {
- should_render_circle_ = should_render_circle;
- }
-
- // ui::LayerDelegate:
- void OnPaintLayer(const ui::PaintContext& context) override;
- void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip) override;
- void OnDeviceScaleFactorChanged(float device_scale_factor) override;
- base::Closure PrepareForLayerBoundsChange() override;
-
- private:
- // The ui::Layer being rendered to.
- ui::Layer* layer_;
-
- // The color to paint.
- SkColor color_;
-
- // When true renders a circle, otherwise renders a rounded rectangle.
- bool should_render_circle_;
+InkDropAnimationController::InkDropAnimationController(
+ InkDropHost* ink_drop_host)
+ : ink_drop_host_(ink_drop_host),
+ layer_tree_(new ui::Layer()),
+ ink_drop_layer_(new ui::Layer()),
+ ink_drop_delegate_(new InkDropDelegate(ink_drop_layer_,
+ kInkDropColor,
+ kCircleRadius,
+ kRoundedRectCorners)),
+ appear_animation_observer_(nullptr),
+ long_press_layer_(new ui::Layer()),
+ long_press_delegate_(new InkDropDelegate(long_press_layer_,
+ kLongPressColor,
+ kCircleRadius,
+ kRoundedRectCorners)),
+ long_press_animation_observer_(nullptr),
+ bounds_(0, 0, 0, 0) {
+ SetupAnimationLayer(long_press_layer_, long_press_delegate_.get());
+ SetupAnimationLayer(ink_drop_layer_, ink_drop_delegate_.get());
- DISALLOW_COPY_AND_ASSIGN(InkDropDelegate);
-};
+ layer_tree_.root()->Add(ink_drop_layer_);
+ layer_tree_.root()->Add(long_press_layer_);
-InkDropDelegate::InkDropDelegate(ui::Layer* layer, SkColor color)
- : layer_(layer), color_(color), should_render_circle_(true) {
+ ink_drop_host_->AddInkDropLayer(layer_tree_.root());
}
-InkDropDelegate::~InkDropDelegate() {
+InkDropAnimationController::~InkDropAnimationController() {
+ // TODO(bruthig): Currently this virtual function is going to be called during
+ // ToolbarButton's destructor, is that ok?? This should hopefully change and
+ // be called when the Ink Drop becomes hidden.
+ ink_drop_host_->RemoveInkDropLayer(layer_tree_.root());
}
-void InkDropDelegate::OnPaintLayer(const ui::PaintContext& context) {
- SkPaint paint;
- paint.setColor(color_);
- paint.setFlags(SkPaint::kAntiAlias_Flag);
- paint.setStyle(SkPaint::kFill_Style);
-
- gfx::Rect bounds = layer_->bounds();
-
- ui::PaintRecorder recorder(context, layer_->size());
- gfx::Canvas* canvas = recorder.canvas();
- if (should_render_circle_) {
- gfx::Point midpoint(bounds.width() * 0.5f, bounds.height() * 0.5f);
- canvas->DrawCircle(midpoint, kCircleRadius, paint);
- } else {
- canvas->DrawRoundRect(bounds, kRoundedRectCorners, paint);
+void InkDropAnimationController::AnimateToState(InkDropState state) {
tdanderson 2015/08/10 16:31:01 It seems we're placing all of our trust in the Ink
bruthig 2015/08/10 21:57:15 I agree, the IDH has a lot of responsibility but i
+ switch (state) {
+ case InkDropState::HIDDEN:
+ AnimateHide();
+ break;
+ case InkDropState::ACTION_PENDING:
+ AnimateTapDown();
+ break;
+ case InkDropState::QUICK_ACTION:
+ AnimateTapDown();
+ AnimateHide();
+ break;
+ case InkDropState::SLOW_ACTION:
+ AnimateLongPress();
+ break;
+ case InkDropState::ACTIVATED:
+ AnimateLongPress();
+ break;
}
}
-void InkDropDelegate::OnDelegatedFrameDamage(
- const gfx::Rect& damage_rect_in_dip) {
-}
-
-void InkDropDelegate::OnDeviceScaleFactorChanged(float device_scale_factor) {
-}
-
-base::Closure InkDropDelegate::PrepareForLayerBoundsChange() {
- return base::Closure();
-}
-
-InkDropAnimationController::InkDropAnimationController(views::View* view)
- : ink_drop_layer_(new ui::Layer()),
- ink_drop_delegate_(
- new InkDropDelegate(ink_drop_layer_.get(), kInkDropColor)),
- appear_animation_observer_(nullptr),
- long_press_layer_(new ui::Layer()),
- long_press_delegate_(
- new InkDropDelegate(long_press_layer_.get(), kLongPressColor)),
- long_press_animation_observer_(nullptr),
- view_(view) {
- view_->SetPaintToLayer(true);
- view_->AddPreTargetHandler(this);
- ui::Layer* layer = view_->layer();
- layer->SetMasksToBounds(!UseCircularFeedback());
- SetupAnimationLayer(layer, long_press_layer_.get(),
- long_press_delegate_.get());
- SetupAnimationLayer(layer, ink_drop_layer_.get(), ink_drop_delegate_.get());
- ink_drop_delegate_->set_should_render_circle(UseCircularFeedback());
+void InkDropAnimationController::SetSize(const gfx::Size& size) {
+ SetBounds(gfx::Rect(bounds().x(), bounds().y(), size.width(), size.height()));
}
-InkDropAnimationController::~InkDropAnimationController() {
- view_->RemovePreTargetHandler(this);
+void InkDropAnimationController::SetBounds(const gfx::Rect& bounds) {
+ bounds_ = bounds;
+ SetLayerBounds(ink_drop_layer_);
+ SetLayerBounds(long_press_layer_);
}
void InkDropAnimationController::AnimateTapDown() {
@@ -283,17 +259,20 @@ void InkDropAnimationController::AnimateTapDown() {
return;
}
appear_animation_observer_.reset(
- new AppearAnimationObserver(ink_drop_layer_.get(), false));
- AnimateShow(ink_drop_layer_.get(), appear_animation_observer_.get(),
- UseCircularFeedback(),
+ new AppearAnimationObserver(ink_drop_layer_, false));
+ AnimateShow(ink_drop_layer_, appear_animation_observer_.get(),
base::TimeDelta::FromMilliseconds(
(UseFastAnimations() ? kShowInkDropAnimationDurationFastMs
: kShowInkDropAnimationDurationSlowMs)));
}
void InkDropAnimationController::AnimateHide() {
- if (appear_animation_observer_)
+ if (appear_animation_observer_ &&
+ appear_animation_observer_->IsAnimationActive()) {
appear_animation_observer_->HideNowIfDoneOrOnceCompleted();
+ } else if (long_press_animation_observer_) {
+ long_press_animation_observer_->HideNowIfDoneOrOnceCompleted();
+ }
}
void InkDropAnimationController::AnimateLongPress() {
@@ -305,10 +284,9 @@ void InkDropAnimationController::AnimateLongPress() {
}
appear_animation_observer_.reset();
long_press_animation_observer_.reset(
- new AppearAnimationObserver(long_press_layer_.get(), true));
- long_press_animation_observer_->SetBackgroundToHide(ink_drop_layer_.get());
- AnimateShow(long_press_layer_.get(), long_press_animation_observer_.get(),
- true,
+ new AppearAnimationObserver(long_press_layer_, false));
+ long_press_animation_observer_->SetBackgroundToHide(ink_drop_layer_);
+ AnimateShow(long_press_layer_, long_press_animation_observer_.get(),
base::TimeDelta::FromMilliseconds(
UseFastAnimations() ? kShowLongPressAnimationDurationFastMs
: kShowLongPressAnimationDurationSlowMs));
@@ -316,14 +294,14 @@ void InkDropAnimationController::AnimateLongPress() {
void InkDropAnimationController::AnimateShow(ui::Layer* layer,
AppearAnimationObserver* observer,
- bool circle,
base::TimeDelta duration) {
- SetLayerBounds(layer, circle, view_->width(), view_->height());
layer->SetVisible(true);
layer->SetOpacity(1.0f);
- float start_x = layer->bounds().width() * kMinimumScaleCenteringOffset;
- float start_y = layer->bounds().height() * kMinimumScaleCenteringOffset;
+ float start_x =
+ bounds_.x() + layer->bounds().width() * kMinimumScaleCenteringOffset;
+ float start_y =
+ bounds_.y() + layer->bounds().height() * kMinimumScaleCenteringOffset;
gfx::Transform initial_transform;
initial_transform.Translate(start_x, start_y);
@@ -335,9 +313,10 @@ void InkDropAnimationController::AnimateShow(ui::Layer* layer,
animation.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
- gfx::Transform identity_transform;
+ gfx::Transform target_transform;
+ target_transform.Translate(bounds_.x(), bounds_.y());
ui::LayerAnimationElement* element =
- ui::LayerAnimationElement::CreateTransformElement(identity_transform,
+ ui::LayerAnimationElement::CreateTransformElement(target_transform,
duration);
ui::LayerAnimationSequence* sequence =
new ui::LayerAnimationSequence(element);
@@ -345,48 +324,24 @@ void InkDropAnimationController::AnimateShow(ui::Layer* layer,
animator->StartAnimation(sequence);
}
-void InkDropAnimationController::SetLayerBounds(ui::Layer* layer,
- bool circle,
- int width,
- int height) {
- float circle_width = circle ? 2.0f * kCircleRadius : width;
- float circle_height = circle ? 2.0f * kCircleRadius : height;
- float circle_x = circle ? (width - circle_width) * 0.5f : 0;
- float circle_y = circle ? (height - circle_height) * 0.5f : 0;
+void InkDropAnimationController::SetLayerBounds(ui::Layer* layer) {
+ bool circle = UseCircularFeedback();
+ gfx::Size size = bounds_.size();
+ float circle_width = circle ? 2.0f * kCircleRadius : size.width();
+ float circle_height = circle ? 2.0f * kCircleRadius : size.height();
+ float circle_x = circle ? (size.width() - circle_width) * 0.5f : 0;
+ float circle_y = circle ? (size.height() - circle_height) * 0.5f : 0;
layer->SetBounds(gfx::Rect(circle_x, circle_y, circle_width, circle_height));
}
void InkDropAnimationController::SetupAnimationLayer(
- ui::Layer* parent,
ui::Layer* layer,
InkDropDelegate* delegate) {
layer->SetFillsBoundsOpaquely(false);
layer->set_delegate(delegate);
layer->SetVisible(false);
layer->SetBounds(gfx::Rect());
- parent->Add(layer);
- parent->StackAtBottom(layer);
-}
-
-void InkDropAnimationController::OnGestureEvent(ui::GestureEvent* event) {
- if (event->target() != view_)
- return;
-
- switch (event->type()) {
- case ui::ET_GESTURE_TAP_DOWN:
- AnimateTapDown();
- break;
- case ui::ET_GESTURE_LONG_PRESS:
- AnimateLongPress();
- break;
- case ui::ET_GESTURE_END:
- case ui::ET_GESTURE_TAP_CANCEL:
- case ui::ET_GESTURE_TAP:
- AnimateHide();
- break;
- default:
- break;
- }
+ delegate->set_should_render_circle(UseCircularFeedback());
}
} // namespace views

Powered by Google App Engine
This is Rietveld 408576698