Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: ui/views/controls/button/custom_button.cc

Issue 2607923002: MacViews: Handle Space and Return keys correctly for Buttons. (Closed)
Patch Set: Address review. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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::KeyClickAction GetKeyClickActionForEvent(
39 const ui::KeyEvent& event) {
40 if (event.key_code() == ui::VKEY_SPACE)
41 return PlatformStyle::kKeyClickActionOnSpace;
42 if (event.key_code() == ui::VKEY_RETURN &&
43 PlatformStyle::kReturnClicksFocusedControl)
44 return CustomButton::CLICK_ON_KEY_PRESS;
45 return CustomButton::CLICK_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
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 (GetKeyClickActionForEvent(event)) {
242 // the Windows native behavior of buttons, where Space clicks the button on 253 case KeyClickAction::CLICK_ON_KEY_RELEASE:
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 InkDropState::ACTION_PENDING) {
246 if (GetInkDrop()->GetTargetInkDropState() != 257 AnimateInkDrop(InkDropState::ACTION_PENDING, nullptr /* event */);
247 views::InkDropState::ACTION_PENDING) { 258 }
248 AnimateInkDrop(views::InkDropState::ACTION_PENDING, nullptr /* event */); 259 return true;
249 } 260 case KeyClickAction::CLICK_ON_KEY_PRESS:
250 } else if (event.key_code() == ui::VKEY_RETURN) { 261 SetState(STATE_NORMAL);
251 SetState(STATE_NORMAL); 262 NotifyClick(event);
252 NotifyClick(event); 263 return true;
253 } else { 264 case KeyClickAction::CLICK_NONE:
265 return false;
266 }
267
268 NOTREACHED();
269 return false;
270 }
271
272 bool CustomButton::OnKeyReleased(const ui::KeyEvent& event) {
273 const bool click_button =
274 state_ == STATE_PRESSED &&
275 GetKeyClickActionForEvent(event) == KeyClickAction::CLICK_ON_KEY_RELEASE;
276 if (!click_button)
254 return false; 277 return false;
255 } 278
279 SetState(STATE_NORMAL);
280 NotifyClick(event);
256 return true; 281 return true;
257 } 282 }
258 283
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) { 284 void CustomButton::OnGestureEvent(ui::GestureEvent* event) {
269 if (state_ == STATE_DISABLED) { 285 if (state_ == STATE_DISABLED) {
270 Button::OnGestureEvent(event); 286 Button::OnGestureEvent(event);
271 return; 287 return;
272 } 288 }
273 289
274 if (event->type() == ui::ET_GESTURE_TAP && IsTriggerableEvent(*event)) { 290 if (event->type() == ui::ET_GESTURE_TAP && IsTriggerableEvent(*event)) {
275 // Set the button state to hot and start the animation fully faded in. The 291 // 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 292 // GESTURE_END event issued immediately after will set the state to
277 // STATE_NORMAL beginning the fade out animation. See 293 // STATE_NORMAL beginning the fade out animation. See
(...skipping 21 matching lines...) Expand all
299 // TODO(beng): remove once NotifyClick takes ui::Event. 315 // TODO(beng): remove once NotifyClick takes ui::Event.
300 ui::MouseEvent synthetic_event( 316 ui::MouseEvent synthetic_event(
301 ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(), ui::EventTimeForNow(), 317 ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(), ui::EventTimeForNow(),
302 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON); 318 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
303 NotifyClick(synthetic_event); 319 NotifyClick(synthetic_event);
304 return true; 320 return true;
305 } 321 }
306 322
307 bool CustomButton::SkipDefaultKeyEventProcessing(const ui::KeyEvent& event) { 323 bool CustomButton::SkipDefaultKeyEventProcessing(const ui::KeyEvent& event) {
308 // If this button is focused and the user presses space or enter, don't let 324 // If this button is focused and the user presses space or enter, don't let
309 // that be treated as an accelerator. 325 // that be treated as an accelerator if there is a key click action
310 return (event.key_code() == ui::VKEY_SPACE) || 326 // corresponding to it.
311 (event.key_code() == ui::VKEY_RETURN); 327 return GetKeyClickActionForEvent(event) != KeyClickAction::CLICK_NONE;
312 } 328 }
313 329
314 void CustomButton::ShowContextMenu(const gfx::Point& p, 330 void CustomButton::ShowContextMenu(const gfx::Point& p,
315 ui::MenuSourceType source_type) { 331 ui::MenuSourceType source_type) {
316 if (!context_menu_controller()) 332 if (!context_menu_controller())
317 return; 333 return;
318 334
319 // We're about to show the context menu. Showing the context menu likely means 335 // 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. 336 // we won't get a mouse exited and reset state. Reset it now to be sure.
321 if (state_ != STATE_DISABLED) 337 if (state_ != STATE_DISABLED)
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 views::InkDropState::ACTION_PENDING || 491 views::InkDropState::ACTION_PENDING ||
476 GetInkDrop()->GetTargetInkDropState() == 492 GetInkDrop()->GetTargetInkDropState() ==
477 views::InkDropState::ALTERNATE_ACTION_PENDING) { 493 views::InkDropState::ALTERNATE_ACTION_PENDING) {
478 AnimateInkDrop(views::InkDropState::HIDDEN, 494 AnimateInkDrop(views::InkDropState::HIDDEN,
479 ui::LocatedEvent::FromIfValid(&event)); 495 ui::LocatedEvent::FromIfValid(&event));
480 } 496 }
481 Button::OnClickCanceled(event); 497 Button::OnClickCanceled(event);
482 } 498 }
483 499
484 } // namespace views 500 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/button/custom_button.h ('k') | ui/views/controls/button/custom_button_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698