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
3 // found in the LICENSE file.
5 #include "ui/views/controls/menu/menu_event_dispatcher.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "ui/aura/window.h"
9 #include "ui/events/event_utils.h"
10 #include "ui/events/keycodes/keyboard_code_conversion.h"
11 #include "ui/events/keycodes/keyboard_codes.h"
12 #include "ui/views/controls/menu/menu_controller.h"
13 #include "ui/views/widget/widget.h"
18 MenuEventDispatcher::MenuEventDispatcher(MenuController
* controller
)
19 : menu_controller_(controller
) {}
21 MenuEventDispatcher::~MenuEventDispatcher() {}
23 bool MenuEventDispatcher::CanDispatchEvent(const ui::PlatformEvent
& event
) {
27 uint32_t MenuEventDispatcher::DispatchEvent(const ui::PlatformEvent
& event
) {
28 bool should_quit
= false;
29 bool should_perform_default
= true;
30 bool should_process_event
= true;
32 // Check if the event should be handled.
33 scoped_ptr
<ui::Event
> ui_event(ui::EventFromNative(event
));
34 if (ui_event
&& menu_controller_
->owner()) {
35 aura::Window
* menu_window
= menu_controller_
->owner()->GetNativeWindow();
36 aura::Window
* target_window
= static_cast<aura::Window
*>(
37 static_cast<ui::EventTarget
*>(menu_window
->GetRootWindow())->
38 GetEventTargeter()->FindTargetForEvent(menu_window
,
40 // TODO(flackr): The event shouldn't be handled if target_window is not
41 // menu_window, however the event targeter does not properly target the
42 // open menu. For now, we allow targeters to prevent handling by the menu.
44 should_process_event
= false;
47 if (menu_controller_
->exit_type() == MenuController::EXIT_ALL
||
48 menu_controller_
->exit_type() == MenuController::EXIT_DESTROYED
) {
50 } else if (ui_event
&& should_process_event
) {
51 switch (ui_event
->type()) {
52 case ui::ET_KEY_PRESSED
: {
53 ui::KeyEvent
* key_event
= static_cast<ui::KeyEvent
*>(ui_event
.get());
54 if (!menu_controller_
->OnKeyDown(key_event
->key_code())) {
56 should_perform_default
= false;
60 // Do not check mnemonics if the Alt or Ctrl modifiers are pressed.
61 int flags
= key_event
->flags();
62 if ((flags
& (ui::EF_CONTROL_DOWN
| ui::EF_ALT_DOWN
)) == 0) {
63 char c
= ui::GetCharacterFromKeyCode(key_event
->key_code(), flags
);
64 if (menu_controller_
->SelectByChar(c
)) {
66 should_perform_default
= false;
71 should_perform_default
= false;
74 case ui::ET_KEY_RELEASED
:
76 should_perform_default
= false;
78 case ui::ET_TOUCH_RELEASED
:
79 case ui::ET_TOUCH_CANCELLED
:
80 // Don't allow the event copy to clear the native touch id
81 // mapping, or we'll lose the mapping before the initial event
82 // has finished being dispatched.
83 static_cast<ui::TouchEvent
*>(ui_event
.get())
84 ->set_should_remove_native_touch_id_mapping(false);
91 if (should_quit
|| menu_controller_
->exit_type() != MenuController::EXIT_NONE
)
92 menu_controller_
->TerminateNestedMessageLoop();
94 return should_perform_default
? ui::POST_DISPATCH_PERFORM_DEFAULT
95 : ui::POST_DISPATCH_NONE
;
98 } // namespace internal