OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/views/controls/menu/menu_message_loop_aura.h" | 5 #include "ui/views/controls/menu/menu_message_loop_aura.h" |
6 | 6 |
7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <windowsx.h> | 8 #include <windowsx.h> |
9 #endif | 9 #endif |
10 | 10 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
89 root_->RemoveObserver(this); | 89 root_->RemoveObserver(this); |
90 root_ = NULL; | 90 root_ = NULL; |
91 } | 91 } |
92 | 92 |
93 MenuController* controller_; | 93 MenuController* controller_; |
94 aura::Window* root_; | 94 aura::Window* root_; |
95 | 95 |
96 DISALLOW_COPY_AND_ASSIGN(ActivationChangeObserverImpl); | 96 DISALLOW_COPY_AND_ASSIGN(ActivationChangeObserverImpl); |
97 }; | 97 }; |
98 | 98 |
99 // The ID that has been given to the last created run_loop. | |
100 int g_last_run_loop_id_ = 0; | |
101 | |
99 } // namespace | 102 } // namespace |
100 | 103 |
101 // static | 104 // static |
102 MenuMessageLoop* MenuMessageLoop::Create() { | 105 MenuMessageLoop* MenuMessageLoop::Create() { |
103 return new MenuMessageLoopAura; | 106 return new MenuMessageLoopAura; |
104 } | 107 } |
105 | 108 |
106 MenuMessageLoopAura::MenuMessageLoopAura() : owner_(NULL) { | 109 MenuMessageLoopAura::MenuMessageLoopAura() |
110 : owner_(NULL), | |
111 menu_event_filter_(new MenuEventFilter) { | |
107 } | 112 } |
108 | 113 |
109 MenuMessageLoopAura::~MenuMessageLoopAura() { | 114 MenuMessageLoopAura::~MenuMessageLoopAura() { |
110 } | 115 } |
111 | 116 |
112 void MenuMessageLoopAura::RepostEventToWindow(const ui::LocatedEvent& event, | 117 void MenuMessageLoopAura::RepostEventToWindow(const ui::LocatedEvent& event, |
113 gfx::NativeWindow window, | 118 gfx::NativeWindow window, |
114 const gfx::Point& screen_loc) { | 119 const gfx::Point& screen_loc) { |
115 aura::Window* root = window->GetRootWindow(); | 120 aura::Window* root = window->GetRootWindow(); |
116 ScreenPositionClient* spc = aura::client::GetScreenPositionClient(root); | 121 ScreenPositionClient* spc = aura::client::GetScreenPositionClient(root); |
(...skipping 15 matching lines...) Expand all Loading... | |
132 // |owner_| may be NULL. | 137 // |owner_| may be NULL. |
133 owner_ = owner; | 138 owner_ = owner; |
134 aura::Window* root = GetOwnerRootWindow(owner_); | 139 aura::Window* root = GetOwnerRootWindow(owner_); |
135 // It is possible for the same MenuMessageLoopAura to start a nested | 140 // 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 | 141 // 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 | 142 // quit-closure gets reset to the outer loop's quit-closure once the innermost |
138 // loop terminates. | 143 // loop terminates. |
139 base::AutoReset<base::Closure> reset_quit_closure(&message_loop_quit_, | 144 base::AutoReset<base::Closure> reset_quit_closure(&message_loop_quit_, |
140 base::Closure()); | 145 base::Closure()); |
141 | 146 |
142 #if defined(OS_WIN) | 147 scoped_ptr<ActivationChangeObserverImpl> observer; |
143 internal::MenuMessagePumpDispatcher nested_dispatcher(controller); | |
144 if (root) { | 148 if (root) { |
145 scoped_ptr<ActivationChangeObserverImpl> observer; | |
146 if (!nested_menu) | 149 if (!nested_menu) |
147 observer.reset(new ActivationChangeObserverImpl(controller, root)); | 150 observer.reset(new ActivationChangeObserverImpl(controller, root)); |
148 aura::client::DispatcherRunLoop run_loop( | 151 MenuEventFilter::Delegate* filter_delegate = |
149 aura::client::GetDispatcherClient(root), &nested_dispatcher); | 152 MenuEventFilter::GetMenuEventFilterDelegate(root); |
pkotwicz
2015/08/26 01:26:54
Don't we still need DispatcherRunLoop on Windows?
afakhry
2015/08/26 18:39:21
Done.
| |
150 message_loop_quit_ = run_loop.QuitClosure(); | 153 if (filter_delegate) |
151 run_loop.Run(); | 154 menu_event_filter_->SetDelegate(filter_delegate); |
152 } else { | |
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(); | |
157 run_loop.Run(); | |
158 } | 155 } |
156 | |
157 CHECK(menu_event_filter_.get()); | |
158 aura::Env::GetInstanceDontCreate()->PrependPreTargetHandler( | |
159 menu_event_filter_.get()); | |
pkotwicz
2015/08/26 01:26:54
Shouldn't we only be attaching the MenuEventFilter
afakhry
2015/08/26 18:39:21
Done. I kept the windows code exactly as is.
| |
160 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); | |
161 base::MessageLoop::ScopedNestableTaskAllower allow(loop); | |
162 | |
163 #if defined(OS_WIN) | |
164 // On Windows we need the following nested dispatcher to handle some | |
165 // Windows-specific events like WM_CONTEXTMENU and others that can't be | |
166 // captured by the MenuEventFilter. | |
167 internal::MenuMessagePumpDispatcher nested_dispatcher(controller); | |
168 base::RunLoop run_loop(&nested_dispatcher); | |
159 #else | 169 #else |
160 internal::MenuEventDispatcher event_dispatcher(controller); | 170 base::RunLoop run_loop; |
161 scoped_ptr<ui::ScopedEventDispatcher> dispatcher_override; | 171 #endif // defined(OS_WIN) |
162 if (ui::PlatformEventSource::GetInstance()) { | 172 message_loop_quit_ = run_loop.QuitClosure(); |
163 dispatcher_override = | 173 // The run_loop is about to start, QuitNow() hasn't been called for it yet. |
164 ui::PlatformEventSource::GetInstance()->OverrideDispatcher( | 174 int current_run_loop_id = ++g_last_run_loop_id_; |
165 &event_dispatcher); | 175 run_loop_ids_stack_.push(current_run_loop_id); |
166 } | 176 run_loops_status_[current_run_loop_id] = false; |
167 if (root) { | 177 |
168 scoped_ptr<ActivationChangeObserverImpl> observer; | 178 run_loop.Run(); |
169 if (!nested_menu) | 179 |
170 observer.reset(new ActivationChangeObserverImpl(controller, root)); | 180 // Now erase this run_loop ID to mark it as returned. |
171 aura::client::DispatcherRunLoop run_loop( | 181 run_loops_status_.erase(current_run_loop_id); |
172 aura::client::GetDispatcherClient(root), NULL); | 182 run_loop_ids_stack_.pop(); |
173 message_loop_quit_ = run_loop.QuitClosure(); | |
174 run_loop.Run(); | |
175 } else { | |
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(); | |
180 run_loop.Run(); | |
181 } | |
182 #endif | |
183 } | 183 } |
184 | 184 |
185 void MenuMessageLoopAura::QuitNow() { | 185 void MenuMessageLoopAura::QuitNow() { |
186 CHECK(!message_loop_quit_.is_null()); | 186 CHECK(!message_loop_quit_.is_null()); |
187 | |
188 if (run_loop_ids_stack_.empty()) | |
189 return; | |
190 | |
191 // This RunLoop has already returned? We should not call QuitNow() again. | |
192 int current_run_loop_id = run_loop_ids_stack_.top(); | |
193 auto itr = run_loops_status_.find(current_run_loop_id); | |
194 if (itr == run_loops_status_.end()) | |
195 return; | |
196 | |
197 // QuitNow() has already been called? | |
198 if (itr->second) | |
199 return; | |
200 | |
201 // Overwrite the status so that QuitNow() is marked as called for this | |
202 // RunLoop. | |
203 run_loops_status_[current_run_loop_id] = true; | |
204 | |
187 message_loop_quit_.Run(); | 205 message_loop_quit_.Run(); |
188 | 206 |
207 aura::Env::GetInstanceDontCreate()->RemovePreTargetHandler( | |
208 menu_event_filter_.get()); | |
209 menu_event_filter_->ClearDelegate(); | |
pkotwicz
2015/08/26 01:26:54
With this CL applied, if I:
1) Open the wrench men
afakhry
2015/08/26 18:39:21
That's weird. I can't repro this at all. In fact t
| |
210 | |
189 #if !defined(OS_WIN) | 211 #if !defined(OS_WIN) |
190 // Ask PlatformEventSource to stop dispatching events in this message loop | 212 // Ask PlatformEventSource to stop dispatching events in this message loop |
191 // iteration. We want our menu's loop to return before the next event. | 213 // iteration. We want our menu's loop to return before the next event. |
192 if (ui::PlatformEventSource::GetInstance()) | 214 if (ui::PlatformEventSource::GetInstance()) |
193 ui::PlatformEventSource::GetInstance()->StopCurrentEventStream(); | 215 ui::PlatformEventSource::GetInstance()->StopCurrentEventStream(); |
194 #endif | 216 #endif |
195 } | 217 } |
196 | 218 |
197 void MenuMessageLoopAura::ClearOwner() { | 219 void MenuMessageLoopAura::ClearOwner() { |
198 owner_ = NULL; | 220 owner_ = NULL; |
199 } | 221 } |
200 | 222 |
201 } // namespace views | 223 } // namespace views |
OLD | NEW |