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/env.h"
14 #include "ui/aura/window.h"
15 #include "ui/aura/window_event_dispatcher.h"
16 #include "ui/aura/window_tree_host.h"
17 #include "ui/events/event.h"
18 #include "ui/events/platform/platform_event_source.h"
19 #include "ui/events/platform/scoped_event_dispatcher.h"
20 #include "ui/views/controls/menu/menu_controller.h"
21 #include "ui/views/widget/widget.h"
22 #include "ui/wm/public/activation_change_observer.h"
23 #include "ui/wm/public/activation_client.h"
24 #include "ui/wm/public/dispatcher_client.h"
25 #include "ui/wm/public/drag_drop_client.h"
28 #include "ui/base/win/internal_constants.h"
29 #include "ui/views/controls/menu/menu_message_pump_dispatcher_win.h"
30 #include "ui/views/win/hwnd_util.h"
32 #include "ui/views/controls/menu/menu_event_dispatcher.h"
35 using aura::client::ScreenPositionClient
;
41 aura::Window
* GetOwnerRootWindow(views::Widget
* owner
) {
42 return owner
? owner
->GetNativeWindow()->GetRootWindow() : NULL
;
45 // ActivationChangeObserverImpl is used to observe activation changes and close
46 // the menu. Additionally it listens for the root window to be destroyed and
47 // cancel the menu as well.
48 class ActivationChangeObserverImpl
49 : public aura::client::ActivationChangeObserver
,
50 public aura::WindowObserver
,
51 public ui::EventHandler
{
53 ActivationChangeObserverImpl(MenuController
* controller
, aura::Window
* root
)
54 : controller_(controller
), root_(root
) {
55 aura::client::GetActivationClient(root_
)->AddObserver(this);
56 root_
->AddObserver(this);
57 root_
->AddPreTargetHandler(this);
60 ~ActivationChangeObserverImpl() override
{ Cleanup(); }
62 // aura::client::ActivationChangeObserver:
63 void OnWindowActivated(
64 aura::client::ActivationChangeObserver::ActivationReason reason
,
65 aura::Window
* gained_active
,
66 aura::Window
* lost_active
) override
{
67 if (!controller_
->drag_in_progress())
68 controller_
->CancelAll();
71 // aura::WindowObserver:
72 void OnWindowDestroying(aura::Window
* window
) override
{ Cleanup(); }
75 void OnCancelMode(ui::CancelModeEvent
* event
) override
{
76 controller_
->CancelAll();
83 // The ActivationClient may have been destroyed by the time we get here.
84 aura::client::ActivationClient
* client
=
85 aura::client::GetActivationClient(root_
);
87 client
->RemoveObserver(this);
88 root_
->RemovePreTargetHandler(this);
89 root_
->RemoveObserver(this);
93 MenuController
* controller_
;
96 DISALLOW_COPY_AND_ASSIGN(ActivationChangeObserverImpl
);
102 MenuMessageLoop
* MenuMessageLoop::Create() {
103 return new MenuMessageLoopAura
;
106 MenuMessageLoopAura::MenuMessageLoopAura() : owner_(NULL
) {
109 MenuMessageLoopAura::~MenuMessageLoopAura() {
112 void MenuMessageLoopAura::RepostEventToWindow(const ui::LocatedEvent
& event
,
113 gfx::NativeWindow window
,
114 const gfx::Point
& screen_loc
) {
115 aura::Window
* root
= window
->GetRootWindow();
116 ScreenPositionClient
* spc
= aura::client::GetScreenPositionClient(root
);
120 gfx::Point
root_loc(screen_loc
);
121 spc
->ConvertPointFromScreen(root
, &root_loc
);
123 ui::MouseEvent
clone(static_cast<const ui::MouseEvent
&>(event
));
124 clone
.set_location(root_loc
);
125 clone
.set_root_location(root_loc
);
126 root
->GetHost()->dispatcher()->RepostEvent(clone
);
129 void MenuMessageLoopAura::Run(MenuController
* controller
,
132 // |owner_| may be NULL.
134 aura::Window
* root
= GetOwnerRootWindow(owner_
);
135 // It is possible for the same MenuMessageLoopAura to start a nested
136 // message-loop while it is already running a nested loop. So make sure the
137 // quit-closure gets reset to the outer loop's quit-closure once the innermost
139 base::AutoReset
<base::Closure
> reset_quit_closure(&message_loop_quit_
,
143 internal::MenuMessagePumpDispatcher
nested_dispatcher(controller
);
145 scoped_ptr
<ActivationChangeObserverImpl
> observer
;
147 observer
.reset(new ActivationChangeObserverImpl(controller
, root
));
148 aura::client::DispatcherRunLoop
run_loop(
149 aura::client::GetDispatcherClient(root
), &nested_dispatcher
);
150 message_loop_quit_
= run_loop
.QuitClosure();
153 base::MessageLoopForUI
* loop
= base::MessageLoopForUI::current();
154 base::MessageLoop::ScopedNestableTaskAllower
allow(loop
);
155 base::RunLoop
run_loop(&nested_dispatcher
);
156 message_loop_quit_
= run_loop
.QuitClosure();
160 internal::MenuEventDispatcher
event_dispatcher(controller
);
161 scoped_ptr
<ui::ScopedEventDispatcher
> dispatcher_override
;
162 if (ui::PlatformEventSource::GetInstance()) {
163 dispatcher_override
=
164 ui::PlatformEventSource::GetInstance()->OverrideDispatcher(
168 scoped_ptr
<ActivationChangeObserverImpl
> observer
;
170 observer
.reset(new ActivationChangeObserverImpl(controller
, root
));
171 aura::client::DispatcherRunLoop
run_loop(
172 aura::client::GetDispatcherClient(root
), NULL
);
173 message_loop_quit_
= run_loop
.QuitClosure();
176 base::MessageLoopForUI
* loop
= base::MessageLoopForUI::current();
177 base::MessageLoop::ScopedNestableTaskAllower
allow(loop
);
178 base::RunLoop run_loop
;
179 message_loop_quit_
= run_loop
.QuitClosure();
185 void MenuMessageLoopAura::QuitNow() {
186 CHECK(!message_loop_quit_
.is_null());
187 message_loop_quit_
.Run();
190 // Ask PlatformEventSource to stop dispatching events in this message loop
191 // iteration. We want our menu's loop to return before the next event.
192 if (ui::PlatformEventSource::GetInstance())
193 ui::PlatformEventSource::GetInstance()->StopCurrentEventStream();
197 void MenuMessageLoopAura::ClearOwner() {