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