Index: ui/views/controls/menu/menu_event_filter.cc |
diff --git a/ui/views/controls/menu/menu_event_filter.cc b/ui/views/controls/menu/menu_event_filter.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..fa32953420d7b44a8348d14e755a961ef8e78178 |
--- /dev/null |
+++ b/ui/views/controls/menu/menu_event_filter.cc |
@@ -0,0 +1,148 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ui/views/controls/menu/menu_event_filter.h" |
+ |
+#include "ui/aura/window.h" |
+#include "ui/aura/window_property.h" |
+#include "ui/base/accelerators/accelerator.h" |
+#include "ui/events/keycodes/keyboard_code_conversion.h" |
+#include "ui/views/controls/menu/menu_controller.h" |
+ |
+DECLARE_WINDOW_PROPERTY_TYPE(views::MenuEventFilter::Delegate*); |
+ |
+DEFINE_LOCAL_WINDOW_PROPERTY_KEY(views::MenuEventFilter::Delegate*, |
+ kMenuEventFilterDelegateKey, |
+ nullptr); |
+ |
+namespace views { |
+ |
+namespace { |
+ |
+const int kKeyFlagsMask = ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN; |
+ |
+// Defines a No-op delegate for non-ash platforms. |
+class DefaultMenuDelegate : public MenuEventFilter::Delegate { |
+ public: |
+ DefaultMenuDelegate() {} |
+ ~DefaultMenuDelegate() override {} |
+ |
+ // MenuEventFilter::Delegate: |
+ void StoreInHistory(const ui::Accelerator& accelerator) override {} |
+ bool ShouldCloseMenuAndRepostAccelerator( |
+ const ui::Accelerator& accelerator) const override { |
+ return false; |
+ } |
+ void ProcessAcceleratorNow(const ui::Accelerator& accelerator) override { |
+ } |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(DefaultMenuDelegate); |
+}; |
+ |
+// Reposts the |accelerator| to be processed later. |
+void RepostAccelerator(const ui::Accelerator& accelerator, |
+ MenuEventFilter::Delegate* delegate) { |
+ base::MessageLoopForUI::current()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&MenuEventFilter::Delegate::ProcessAcceleratorNow, |
+ base::Unretained(delegate), |
+ accelerator)); |
+} |
+ |
+} // namespace |
+ |
+MenuEventFilter::MenuEventFilter(MenuController* menu_controller, |
+ MenuEventFilter::Delegate* delegate) |
+ : menu_controller_(menu_controller), |
+ default_delegate_(new DefaultMenuDelegate), |
+ filter_delegate_(delegate) { |
+ DCHECK(menu_controller_); |
+} |
+ |
+MenuEventFilter::~MenuEventFilter() { |
+} |
+ |
+// static |
+void MenuEventFilter::SetMenuEventFilterDelegate( |
+ aura::Window* root_window, |
+ MenuEventFilter::Delegate* delegate) { |
+ DCHECK(delegate); |
+ DCHECK_EQ(root_window->GetRootWindow(), root_window); |
oshima
2015/09/02 17:08:21
DCHECK(root_window->IsRootWindow()) ?
afakhry
2015/09/02 18:18:38
Done.
|
+ root_window->SetProperty(kMenuEventFilterDelegateKey, delegate); |
+} |
+ |
+// static |
+MenuEventFilter::Delegate* MenuEventFilter::GetMenuEventFilterDelegate( |
+ aura::Window* root_window) { |
+ DCHECK_EQ(root_window->GetRootWindow(), root_window); |
oshima
2015/09/02 17:08:21
ditto
afakhry
2015/09/02 18:18:38
Done.
|
+ return root_window->GetProperty(kMenuEventFilterDelegateKey); |
+} |
+ |
+void MenuEventFilter::OnKeyEvent(ui::KeyEvent* event) { |
+ MenuEventFilter::Delegate* delegate = |
+ filter_delegate_ ? filter_delegate_ : default_delegate_.get(); |
+ |
+ bool should_close_and_repost_accelerator = false; |
+ if (menu_controller_->exit_type() == MenuController::EXIT_ALL || |
+ menu_controller_->exit_type() == MenuController::EXIT_DESTROYED) { |
+ // If the event has arrived after the menu's exit type had changed but |
+ // before its message loop terminated, the event will continue its normal |
+ // propagation for the following reason: |
+ // If the user accepts a menu item in a nested menu, the menu item action is |
+ // run after the base::RunLoop for the innermost menu has quit but before |
+ // the base::RunLoop for the outermost menu has quit. If the menu item |
+ // action starts a base::RunLoop, the outermost menu's base::RunLoop will |
+ // not quit till the action's base::RunLoop ends. IDC_BOOKMARK_BAR_OPEN_ALL |
+ // sometimes opens a modal dialog. The modal dialog starts a base::RunLoop |
+ // and keeps the base::RunLoop running for the duration of its lifetime. |
+ menu_controller_->TerminateNestedMessageLoop(); |
+ return; |
+ } else { |
+ if (event->type() == ui::ET_KEY_PRESSED) { |
+ menu_controller_->OnKeyDown(event->key_code()); |
oshima
2015/09/02 17:08:21
can you add comment that this is for mnemonics?
afakhry
2015/09/02 18:18:38
Done.
|
+ |
+ const int flags = event->flags(); |
+ if (menu_controller_->exit_type() == MenuController::EXIT_NONE && |
+ (flags & kKeyFlagsMask) == 0) { |
+ char c = ui::GetCharacterFromKeyCode(event->key_code(), flags); |
+ menu_controller_->SelectByChar(c); |
+ } |
+ } |
+ } |
+ |
+ ui::Accelerator accelerator(*event); |
+ if (menu_controller_->exit_type() == MenuController::EXIT_NONE) { |
+ should_close_and_repost_accelerator = |
+ delegate->ShouldCloseMenuAndRepostAccelerator(accelerator); |
+ } else { |
+ menu_controller_->TerminateNestedMessageLoop(); |
+ } |
+ |
+ event->StopPropagation(); |
+ |
+ // Since we stop the propagation of the event, |
+ // wm::AcceleratorFilter::OnKeyEvent() won't get the key event so that it can |
+ // store it in the history. We must manually store it here. |
+ delegate->StoreInHistory(accelerator); |
+ |
+ if (should_close_and_repost_accelerator) { |
+ menu_controller_->CancelAll(); |
+ RepostAccelerator(accelerator, delegate); |
+ } else { |
+ delegate->ProcessAcceleratorNow(accelerator); |
+ } |
+} |
+ |
+void MenuEventFilter::OnTouchEvent(ui::TouchEvent* event) { |
+ if (event->type() == ui::ET_TOUCH_RELEASED || |
+ event->type() == ui::ET_TOUCH_CANCELLED) { |
+ // Don't allow the event copy to clear the native touch id |
+ // mapping, or we'll lose the mapping before the initial event |
+ // has finished being dispatched. |
+ event->set_should_remove_native_touch_id_mapping(false); |
+ } |
+} |
+ |
+} // namespace views |