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 bool should_enter_pushed = ShouldEnterPushedState(event); | 166 bool should_enter_pushed = ShouldEnterPushedState(event); |
168 if (HitTestPoint(event.location())) { | 167 if (HitTestPoint(event.location())) { |
169 SetState(should_enter_pushed ? STATE_PRESSED : STATE_HOVERED); | 168 SetState(should_enter_pushed ? STATE_PRESSED : STATE_HOVERED); |
170 if (!InDrag() && should_enter_pushed && ink_drop_delegate() && | 169 if (!InDrag() && should_enter_pushed && |
171 ink_drop_delegate()->GetTargetInkDropState() == | 170 ink_drop()->GetTargetInkDropState() == views::InkDropState::HIDDEN) { |
172 views::InkDropState::HIDDEN) | 171 AnimateInkDrop(views::InkDropState::ACTION_PENDING); |
173 ink_drop_delegate()->OnAction(views::InkDropState::ACTION_PENDING); | 172 } |
174 } else { | 173 } else { |
175 SetState(STATE_NORMAL); | 174 SetState(STATE_NORMAL); |
176 if (!InDrag() && should_enter_pushed && ink_drop_delegate() && | 175 if (!InDrag() && should_enter_pushed && |
177 ink_drop_delegate()->GetTargetInkDropState() == | 176 ink_drop()->GetTargetInkDropState() == |
178 views::InkDropState::ACTION_PENDING) | 177 views::InkDropState::ACTION_PENDING) { |
179 ink_drop_delegate()->OnAction(views::InkDropState::HIDDEN); | 178 AnimateInkDrop(views::InkDropState::HIDDEN); |
| 179 } |
180 } | 180 } |
181 } | 181 } |
182 return true; | 182 return true; |
183 } | 183 } |
184 | 184 |
185 void CustomButton::OnMouseReleased(const ui::MouseEvent& event) { | 185 void CustomButton::OnMouseReleased(const ui::MouseEvent& event) { |
186 if (state_ != STATE_DISABLED) { | 186 if (state_ != STATE_DISABLED) { |
187 if (!HitTestPoint(event.location())) { | 187 if (!HitTestPoint(event.location())) { |
188 SetState(STATE_NORMAL); | 188 SetState(STATE_NORMAL); |
189 } else { | 189 } else { |
(...skipping 11 matching lines...) Expand all Loading... |
201 } | 201 } |
202 | 202 |
203 void CustomButton::OnMouseCaptureLost() { | 203 void CustomButton::OnMouseCaptureLost() { |
204 // Starting a drag results in a MouseCaptureLost. Reset button state. | 204 // Starting a drag results in a MouseCaptureLost. Reset button state. |
205 // TODO(varkha) While in drag only reset the state with Material Design. | 205 // TODO(varkha) While in drag only reset the state with Material Design. |
206 // The same logic may applies everywhere so gather any feedback and update. | 206 // The same logic may applies everywhere so gather any feedback and update. |
207 bool reset_button_state = | 207 bool reset_button_state = |
208 !InDrag() || ui::MaterialDesignController::IsModeMaterial(); | 208 !InDrag() || ui::MaterialDesignController::IsModeMaterial(); |
209 if (state_ != STATE_DISABLED && reset_button_state) | 209 if (state_ != STATE_DISABLED && reset_button_state) |
210 SetState(STATE_NORMAL); | 210 SetState(STATE_NORMAL); |
211 if (ink_drop_delegate()) | 211 AnimateInkDrop(views::InkDropState::HIDDEN); |
212 ink_drop_delegate()->OnAction(views::InkDropState::HIDDEN); | |
213 } | 212 } |
214 | 213 |
215 void CustomButton::OnMouseEntered(const ui::MouseEvent& event) { | 214 void CustomButton::OnMouseEntered(const ui::MouseEvent& event) { |
216 if (state_ != STATE_DISABLED) | 215 if (state_ != STATE_DISABLED) |
217 SetState(STATE_HOVERED); | 216 SetState(STATE_HOVERED); |
218 } | 217 } |
219 | 218 |
220 void CustomButton::OnMouseExited(const ui::MouseEvent& event) { | 219 void CustomButton::OnMouseExited(const ui::MouseEvent& event) { |
221 // Starting a drag results in a MouseExited, we need to ignore it. | 220 // Starting a drag results in a MouseExited, we need to ignore it. |
222 if (state_ != STATE_DISABLED && !InDrag()) | 221 if (state_ != STATE_DISABLED && !InDrag()) |
223 SetState(STATE_NORMAL); | 222 SetState(STATE_NORMAL); |
224 } | 223 } |
225 | 224 |
226 void CustomButton::OnMouseMoved(const ui::MouseEvent& event) { | 225 void CustomButton::OnMouseMoved(const ui::MouseEvent& event) { |
227 if (state_ != STATE_DISABLED) | 226 if (state_ != STATE_DISABLED) |
228 SetState(HitTestPoint(event.location()) ? STATE_HOVERED : STATE_NORMAL); | 227 SetState(HitTestPoint(event.location()) ? STATE_HOVERED : STATE_NORMAL); |
229 } | 228 } |
230 | 229 |
231 bool CustomButton::OnKeyPressed(const ui::KeyEvent& event) { | 230 bool CustomButton::OnKeyPressed(const ui::KeyEvent& event) { |
232 if (state_ == STATE_DISABLED) | 231 if (state_ == STATE_DISABLED) |
233 return false; | 232 return false; |
234 | 233 |
235 // Space sets button state to pushed. Enter clicks the button. This matches | 234 // Space sets button state to pushed. Enter clicks the button. This matches |
236 // the Windows native behavior of buttons, where Space clicks the button on | 235 // the Windows native behavior of buttons, where Space clicks the button on |
237 // KeyRelease and Enter clicks the button on KeyPressed. | 236 // KeyRelease and Enter clicks the button on KeyPressed. |
238 if (event.key_code() == ui::VKEY_SPACE) { | 237 if (event.key_code() == ui::VKEY_SPACE) { |
239 SetState(STATE_PRESSED); | 238 SetState(STATE_PRESSED); |
240 if (ink_drop_delegate() && | 239 if (ink_drop()->GetTargetInkDropState() != |
241 ink_drop_delegate()->GetTargetInkDropState() != | 240 views::InkDropState::ACTION_PENDING) { |
242 views::InkDropState::ACTION_PENDING) | 241 AnimateInkDrop(views::InkDropState::ACTION_PENDING); |
243 ink_drop_delegate()->OnAction(views::InkDropState::ACTION_PENDING); | 242 } |
244 } else if (event.key_code() == ui::VKEY_RETURN) { | 243 } else if (event.key_code() == ui::VKEY_RETURN) { |
245 SetState(STATE_NORMAL); | 244 SetState(STATE_NORMAL); |
246 NotifyClick(event); | 245 NotifyClick(event); |
247 } else { | 246 } else { |
248 return false; | 247 return false; |
249 } | 248 } |
250 return true; | 249 return true; |
251 } | 250 } |
252 | 251 |
253 bool CustomButton::OnKeyReleased(const ui::KeyEvent& event) { | 252 bool CustomButton::OnKeyReleased(const ui::KeyEvent& event) { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 | 306 |
308 void CustomButton::ShowContextMenu(const gfx::Point& p, | 307 void CustomButton::ShowContextMenu(const gfx::Point& p, |
309 ui::MenuSourceType source_type) { | 308 ui::MenuSourceType source_type) { |
310 if (!context_menu_controller()) | 309 if (!context_menu_controller()) |
311 return; | 310 return; |
312 | 311 |
313 // We're about to show the context menu. Showing the context menu likely means | 312 // We're about to show the context menu. Showing the context menu likely means |
314 // we won't get a mouse exited and reset state. Reset it now to be sure. | 313 // we won't get a mouse exited and reset state. Reset it now to be sure. |
315 if (state_ != STATE_DISABLED) | 314 if (state_ != STATE_DISABLED) |
316 SetState(STATE_NORMAL); | 315 SetState(STATE_NORMAL); |
317 if (hide_ink_drop_when_showing_context_menu_ && ink_drop_delegate()) { | 316 if (hide_ink_drop_when_showing_context_menu_) { |
318 ink_drop_delegate()->SetHovered(false); | 317 ink_drop()->SetHovered(false); |
319 ink_drop_delegate()->OnAction(InkDropState::HIDDEN); | 318 AnimateInkDrop(InkDropState::HIDDEN); |
320 } | 319 } |
321 View::ShowContextMenu(p, source_type); | 320 View::ShowContextMenu(p, source_type); |
322 } | 321 } |
323 | 322 |
324 void CustomButton::OnDragDone() { | 323 void CustomButton::OnDragDone() { |
325 // Only reset the state to normal if the button isn't currently disabled | 324 // Only reset the state to normal if the button isn't currently disabled |
326 // (since disabled buttons may still be able to be dragged). | 325 // (since disabled buttons may still be able to be dragged). |
327 if (state_ != STATE_DISABLED) | 326 if (state_ != STATE_DISABLED) |
328 SetState(STATE_NORMAL); | 327 SetState(STATE_NORMAL); |
329 if (ink_drop_delegate()) | 328 AnimateInkDrop(InkDropState::HIDDEN); |
330 ink_drop_delegate()->OnAction(InkDropState::HIDDEN); | |
331 } | 329 } |
332 | 330 |
333 void CustomButton::GetAccessibleState(ui::AXViewState* state) { | 331 void CustomButton::GetAccessibleState(ui::AXViewState* state) { |
334 Button::GetAccessibleState(state); | 332 Button::GetAccessibleState(state); |
335 switch (state_) { | 333 switch (state_) { |
336 case STATE_HOVERED: | 334 case STATE_HOVERED: |
337 state->AddStateFlag(ui::AX_STATE_HOVERED); | 335 state->AddStateFlag(ui::AX_STATE_HOVERED); |
338 break; | 336 break; |
339 case STATE_PRESSED: | 337 case STATE_PRESSED: |
340 state->AddStateFlag(ui::AX_STATE_PRESSED); | 338 state->AddStateFlag(ui::AX_STATE_PRESSED); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 void CustomButton::ViewHierarchyChanged( | 375 void CustomButton::ViewHierarchyChanged( |
378 const ViewHierarchyChangedDetails& details) { | 376 const ViewHierarchyChangedDetails& details) { |
379 if (!details.is_add && state_ != STATE_DISABLED) | 377 if (!details.is_add && state_ != STATE_DISABLED) |
380 SetState(STATE_NORMAL); | 378 SetState(STATE_NORMAL); |
381 } | 379 } |
382 | 380 |
383 void CustomButton::OnBlur() { | 381 void CustomButton::OnBlur() { |
384 Button::OnBlur(); | 382 Button::OnBlur(); |
385 if (IsHotTracked() || state_ == STATE_PRESSED) { | 383 if (IsHotTracked() || state_ == STATE_PRESSED) { |
386 SetState(STATE_NORMAL); | 384 SetState(STATE_NORMAL); |
387 if (ink_drop_delegate() && | 385 if (ink_drop()->GetTargetInkDropState() != views::InkDropState::HIDDEN) |
388 ink_drop_delegate()->GetTargetInkDropState() != | 386 AnimateInkDrop(views::InkDropState::HIDDEN); |
389 views::InkDropState::HIDDEN) | |
390 ink_drop_delegate()->OnAction(views::InkDropState::HIDDEN); | |
391 // TODO(bruthig) : Fix CustomButtons to work well when multiple input | 387 // TODO(bruthig) : Fix CustomButtons to work well when multiple input |
392 // methods are interacting with a button.e.g.By animating to HIDDEN here | 388 // methods are interacting with a button.e.g.By animating to HIDDEN here |
393 // it is possible for a Mouse Release to trigger an action however there | 389 // it is possible for a Mouse Release to trigger an action however there |
394 // would be no visual cue to the user that this will occur. | 390 // would be no visual cue to the user that this will occur. |
395 } | 391 } |
396 } | 392 } |
397 | 393 |
398 bool CustomButton::ShouldShowInkDropForFocus() const { | 394 bool CustomButton::ShouldShowInkDropForFocus() const { |
399 return true; | 395 return true; |
400 } | 396 } |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 } | 454 } |
459 #endif | 455 #endif |
460 | 456 |
461 return check_mouse_position && IsMouseHovered(); | 457 return check_mouse_position && IsMouseHovered(); |
462 } | 458 } |
463 | 459 |
464 //////////////////////////////////////////////////////////////////////////////// | 460 //////////////////////////////////////////////////////////////////////////////// |
465 // CustomButton, Button overrides (protected): | 461 // CustomButton, Button overrides (protected): |
466 | 462 |
467 void CustomButton::NotifyClick(const ui::Event& event) { | 463 void CustomButton::NotifyClick(const ui::Event& event) { |
468 if (ink_drop_delegate() && has_ink_drop_action_on_click_) | 464 if (has_ink_drop_action_on_click_) |
469 ink_drop_delegate()->OnAction(ink_drop_action_on_click_); | 465 AnimateInkDrop(ink_drop_action_on_click_); |
470 Button::NotifyClick(event); | 466 Button::NotifyClick(event); |
471 } | 467 } |
472 | 468 |
473 void CustomButton::OnClickCanceled(const ui::Event& event) { | 469 void CustomButton::OnClickCanceled(const ui::Event& event) { |
474 if (ink_drop_delegate()) | 470 AnimateInkDrop(views::InkDropState::HIDDEN); |
475 ink_drop_delegate()->OnAction(views::InkDropState::HIDDEN); | |
476 Button::OnClickCanceled(event); | 471 Button::OnClickCanceled(event); |
477 } | 472 } |
478 | 473 |
479 } // namespace views | 474 } // namespace views |
OLD | NEW |