OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "ash/accelerators/nested_accelerator_delegate.h" | 5 #include "ash/accelerators/nested_accelerator_delegate.h" |
6 | 6 |
7 #include "ash/accelerators/accelerator_controller.h" | 7 #include "ash/accelerators/accelerator_controller.h" |
8 #include "ash/shell.h" | 8 #include "ash/shell.h" |
9 #include "ui/aura/window_event_dispatcher.h" | 9 #include "ui/aura/window_event_dispatcher.h" |
10 #include "ui/base/accelerators/accelerator.h" | 10 #include "ui/base/accelerators/accelerator.h" |
11 #include "ui/events/event.h" | 11 #include "ui/events/event.h" |
12 #include "ui/events/event_constants.h" | 12 #include "ui/events/event_constants.h" |
13 #include "ui/events/event_utils.h" | 13 #include "ui/events/event_utils.h" |
14 #include "ui/views/controls/menu/menu_controller.h" | 14 #include "ui/views/controls/menu/menu_controller.h" |
15 | 15 |
16 namespace ash { | 16 namespace ash { |
17 namespace { | 17 namespace { |
18 | 18 |
19 bool IsPossibleAcceleratorNotForMenu(const ui::KeyEvent& key_event) { | 19 bool IsPossibleAcceleratorNotForMenu(const ui::Accelerator& accelerator) { |
20 // For shortcuts generated by Ctrl or Alt plus a letter, number or | 20 // For shortcuts generated by Ctrl or Alt plus a letter, number or |
21 // the tab key, we want to exit the context menu first and then | 21 // the tab key, we want to exit the context menu first and then |
22 // repost the event. That allows for the shortcut execution after | 22 // repost the event. That allows for the shortcut execution after |
23 // the context menu has exited. | 23 // the context menu has exited. |
24 if (key_event.type() == ui::ET_KEY_PRESSED && | 24 if (accelerator.type() == ui::ET_KEY_PRESSED && |
25 (key_event.flags() & (ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN))) { | 25 (accelerator.modifiers() & (ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN))) { |
26 const ui::KeyboardCode key_code = key_event.key_code(); | 26 const ui::KeyboardCode key_code = accelerator.key_code(); |
27 if ((key_code >= ui::VKEY_A && key_code <= ui::VKEY_Z) || | 27 if ((key_code >= ui::VKEY_A && key_code <= ui::VKEY_Z) || |
28 (key_code >= ui::VKEY_0 && key_code <= ui::VKEY_9) || | 28 (key_code >= ui::VKEY_0 && key_code <= ui::VKEY_9) || |
29 (key_code == ui::VKEY_TAB)) { | 29 (key_code == ui::VKEY_TAB)) { |
30 return true; | 30 return true; |
31 } | 31 } |
32 } | 32 } |
33 return false; | 33 return false; |
34 } | 34 } |
35 | 35 |
36 } // namespace | 36 bool ShouldProcessAcceleratorNow(const ui::Accelerator& accelerator) { |
37 | 37 if (!IsPossibleAcceleratorNotForMenu(accelerator)) |
38 NestedAcceleratorDelegate::NestedAcceleratorDelegate() { | |
39 } | |
40 | |
41 NestedAcceleratorDelegate::~NestedAcceleratorDelegate() { | |
42 } | |
43 | |
44 bool NestedAcceleratorDelegate::ShouldProcessEventNow( | |
45 const ui::KeyEvent& key_event) { | |
46 if (!IsPossibleAcceleratorNotForMenu(key_event)) | |
47 return true; | 38 return true; |
48 | 39 |
49 if (views::MenuController* menu_controller = | 40 if (views::MenuController* menu_controller = |
50 views::MenuController::GetActiveInstance()) { | 41 views::MenuController::GetActiveInstance()) { |
51 menu_controller->CancelAll(); | 42 menu_controller->CancelAll(); |
52 return false; | 43 return false; |
53 } | 44 } |
54 return true; | 45 return true; |
55 } | 46 } |
56 | 47 |
57 bool NestedAcceleratorDelegate::ProcessEvent(const ui::KeyEvent& key_event) { | 48 } // namespace |
| 49 |
| 50 NestedAcceleratorDelegate::NestedAcceleratorDelegate() { |
| 51 } |
| 52 |
| 53 NestedAcceleratorDelegate::~NestedAcceleratorDelegate() { |
| 54 } |
| 55 |
| 56 NestedAcceleratorDelegate::Result NestedAcceleratorDelegate::ProcessAccelerator( |
| 57 const ui::Accelerator& accelerator) { |
| 58 if (!ShouldProcessAcceleratorNow(accelerator)) |
| 59 return RESULT_PROCESS_LATER; |
| 60 |
58 ash::AcceleratorController* accelerator_controller = | 61 ash::AcceleratorController* accelerator_controller = |
59 ash::Shell::GetInstance()->accelerator_controller(); | 62 ash::Shell::GetInstance()->accelerator_controller(); |
60 if (!accelerator_controller) | 63 if (!accelerator_controller) |
61 return false; | 64 return RESULT_NOT_PROCESSED; |
62 const int kModifierMask = | 65 |
63 (ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN); | |
64 ui::Accelerator accelerator(key_event.key_code(), | |
65 key_event.flags() & kModifierMask); | |
66 if (key_event.type() == ui::ET_KEY_RELEASED) | |
67 accelerator.set_type(ui::ET_KEY_RELEASED); | |
68 // Fill out context object so AcceleratorController will know what | 66 // Fill out context object so AcceleratorController will know what |
69 // was the previous accelerator or if the current accelerator is repeated. | 67 // was the previous accelerator or if the current accelerator is repeated. |
70 Shell::GetInstance()->accelerator_controller()->context()->UpdateContext( | 68 Shell::GetInstance()->accelerator_controller()->context()->UpdateContext( |
71 accelerator); | 69 accelerator); |
72 return accelerator_controller->Process(accelerator); | 70 return accelerator_controller->Process(accelerator) ? RESULT_PROCESSED |
| 71 : RESULT_NOT_PROCESSED; |
73 } | 72 } |
74 | 73 |
75 } // namespace ash | 74 } // namespace ash |
OLD | NEW |