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