| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/views/controls/menu/menu_pre_target_handler.h" |
| 6 |
| 7 #include "ui/aura/env.h" |
| 8 #include "ui/aura/window.h" |
| 9 #include "ui/views/controls/menu/menu_controller.h" |
| 10 #include "ui/views/widget/widget.h" |
| 11 #include "ui/wm/public/activation_client.h" |
| 12 |
| 13 namespace views { |
| 14 |
| 15 namespace { |
| 16 |
| 17 aura::Window* GetOwnerRootWindow(views::Widget* owner) { |
| 18 return owner ? owner->GetNativeWindow()->GetRootWindow() : nullptr; |
| 19 } |
| 20 |
| 21 } // namespace |
| 22 |
| 23 MenuPreTargetHandler::MenuPreTargetHandler(MenuController* controller, |
| 24 Widget* owner) |
| 25 : controller_(controller), root_(GetOwnerRootWindow(owner)) { |
| 26 aura::Env::GetInstanceDontCreate()->PrependPreTargetHandler(this); |
| 27 aura::client::GetActivationClient(root_)->AddObserver(this); |
| 28 root_->AddObserver(this); |
| 29 } |
| 30 |
| 31 MenuPreTargetHandler::~MenuPreTargetHandler() { |
| 32 Cleanup(); |
| 33 } |
| 34 |
| 35 void MenuPreTargetHandler::OnWindowActivated( |
| 36 aura::client::ActivationChangeObserver::ActivationReason reason, |
| 37 aura::Window* gained_active, |
| 38 aura::Window* lost_active) { |
| 39 if (!controller_->drag_in_progress()) |
| 40 controller_->CancelAll(); |
| 41 } |
| 42 |
| 43 void MenuPreTargetHandler::OnWindowDestroying(aura::Window* window) { |
| 44 Cleanup(); |
| 45 } |
| 46 |
| 47 void MenuPreTargetHandler::OnCancelMode(ui::CancelModeEvent* event) { |
| 48 controller_->CancelAll(); |
| 49 } |
| 50 |
| 51 void MenuPreTargetHandler::OnKeyEvent(ui::KeyEvent* event) { |
| 52 controller_->OnWillDispatchKeyEvent(event); |
| 53 } |
| 54 |
| 55 void MenuPreTargetHandler::Cleanup() { |
| 56 if (!root_) |
| 57 return; |
| 58 aura::Env::GetInstanceDontCreate()->RemovePreTargetHandler(this); |
| 59 // The ActivationClient may have been destroyed by the time we get here. |
| 60 aura::client::ActivationClient* client = |
| 61 aura::client::GetActivationClient(root_); |
| 62 if (client) |
| 63 client->RemoveObserver(this); |
| 64 root_->RemoveObserver(this); |
| 65 root_ = nullptr; |
| 66 } |
| 67 |
| 68 } // namespace views |
| OLD | NEW |