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_node_data.h" | 7 #include "ui/accessibility/ax_node_data.h" |
8 #include "ui/events/event.h" | 8 #include "ui/events/event.h" |
9 #include "ui/events/event_utils.h" | 9 #include "ui/events/event_utils.h" |
10 #include "ui/events/keycodes/keyboard_codes.h" | 10 #include "ui/events/keycodes/keyboard_codes.h" |
11 #include "ui/gfx/animation/throb_animation.h" | 11 #include "ui/gfx/animation/throb_animation.h" |
12 #include "ui/gfx/color_palette.h" | 12 #include "ui/gfx/color_palette.h" |
13 #include "ui/native_theme/native_theme.h" | 13 #include "ui/native_theme/native_theme.h" |
14 #include "ui/views/animation/ink_drop_highlight.h" | 14 #include "ui/views/animation/ink_drop_highlight.h" |
15 #include "ui/views/animation/ink_drop_impl.h" | 15 #include "ui/views/animation/ink_drop_impl.h" |
16 #include "ui/views/controls/button/blue_button.h" | 16 #include "ui/views/controls/button/blue_button.h" |
17 #include "ui/views/controls/button/checkbox.h" | 17 #include "ui/views/controls/button/checkbox.h" |
18 #include "ui/views/controls/button/image_button.h" | 18 #include "ui/views/controls/button/image_button.h" |
19 #include "ui/views/controls/button/label_button.h" | 19 #include "ui/views/controls/button/label_button.h" |
20 #include "ui/views/controls/button/menu_button.h" | 20 #include "ui/views/controls/button/menu_button.h" |
21 #include "ui/views/controls/button/radio_button.h" | 21 #include "ui/views/controls/button/radio_button.h" |
22 #include "ui/views/controls/button/toggle_button.h" | 22 #include "ui/views/controls/button/toggle_button.h" |
23 #include "ui/views/style/platform_style.h" | |
23 #include "ui/views/widget/widget.h" | 24 #include "ui/views/widget/widget.h" |
24 | 25 |
25 #if defined(USE_AURA) | 26 #if defined(USE_AURA) |
26 #include "ui/aura/client/capture_client.h" | 27 #include "ui/aura/client/capture_client.h" |
27 #include "ui/aura/window.h" | 28 #include "ui/aura/window.h" |
28 #endif | 29 #endif |
29 | 30 |
30 namespace views { | 31 namespace views { |
31 | 32 |
32 namespace { | 33 namespace { |
33 | 34 |
34 // How long the hover animation takes if uninterrupted. | 35 // How long the hover animation takes if uninterrupted. |
35 const int kHoverFadeDurationMs = 150; | 36 const int kHoverFadeDurationMs = 150; |
36 | 37 |
38 CustomButton::ButtonAction GetButtonActionForKeyEvent( | |
39 const ui::KeyEvent& event) { | |
40 if (event.key_code() == ui::VKEY_SPACE) | |
41 return PlatformStyle::kButtonActionOnSpace; | |
42 else if (event.key_code() == ui::VKEY_RETURN && | |
tapted
2017/01/04 04:54:31
nit: "no else after return" - https://chromium.goo
karandeepb
2017/01/04 06:14:45
Done.
| |
43 PlatformStyle::kClickControlOnReturn) | |
44 return CustomButton::ACTION_CLICK; | |
45 return CustomButton::ACTION_NONE; | |
46 } | |
47 | |
37 } // namespace | 48 } // namespace |
38 | 49 |
39 //////////////////////////////////////////////////////////////////////////////// | 50 //////////////////////////////////////////////////////////////////////////////// |
40 // CustomButton, public: | 51 // CustomButton, public: |
41 | 52 |
42 // static | 53 // static |
43 const char CustomButton::kViewClassName[] = "CustomButton"; | 54 const char CustomButton::kViewClassName[] = "CustomButton"; |
44 | 55 |
45 // static | 56 // static |
46 const CustomButton* CustomButton::AsCustomButton(const views::View* view) { | 57 const CustomButton* CustomButton::AsCustomButton(const views::View* view) { |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
231 | 242 |
232 void CustomButton::OnMouseMoved(const ui::MouseEvent& event) { | 243 void CustomButton::OnMouseMoved(const ui::MouseEvent& event) { |
233 if (state_ != STATE_DISABLED) | 244 if (state_ != STATE_DISABLED) |
234 SetState(HitTestPoint(event.location()) ? STATE_HOVERED : STATE_NORMAL); | 245 SetState(HitTestPoint(event.location()) ? STATE_HOVERED : STATE_NORMAL); |
235 } | 246 } |
236 | 247 |
237 bool CustomButton::OnKeyPressed(const ui::KeyEvent& event) { | 248 bool CustomButton::OnKeyPressed(const ui::KeyEvent& event) { |
238 if (state_ == STATE_DISABLED) | 249 if (state_ == STATE_DISABLED) |
239 return false; | 250 return false; |
240 | 251 |
241 // Space sets button state to pushed. Enter clicks the button. This matches | 252 switch (GetButtonActionForKeyEvent(event)) { |
242 // the Windows native behavior of buttons, where Space clicks the button on | 253 case ButtonAction::ACTION_PRESS: |
243 // KeyRelease and Enter clicks the button on KeyPressed. | 254 SetState(STATE_PRESSED); |
244 if (event.key_code() == ui::VKEY_SPACE) { | 255 if (GetInkDrop()->GetTargetInkDropState() != |
245 SetState(STATE_PRESSED); | 256 views::InkDropState::ACTION_PENDING) { |
tapted
2017/01/04 04:54:31
views:: not needed, same below
karandeepb
2017/01/04 06:14:45
Done.
| |
246 if (GetInkDrop()->GetTargetInkDropState() != | 257 AnimateInkDrop(views::InkDropState::ACTION_PENDING, |
247 views::InkDropState::ACTION_PENDING) { | 258 nullptr /* event */); |
248 AnimateInkDrop(views::InkDropState::ACTION_PENDING, nullptr /* event */); | 259 } |
249 } | 260 return true; |
250 } else if (event.key_code() == ui::VKEY_RETURN) { | 261 break; |
tapted
2017/01/04 04:54:31
nit: no break
karandeepb
2017/01/04 06:14:45
Done.
| |
251 SetState(STATE_NORMAL); | 262 case ButtonAction::ACTION_CLICK: |
252 NotifyClick(event); | 263 SetState(STATE_NORMAL); |
253 } else { | 264 NotifyClick(event); |
265 return true; | |
266 break; | |
tapted
2017/01/04 04:54:31
nit: no break
karandeepb
2017/01/04 06:14:45
Done.
| |
267 case ButtonAction::ACTION_NONE: | |
268 return false; | |
269 } | |
270 | |
271 NOTREACHED(); | |
272 return false; | |
273 } | |
274 | |
275 bool CustomButton::OnKeyReleased(const ui::KeyEvent& event) { | |
276 const bool click_button = | |
277 state_ == STATE_PRESSED && | |
278 GetButtonActionForKeyEvent(event) == ButtonAction::ACTION_PRESS; | |
279 if (!click_button) | |
254 return false; | 280 return false; |
255 } | 281 |
282 SetState(STATE_NORMAL); | |
283 NotifyClick(event); | |
256 return true; | 284 return true; |
257 } | 285 } |
258 | 286 |
259 bool CustomButton::OnKeyReleased(const ui::KeyEvent& event) { | |
260 if ((state_ == STATE_PRESSED) && (event.key_code() == ui::VKEY_SPACE)) { | |
261 SetState(STATE_NORMAL); | |
262 NotifyClick(event); | |
263 return true; | |
264 } | |
265 return false; | |
266 } | |
267 | |
268 void CustomButton::OnGestureEvent(ui::GestureEvent* event) { | 287 void CustomButton::OnGestureEvent(ui::GestureEvent* event) { |
269 if (state_ == STATE_DISABLED) { | 288 if (state_ == STATE_DISABLED) { |
270 Button::OnGestureEvent(event); | 289 Button::OnGestureEvent(event); |
271 return; | 290 return; |
272 } | 291 } |
273 | 292 |
274 if (event->type() == ui::ET_GESTURE_TAP && IsTriggerableEvent(*event)) { | 293 if (event->type() == ui::ET_GESTURE_TAP && IsTriggerableEvent(*event)) { |
275 // Set the button state to hot and start the animation fully faded in. The | 294 // Set the button state to hot and start the animation fully faded in. The |
276 // GESTURE_END event issued immediately after will set the state to | 295 // GESTURE_END event issued immediately after will set the state to |
277 // STATE_NORMAL beginning the fade out animation. See | 296 // STATE_NORMAL beginning the fade out animation. See |
(...skipping 21 matching lines...) Expand all Loading... | |
299 // TODO(beng): remove once NotifyClick takes ui::Event. | 318 // TODO(beng): remove once NotifyClick takes ui::Event. |
300 ui::MouseEvent synthetic_event( | 319 ui::MouseEvent synthetic_event( |
301 ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(), ui::EventTimeForNow(), | 320 ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(), ui::EventTimeForNow(), |
302 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON); | 321 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON); |
303 NotifyClick(synthetic_event); | 322 NotifyClick(synthetic_event); |
304 return true; | 323 return true; |
305 } | 324 } |
306 | 325 |
307 bool CustomButton::SkipDefaultKeyEventProcessing(const ui::KeyEvent& event) { | 326 bool CustomButton::SkipDefaultKeyEventProcessing(const ui::KeyEvent& event) { |
308 // If this button is focused and the user presses space or enter, don't let | 327 // If this button is focused and the user presses space or enter, don't let |
309 // that be treated as an accelerator. | 328 // that be treated as an accelerator if there is a button action corresponding |
310 return (event.key_code() == ui::VKEY_SPACE) || | 329 // to it. |
311 (event.key_code() == ui::VKEY_RETURN); | 330 return GetButtonActionForKeyEvent(event) != ButtonAction::ACTION_NONE; |
karandeepb
2017/01/04 03:55:40
I am not entirely sure about the reason behind thi
tapted
2017/01/04 04:54:31
I think it would reach here if CustomButton::OnKey
karandeepb
2017/01/04 06:14:45
Yeah that was my guess as well. This I guess can h
tapted
2017/01/05 00:34:41
Ah. Yup.
karandeepb
2017/01/05 10:50:08
Will do!
| |
312 } | 331 } |
313 | 332 |
314 void CustomButton::ShowContextMenu(const gfx::Point& p, | 333 void CustomButton::ShowContextMenu(const gfx::Point& p, |
315 ui::MenuSourceType source_type) { | 334 ui::MenuSourceType source_type) { |
316 if (!context_menu_controller()) | 335 if (!context_menu_controller()) |
317 return; | 336 return; |
318 | 337 |
319 // We're about to show the context menu. Showing the context menu likely means | 338 // We're about to show the context menu. Showing the context menu likely means |
320 // we won't get a mouse exited and reset state. Reset it now to be sure. | 339 // we won't get a mouse exited and reset state. Reset it now to be sure. |
321 if (state_ != STATE_DISABLED) | 340 if (state_ != STATE_DISABLED) |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
475 views::InkDropState::ACTION_PENDING || | 494 views::InkDropState::ACTION_PENDING || |
476 GetInkDrop()->GetTargetInkDropState() == | 495 GetInkDrop()->GetTargetInkDropState() == |
477 views::InkDropState::ALTERNATE_ACTION_PENDING) { | 496 views::InkDropState::ALTERNATE_ACTION_PENDING) { |
478 AnimateInkDrop(views::InkDropState::HIDDEN, | 497 AnimateInkDrop(views::InkDropState::HIDDEN, |
479 ui::LocatedEvent::FromIfValid(&event)); | 498 ui::LocatedEvent::FromIfValid(&event)); |
480 } | 499 } |
481 Button::OnClickCanceled(event); | 500 Button::OnClickCanceled(event); |
482 } | 501 } |
483 | 502 |
484 } // namespace views | 503 } // namespace views |
OLD | NEW |