OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_host_view.h" | 5 #include "ui/views/animation/ink_drop_host_view.h" |
6 | 6 |
7 #include "ui/events/event.h" | 7 #include "ui/events/event.h" |
8 #include "ui/events/scoped_target_handler.h" | 8 #include "ui/events/scoped_target_handler.h" |
9 #include "ui/gfx/color_palette.h" | 9 #include "ui/gfx/color_palette.h" |
10 #include "ui/gfx/geometry/size_conversions.h" | 10 #include "ui/gfx/geometry/size_conversions.h" |
(...skipping 23 matching lines...) Expand all Loading... | |
34 // static | 34 // static |
35 const int InkDropHostView::kInkDropSmallCornerRadius = 2; | 35 const int InkDropHostView::kInkDropSmallCornerRadius = 2; |
36 | 36 |
37 // An EventHandler that is guaranteed to be invoked and is not prone to | 37 // An EventHandler that is guaranteed to be invoked and is not prone to |
38 // InkDropHostView descendents who do not call | 38 // InkDropHostView descendents who do not call |
39 // InkDropHostView::OnGestureEvent(). | 39 // InkDropHostView::OnGestureEvent(). |
40 // | 40 // |
41 // TODO(bruthig): Consider getting rid of this class. | 41 // TODO(bruthig): Consider getting rid of this class. |
42 class InkDropHostView::InkDropGestureHandler : public ui::EventHandler { | 42 class InkDropHostView::InkDropGestureHandler : public ui::EventHandler { |
43 public: | 43 public: |
44 InkDropGestureHandler(View* view, InkDrop* ink_drop) | 44 InkDropGestureHandler(InkDropHostView* host_view, InkDrop* ink_drop) |
45 : ink_drop_(ink_drop), target_handler_(view, this) {} | 45 : target_handler_(new ui::ScopedTargetHandler(host_view, this)), |
46 host_view_(host_view), | |
47 ink_drop_(ink_drop) {} | |
46 | 48 |
47 ~InkDropGestureHandler() override {} | 49 ~InkDropGestureHandler() override {} |
48 | 50 |
49 // ui::EventHandler: | 51 // ui::EventHandler: |
50 void OnGestureEvent(ui::GestureEvent* event) override { | 52 void OnGestureEvent(ui::GestureEvent* event) override { |
51 InkDropState current_ink_drop_state = ink_drop_->GetTargetInkDropState(); | 53 InkDropState current_ink_drop_state = ink_drop_->GetTargetInkDropState(); |
52 | 54 |
53 InkDropState ink_drop_state = InkDropState::HIDDEN; | 55 InkDropState ink_drop_state = InkDropState::HIDDEN; |
54 switch (event->type()) { | 56 switch (event->type()) { |
55 case ui::ET_GESTURE_TAP_DOWN: | 57 case ui::ET_GESTURE_TAP_DOWN: |
(...skipping 22 matching lines...) Expand all Loading... | |
78 | 80 |
79 if (ink_drop_state == InkDropState::HIDDEN && | 81 if (ink_drop_state == InkDropState::HIDDEN && |
80 (current_ink_drop_state == InkDropState::ACTION_TRIGGERED || | 82 (current_ink_drop_state == InkDropState::ACTION_TRIGGERED || |
81 current_ink_drop_state == InkDropState::ALTERNATE_ACTION_TRIGGERED || | 83 current_ink_drop_state == InkDropState::ALTERNATE_ACTION_TRIGGERED || |
82 current_ink_drop_state == InkDropState::DEACTIVATED)) { | 84 current_ink_drop_state == InkDropState::DEACTIVATED)) { |
83 // These InkDropStates automatically transition to the HIDDEN state so we | 85 // These InkDropStates automatically transition to the HIDDEN state so we |
84 // don't make an explicit call. Explicitly animating to HIDDEN in this | 86 // don't make an explicit call. Explicitly animating to HIDDEN in this |
85 // case would prematurely pre-empt these animations. | 87 // case would prematurely pre-empt these animations. |
86 return; | 88 return; |
87 } | 89 } |
88 ink_drop_->AnimateToState(ink_drop_state); | 90 host_view_->AnimateInkDrop(ink_drop_state, event); |
89 } | 91 } |
90 | 92 |
91 private: | 93 private: |
94 // Allows |this| to handle all GestureEvents on |host_view_|. | |
95 std::unique_ptr<ui::ScopedTargetHandler> target_handler_; | |
96 | |
97 // The host view to cache ui::Events to when animating the ink drop. | |
98 InkDropHostView* host_view_; | |
99 | |
92 // Animation controller for the ink drop ripple effect. | 100 // Animation controller for the ink drop ripple effect. |
93 InkDrop* ink_drop_; | 101 InkDrop* ink_drop_; |
94 | 102 |
95 // An instance of ScopedTargetHandler allowing |this| to handle events. | |
96 ui::ScopedTargetHandler target_handler_; | |
97 | |
98 DISALLOW_COPY_AND_ASSIGN(InkDropGestureHandler); | 103 DISALLOW_COPY_AND_ASSIGN(InkDropGestureHandler); |
99 }; | 104 }; |
100 | 105 |
101 InkDropHostView::InkDropHostView() | 106 InkDropHostView::InkDropHostView() |
102 : ink_drop_(new InkDropStub()), | 107 : ink_drop_(new InkDropStub()), |
103 ink_drop_size_(kInkDropSize, kInkDropSize), | 108 ink_drop_size_(kInkDropSize, kInkDropSize), |
104 destroying_(false) {} | 109 destroying_(false) {} |
105 | 110 |
106 InkDropHostView::~InkDropHostView() { | 111 InkDropHostView::~InkDropHostView() { |
107 // TODO(bruthig): Improve InkDropImpl to be safer about calling back to | 112 // TODO(bruthig): Improve InkDropImpl to be safer about calling back to |
(...skipping 12 matching lines...) Expand all Loading... | |
120 // No need to do anything when called during shutdown, and if a derived | 125 // No need to do anything when called during shutdown, and if a derived |
121 // class has overridden Add/RemoveInkDropLayer, running this implementation | 126 // class has overridden Add/RemoveInkDropLayer, running this implementation |
122 // would be wrong. | 127 // would be wrong. |
123 if (destroying_) | 128 if (destroying_) |
124 return; | 129 return; |
125 layer()->Remove(ink_drop_layer); | 130 layer()->Remove(ink_drop_layer); |
126 SetPaintToLayer(false); | 131 SetPaintToLayer(false); |
127 } | 132 } |
128 | 133 |
129 std::unique_ptr<InkDropRipple> InkDropHostView::CreateInkDropRipple() const { | 134 std::unique_ptr<InkDropRipple> InkDropHostView::CreateInkDropRipple() const { |
135 return CreateDefaultInkDropRipple(GetLocalBounds().CenterPoint()); | |
136 } | |
137 | |
138 std::unique_ptr<InkDropHighlight> InkDropHostView::CreateInkDropHighlight() | |
139 const { | |
140 return CreateDefaultInkDropHighlight(GetLocalBounds().CenterPoint()); | |
141 } | |
142 | |
143 // static | |
144 const ui::LocatedEvent* InkDropHostView::AsLocatedEvent( | |
Evan Stade
2016/06/15 20:34:42
I'm not a big fan of putting this here. It's not p
bruthig
2016/06/15 21:43:19
Moved to event_utils(h|cc) as ToLocatedEventOrNull
| |
145 const ui::Event* event) { | |
146 return event && event->IsLocatedEvent() ? event->AsLocatedEvent() : nullptr; | |
147 } | |
148 | |
149 std::unique_ptr<InkDropRipple> InkDropHostView::CreateDefaultInkDropRipple( | |
150 const gfx::Point& center_point) const { | |
130 std::unique_ptr<InkDropRipple> ripple(new SquareInkDropRipple( | 151 std::unique_ptr<InkDropRipple> ripple(new SquareInkDropRipple( |
131 CalculateLargeInkDropSize(ink_drop_size_), kInkDropLargeCornerRadius, | 152 CalculateLargeInkDropSize(ink_drop_size_), kInkDropLargeCornerRadius, |
132 ink_drop_size_, kInkDropSmallCornerRadius, GetInkDropCenter(), | 153 ink_drop_size_, kInkDropSmallCornerRadius, center_point, |
133 GetInkDropBaseColor())); | 154 GetInkDropBaseColor())); |
134 return ripple; | 155 return ripple; |
135 } | 156 } |
136 | 157 |
137 std::unique_ptr<InkDropHighlight> InkDropHostView::CreateInkDropHighlight() | 158 std::unique_ptr<InkDropHighlight> |
138 const { | 159 InkDropHostView::CreateDefaultInkDropHighlight( |
160 const gfx::Point& center_point) const { | |
139 std::unique_ptr<InkDropHighlight> highlight( | 161 std::unique_ptr<InkDropHighlight> highlight( |
140 new InkDropHighlight(ink_drop_size_, kInkDropSmallCornerRadius, | 162 new InkDropHighlight(ink_drop_size_, kInkDropSmallCornerRadius, |
141 GetInkDropCenter(), GetInkDropBaseColor())); | 163 center_point, GetInkDropBaseColor())); |
142 highlight->set_explode_size(CalculateLargeInkDropSize(ink_drop_size_)); | 164 highlight->set_explode_size(CalculateLargeInkDropSize(ink_drop_size_)); |
143 return highlight; | 165 return highlight; |
144 } | 166 } |
145 | 167 |
168 gfx::Point InkDropHostView::GetInkDropCenterBasedOnLastEvent() const { | |
169 return last_ripple_triggering_event_ | |
170 ? last_ripple_triggering_event_->location() | |
171 : GetLocalBounds().CenterPoint(); | |
172 } | |
173 | |
174 void InkDropHostView::AnimateInkDrop(InkDropState state, | |
175 const ui::LocatedEvent* event) { | |
176 last_ripple_triggering_event_.reset( | |
177 event ? ui::Event::Clone(*event).release()->AsLocatedEvent() : nullptr); | |
178 ink_drop_->AnimateToState(state); | |
179 } | |
180 | |
146 void InkDropHostView::VisibilityChanged(View* starting_from, bool is_visible) { | 181 void InkDropHostView::VisibilityChanged(View* starting_from, bool is_visible) { |
147 View::VisibilityChanged(starting_from, is_visible); | 182 View::VisibilityChanged(starting_from, is_visible); |
148 if (GetWidget() && !is_visible) { | 183 if (GetWidget() && !is_visible) { |
149 ink_drop()->AnimateToState(InkDropState::HIDDEN); | 184 ink_drop()->AnimateToState(InkDropState::HIDDEN); |
150 ink_drop()->SetHovered(false); | 185 ink_drop()->SetHovered(false); |
151 } | 186 } |
152 } | 187 } |
153 | 188 |
154 void InkDropHostView::OnFocus() { | 189 void InkDropHostView::OnFocus() { |
155 views::View::OnFocus(); | 190 views::View::OnFocus(); |
(...skipping 14 matching lines...) Expand all Loading... | |
170 break; | 205 break; |
171 case ui::ET_MOUSE_EXITED: | 206 case ui::ET_MOUSE_EXITED: |
172 ink_drop_->SetHovered(false); | 207 ink_drop_->SetHovered(false); |
173 break; | 208 break; |
174 default: | 209 default: |
175 break; | 210 break; |
176 } | 211 } |
177 View::OnMouseEvent(event); | 212 View::OnMouseEvent(event); |
178 } | 213 } |
179 | 214 |
180 gfx::Point InkDropHostView::GetInkDropCenter() const { | |
181 return GetLocalBounds().CenterPoint(); | |
182 } | |
183 | |
184 SkColor InkDropHostView::GetInkDropBaseColor() const { | 215 SkColor InkDropHostView::GetInkDropBaseColor() const { |
185 NOTREACHED(); | 216 NOTREACHED(); |
186 return gfx::kPlaceholderColor; | 217 return gfx::kPlaceholderColor; |
187 } | 218 } |
188 | 219 |
189 bool InkDropHostView::ShouldShowInkDropForFocus() const { | 220 bool InkDropHostView::ShouldShowInkDropForFocus() const { |
190 return false; | 221 return false; |
191 } | 222 } |
192 | 223 |
193 void InkDropHostView::SetHasInkDrop(bool has_an_ink_drop) { | 224 void InkDropHostView::SetHasInkDrop(bool has_an_ink_drop) { |
194 if (has_an_ink_drop) { | 225 if (has_an_ink_drop) { |
195 ink_drop_ = InkDropFactory::CreateInkDrop(this); | 226 ink_drop_ = InkDropFactory::CreateInkDrop(this); |
196 gesture_handler_.reset(new InkDropGestureHandler(this, ink_drop_.get())); | 227 gesture_handler_.reset(new InkDropGestureHandler(this, ink_drop_.get())); |
197 } else { | 228 } else { |
198 gesture_handler_.reset(); | 229 gesture_handler_.reset(); |
199 ink_drop_.reset(new InkDropStub()); | 230 ink_drop_.reset(new InkDropStub()); |
200 } | 231 } |
201 } | 232 } |
202 | 233 |
203 void InkDropHostView::AnimateInkDrop(InkDropState ink_drop_state) { | |
204 ink_drop_->AnimateToState(ink_drop_state); | |
205 } | |
206 | |
207 } // namespace views | 234 } // namespace views |
OLD | NEW |