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

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

Issue 1390113006: Added material design mouse hover feedback support. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Moved MouseEntered/Exit() to hover handling in to the ButtonInkDropDelegate. Created 4 years, 11 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_impl.cc
diff --git a/ui/views/animation/ink_drop_animation_controller_impl.cc b/ui/views/animation/ink_drop_animation_controller_impl.cc
index 74d681d8d637422bd900240fac416aa68d3a33d2..b2c99ad315c294f599e463ae6ff447711de5e456 100644
--- a/ui/views/animation/ink_drop_animation_controller_impl.cc
+++ b/ui/views/animation/ink_drop_animation_controller_impl.cc
@@ -4,19 +4,54 @@
#include "ui/views/animation/ink_drop_animation_controller_impl.h"
+#include "base/timer/timer.h"
+#include "ui/compositor/layer.h"
#include "ui/views/animation/ink_drop_animation.h"
#include "ui/views/animation/ink_drop_host.h"
+#include "ui/views/animation/ink_drop_hover.h"
namespace views {
+namespace {
+
+// The duration, in milliseconds, of the hover state fade in animation when it
+// is triggered by user input.
+const int kHoverFadeInFromUserInputDurationInMs = 250;
+
+// The duration, in milliseconds, of the hover state fade out animation when it
+// is triggered by user input.
+const int kHoverFadeOutFromUserInputDurationInMs = 250;
+
+// The duration, in milliseconds, of the hover state fade in animation when it
+// is triggered by an ink drop ripple animation ending.
+const int kHoverFadeInAfterAnimationDurationInMs = 250;
+
+// The duration, in milliseconds, of the hover state fade out animation when it
+// is triggered by an ink drop ripple animation starting.
+const int kHoverFadeOutBeforeAnimationDurationInMs = 0;
+
+// The amount of time in milliseconds that |hover_| should delay after a ripple
+// animation before fading in.
+const int kHoverFadeInAfterAnimationDelayInMs = 1000;
+
+} // namespace
+
InkDropAnimationControllerImpl::InkDropAnimationControllerImpl(
InkDropHost* ink_drop_host)
- : ink_drop_host_(ink_drop_host) {}
+ : ink_drop_host_(ink_drop_host),
+ ink_drop_large_corner_radius_(0),
+ ink_drop_small_corner_radius_(0),
+ root_layer_(new ui::Layer(ui::LAYER_NOT_DRAWN)),
+ hover_after_animation_timer_(new base::OneShotTimer()) {
+ root_layer_->set_name("InkDropAnimationControllerImpl:RootLayer");
+ ink_drop_host_->AddInkDropLayer(root_layer_.get());
+}
InkDropAnimationControllerImpl::~InkDropAnimationControllerImpl() {
// Explicitly destroy the InkDropAnimation so that this still exists if
// views::InkDropAnimationObserver methods are called on this.
DestroyInkDropAnimation();
+ ink_drop_host_->RemoveInkDropLayer(root_layer_.get());
}
InkDropState InkDropAnimationControllerImpl::GetInkDropState() const {
@@ -32,6 +67,18 @@ void InkDropAnimationControllerImpl::AnimateToState(
ink_drop_animation_->AnimateToState(ink_drop_state);
}
+void InkDropAnimationControllerImpl::SetHovered(bool is_hovered) {
+ SetHoveredInternal(is_hovered,
+ is_hovered ? base::TimeDelta::FromMilliseconds(
+ kHoverFadeInFromUserInputDurationInMs)
+ : base::TimeDelta::FromMilliseconds(
+ kHoverFadeOutFromUserInputDurationInMs));
+}
+
+bool InkDropAnimationControllerImpl::IsHovered() const {
+ return hover_ && hover_->IsVisible();
+}
+
gfx::Size InkDropAnimationControllerImpl::GetInkDropLargeSize() const {
return ink_drop_large_size_;
}
@@ -49,7 +96,9 @@ void InkDropAnimationControllerImpl::SetInkDropSize(const gfx::Size& large_size,
ink_drop_large_corner_radius_ = large_corner_radius;
ink_drop_small_size_ = small_size;
ink_drop_small_corner_radius_ = small_corner_radius;
- ink_drop_animation_.reset();
+
+ DestroyInkDropAnimation();
+ DestroyInkDropHover();
}
void InkDropAnimationControllerImpl::SetInkDropCenter(
@@ -57,6 +106,8 @@ void InkDropAnimationControllerImpl::SetInkDropCenter(
ink_drop_center_ = center_point;
if (ink_drop_animation_)
ink_drop_animation_->SetCenterPoint(ink_drop_center_);
+ if (hover_)
+ hover_->SetCenterPoint(ink_drop_center_);
}
void InkDropAnimationControllerImpl::CreateInkDropAnimation() {
@@ -68,19 +119,40 @@ void InkDropAnimationControllerImpl::CreateInkDropAnimation() {
ink_drop_animation_->AddObserver(this);
ink_drop_animation_->SetCenterPoint(ink_drop_center_);
- ink_drop_host_->AddInkDropLayer(ink_drop_animation_->root_layer());
+ root_layer_->Add(ink_drop_animation_->root_layer());
}
void InkDropAnimationControllerImpl::DestroyInkDropAnimation() {
if (!ink_drop_animation_)
return;
- ink_drop_host_->RemoveInkDropLayer(ink_drop_animation_->root_layer());
+ root_layer_->Remove(ink_drop_animation_->root_layer());
ink_drop_animation_->RemoveObserver(this);
ink_drop_animation_.reset();
}
+void InkDropAnimationControllerImpl::CreateInkDropHover() {
+ DestroyInkDropHover();
+
+ hover_.reset(
+ new InkDropHover(ink_drop_small_size_, ink_drop_small_corner_radius_));
+ hover_->SetCenterPoint(ink_drop_center_);
+ root_layer_->Add(hover_->layer());
+}
+
+void InkDropAnimationControllerImpl::DestroyInkDropHover() {
+ if (!hover_)
+ return;
+ root_layer_->Remove(hover_->layer());
+ hover_.reset();
+}
+
void InkDropAnimationControllerImpl::InkDropAnimationStarted(
- InkDropState ink_drop_state) {}
+ InkDropState ink_drop_state) {
+ if (ink_drop_state != views::InkDropState::HIDDEN) {
+ SetHoveredInternal(false, base::TimeDelta::FromMilliseconds(
+ kHoverFadeOutBeforeAnimationDurationInMs));
+ }
+}
void InkDropAnimationControllerImpl::InkDropAnimationEnded(
InkDropState ink_drop_state,
@@ -94,6 +166,7 @@ void InkDropAnimationControllerImpl::InkDropAnimationEnded(
ink_drop_animation_->AnimateToState(views::InkDropState::HIDDEN);
break;
case views::InkDropState::HIDDEN:
+ StartHoverAfterAnimationTimer();
// TODO(bruthig): Investigate whether creating and destroying
// InkDropAnimations is expensive and consider creating an
// InkDropAnimationPool. See www.crbug.com/522175.
@@ -104,4 +177,44 @@ void InkDropAnimationControllerImpl::InkDropAnimationEnded(
}
}
+void InkDropAnimationControllerImpl::SetHoveredInternal(
+ bool is_hovered,
+ base::TimeDelta animation_duration) {
+ StopHoverAfterAnimationTimer();
+
+ if (IsHovered() == is_hovered)
+ return;
+
+ if (is_hovered) {
+ if (!hover_)
+ CreateInkDropHover();
+ if (GetInkDropState() == views::InkDropState::HIDDEN) {
+ hover_->FadeIn(animation_duration);
+ }
+ } else {
+ hover_->FadeOut(animation_duration);
+ }
+}
+
+void InkDropAnimationControllerImpl::StartHoverAfterAnimationTimer() {
+ StopHoverAfterAnimationTimer();
+
+ hover_after_animation_timer_->Start(
+ FROM_HERE,
+ base::TimeDelta::FromMilliseconds(kHoverFadeInAfterAnimationDelayInMs),
+ base::Bind(&InkDropAnimationControllerImpl::HoverAfterAnimationTimerFired,
+ base::Unretained(this)));
+}
+
+void InkDropAnimationControllerImpl::StopHoverAfterAnimationTimer() {
+ if (hover_after_animation_timer_->IsRunning())
+ hover_after_animation_timer_->Reset();
+}
+
+void InkDropAnimationControllerImpl::HoverAfterAnimationTimerFired() {
+ SetHoveredInternal(ink_drop_host_->ShouldShowInkDropHover(),
+ base::TimeDelta::FromMilliseconds(
+ kHoverFadeInAfterAnimationDurationInMs));
+}
+
} // namespace views

Powered by Google App Engine
This is Rietveld 408576698