Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(139)

Unified Diff: ui/views/controls/menu/menu_event_filter.cc

Issue 1138523006: Enable keyboard accelerators while a menu is open (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixed a bug in restoring the delegate of the parent nested runloop. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..377ba7f1a48e192972c036397bbe9857bd2fcf91
--- /dev/null
+++ b/ui/views/controls/menu/menu_event_filter.cc
@@ -0,0 +1,162 @@
+// 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 |
+ ui::EF_COMMAND_DOWN;
+
+// Defines a NULL-Object 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()
+ : default_delegate_(new DefaultMenuDelegate),
+ filter_delegate_(nullptr) {
+}
+
+MenuEventFilter::~MenuEventFilter() {
+}
+
+// static
+void MenuEventFilter::SetMenuEventFilterDelegate(
+ aura::Window* root_window,
+ MenuEventFilter::Delegate* delegate) {
+ DCHECK(root_window);
+ DCHECK(delegate);
+ DCHECK_EQ(root_window->GetRootWindow(), root_window);
+ root_window->SetProperty(kMenuEventFilterDelegateKey, delegate);
+}
+
+// static
+MenuEventFilter::Delegate* MenuEventFilter::GetMenuEventFilterDelegate(
+ aura::Window* root_window) {
+ DCHECK(root_window);
+ DCHECK_EQ(root_window->GetRootWindow(), root_window);
+ return root_window->GetProperty(kMenuEventFilterDelegateKey);
+}
+
+void MenuEventFilter::OnKeyEvent(ui::KeyEvent* event) {
+ DCHECK(event);
+
+ MenuEventFilter::Delegate* delegate =
+ filter_delegate_ ? filter_delegate_ : default_delegate_.get();
+ DCHECK(delegate);
+
+ // First record the current accelerator (this is normally done by the
+ // AcceleratorFilter, but since the MenuEventFilter will precede the
+ // AcceleratorFilter in the pre-target handlers list, we have to do it here).
+ ui::Accelerator accelerator(*event);
+ delegate->StoreInHistory(accelerator);
+
+ MenuController* menu_controller = MenuController::GetActiveInstance();
pkotwicz 2015/08/27 17:19:55 Nit: Can you pass in the active menu controller in
afakhry 2015/08/28 01:24:41 Great idea! And it opened the path to create the e
+ CHECK(menu_controller);
+
+ 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 accelerator should be reposted
+ // after the message loop terminates.
+ should_close_and_repost_accelerator = true;
pkotwicz 2015/08/27 17:19:55 Can we just drop the accelerator in this case? Not
afakhry 2015/08/28 01:24:41 Done.
+ } else {
+ if (event->type() == ui::ET_KEY_PRESSED) {
+ menu_controller->OnKeyDown(event->key_code());
+
+ const int flags = event->flags();
+ if (menu_controller->exit_type() == MenuController::EXIT_NONE &&
+ (flags & kKeyFlagsMask) == 0) {
pkotwicz 2015/08/27 17:19:55 menu_event_dispatcher.cc still runs MenuController
afakhry 2015/08/28 01:24:41 I know but isn't that something we want to prevent
+ // Only check mnemonics if the menu hasn't exited as a result from
+ // MenuController::OnKeyDown() and no modifiers are pressed.
+ char c = ui::GetCharacterFromKeyCode(event->key_code(), flags);
+ menu_controller->SelectByChar(c);
+ }
+ }
+ }
+
+ if (menu_controller->exit_type() == MenuController::EXIT_NONE) {
+ should_close_and_repost_accelerator =
+ delegate->ShouldCloseMenuAndRepostAccelerator(accelerator);
+ } else {
+ menu_controller->TerminateNestedMessageLoop();
+ }
+
+ // The event's propagation will always be stopped.
+ event->StopPropagation();
+
+ 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);
+ }
+}
+
+void MenuEventFilter::SetDelegate(MenuEventFilter::Delegate* delegate) {
+ filter_delegate_ = delegate;
+}
+
+void MenuEventFilter::ClearDelegate() {
+ filter_delegate_ = nullptr;
+}
+
+MenuEventFilter::Delegate* MenuEventFilter::GetCurrentDelegate() const {
+ return filter_delegate_;
+}
+
+} // namespace views

Powered by Google App Engine
This is Rietveld 408576698