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"
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"
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"
31 #include "ui/views/controls/menu/menu_key_event_handler.h"
34 using aura::client::ScreenPositionClient
;
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
{
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(); }
74 void OnCancelMode(ui::CancelModeEvent
* event
) override
{
75 controller_
->CancelAll();
82 // The ActivationClient may have been destroyed by the time we get here.
83 aura::client::ActivationClient
* client
=
84 aura::client::GetActivationClient(root_
);
86 client
->RemoveObserver(this);
87 root_
->RemovePreTargetHandler(this);
88 root_
->RemoveObserver(this);
92 MenuController
* controller_
;
95 DISALLOW_COPY_AND_ASSIGN(ActivationChangeObserverImpl
);
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
);
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
,
131 // |owner_| may be NULL.
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
138 base::AutoReset
<base::Closure
> reset_quit_closure(&message_loop_quit_
,
142 internal::MenuMessagePumpDispatcher
nested_dispatcher(controller
);
144 scoped_ptr
<ActivationChangeObserverImpl
> observer
;
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();
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();
159 scoped_ptr
<ActivationChangeObserverImpl
> observer
;
162 observer
.reset(new ActivationChangeObserverImpl(controller
, root
));
165 scoped_ptr
<MenuKeyEventHandler
> menu_event_filter
;
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();
178 #endif // defined(OS_WIN)
181 void MenuMessageLoopAura::QuitNow() {
182 CHECK(!message_loop_quit_
.is_null());
183 message_loop_quit_
.Run();
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();
193 void MenuMessageLoopAura::ClearOwner() {