1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/controls/menu/menu_message_loop_aura.h"
#if defined(OS_WIN)
#include <windowsx.h>
#endif
#include "base/run_loop.h"
#include "ui/aura/client/screen_position_client.h"
#include "ui/aura/env.h"
#include "ui/aura/window.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/aura/window_tree_host.h"
#include "ui/events/event.h"
#include "ui/events/platform/platform_event_source.h"
#include "ui/events/platform/scoped_event_dispatcher.h"
#include "ui/views/controls/menu/menu_controller.h"
#include "ui/views/widget/widget.h"
#include "ui/wm/public/activation_change_observer.h"
#include "ui/wm/public/activation_client.h"
#include "ui/wm/public/dispatcher_client.h"
#include "ui/wm/public/drag_drop_client.h"
#if defined(OS_WIN)
#include "ui/base/win/internal_constants.h"
#include "ui/views/controls/menu/menu_message_pump_dispatcher_win.h"
#include "ui/views/win/hwnd_util.h"
#else
#include "ui/views/controls/menu/menu_event_dispatcher_linux.h"
#endif
using aura::client::ScreenPositionClient;
namespace views {
namespace {
aura::Window* GetOwnerRootWindow(views::Widget* owner) {
return owner ? owner->GetNativeWindow()->GetRootWindow() : NULL;
}
// ActivationChangeObserverImpl is used to observe activation changes and close
// the menu. Additionally it listens for the root window to be destroyed and
// cancel the menu as well.
class ActivationChangeObserverImpl
: public aura::client::ActivationChangeObserver,
public aura::WindowObserver,
public ui::EventHandler {
public:
ActivationChangeObserverImpl(MenuController* controller, aura::Window* root)
: controller_(controller), root_(root) {
aura::client::GetActivationClient(root_)->AddObserver(this);
root_->AddObserver(this);
root_->AddPreTargetHandler(this);
}
~ActivationChangeObserverImpl() override { Cleanup(); }
// aura::client::ActivationChangeObserver:
void OnWindowActivated(aura::Window* gained_active,
aura::Window* lost_active) override {
if (!controller_->drag_in_progress())
controller_->CancelAll();
}
// aura::WindowObserver:
void OnWindowDestroying(aura::Window* window) override { Cleanup(); }
// ui::EventHandler:
void OnCancelMode(ui::CancelModeEvent* event) override {
controller_->CancelAll();
}
private:
void Cleanup() {
if (!root_)
return;
// The ActivationClient may have been destroyed by the time we get here.
aura::client::ActivationClient* client =
aura::client::GetActivationClient(root_);
if (client)
client->RemoveObserver(this);
root_->RemovePreTargetHandler(this);
root_->RemoveObserver(this);
root_ = NULL;
}
MenuController* controller_;
aura::Window* root_;
DISALLOW_COPY_AND_ASSIGN(ActivationChangeObserverImpl);
};
} // namespace
// static
MenuMessageLoop* MenuMessageLoop::Create() {
return new MenuMessageLoopAura;
}
MenuMessageLoopAura::MenuMessageLoopAura() : owner_(NULL) {
}
MenuMessageLoopAura::~MenuMessageLoopAura() {
}
void MenuMessageLoopAura::RepostEventToWindow(const ui::LocatedEvent& event,
gfx::NativeWindow window,
const gfx::Point& screen_loc) {
aura::Window* root = window->GetRootWindow();
ScreenPositionClient* spc = aura::client::GetScreenPositionClient(root);
if (!spc)
return;
gfx::Point root_loc(screen_loc);
spc->ConvertPointFromScreen(root, &root_loc);
ui::MouseEvent clone(static_cast<const ui::MouseEvent&>(event));
clone.set_location(root_loc);
clone.set_root_location(root_loc);
root->GetHost()->dispatcher()->RepostEvent(clone);
}
void MenuMessageLoopAura::Run(MenuController* controller,
Widget* owner,
bool nested_menu) {
// |owner_| may be NULL.
owner_ = owner;
aura::Window* root = GetOwnerRootWindow(owner_);
// It is possible for the same MenuMessageLoopAura to start a nested
// message-loop while it is already running a nested loop. So make sure the
// quit-closure gets reset to the outer loop's quit-closure once the innermost
// loop terminates.
base::AutoReset<base::Closure> reset_quit_closure(&message_loop_quit_,
base::Closure());
#if defined(OS_WIN)
internal::MenuMessagePumpDispatcher nested_dispatcher(controller);
if (root) {
scoped_ptr<ActivationChangeObserverImpl> observer;
if (!nested_menu)
observer.reset(new ActivationChangeObserverImpl(controller, root));
aura::client::DispatcherRunLoop run_loop(
aura::client::GetDispatcherClient(root), &nested_dispatcher);
message_loop_quit_ = run_loop.QuitClosure();
run_loop.Run();
} else {
base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
base::MessageLoop::ScopedNestableTaskAllower allow(loop);
base::RunLoop run_loop(&nested_dispatcher);
message_loop_quit_ = run_loop.QuitClosure();
run_loop.Run();
}
#else
internal::MenuEventDispatcher event_dispatcher(controller);
scoped_ptr<ui::ScopedEventDispatcher> dispatcher_override;
if (ui::PlatformEventSource::GetInstance()) {
dispatcher_override =
ui::PlatformEventSource::GetInstance()->OverrideDispatcher(
&event_dispatcher);
}
if (root) {
scoped_ptr<ActivationChangeObserverImpl> observer;
if (!nested_menu)
observer.reset(new ActivationChangeObserverImpl(controller, root));
aura::client::DispatcherRunLoop run_loop(
aura::client::GetDispatcherClient(root), NULL);
message_loop_quit_ = run_loop.QuitClosure();
run_loop.Run();
} else {
base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
base::MessageLoop::ScopedNestableTaskAllower allow(loop);
base::RunLoop run_loop;
message_loop_quit_ = run_loop.QuitClosure();
run_loop.Run();
}
#endif
}
void MenuMessageLoopAura::QuitNow() {
CHECK(!message_loop_quit_.is_null());
message_loop_quit_.Run();
}
void MenuMessageLoopAura::ClearOwner() {
owner_ = NULL;
}
} // namespace views
|