1 // Copyright (c) 2012 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.
5 #include "ui/views/controls/menu/menu_controller.h"
7 #include "base/run_loop.h"
8 #include "ui/aura/client/activation_change_observer.h"
9 #include "ui/aura/client/activation_client.h"
10 #include "ui/aura/client/dispatcher_client.h"
11 #include "ui/aura/client/drag_drop_client.h"
12 #include "ui/aura/window.h"
13 #include "ui/aura/window_observer.h"
14 #include "ui/gfx/screen.h"
15 #include "ui/views/widget/widget.h"
21 // ActivationChangeObserverImpl is used to observe activation changes and close
22 // the menu. Additionally it listens for the root window to be destroyed and
23 // cancel the menu as well.
24 class ActivationChangeObserverImpl
25 : public aura::client::ActivationChangeObserver,
26 public aura::WindowObserver,
27 public ui::EventHandler {
29 ActivationChangeObserverImpl(MenuController* controller,
31 : controller_(controller),
33 aura::client::GetActivationClient(root_)->AddObserver(this);
34 root_->AddObserver(this);
35 root_->AddPreTargetHandler(this);
38 virtual ~ActivationChangeObserverImpl() {
42 // aura::client::ActivationChangeObserver overrides:
43 virtual void OnWindowActivated(aura::Window* gained_active,
44 aura::Window* lost_active) OVERRIDE {
45 if (!controller_->drag_in_progress())
46 controller_->CancelAll();
49 // aura::WindowObserver overrides:
50 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {
54 // ui::EventHandler overrides:
55 virtual void OnCancelMode(ui::CancelModeEvent* event) OVERRIDE {
56 controller_->CancelAll();
63 // The ActivationClient may have been destroyed by the time we get here.
64 aura::client::ActivationClient* client =
65 aura::client::GetActivationClient(root_);
67 client->RemoveObserver(this);
68 root_->RemovePreTargetHandler(this);
69 root_->RemoveObserver(this);
73 MenuController* controller_;
76 DISALLOW_COPY_AND_ASSIGN(ActivationChangeObserverImpl);
79 aura::Window* GetOwnerRootWindow(views::Widget* owner) {
80 return owner ? owner->GetNativeWindow()->GetRootWindow() : NULL;
85 void MenuController::RunMessageLoop(bool nested_menu) {
86 // |owner_| may be NULL.
87 aura::Window* root = GetOwnerRootWindow(owner_);
89 scoped_ptr<ActivationChangeObserverImpl> observer;
91 observer.reset(new ActivationChangeObserverImpl(this, root));
92 aura::client::GetDispatcherClient(root)->
93 RunWithDispatcher(this, owner_->GetNativeWindow());
95 base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
96 base::MessageLoop::ScopedNestableTaskAllower allow(loop);
97 base::RunLoop run_loop(this);
102 bool MenuController::ShouldQuitNow() const {
103 aura::Window* root = GetOwnerRootWindow(owner_);
104 return !aura::client::GetDragDropClient(root) ||
105 !aura::client::GetDragDropClient(root)->IsDragDropInProgress();
108 gfx::Screen* MenuController::GetScreen() {
109 aura::Window* root = GetOwnerRootWindow(owner_);
111 gfx::Screen::GetScreenFor(root) : gfx::Screen::GetNativeScreen();