| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "ui/views/controls/menu/menu_key_event_handler.h" | 5 #include "ui/views/controls/menu/menu_key_event_handler.h" |
| 6 | 6 |
| 7 #include "ui/aura/env.h" | 7 #include "ui/aura/env.h" |
| 8 #include "ui/views/controls/menu/menu_controller.h" | 8 #include "ui/views/controls/menu/menu_controller.h" |
| 9 #include "ui/views/views_delegate.h" | 9 #include "ui/views/views_delegate.h" |
| 10 | 10 |
| 11 namespace views { | 11 namespace views { |
| 12 | 12 |
| 13 namespace { | |
| 14 | |
| 15 const int kKeyFlagsMask = ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN; | |
| 16 | |
| 17 } // namespace | |
| 18 | |
| 19 MenuKeyEventHandler::MenuKeyEventHandler() { | 13 MenuKeyEventHandler::MenuKeyEventHandler() { |
| 20 aura::Env::GetInstanceDontCreate()->PrependPreTargetHandler(this); | 14 aura::Env::GetInstanceDontCreate()->PrependPreTargetHandler(this); |
| 21 } | 15 } |
| 22 | 16 |
| 23 MenuKeyEventHandler::~MenuKeyEventHandler() { | 17 MenuKeyEventHandler::~MenuKeyEventHandler() { |
| 24 aura::Env::GetInstanceDontCreate()->RemovePreTargetHandler(this); | 18 aura::Env::GetInstanceDontCreate()->RemovePreTargetHandler(this); |
| 25 } | 19 } |
| 26 | 20 |
| 27 void MenuKeyEventHandler::OnKeyEvent(ui::KeyEvent* event) { | 21 void MenuKeyEventHandler::OnKeyEvent(ui::KeyEvent* event) { |
| 28 MenuController* menu_controller = MenuController::GetActiveInstance(); | 22 MenuController* menu_controller = MenuController::GetActiveInstance(); |
| 29 DCHECK(menu_controller); | 23 DCHECK(menu_controller); |
| 30 | 24 menu_controller->OnWillDispatchKeyEvent(event); |
| 31 if (menu_controller->exit_type() == MenuController::EXIT_ALL || | |
| 32 menu_controller->exit_type() == MenuController::EXIT_DESTROYED) { | |
| 33 // If the event has arrived after the menu's exit type had changed but | |
| 34 // before its message loop terminated, the event will continue its normal | |
| 35 // propagation for the following reason: | |
| 36 // If the user accepts a menu item in a nested menu, the menu item action is | |
| 37 // run after the base::RunLoop for the innermost menu has quit but before | |
| 38 // the base::RunLoop for the outermost menu has quit. If the menu item | |
| 39 // action starts a base::RunLoop, the outermost menu's base::RunLoop will | |
| 40 // not quit till the action's base::RunLoop ends. IDC_BOOKMARK_BAR_OPEN_ALL | |
| 41 // sometimes opens a modal dialog. The modal dialog starts a base::RunLoop | |
| 42 // and keeps the base::RunLoop running for the duration of its lifetime. | |
| 43 menu_controller->TerminateNestedMessageLoopIfNecessary(); | |
| 44 return; | |
| 45 } | |
| 46 | |
| 47 event->StopPropagation(); | |
| 48 | |
| 49 if (event->type() == ui::ET_KEY_PRESSED) { | |
| 50 menu_controller->OnKeyDown(event->key_code()); | |
| 51 // Menu controller might have been deleted. | |
| 52 if (!MenuController::GetActiveInstance()) | |
| 53 return; | |
| 54 | |
| 55 // Do not check mnemonics if the Alt or Ctrl modifiers are pressed. For | |
| 56 // example Ctrl+<T> is an accelerator, but <T> only is a mnemonic. | |
| 57 const int flags = event->flags(); | |
| 58 if (menu_controller->exit_type() == MenuController::EXIT_NONE && | |
| 59 (flags & kKeyFlagsMask) == 0) { | |
| 60 base::char16 c = event->GetCharacter(); | |
| 61 menu_controller->SelectByChar(c); | |
| 62 // Menu controller might have been deleted. | |
| 63 if (!MenuController::GetActiveInstance()) | |
| 64 return; | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 if (!menu_controller->TerminateNestedMessageLoopIfNecessary()) { | |
| 69 ui::Accelerator accelerator(*event); | |
| 70 ViewsDelegate::ProcessMenuAcceleratorResult result = | |
| 71 ViewsDelegate::GetInstance()->ProcessAcceleratorWhileMenuShowing( | |
| 72 accelerator); | |
| 73 if (result == ViewsDelegate::ProcessMenuAcceleratorResult::CLOSE_MENU) | |
| 74 menu_controller->CancelAll(); | |
| 75 } | |
| 76 } | 25 } |
| 77 | 26 |
| 78 } // namespace views | 27 } // namespace views |
| OLD | NEW |