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

Side by Side Diff: ui/views/animation/ink_drop_hover.cc

Issue 1390113006: Added material design mouse hover feedback support. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 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 unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "ui/views/animation/ink_drop_hover.h"
6
7 #include "third_party/skia/include/core/SkColor.h"
8 #include "ui/compositor/callback_layer_animation_observer.h"
9 #include "ui/compositor/layer.h"
10 #include "ui/compositor/layer_animation_sequence.h"
11 #include "ui/compositor/scoped_layer_animation_settings.h"
12 #include "ui/views/animation/ink_drop_painted_layer_delegates.h"
13
14 namespace views {
15
16 namespace {
17
18 // The opacity of the hover when it is visible.
19 const float kHoverVisibleOpacity = 0.08f;
20
21 // The opacity of the hover when it is not visible.
22 const float kHiddenOpacity = 0.0f;
23
24 // The hover color.
25 const SkColor kHoverColor = SK_ColorBLACK;
26
27 } // namespace
28
29 InkDropHover::InkDropHover(const gfx::Size& size, int corner_radius)
30 : is_hovered_(false),
31 layer_delegate_(
32 new RoundedRectangleLayerDelegate(kHoverColor, size, corner_radius)),
33 layer_(new ui::Layer()) {
34 layer_->SetBounds(gfx::Rect(size));
35 layer_->SetFillsBoundsOpaquely(false);
36 layer_->set_delegate(layer_delegate_.get());
37 layer_->SetVisible(false);
38 layer_->SetOpacity(kHoverVisibleOpacity);
39 layer_->SetMasksToBounds(false);
40 SetCenterPoint(gfx::Rect(size).CenterPoint());
41 }
42
43 InkDropHover::~InkDropHover() {}
44
45 void InkDropHover::FadeIn(const base::TimeDelta& duration) {
46 layer_->SetOpacity(kHiddenOpacity);
47 layer_->SetVisible(true);
48 AnimateFade(FADE_IN, duration);
49 }
50
51 void InkDropHover::FadeOut(const base::TimeDelta& duration) {
52 AnimateFade(FADE_OUT, duration);
53 }
54
55 void InkDropHover::AnimateFade(HoverAnimation hover_animation,
56 const base::TimeDelta& duration) {
57 ui::CallbackLayerAnimationObserver* animation_observer =
tdanderson 2015/10/15 14:46:54 Do you really want |animation_observer| and |anima
bruthig 2015/10/15 21:49:29 I've added a comment about the |animation_observer
58 new ui::CallbackLayerAnimationObserver(
59 base::Bind(&InkDropHover::AnimationStartedCallback,
60 base::Unretained(this), hover_animation),
61 base::Bind(&InkDropHover::AnimationEndedCallback,
62 base::Unretained(this), hover_animation));
63
64 ui::LayerAnimator* animator = layer_->GetAnimator();
65 ui::ScopedLayerAnimationSettings animation(animator);
66 animation.SetPreemptionStrategy(
67 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
tdanderson 2015/10/15 14:46:54 from in person conversation: should this be preemp
bruthig 2015/10/15 21:49:29 I double checked and IMMEDIATELY_ANIMATE_TO_NEW_TA
68 ui::LayerAnimationElement* animation_element =
69 ui::LayerAnimationElement::CreateOpacityElement(
70 hover_animation == FADE_IN ? kHoverVisibleOpacity : kHiddenOpacity,
71 duration);
72 ui::LayerAnimationSequence* animation_sequence =
73 new ui::LayerAnimationSequence(animation_element);
74 animation_sequence->AddObserver(animation_observer);
75
76 animator->StartAnimation(animation_sequence);
77
78 animation_observer->SetActive();
79 }
80
81 void InkDropHover::SetCenterPoint(const gfx::Point& center_point) {
82 gfx::Transform transform;
83 transform.Translate(center_point.x() - layer_->bounds().CenterPoint().x(),
84 center_point.y() - layer_->bounds().CenterPoint().y());
85 layer_->SetTransform(transform);
86 }
87
88 void InkDropHover::AnimationStartedCallback(
89 HoverAnimation hover_animation,
90 const ui::CallbackLayerAnimationObserver& observer) {
91 switch (hover_animation) {
92 case FADE_IN:
93 is_hovered_ = true;
94 break;
95 case FADE_OUT:
96 break;
97 }
98 }
99
100 bool InkDropHover::AnimationEndedCallback(
101 HoverAnimation hover_animation,
102 const ui::CallbackLayerAnimationObserver& observer) {
103 switch (hover_animation) {
104 case FADE_IN:
105 break;
106 case FADE_OUT:
107 is_hovered_ = false;
108 layer_->SetVisible(false);
109 break;
110 }
111 return true;
112 }
113
114 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698