Chromium Code Reviews| 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..e054fe1b35bd580ff4caafef8822d0882edbba3a |
| --- /dev/null |
| +++ b/ui/views/controls/menu/menu_event_filter.cc |
| @@ -0,0 +1,134 @@ |
| +// 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_SHIFT_DOWN | 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 ProcessAccelerator(const ui::Accelerator& accelerator, |
| + MenuEventFilter* owner) override { |
| + return true; |
| + } |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(DefaultMenuDelegate); |
| +}; |
| + |
| +} // 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(); |
| + CHECK(menu_controller); |
| + |
| + bool should_stop_propagation = true; |
| + const int flags = event->flags(); |
| + if (event->type() != ui::ET_KEY_PRESSED || (flags & kKeyFlagsMask) != 0) { |
|
pkotwicz
2015/06/16 16:04:59
There's a bunch of subtle differences between Menu
afakhry
2015/06/16 23:56:39
Right, in the old code, we call MenuController::Se
pkotwicz
2015/06/17 14:26:24
I prefer making these subtle changes in a differen
pkotwicz
2015/07/10 17:33:21
I still think that making these changes in a separ
|
| + should_stop_propagation = |
| + delegate->ProcessAccelerator(accelerator, this); |
| + } else { |
| + if (menu_controller->OnKeyDown(event->key_code())) { |
| + char c = ui::GetCharacterFromKeyCode(event->key_code(), flags); |
| + if (!menu_controller->SelectByChar(c)) { |
| + should_stop_propagation = delegate->ProcessAccelerator(accelerator, |
| + this); |
| + } |
| + } |
| + } |
| + |
| + if (menu_controller->exit_type() != MenuController::EXIT_NONE) |
| + menu_controller->TerminateNestedMessageLoop(); |
| + |
| + if (should_stop_propagation) |
| + event->StopPropagation(); |
|
pkotwicz
2015/06/16 16:04:59
Mnemonics are only run when base::RunLoop has fini
afakhry
2015/06/16 23:56:39
That's exactly what we're doing here, right? Take
pkotwicz
2015/06/17 14:26:24
Sorry that I was unclear.
In your CL, if a user pr
afakhry
2015/06/17 18:48:00
Thanks for clarifying. I understand what you mean
pkotwicz
2015/06/18 19:45:57
I am unsure what the right thing to do is. It woul
|
| +} |
| + |
| +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) { |
| + DCHECK(delegate); |
| + filter_delegate_ = delegate; |
| +} |
| + |
| +void MenuEventFilter::ClearDelegate() { |
| + filter_delegate_ = nullptr; |
| +} |
| + |
| +void MenuEventFilter::CloseMenu() const { |
| + MenuController* menu_controller = MenuController::GetActiveInstance(); |
| + CHECK(menu_controller); |
| + menu_controller->CancelAll(); |
| +} |
| + |
| +} // namespace views |