OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/controls/button/custom_button.h" | 5 #include "ui/views/controls/button/custom_button.h" |
6 | 6 |
7 #include "ui/accessibility/ax_view_state.h" | 7 #include "ui/accessibility/ax_view_state.h" |
8 #include "ui/base/material_design/material_design_controller.h" | 8 #include "ui/base/material_design/material_design_controller.h" |
9 #include "ui/events/event.h" | 9 #include "ui/events/event.h" |
10 #include "ui/events/event_utils.h" | 10 #include "ui/events/event_utils.h" |
11 #include "ui/events/keycodes/keyboard_codes.h" | 11 #include "ui/events/keycodes/keyboard_codes.h" |
12 #include "ui/gfx/animation/throb_animation.h" | 12 #include "ui/gfx/animation/throb_animation.h" |
13 #include "ui/gfx/color_palette.h" | 13 #include "ui/gfx/color_palette.h" |
14 #include "ui/native_theme/native_theme.h" | 14 #include "ui/native_theme/native_theme.h" |
15 #include "ui/views/animation/ink_drop_delegate.h" | |
16 #include "ui/views/animation/ink_drop_highlight.h" | 15 #include "ui/views/animation/ink_drop_highlight.h" |
17 #include "ui/views/controls/button/blue_button.h" | 16 #include "ui/views/controls/button/blue_button.h" |
18 #include "ui/views/controls/button/checkbox.h" | 17 #include "ui/views/controls/button/checkbox.h" |
19 #include "ui/views/controls/button/image_button.h" | 18 #include "ui/views/controls/button/image_button.h" |
20 #include "ui/views/controls/button/label_button.h" | 19 #include "ui/views/controls/button/label_button.h" |
21 #include "ui/views/controls/button/menu_button.h" | 20 #include "ui/views/controls/button/menu_button.h" |
22 #include "ui/views/controls/button/radio_button.h" | 21 #include "ui/views/controls/button/radio_button.h" |
23 #include "ui/views/widget/widget.h" | 22 #include "ui/views/widget/widget.h" |
24 | 23 |
25 #if defined(USE_AURA) | 24 #if defined(USE_AURA) |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 // TODO(bruthig): Is there any reason we are not calling | 127 // TODO(bruthig): Is there any reason we are not calling |
129 // Button::OnEnabledChanged() here? | 128 // Button::OnEnabledChanged() here? |
130 if (enabled() ? (state_ != STATE_DISABLED) : (state_ == STATE_DISABLED)) | 129 if (enabled() ? (state_ != STATE_DISABLED) : (state_ == STATE_DISABLED)) |
131 return; | 130 return; |
132 | 131 |
133 if (enabled()) | 132 if (enabled()) |
134 SetState(ShouldEnterHoveredState() ? STATE_HOVERED : STATE_NORMAL); | 133 SetState(ShouldEnterHoveredState() ? STATE_HOVERED : STATE_NORMAL); |
135 else | 134 else |
136 SetState(STATE_DISABLED); | 135 SetState(STATE_DISABLED); |
137 | 136 |
138 if (ink_drop_delegate()) | 137 // TODO(bruthig): This should only be using the hover signal only and not the |
139 ink_drop_delegate()->SetHovered(ShouldShowInkDropHighlight()); | 138 // focus signal as well. |
| 139 ink_drop()->SetHovered(ShouldShowInkDropHighlight()); |
140 } | 140 } |
141 | 141 |
142 const char* CustomButton::GetClassName() const { | 142 const char* CustomButton::GetClassName() const { |
143 return kViewClassName; | 143 return kViewClassName; |
144 } | 144 } |
145 | 145 |
146 bool CustomButton::OnMousePressed(const ui::MouseEvent& event) { | 146 bool CustomButton::OnMousePressed(const ui::MouseEvent& event) { |
147 if (state_ == STATE_DISABLED) | 147 if (state_ == STATE_DISABLED) |
148 return true; | 148 return true; |
149 if (state_ != STATE_PRESSED && ShouldEnterPushedState(event) && | 149 if (state_ != STATE_PRESSED && ShouldEnterPushedState(event) && |
150 HitTestPoint(event.location())) { | 150 HitTestPoint(event.location())) { |
151 SetState(STATE_PRESSED); | 151 SetState(STATE_PRESSED); |
152 if (ink_drop_delegate()) | 152 AnimateInkDrop(views::InkDropState::ACTION_PENDING); |
153 ink_drop_delegate()->OnAction(views::InkDropState::ACTION_PENDING); | |
154 } | 153 } |
155 if (request_focus_on_press_) | 154 if (request_focus_on_press_) |
156 RequestFocus(); | 155 RequestFocus(); |
157 if (IsTriggerableEvent(event) && notify_action_ == NOTIFY_ON_PRESS) { | 156 if (IsTriggerableEvent(event) && notify_action_ == NOTIFY_ON_PRESS) { |
158 NotifyClick(event); | 157 NotifyClick(event); |
159 // NOTE: We may be deleted at this point (by the listener's notification | 158 // NOTE: We may be deleted at this point (by the listener's notification |
160 // handler). | 159 // handler). |
161 } | 160 } |
162 return true; | 161 return true; |
163 } | 162 } |
164 | 163 |
165 bool CustomButton::OnMouseDragged(const ui::MouseEvent& event) { | 164 bool CustomButton::OnMouseDragged(const ui::MouseEvent& event) { |
166 if (state_ != STATE_DISABLED) { | 165 if (state_ != STATE_DISABLED) { |
167 if (HitTestPoint(event.location())) { | 166 if (HitTestPoint(event.location())) { |
168 SetState(ShouldEnterPushedState(event) ? STATE_PRESSED : STATE_HOVERED); | 167 SetState(ShouldEnterPushedState(event) ? STATE_PRESSED : STATE_HOVERED); |
169 if (!InDrag() && ink_drop_delegate() && | 168 if (!InDrag() && |
170 ink_drop_delegate()->GetTargetInkDropState() == | 169 ink_drop()->GetTargetInkDropState() == views::InkDropState::HIDDEN) { |
171 views::InkDropState::HIDDEN) | 170 AnimateInkDrop(views::InkDropState::ACTION_PENDING); |
172 ink_drop_delegate()->OnAction(views::InkDropState::ACTION_PENDING); | 171 } |
173 } else { | 172 } else { |
174 SetState(STATE_NORMAL); | 173 SetState(STATE_NORMAL); |
175 if (!InDrag() && ink_drop_delegate() && | 174 if (!InDrag() && |
176 ink_drop_delegate()->GetTargetInkDropState() == | 175 ink_drop()->GetTargetInkDropState() == |
177 views::InkDropState::ACTION_PENDING) | 176 views::InkDropState::ACTION_PENDING) { |
178 ink_drop_delegate()->OnAction(views::InkDropState::HIDDEN); | 177 AnimateInkDrop(views::InkDropState::HIDDEN); |
| 178 } |
179 } | 179 } |
180 } | 180 } |
181 return true; | 181 return true; |
182 } | 182 } |
183 | 183 |
184 void CustomButton::OnMouseReleased(const ui::MouseEvent& event) { | 184 void CustomButton::OnMouseReleased(const ui::MouseEvent& event) { |
185 if (state_ != STATE_DISABLED) { | 185 if (state_ != STATE_DISABLED) { |
186 if (!HitTestPoint(event.location())) { | 186 if (!HitTestPoint(event.location())) { |
187 SetState(STATE_NORMAL); | 187 SetState(STATE_NORMAL); |
188 } else { | 188 } else { |
(...skipping 11 matching lines...) Expand all Loading... |
200 } | 200 } |
201 | 201 |
202 void CustomButton::OnMouseCaptureLost() { | 202 void CustomButton::OnMouseCaptureLost() { |
203 // Starting a drag results in a MouseCaptureLost. Reset button state. | 203 // Starting a drag results in a MouseCaptureLost. Reset button state. |
204 // TODO(varkha) While in drag only reset the state with Material Design. | 204 // TODO(varkha) While in drag only reset the state with Material Design. |
205 // The same logic may applies everywhere so gather any feedback and update. | 205 // The same logic may applies everywhere so gather any feedback and update. |
206 bool reset_button_state = | 206 bool reset_button_state = |
207 !InDrag() || ui::MaterialDesignController::IsModeMaterial(); | 207 !InDrag() || ui::MaterialDesignController::IsModeMaterial(); |
208 if (state_ != STATE_DISABLED && reset_button_state) | 208 if (state_ != STATE_DISABLED && reset_button_state) |
209 SetState(STATE_NORMAL); | 209 SetState(STATE_NORMAL); |
210 if (ink_drop_delegate()) | 210 AnimateInkDrop(views::InkDropState::HIDDEN); |
211 ink_drop_delegate()->OnAction(views::InkDropState::HIDDEN); | |
212 } | 211 } |
213 | 212 |
214 void CustomButton::OnMouseEntered(const ui::MouseEvent& event) { | 213 void CustomButton::OnMouseEntered(const ui::MouseEvent& event) { |
215 if (state_ != STATE_DISABLED) | 214 if (state_ != STATE_DISABLED) |
216 SetState(STATE_HOVERED); | 215 SetState(STATE_HOVERED); |
217 } | 216 } |
218 | 217 |
219 void CustomButton::OnMouseExited(const ui::MouseEvent& event) { | 218 void CustomButton::OnMouseExited(const ui::MouseEvent& event) { |
220 // Starting a drag results in a MouseExited, we need to ignore it. | 219 // Starting a drag results in a MouseExited, we need to ignore it. |
221 if (state_ != STATE_DISABLED && !InDrag()) | 220 if (state_ != STATE_DISABLED && !InDrag()) |
222 SetState(STATE_NORMAL); | 221 SetState(STATE_NORMAL); |
223 } | 222 } |
224 | 223 |
225 void CustomButton::OnMouseMoved(const ui::MouseEvent& event) { | 224 void CustomButton::OnMouseMoved(const ui::MouseEvent& event) { |
226 if (state_ != STATE_DISABLED) | 225 if (state_ != STATE_DISABLED) |
227 SetState(HitTestPoint(event.location()) ? STATE_HOVERED : STATE_NORMAL); | 226 SetState(HitTestPoint(event.location()) ? STATE_HOVERED : STATE_NORMAL); |
228 } | 227 } |
229 | 228 |
230 bool CustomButton::OnKeyPressed(const ui::KeyEvent& event) { | 229 bool CustomButton::OnKeyPressed(const ui::KeyEvent& event) { |
231 if (state_ == STATE_DISABLED) | 230 if (state_ == STATE_DISABLED) |
232 return false; | 231 return false; |
233 | 232 |
234 // Space sets button state to pushed. Enter clicks the button. This matches | 233 // Space sets button state to pushed. Enter clicks the button. This matches |
235 // the Windows native behavior of buttons, where Space clicks the button on | 234 // the Windows native behavior of buttons, where Space clicks the button on |
236 // KeyRelease and Enter clicks the button on KeyPressed. | 235 // KeyRelease and Enter clicks the button on KeyPressed. |
237 if (event.key_code() == ui::VKEY_SPACE) { | 236 if (event.key_code() == ui::VKEY_SPACE) { |
238 SetState(STATE_PRESSED); | 237 SetState(STATE_PRESSED); |
239 if (ink_drop_delegate() && | 238 if (ink_drop()->GetTargetInkDropState() != |
240 ink_drop_delegate()->GetTargetInkDropState() != | 239 views::InkDropState::ACTION_PENDING) |
241 views::InkDropState::ACTION_PENDING) | 240 AnimateInkDrop(views::InkDropState::ACTION_PENDING); |
242 ink_drop_delegate()->OnAction(views::InkDropState::ACTION_PENDING); | |
243 } else if (event.key_code() == ui::VKEY_RETURN) { | 241 } else if (event.key_code() == ui::VKEY_RETURN) { |
244 SetState(STATE_NORMAL); | 242 SetState(STATE_NORMAL); |
245 NotifyClick(event); | 243 NotifyClick(event); |
246 } else { | 244 } else { |
247 return false; | 245 return false; |
248 } | 246 } |
249 return true; | 247 return true; |
250 } | 248 } |
251 | 249 |
252 bool CustomButton::OnKeyReleased(const ui::KeyEvent& event) { | 250 bool CustomButton::OnKeyReleased(const ui::KeyEvent& event) { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 | 304 |
307 void CustomButton::ShowContextMenu(const gfx::Point& p, | 305 void CustomButton::ShowContextMenu(const gfx::Point& p, |
308 ui::MenuSourceType source_type) { | 306 ui::MenuSourceType source_type) { |
309 if (!context_menu_controller()) | 307 if (!context_menu_controller()) |
310 return; | 308 return; |
311 | 309 |
312 // We're about to show the context menu. Showing the context menu likely means | 310 // We're about to show the context menu. Showing the context menu likely means |
313 // we won't get a mouse exited and reset state. Reset it now to be sure. | 311 // we won't get a mouse exited and reset state. Reset it now to be sure. |
314 if (state_ != STATE_DISABLED) | 312 if (state_ != STATE_DISABLED) |
315 SetState(STATE_NORMAL); | 313 SetState(STATE_NORMAL); |
316 if (hide_ink_drop_when_showing_context_menu_ && ink_drop_delegate()) { | 314 if (hide_ink_drop_when_showing_context_menu_) { |
317 ink_drop_delegate()->SetHovered(false); | 315 ink_drop()->SetHovered(false); |
318 ink_drop_delegate()->OnAction(InkDropState::HIDDEN); | 316 AnimateInkDrop(InkDropState::HIDDEN); |
319 } | 317 } |
320 View::ShowContextMenu(p, source_type); | 318 View::ShowContextMenu(p, source_type); |
321 } | 319 } |
322 | 320 |
323 void CustomButton::OnDragDone() { | 321 void CustomButton::OnDragDone() { |
324 // Only reset the state to normal if the button isn't currently disabled | 322 // Only reset the state to normal if the button isn't currently disabled |
325 // (since disabled buttons may still be able to be dragged). | 323 // (since disabled buttons may still be able to be dragged). |
326 if (state_ != STATE_DISABLED) | 324 if (state_ != STATE_DISABLED) |
327 SetState(STATE_NORMAL); | 325 SetState(STATE_NORMAL); |
328 if (ink_drop_delegate()) | 326 AnimateInkDrop(InkDropState::HIDDEN); |
329 ink_drop_delegate()->OnAction(InkDropState::HIDDEN); | |
330 } | 327 } |
331 | 328 |
332 void CustomButton::GetAccessibleState(ui::AXViewState* state) { | 329 void CustomButton::GetAccessibleState(ui::AXViewState* state) { |
333 Button::GetAccessibleState(state); | 330 Button::GetAccessibleState(state); |
334 switch (state_) { | 331 switch (state_) { |
335 case STATE_HOVERED: | 332 case STATE_HOVERED: |
336 state->AddStateFlag(ui::AX_STATE_HOVERED); | 333 state->AddStateFlag(ui::AX_STATE_HOVERED); |
337 break; | 334 break; |
338 case STATE_PRESSED: | 335 case STATE_PRESSED: |
339 state->AddStateFlag(ui::AX_STATE_PRESSED); | 336 state->AddStateFlag(ui::AX_STATE_PRESSED); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 void CustomButton::ViewHierarchyChanged( | 373 void CustomButton::ViewHierarchyChanged( |
377 const ViewHierarchyChangedDetails& details) { | 374 const ViewHierarchyChangedDetails& details) { |
378 if (!details.is_add && state_ != STATE_DISABLED) | 375 if (!details.is_add && state_ != STATE_DISABLED) |
379 SetState(STATE_NORMAL); | 376 SetState(STATE_NORMAL); |
380 } | 377 } |
381 | 378 |
382 void CustomButton::OnBlur() { | 379 void CustomButton::OnBlur() { |
383 Button::OnBlur(); | 380 Button::OnBlur(); |
384 if (IsHotTracked() || state_ == STATE_PRESSED) { | 381 if (IsHotTracked() || state_ == STATE_PRESSED) { |
385 SetState(STATE_NORMAL); | 382 SetState(STATE_NORMAL); |
386 if (ink_drop_delegate() && | 383 if (ink_drop()->GetTargetInkDropState() != views::InkDropState::HIDDEN) |
387 ink_drop_delegate()->GetTargetInkDropState() != | 384 AnimateInkDrop(views::InkDropState::HIDDEN); |
388 views::InkDropState::HIDDEN) | |
389 ink_drop_delegate()->OnAction(views::InkDropState::HIDDEN); | |
390 // TODO(bruthig) : Fix CustomButtons to work well when multiple input | 385 // TODO(bruthig) : Fix CustomButtons to work well when multiple input |
391 // methods are interacting with a button.e.g.By animating to HIDDEN here | 386 // methods are interacting with a button.e.g.By animating to HIDDEN here |
392 // it is possible for a Mouse Release to trigger an action however there | 387 // it is possible for a Mouse Release to trigger an action however there |
393 // would be no visual cue to the user that this will occur. | 388 // would be no visual cue to the user that this will occur. |
394 } | 389 } |
395 } | 390 } |
396 | 391 |
397 bool CustomButton::ShouldShowInkDropForFocus() const { | 392 bool CustomButton::ShouldShowInkDropForFocus() const { |
398 return true; | 393 return true; |
399 } | 394 } |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 } | 452 } |
458 #endif | 453 #endif |
459 | 454 |
460 return check_mouse_position && IsMouseHovered(); | 455 return check_mouse_position && IsMouseHovered(); |
461 } | 456 } |
462 | 457 |
463 //////////////////////////////////////////////////////////////////////////////// | 458 //////////////////////////////////////////////////////////////////////////////// |
464 // CustomButton, Button overrides (protected): | 459 // CustomButton, Button overrides (protected): |
465 | 460 |
466 void CustomButton::NotifyClick(const ui::Event& event) { | 461 void CustomButton::NotifyClick(const ui::Event& event) { |
467 if (ink_drop_delegate() && has_ink_drop_action_on_click_) | 462 if (has_ink_drop_action_on_click_) |
468 ink_drop_delegate()->OnAction(ink_drop_action_on_click_); | 463 AnimateInkDrop(ink_drop_action_on_click_); |
469 Button::NotifyClick(event); | 464 Button::NotifyClick(event); |
470 } | 465 } |
471 | 466 |
472 void CustomButton::OnClickCanceled(const ui::Event& event) { | 467 void CustomButton::OnClickCanceled(const ui::Event& event) { |
473 if (ink_drop_delegate()) | 468 AnimateInkDrop(views::InkDropState::HIDDEN); |
474 ink_drop_delegate()->OnAction(views::InkDropState::HIDDEN); | |
475 Button::OnClickCanceled(event); | 469 Button::OnClickCanceled(event); |
476 } | 470 } |
477 | 471 |
478 } // namespace views | 472 } // namespace views |
OLD | NEW |