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_controller_impl.h" | 5 #include "ui/views/animation/ink_drop_animation_controller_impl.h" |
6 | 6 |
| 7 #include "ui/compositor/layer.h" |
7 #include "ui/views/animation/ink_drop_animation.h" | 8 #include "ui/views/animation/ink_drop_animation.h" |
8 #include "ui/views/animation/ink_drop_host.h" | 9 #include "ui/views/animation/ink_drop_host.h" |
| 10 #include "ui/views/animation/ink_drop_hover.h" |
9 | 11 |
10 namespace views { | 12 namespace views { |
11 | 13 |
| 14 namespace { |
| 15 |
| 16 // The duration of the hover state fade in animation. |
| 17 const int kFadeInDurationInMs = 250; |
| 18 |
| 19 // The duration of the hover state fade out animation. |
| 20 const int kFadeOutDurationInMs = 250; |
| 21 |
| 22 } // namespace |
| 23 |
12 InkDropAnimationControllerImpl::InkDropAnimationControllerImpl( | 24 InkDropAnimationControllerImpl::InkDropAnimationControllerImpl( |
13 InkDropHost* ink_drop_host) | 25 InkDropHost* ink_drop_host) |
14 : ink_drop_host_(ink_drop_host) {} | 26 : ink_drop_host_(ink_drop_host), |
| 27 root_layer_(new ui::Layer(ui::LAYER_NOT_DRAWN)), |
| 28 is_hovered_(false) { |
| 29 ink_drop_host_->AddInkDropLayer(root_layer_.get()); |
| 30 } |
15 | 31 |
16 InkDropAnimationControllerImpl::~InkDropAnimationControllerImpl() { | 32 InkDropAnimationControllerImpl::~InkDropAnimationControllerImpl() { |
17 // Explicitly destroy the InkDropAnimation so that this still exists if | 33 // Explicitly destroy the InkDropAnimation so that this still exists if |
18 // views::InkDropAnimationObserver methods are called on this. | 34 // views::InkDropAnimationObserver methods are called on this. |
19 DestroyInkDropAnimation(); | 35 DestroyInkDropAnimation(); |
20 } | 36 } |
21 | 37 |
22 InkDropState InkDropAnimationControllerImpl::GetInkDropState() const { | 38 InkDropState InkDropAnimationControllerImpl::GetInkDropState() const { |
23 if (!ink_drop_animation_) | 39 if (!ink_drop_animation_) |
24 return InkDropState::HIDDEN; | 40 return InkDropState::HIDDEN; |
25 return ink_drop_animation_->ink_drop_state(); | 41 return ink_drop_animation_->ink_drop_state(); |
26 } | 42 } |
27 | 43 |
28 void InkDropAnimationControllerImpl::AnimateToState( | 44 void InkDropAnimationControllerImpl::AnimateToState( |
29 InkDropState ink_drop_state) { | 45 InkDropState ink_drop_state) { |
30 if (!ink_drop_animation_) | 46 if (!ink_drop_animation_) |
31 CreateInkDropAnimation(); | 47 CreateInkDropAnimation(); |
32 ink_drop_animation_->AnimateToState(ink_drop_state); | 48 ink_drop_animation_->AnimateToState(ink_drop_state); |
33 } | 49 } |
34 | 50 |
| 51 void InkDropAnimationControllerImpl::SetHovered(bool is_hovered) { |
| 52 if (is_hovered_ == is_hovered) |
| 53 return; |
| 54 |
| 55 is_hovered_ = is_hovered; |
| 56 if (is_hovered_) { |
| 57 if (!hover_) |
| 58 CreateInkDropHover(); |
| 59 if (GetInkDropState() == views::InkDropState::HIDDEN) { |
| 60 base::TimeDelta duration = |
| 61 base::TimeDelta::FromMilliseconds(kFadeInDurationInMs); |
| 62 hover_->FadeIn(duration); |
| 63 } |
| 64 } else { |
| 65 base::TimeDelta duration = |
| 66 base::TimeDelta::FromMilliseconds(kFadeOutDurationInMs); |
| 67 hover_->FadeOut(duration); |
| 68 } |
| 69 } |
| 70 |
| 71 bool InkDropAnimationControllerImpl::IsHovered() const { |
| 72 return is_hovered_; |
| 73 } |
| 74 |
35 gfx::Size InkDropAnimationControllerImpl::GetInkDropLargeSize() const { | 75 gfx::Size InkDropAnimationControllerImpl::GetInkDropLargeSize() const { |
36 return ink_drop_large_size_; | 76 return ink_drop_large_size_; |
37 } | 77 } |
38 | 78 |
39 void InkDropAnimationControllerImpl::SetInkDropSize(const gfx::Size& large_size, | 79 void InkDropAnimationControllerImpl::SetInkDropSize(const gfx::Size& large_size, |
40 int large_corner_radius, | 80 int large_corner_radius, |
41 const gfx::Size& small_size, | 81 const gfx::Size& small_size, |
42 int small_corner_radius) { | 82 int small_corner_radius) { |
43 // TODO(bruthig): Fix the ink drop animations to work for non-square sizes. | 83 // TODO(bruthig): Fix the ink drop animations to work for non-square sizes. |
44 DCHECK_EQ(large_size.width(), large_size.height()) | 84 DCHECK_EQ(large_size.width(), large_size.height()) |
45 << "The ink drop animation does not currently support non-square sizes."; | 85 << "The ink drop animation does not currently support non-square sizes."; |
46 DCHECK_EQ(small_size.width(), small_size.height()) | 86 DCHECK_EQ(small_size.width(), small_size.height()) |
47 << "The ink drop animation does not currently support non-square sizes."; | 87 << "The ink drop animation does not currently support non-square sizes."; |
48 ink_drop_large_size_ = large_size; | 88 ink_drop_large_size_ = large_size; |
49 ink_drop_large_corner_radius_ = large_corner_radius; | 89 ink_drop_large_corner_radius_ = large_corner_radius; |
50 ink_drop_small_size_ = small_size; | 90 ink_drop_small_size_ = small_size; |
51 ink_drop_small_corner_radius_ = small_corner_radius; | 91 ink_drop_small_corner_radius_ = small_corner_radius; |
52 ink_drop_animation_.reset(); | 92 |
| 93 DestroyInkDropAnimation(); |
| 94 DestroyInkDropHover(); |
53 } | 95 } |
54 | 96 |
55 void InkDropAnimationControllerImpl::SetInkDropCenter( | 97 void InkDropAnimationControllerImpl::SetInkDropCenter( |
56 const gfx::Point& center_point) { | 98 const gfx::Point& center_point) { |
57 ink_drop_center_ = center_point; | 99 ink_drop_center_ = center_point; |
58 if (ink_drop_animation_) | 100 if (ink_drop_animation_) |
59 ink_drop_animation_->SetCenterPoint(ink_drop_center_); | 101 ink_drop_animation_->SetCenterPoint(ink_drop_center_); |
| 102 if (hover_) |
| 103 hover_->SetCenterPoint(ink_drop_center_); |
60 } | 104 } |
61 | 105 |
62 void InkDropAnimationControllerImpl::CreateInkDropAnimation() { | 106 void InkDropAnimationControllerImpl::CreateInkDropAnimation() { |
63 DestroyInkDropAnimation(); | 107 DestroyInkDropAnimation(); |
64 | 108 |
65 ink_drop_animation_.reset(new InkDropAnimation( | 109 ink_drop_animation_.reset(new InkDropAnimation( |
66 ink_drop_large_size_, ink_drop_large_corner_radius_, ink_drop_small_size_, | 110 ink_drop_large_size_, ink_drop_large_corner_radius_, ink_drop_small_size_, |
67 ink_drop_small_corner_radius_)); | 111 ink_drop_small_corner_radius_)); |
68 | 112 |
69 ink_drop_animation_->AddObserver(this); | 113 ink_drop_animation_->AddObserver(this); |
70 ink_drop_animation_->SetCenterPoint(ink_drop_center_); | 114 ink_drop_animation_->SetCenterPoint(ink_drop_center_); |
71 ink_drop_host_->AddInkDropLayer(ink_drop_animation_->root_layer()); | 115 root_layer_->Add(ink_drop_animation_->root_layer()); |
72 } | 116 } |
73 | 117 |
74 void InkDropAnimationControllerImpl::DestroyInkDropAnimation() { | 118 void InkDropAnimationControllerImpl::DestroyInkDropAnimation() { |
75 if (!ink_drop_animation_) | 119 if (!ink_drop_animation_) |
76 return; | 120 return; |
77 ink_drop_host_->RemoveInkDropLayer(ink_drop_animation_->root_layer()); | 121 root_layer_->Remove(ink_drop_animation_->root_layer()); |
78 ink_drop_animation_->RemoveObserver(this); | 122 ink_drop_animation_->RemoveObserver(this); |
79 ink_drop_animation_.reset(); | 123 ink_drop_animation_.reset(); |
80 } | 124 } |
81 | 125 |
| 126 void InkDropAnimationControllerImpl::CreateInkDropHover() { |
| 127 DestroyInkDropHover(); |
| 128 |
| 129 hover_.reset( |
| 130 new InkDropHover(ink_drop_small_size_, ink_drop_small_corner_radius_)); |
| 131 hover_->SetCenterPoint(ink_drop_center_); |
| 132 root_layer_->Add(hover_->root_layer()); |
| 133 } |
| 134 |
| 135 void InkDropAnimationControllerImpl::DestroyInkDropHover() { |
| 136 if (!hover_) |
| 137 return; |
| 138 root_layer_->Remove(hover_->root_layer()); |
| 139 hover_.reset(); |
| 140 } |
| 141 |
82 void InkDropAnimationControllerImpl::InkDropAnimationStarted( | 142 void InkDropAnimationControllerImpl::InkDropAnimationStarted( |
83 InkDropState ink_drop_state) {} | 143 InkDropState ink_drop_state) { |
| 144 switch (ink_drop_state) { |
| 145 case views::InkDropState::HIDDEN: |
| 146 break; |
| 147 case views::InkDropState::ACTION_PENDING: |
| 148 case views::InkDropState::QUICK_ACTION: |
| 149 case views::InkDropState::SLOW_ACTION_PENDING: |
| 150 case views::InkDropState::SLOW_ACTION: |
| 151 case views::InkDropState::ACTIVATED: |
| 152 case views::InkDropState::DEACTIVATED: |
| 153 if (is_hovered_ && hover_) { |
| 154 hover_->FadeOut( |
| 155 base::TimeDelta::FromMilliseconds(kFadeOutDurationInMs)); |
| 156 } |
| 157 break; |
| 158 } |
| 159 } |
84 | 160 |
85 void InkDropAnimationControllerImpl::InkDropAnimationEnded( | 161 void InkDropAnimationControllerImpl::InkDropAnimationEnded( |
86 InkDropState ink_drop_state, | 162 InkDropState ink_drop_state, |
87 InkDropAnimationEndedReason reason) { | 163 InkDropAnimationEndedReason reason) { |
88 if (reason != SUCCESS) | 164 if (reason != SUCCESS) |
89 return; | 165 return; |
90 switch (ink_drop_state) { | 166 switch (ink_drop_state) { |
91 case views::InkDropState::QUICK_ACTION: | 167 case views::InkDropState::QUICK_ACTION: |
92 case views::InkDropState::SLOW_ACTION: | 168 case views::InkDropState::SLOW_ACTION: |
93 case views::InkDropState::DEACTIVATED: | 169 case views::InkDropState::DEACTIVATED: |
94 ink_drop_animation_->AnimateToState(views::InkDropState::HIDDEN); | 170 ink_drop_animation_->AnimateToState(views::InkDropState::HIDDEN); |
95 break; | 171 break; |
96 case views::InkDropState::HIDDEN: | 172 case views::InkDropState::HIDDEN: |
97 // TODO(bruthig): Investigate whether creating and destroying | 173 // TODO(bruthig): Investigate whether creating and destroying |
98 // InkDropAnimations is expensive and consider creating an | 174 // InkDropAnimations is expensive and consider creating an |
99 // InkDropAnimationPool. See www.crbug.com/522175. | 175 // InkDropAnimationPool. See www.crbug.com/522175. |
100 DestroyInkDropAnimation(); | 176 DestroyInkDropAnimation(); |
| 177 |
| 178 if (is_hovered_ && hover_) { |
| 179 hover_->FadeIn(base::TimeDelta::FromMilliseconds(kFadeInDurationInMs)); |
| 180 } |
101 break; | 181 break; |
102 default: | 182 default: |
103 break; | 183 break; |
104 } | 184 } |
105 } | 185 } |
106 | 186 |
107 } // namespace views | 187 } // namespace views |
OLD | NEW |