Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / views / controls / menu / menu_message_loop_aura.cc
blobd28f6743117681abf03e461a4ff5313d067bb721
1 // Copyright 2014 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_message_loop_aura.h"
7 #if defined(OS_WIN)
8 #include <windowsx.h>
9 #endif
11 #include "base/run_loop.h"
12 #include "ui/aura/client/screen_position_client.h"
13 #include "ui/aura/window.h"
14 #include "ui/aura/window_event_dispatcher.h"
15 #include "ui/aura/window_tree_host.h"
16 #include "ui/events/event.h"
17 #include "ui/events/platform/platform_event_source.h"
18 #include "ui/events/platform/scoped_event_dispatcher.h"
19 #include "ui/views/controls/menu/menu_controller.h"
20 #include "ui/views/widget/widget.h"
21 #include "ui/wm/public/activation_change_observer.h"
22 #include "ui/wm/public/activation_client.h"
23 #include "ui/wm/public/dispatcher_client.h"
24 #include "ui/wm/public/drag_drop_client.h"
26 #if defined(OS_WIN)
27 #include "ui/base/win/internal_constants.h"
28 #include "ui/views/controls/menu/menu_message_pump_dispatcher_win.h"
29 #include "ui/views/win/hwnd_util.h"
30 #else
31 #include "ui/views/controls/menu/menu_key_event_handler.h"
32 #endif
34 using aura::client::ScreenPositionClient;
36 namespace views {
38 namespace {
40 aura::Window* GetOwnerRootWindow(views::Widget* owner) {
41 return owner ? owner->GetNativeWindow()->GetRootWindow() : NULL;
44 // ActivationChangeObserverImpl is used to observe activation changes and close
45 // the menu. Additionally it listens for the root window to be destroyed and
46 // cancel the menu as well.
47 class ActivationChangeObserverImpl
48 : public aura::client::ActivationChangeObserver,
49 public aura::WindowObserver,
50 public ui::EventHandler {
51 public:
52 ActivationChangeObserverImpl(MenuController* controller, aura::Window* root)
53 : controller_(controller), root_(root) {
54 aura::client::GetActivationClient(root_)->AddObserver(this);
55 root_->AddObserver(this);
56 root_->AddPreTargetHandler(this);
59 ~ActivationChangeObserverImpl() override { Cleanup(); }
61 // aura::client::ActivationChangeObserver:
62 void OnWindowActivated(
63 aura::client::ActivationChangeObserver::ActivationReason reason,
64 aura::Window* gained_active,
65 aura::Window* lost_active) override {
66 if (!controller_->drag_in_progress())
67 controller_->CancelAll();
70 // aura::WindowObserver:
71 void OnWindowDestroying(aura::Window* window) override { Cleanup(); }
73 // ui::EventHandler:
74 void OnCancelMode(ui::CancelModeEvent* event) override {
75 controller_->CancelAll();
78 private:
79 void Cleanup() {
80 if (!root_)
81 return;
82 // The ActivationClient may have been destroyed by the time we get here.
83 aura::client::ActivationClient* client =
84 aura::client::GetActivationClient(root_);
85 if (client)
86 client->RemoveObserver(this);
87 root_->RemovePreTargetHandler(this);
88 root_->RemoveObserver(this);
89 root_ = NULL;
92 MenuController* controller_;
93 aura::Window* root_;
95 DISALLOW_COPY_AND_ASSIGN(ActivationChangeObserverImpl);
98 } // namespace
100 // static
101 MenuMessageLoop* MenuMessageLoop::Create() {
102 return new MenuMessageLoopAura;
105 MenuMessageLoopAura::MenuMessageLoopAura() : owner_(nullptr) {
108 MenuMessageLoopAura::~MenuMessageLoopAura() {
111 void MenuMessageLoopAura::RepostEventToWindow(const ui::LocatedEvent& event,
112 gfx::NativeWindow window,
113 const gfx::Point& screen_loc) {
114 aura::Window* root = window->GetRootWindow();
115 ScreenPositionClient* spc = aura::client::GetScreenPositionClient(root);
116 if (!spc)
117 return;
119 gfx::Point root_loc(screen_loc);
120 spc->ConvertPointFromScreen(root, &root_loc);
122 ui::MouseEvent clone(static_cast<const ui::MouseEvent&>(event));
123 clone.set_location(root_loc);
124 clone.set_root_location(root_loc);
125 root->GetHost()->dispatcher()->RepostEvent(clone);
128 void MenuMessageLoopAura::Run(MenuController* controller,
129 Widget* owner,
130 bool nested_menu) {
131 // |owner_| may be NULL.
132 owner_ = owner;
133 aura::Window* root = GetOwnerRootWindow(owner_);
134 // It is possible for the same MenuMessageLoopAura to start a nested
135 // message-loop while it is already running a nested loop. So make sure the
136 // quit-closure gets reset to the outer loop's quit-closure once the innermost
137 // loop terminates.
138 base::AutoReset<base::Closure> reset_quit_closure(&message_loop_quit_,
139 base::Closure());
141 #if defined(OS_WIN)
142 internal::MenuMessagePumpDispatcher nested_dispatcher(controller);
143 if (root) {
144 scoped_ptr<ActivationChangeObserverImpl> observer;
145 if (!nested_menu)
146 observer.reset(new ActivationChangeObserverImpl(controller, root));
147 aura::client::DispatcherRunLoop run_loop(
148 aura::client::GetDispatcherClient(root), &nested_dispatcher);
149 message_loop_quit_ = run_loop.QuitClosure();
150 run_loop.Run();
151 } else {
152 base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
153 base::MessageLoop::ScopedNestableTaskAllower allow(loop);
154 base::RunLoop run_loop(&nested_dispatcher);
155 message_loop_quit_ = run_loop.QuitClosure();
156 run_loop.Run();
158 #else
159 scoped_ptr<ActivationChangeObserverImpl> observer;
160 if (root) {
161 if (!nested_menu)
162 observer.reset(new ActivationChangeObserverImpl(controller, root));
165 scoped_ptr<MenuKeyEventHandler> menu_event_filter;
166 if (!nested_menu) {
167 // If this is a nested menu, then the MenuKeyEventHandler would have been
168 // created already in the top parent menu. So no need to recreate it here.
169 menu_event_filter.reset(new MenuKeyEventHandler);
172 base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
173 base::MessageLoop::ScopedNestableTaskAllower allow(loop);
174 base::RunLoop run_loop;
175 message_loop_quit_ = run_loop.QuitClosure();
177 run_loop.Run();
178 #endif // defined(OS_WIN)
181 void MenuMessageLoopAura::QuitNow() {
182 CHECK(!message_loop_quit_.is_null());
183 message_loop_quit_.Run();
185 #if !defined(OS_WIN)
186 // Ask PlatformEventSource to stop dispatching events in this message loop
187 // iteration. We want our menu's loop to return before the next event.
188 if (ui::PlatformEventSource::GetInstance())
189 ui::PlatformEventSource::GetInstance()->StopCurrentEventStream();
190 #endif
193 void MenuMessageLoopAura::ClearOwner() {
194 owner_ = NULL;
197 } // namespace views