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_perform_default
= true;
29 bool should_process_event
= true;
31 // Check if the event should be handled.
32 scoped_ptr
<ui::Event
> ui_event(ui::EventFromNative(event
));
33 if (ui_event
&& menu_controller_
->owner()) {
34 aura::Window
* menu_window
= menu_controller_
->owner()->GetNativeWindow();
35 aura::Window
* target_window
= static_cast<aura::Window
*>(
36 static_cast<ui::EventTarget
*>(menu_window
->GetRootWindow())->
37 GetEventTargeter()->FindTargetForEvent(menu_window
,
39 // TODO(flackr): The event shouldn't be handled if target_window is not
40 // menu_window, however the event targeter does not properly target the
41 // open menu. For now, we allow targeters to prevent handling by the menu.
43 should_process_event
= false;
46 if (menu_controller_
->exit_type() != MenuController::EXIT_ALL
&&
47 menu_controller_
->exit_type() != MenuController::EXIT_DESTROYED
&&
48 ui_event
&& should_process_event
) {
49 switch (ui_event
->type()) {
50 case ui::ET_KEY_PRESSED
: {
51 should_perform_default
= false;
53 ui::KeyEvent
* key_event
= static_cast<ui::KeyEvent
*>(ui_event
.get());
54 menu_controller_
->OnKeyDown(key_event
->key_code());
55 if (menu_controller_
->exit_type() != MenuController::EXIT_NONE
)
58 // Do not check mnemonics if the Alt or Ctrl modifiers are pressed.
59 int flags
= key_event
->flags();
60 if ((flags
& (ui::EF_CONTROL_DOWN
| ui::EF_ALT_DOWN
)) == 0) {
61 char c
= ui::DomCodeToUsLayoutCharacter(key_event
->code(), flags
);
62 menu_controller_
->SelectByChar(c
);
66 case ui::ET_KEY_RELEASED
:
67 should_perform_default
= false;
69 case ui::ET_TOUCH_RELEASED
:
70 case ui::ET_TOUCH_CANCELLED
:
71 // Don't allow the event copy to clear the native touch id
72 // mapping, or we'll lose the mapping before the initial event
73 // has finished being dispatched.
74 static_cast<ui::TouchEvent
*>(ui_event
.get())
75 ->set_should_remove_native_touch_id_mapping(false);
82 if (menu_controller_
->exit_type() != MenuController::EXIT_NONE
)
83 menu_controller_
->TerminateNestedMessageLoop();
85 return should_perform_default
? ui::POST_DISPATCH_PERFORM_DEFAULT
86 : ui::POST_DISPATCH_NONE
;
89 } // namespace internal