OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "ui/views/controls/menu/menu_message_loop_aura.h" | |
6 | |
7 #if defined(OS_WIN) | |
8 #include <windowsx.h> | |
9 #endif | |
10 | |
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" | |
26 | |
27 #if defined(OS_WIN) | |
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" | |
31 #else | |
32 #include "ui/views/controls/menu/menu_event_dispatcher_linux.h" | |
33 #endif | |
34 | |
35 using aura::client::ScreenPositionClient; | |
36 | |
37 namespace views { | |
38 | |
39 namespace { | |
40 | |
41 aura::Window* GetOwnerRootWindow(views::Widget* owner) { | |
42 return owner ? owner->GetNativeWindow()->GetRootWindow() : NULL; | |
43 } | |
44 | |
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 { | |
52 public: | |
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); | |
58 } | |
59 | |
60 virtual ~ActivationChangeObserverImpl() { Cleanup(); } | |
61 | |
62 // aura::client::ActivationChangeObserver: | |
63 virtual void OnWindowActivated(aura::Window* gained_active, | |
64 aura::Window* lost_active) OVERRIDE { | |
65 if (!controller_->drag_in_progress()) | |
66 controller_->CancelAll(); | |
67 } | |
68 | |
69 // aura::WindowObserver: | |
70 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE { Cleanup(); } | |
71 | |
72 // ui::EventHandler: | |
73 virtual void OnCancelMode(ui::CancelModeEvent* event) OVERRIDE { | |
74 controller_->CancelAll(); | |
75 } | |
76 | |
77 private: | |
78 void Cleanup() { | |
79 if (!root_) | |
80 return; | |
81 // The ActivationClient may have been destroyed by the time we get here. | |
82 aura::client::ActivationClient* client = | |
83 aura::client::GetActivationClient(root_); | |
84 if (client) | |
85 client->RemoveObserver(this); | |
86 root_->RemovePreTargetHandler(this); | |
87 root_->RemoveObserver(this); | |
88 root_ = NULL; | |
89 } | |
90 | |
91 MenuController* controller_; | |
92 aura::Window* root_; | |
93 | |
94 DISALLOW_COPY_AND_ASSIGN(ActivationChangeObserverImpl); | |
95 }; | |
96 | |
97 } // namespace | |
98 | |
99 // static | |
100 MenuMessageLoop* MenuMessageLoop::Create() { | |
101 return new MenuMessageLoopAura; | |
102 } | |
103 | |
104 MenuMessageLoopAura::MenuMessageLoopAura() | |
105 : owner_(NULL), message_loop_depth_(0) { | |
sky
2014/05/15 16:07:32
when you wrap, each param on its own line.
Andre
2014/05/15 18:45:59
Done.
Now fits in one line after the name change t
| |
106 } | |
107 | |
108 MenuMessageLoopAura::~MenuMessageLoopAura() { | |
109 } | |
110 | |
111 void MenuMessageLoopAura::RepostEventToWindow(const ui::LocatedEvent& event, | |
112 gfx::NativeWindow window, | |
113 gfx::Point screen_loc) { | |
114 aura::Window* root = window->GetRootWindow(); | |
115 ScreenPositionClient* spc = aura::client::GetScreenPositionClient(root); | |
116 if (!spc) | |
117 return; | |
118 | |
119 gfx::Point root_loc(screen_loc); | |
120 spc->ConvertPointFromScreen(root, &root_loc); | |
121 | |
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); | |
126 } | |
127 | |
128 void MenuMessageLoopAura::Run(MenuController* controller, | |
129 Widget* owner, | |
130 bool nested_menu) { | |
131 // We need to turn on nestable tasks as in some situations (pressing alt-f for | |
132 // one) the menus are run from a task. If we don't do this and are invoked | |
133 // from a task none of the tasks we schedule are processed and the menu | |
134 // appears totally broken. | |
135 message_loop_depth_++; | |
136 DCHECK_LE(message_loop_depth_, 2); | |
137 | |
138 // |owner_| may be NULL. | |
139 owner_ = owner; | |
140 aura::Window* root = GetOwnerRootWindow(owner_); | |
141 | |
142 #if defined(OS_WIN) | |
143 internal::MenuMessagePumpDispatcher nested_dispatcher(controller); | |
144 if (root) { | |
145 scoped_ptr<ActivationChangeObserverImpl> observer; | |
146 if (!nested_menu) | |
147 observer.reset(new ActivationChangeObserverImpl(controller, root)); | |
148 aura::client::GetDispatcherClient(root) | |
149 ->RunWithDispatcher(&nested_dispatcher); | |
150 } else { | |
151 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); | |
152 base::MessageLoop::ScopedNestableTaskAllower allow(loop); | |
153 base::RunLoop run_loop(&nested_dispatcher); | |
154 run_loop.Run(); | |
155 } | |
156 #else | |
157 internal::MenuEventDispatcher event_dispatcher(controller); | |
158 scoped_ptr<ui::ScopedEventDispatcher> old_dispatcher = | |
159 nested_dispatcher_.Pass(); | |
160 if (ui::PlatformEventSource::GetInstance()) { | |
161 nested_dispatcher_ = | |
162 ui::PlatformEventSource::GetInstance()->OverrideDispatcher( | |
163 &event_dispatcher); | |
164 } | |
165 if (root) { | |
166 scoped_ptr<ActivationChangeObserverImpl> observer; | |
167 if (!nested_menu) | |
168 observer.reset(new ActivationChangeObserverImpl(controller, root)); | |
169 aura::client::GetDispatcherClient(root)->RunWithDispatcher(NULL); | |
170 } else { | |
171 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); | |
sky
2014/05/15 16:07:32
How about a common function that has these four li
Andre
2014/05/15 18:45:59
Lines 153 and 173 are different, and nested_dispat
| |
172 base::MessageLoop::ScopedNestableTaskAllower allow(loop); | |
173 base::RunLoop run_loop; | |
174 run_loop.Run(); | |
175 } | |
176 nested_dispatcher_ = old_dispatcher.Pass(); | |
177 #endif | |
178 | |
179 message_loop_depth_--; | |
180 if (message_loop_depth_ == 0) | |
181 owner_ = NULL; | |
182 } | |
183 | |
184 bool MenuMessageLoopAura::ShouldQuitNow() const { | |
185 aura::Window* root = GetOwnerRootWindow(owner_); | |
186 return !aura::client::GetDragDropClient(root) || | |
187 !aura::client::GetDragDropClient(root)->IsDragDropInProgress(); | |
188 } | |
189 | |
190 void MenuMessageLoopAura::QuitNow() { | |
191 if (owner_) { | |
192 // It's safe to invoke QuitNestedMessageLoop() multiple times, it only | |
193 // effects the current loop. | |
194 aura::Window* root = owner_->GetNativeWindow()->GetRootWindow(); | |
195 aura::client::GetDispatcherClient(root)->QuitNestedMessageLoop(); | |
196 } else { | |
197 base::MessageLoop::current()->QuitNow(); | |
198 } | |
199 // Restore the previous dispatcher. | |
200 nested_dispatcher_.reset(); | |
201 } | |
202 | |
203 int MenuMessageLoopAura::message_loop_depth() const { | |
204 return message_loop_depth_; | |
205 } | |
206 | |
207 } // namespace views | |
OLD | NEW |