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 "ash/accelerators/accelerator_dispatcher.h" | 5 #include "ash/accelerators/accelerator_dispatcher.h" |
6 | 6 |
7 #if defined(USE_X11) | |
8 #include <X11/Xlib.h> | |
9 #endif // defined(USE_X11) | |
10 | |
11 #include "ash/accelerators/accelerator_controller.h" | 7 #include "ash/accelerators/accelerator_controller.h" |
12 #include "ash/shell.h" | 8 #include "ash/shell.h" |
13 #include "ui/aura/env.h" | |
14 #include "ui/aura/window_event_dispatcher.h" | 9 #include "ui/aura/window_event_dispatcher.h" |
15 #include "ui/base/accelerators/accelerator.h" | 10 #include "ui/base/accelerators/accelerator.h" |
16 #include "ui/events/event.h" | 11 #include "ui/events/event.h" |
17 #include "ui/events/event_constants.h" | 12 #include "ui/events/event_constants.h" |
18 #include "ui/events/event_utils.h" | 13 #include "ui/events/event_utils.h" |
19 #include "ui/views/controls/menu/menu_controller.h" | 14 #include "ui/views/controls/menu/menu_controller.h" |
20 | 15 |
21 namespace ash { | 16 namespace ash { |
22 namespace { | 17 namespace { |
23 | 18 |
24 const int kModifierMask = (ui::EF_SHIFT_DOWN | | |
25 ui::EF_CONTROL_DOWN | | |
26 ui::EF_ALT_DOWN); | |
27 #if defined(OS_WIN) | |
28 bool IsKeyEvent(const MSG& msg) { | |
29 return | |
30 msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN || | |
31 msg.message == WM_KEYUP || msg.message == WM_SYSKEYUP; | |
32 } | |
33 #elif defined(USE_X11) | |
34 bool IsKeyEvent(const XEvent* xev) { | |
35 return xev->type == KeyPress || xev->type == KeyRelease; | |
36 } | |
37 #elif defined(USE_OZONE) | |
38 bool IsKeyEvent(const base::NativeEvent& native_event) { | |
39 const ui::KeyEvent* event = static_cast<const ui::KeyEvent*>(native_event); | |
40 return event->IsKeyEvent(); | |
41 } | |
42 #endif | |
43 | |
44 bool IsPossibleAcceleratorNotForMenu(const ui::KeyEvent& key_event) { | 19 bool IsPossibleAcceleratorNotForMenu(const ui::KeyEvent& key_event) { |
45 // 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 |
46 // 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 |
47 // repost the event. That allows for the shortcut execution after | 22 // repost the event. That allows for the shortcut execution after |
48 // the context menu has exited. | 23 // the context menu has exited. |
49 if (key_event.type() == ui::ET_KEY_PRESSED && | 24 if (key_event.type() == ui::ET_KEY_PRESSED && |
50 (key_event.flags() & (ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN))) { | 25 (key_event.flags() & (ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN))) { |
51 const ui::KeyboardCode key_code = key_event.key_code(); | 26 const ui::KeyboardCode key_code = key_event.key_code(); |
52 if ((key_code >= ui::VKEY_A && key_code <= ui::VKEY_Z) || | 27 if ((key_code >= ui::VKEY_A && key_code <= ui::VKEY_Z) || |
53 (key_code >= ui::VKEY_0 && key_code <= ui::VKEY_9) || | 28 (key_code >= ui::VKEY_0 && key_code <= ui::VKEY_9) || |
54 (key_code == ui::VKEY_TAB)) { | 29 (key_code == ui::VKEY_TAB)) { |
55 return true; | 30 return true; |
56 } | 31 } |
57 } | 32 } |
58 return false; | 33 return false; |
59 } | 34 } |
60 | 35 |
61 } // namespace | 36 } // namespace |
62 | 37 |
63 AcceleratorDispatcher::AcceleratorDispatcher( | 38 bool AcceleratorDispatcher::MenuClosedForPossibleAccelerator( |
64 base::MessagePumpDispatcher* nested_dispatcher) | 39 const ui::KeyEvent& key_event) { |
65 : nested_dispatcher_(nested_dispatcher) { | 40 if (!IsPossibleAcceleratorNotForMenu(key_event)) |
| 41 return false; |
| 42 |
| 43 if (views::MenuController* menu_controller = |
| 44 views::MenuController::GetActiveInstance()) { |
| 45 menu_controller->CancelAll(); |
| 46 return true; |
| 47 } |
| 48 return false; |
66 } | 49 } |
67 | 50 |
68 AcceleratorDispatcher::~AcceleratorDispatcher() { | 51 bool AcceleratorDispatcher::AcceleratorProcessedForKeyEvent( |
69 } | 52 const ui::KeyEvent& key_event) { |
70 | 53 ash::AcceleratorController* accelerator_controller = |
71 uint32_t AcceleratorDispatcher::Dispatch(const base::NativeEvent& event) { | 54 ash::Shell::GetInstance()->accelerator_controller(); |
72 if (IsKeyEvent(event)) { | 55 if (!accelerator_controller) |
73 ui::KeyEvent key_event(event, false); | 56 return false; |
74 if (IsPossibleAcceleratorNotForMenu(key_event)) { | 57 const int kModifierMask = |
75 if (views::MenuController* menu_controller = | 58 (ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN); |
76 views::MenuController::GetActiveInstance()) { | 59 ui::Accelerator accelerator(key_event.key_code(), |
77 menu_controller->CancelAll(); | 60 key_event.flags() & kModifierMask); |
78 #if defined(USE_X11) | 61 if (key_event.type() == ui::ET_KEY_RELEASED) |
79 XPutBackEvent(event->xany.display, event); | 62 accelerator.set_type(ui::ET_KEY_RELEASED); |
80 #else | 63 // Fill out context object so AcceleratorController will know what |
81 NOTIMPLEMENTED() << " Repost NativeEvent here."; | 64 // was the previous accelerator or if the current accelerator is repeated. |
82 #endif | 65 Shell::GetInstance()->accelerator_controller()->context()->UpdateContext( |
83 return POST_DISPATCH_QUIT_LOOP; | 66 accelerator); |
84 } | 67 return accelerator_controller->Process(accelerator); |
85 } | |
86 | |
87 ash::AcceleratorController* accelerator_controller = | |
88 ash::Shell::GetInstance()->accelerator_controller(); | |
89 if (accelerator_controller) { | |
90 ui::Accelerator accelerator(key_event.key_code(), | |
91 key_event.flags() & kModifierMask); | |
92 if (key_event.type() == ui::ET_KEY_RELEASED) | |
93 accelerator.set_type(ui::ET_KEY_RELEASED); | |
94 // Fill out context object so AcceleratorController will know what | |
95 // was the previous accelerator or if the current accelerator is repeated. | |
96 Shell::GetInstance()->accelerator_controller()->context()-> | |
97 UpdateContext(accelerator); | |
98 if (accelerator_controller->Process(accelerator)) | |
99 return POST_DISPATCH_NONE; | |
100 } | |
101 | |
102 return nested_dispatcher_ | |
103 ? nested_dispatcher_->Dispatch(key_event.native_event()) | |
104 : POST_DISPATCH_PERFORM_DEFAULT; | |
105 } | |
106 | |
107 return nested_dispatcher_ ? nested_dispatcher_->Dispatch(event) | |
108 : POST_DISPATCH_PERFORM_DEFAULT; | |
109 } | 68 } |
110 | 69 |
111 } // namespace ash | 70 } // namespace ash |
OLD | NEW |