OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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. | |
4 | |
5 #include "ui/views/controls/menu/menu_key_event_handler.h" | |
6 | |
7 #include "ui/aura/env.h" | |
8 #include "ui/events/keycodes/keyboard_code_conversion.h" | |
9 #include "ui/views/controls/menu/menu_controller.h" | |
10 #include "ui/views/views_delegate.h" | |
11 | |
12 namespace views { | |
13 | |
14 namespace { | |
15 | |
16 const int kKeyFlagsMask = ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN; | |
17 | |
18 } // namespace | |
19 | |
20 MenuKeyEventHandler::MenuKeyEventHandler() { | |
21 aura::Env::GetInstanceDontCreate()->PrependPreTargetHandler(this); | |
22 } | |
23 | |
24 MenuKeyEventHandler::~MenuKeyEventHandler() { | |
25 aura::Env::GetInstanceDontCreate()->RemovePreTargetHandler(this); | |
26 } | |
27 | |
28 void MenuKeyEventHandler::OnKeyEvent(ui::KeyEvent* event) { | |
29 MenuController* menu_controller = MenuController::GetActiveInstance(); | |
30 DCHECK(menu_controller); | |
31 | |
32 if (menu_controller->exit_type() == MenuController::EXIT_ALL || | |
33 menu_controller->exit_type() == MenuController::EXIT_DESTROYED) { | |
34 // If the event has arrived after the menu's exit type had changed but | |
35 // before its message loop terminated, the event will continue its normal | |
36 // propagation for the following reason: | |
37 // If the user accepts a menu item in a nested menu, the menu item action is | |
38 // run after the base::RunLoop for the innermost menu has quit but before | |
39 // the base::RunLoop for the outermost menu has quit. If the menu item | |
40 // action starts a base::RunLoop, the outermost menu's base::RunLoop will | |
41 // not quit till the action's base::RunLoop ends. IDC_BOOKMARK_BAR_OPEN_ALL | |
42 // sometimes opens a modal dialog. The modal dialog starts a base::RunLoop | |
43 // and keeps the base::RunLoop running for the duration of its lifetime. | |
44 menu_controller->TerminateNestedMessageLoop(); | |
45 return; | |
46 } | |
47 | |
48 if (event->type() == ui::ET_KEY_PRESSED) { | |
49 menu_controller->OnKeyDown(event->key_code()); | |
50 | |
51 // Do not check mnemonics if the Alt or Ctrl modifiers are pressed. For | |
52 // example Ctrl+<T> is an accelerator, but <T> only is a mnemonic. | |
53 const int flags = event->flags(); | |
54 if (menu_controller->exit_type() == MenuController::EXIT_NONE && | |
55 (flags & kKeyFlagsMask) == 0) { | |
56 char c = ui::DomCodeToUsLayoutCharacter(event->code(), flags); | |
57 menu_controller->SelectByChar(c); | |
58 } | |
59 } | |
60 | |
61 ui::Accelerator accelerator(*event); | |
62 ViewsDelegate::ProcessMenuAcceleratorResult result = | |
63 ViewsDelegate::GetInstance()->ProcessAcceleratorWhileMenuShowing( | |
64 accelerator); | |
pkotwicz
2015/09/10 19:13:00
Nit: It would make me sleep easier if this block o
afakhry
2015/09/10 20:57:13
Makes sense. However you probably meant
"menu_con
| |
65 | |
66 if (menu_controller->exit_type() != MenuController::EXIT_NONE || | |
67 result == ViewsDelegate::ProcessMenuAcceleratorResult::CLOSE_MENU) { | |
68 menu_controller->TerminateNestedMessageLoop(); | |
69 } | |
70 | |
71 event->StopPropagation(); | |
72 } | |
73 | |
74 void MenuKeyEventHandler::OnTouchEvent(ui::TouchEvent* event) { | |
75 if (event->type() == ui::ET_TOUCH_RELEASED || | |
76 event->type() == ui::ET_TOUCH_CANCELLED) { | |
77 // Don't allow the event copy to clear the native touch id | |
78 // mapping, or we'll lose the mapping before the initial event | |
79 // has finished being dispatched. | |
80 event->set_should_remove_native_touch_id_mapping(false); | |
81 } | |
82 } | |
83 | |
84 } // namespace views | |
OLD | NEW |