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..80e0ca00dc3ba64535dbca15fbd51f854ea6e26b |
| --- /dev/null |
| +++ b/ui/views/controls/menu/menu_event_filter.cc |
| @@ -0,0 +1,174 @@ |
| +// 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" |
| +#include "ui/views/widget/widget.h" |
| +#include "ui/wm/public/drag_drop_client.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 {} |
| + Result ProcessAccelerator(const ui::Accelerator& accelerator) override { |
| + return RESULT_PROCESSED; |
| + } |
| + void ProcessAcceleratorNow(const ui::Accelerator& accelerator) override { |
| + } |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(DefaultMenuDelegate); |
| +}; |
| + |
| +aura::Window* GetWidgetRootWindow(views::Widget* widget) { |
| + return widget ? widget->GetNativeWindow()->GetRootWindow() : nullptr; |
| +} |
| + |
| +// 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(); |
| + CHECK(menu_controller); |
| + |
| + Delegate::Result result = Delegate::RESULT_PROCESSED; |
| + |
| + 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. |
| + result = Delegate::RESULT_PROCESS_LATER; |
| + } else { |
| + const int flags = event->flags(); |
| + if (event->type() != ui::ET_KEY_PRESSED || (flags & kKeyFlagsMask) != 0) { |
| + // The above flags are an indication that this event is not to be handled |
| + // by the MenuController. |
| + result = delegate->ProcessAccelerator(accelerator); |
|
pkotwicz
2015/08/26 01:26:54
With this CL applied, it is no longer possible to
afakhry
2015/08/26 18:39:21
Done. I modified this code so now it looks almost
pkotwicz
2015/08/27 17:19:55
The code looks much cleaner now!
|
| + } else { |
| + menu_controller->OnKeyDown(event->key_code()); |
| + if (menu_controller->exit_type() == MenuController::EXIT_NONE) { |
| + // Only check mnemonics if the menu hasn't exited as a result from |
| + // MenuController::OnKeyDown(). |
| + char c = ui::GetCharacterFromKeyCode(event->key_code(), flags); |
| + menu_controller->SelectByChar(c); |
| + if (menu_controller->exit_type() == MenuController::EXIT_NONE) { |
| + // If a match for the mnemonic was not found and the menu hasn't |
| + // existed, then process the accelerator. |
| + result = delegate->ProcessAccelerator(accelerator); |
| + } |
| + } |
| + } |
| + } |
| + |
|
pkotwicz
2015/08/26 01:26:54
I think you can move the calling of MenuEventFilte
afakhry
2015/08/26 18:39:21
I agree, it used to be confusing. Done!
|
| + if (menu_controller->exit_type() != MenuController::EXIT_NONE) |
| + menu_controller->TerminateNestedMessageLoop(); |
| + |
| + // The event's propagation will always be stopped. |
| + event->StopPropagation(); |
| + |
| + if (result == Delegate::RESULT_PROCESS_LATER) { |
|
pkotwicz
2015/08/26 01:26:54
I would remove the drag and drop specific logic
afakhry
2015/08/26 18:39:21
Done.
|
| + // Check for drag and drop and cancel it if any. |
| + aura::Window* root = GetWidgetRootWindow(menu_controller->owner()); |
| + if (root) { |
| + aura::client::DragDropClient* client = |
| + aura::client::GetDragDropClient(root); |
| + if (client && client->IsDragDropInProgress()) |
| + client->DragCancel(); |
| + } |
| + |
| + menu_controller->CancelAll(); |
| + RepostAccelerator(accelerator, delegate); |
| + } |
| +} |
| + |
| +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; |
| +} |
| + |
| +} // namespace views |