Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(330)

Side by Side Diff: ui/views/controls/menu/menu_message_loop_aura.cc

Issue 1138523006: Enable keyboard accelerators while a menu is open (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixed a bug in restoring the delegate of the parent nested runloop. Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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
147 // The run_loop is about to start, QuitNow() hasn't been called for it yet.
148 int current_run_loop_id = ++g_last_run_loop_id_;
149 run_loop_ids_stack_.push(current_run_loop_id);
150 run_loops_status_[current_run_loop_id] = false;
151
142 #if defined(OS_WIN) 152 #if defined(OS_WIN)
143 internal::MenuMessagePumpDispatcher nested_dispatcher(controller); 153 internal::MenuMessagePumpDispatcher nested_dispatcher(controller);
144 if (root) { 154 if (root) {
145 scoped_ptr<ActivationChangeObserverImpl> observer; 155 scoped_ptr<ActivationChangeObserverImpl> observer;
146 if (!nested_menu) 156 if (!nested_menu)
147 observer.reset(new ActivationChangeObserverImpl(controller, root)); 157 observer.reset(new ActivationChangeObserverImpl(controller, root));
148 aura::client::DispatcherRunLoop run_loop( 158 aura::client::DispatcherRunLoop run_loop(
149 aura::client::GetDispatcherClient(root), &nested_dispatcher); 159 aura::client::GetDispatcherClient(root), &nested_dispatcher);
150 message_loop_quit_ = run_loop.QuitClosure(); 160 message_loop_quit_ = run_loop.QuitClosure();
151 run_loop.Run(); 161 run_loop.Run();
152 } else { 162 } else {
153 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); 163 base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
154 base::MessageLoop::ScopedNestableTaskAllower allow(loop); 164 base::MessageLoop::ScopedNestableTaskAllower allow(loop);
155 base::RunLoop run_loop(&nested_dispatcher); 165 base::RunLoop run_loop(&nested_dispatcher);
156 message_loop_quit_ = run_loop.QuitClosure(); 166 message_loop_quit_ = run_loop.QuitClosure();
157 run_loop.Run(); 167 run_loop.Run();
158 } 168 }
159 #else 169 #else
160 internal::MenuEventDispatcher event_dispatcher(controller); 170 DCHECK(menu_event_filter_.get());
161 scoped_ptr<ui::ScopedEventDispatcher> dispatcher_override; 171
162 if (ui::PlatformEventSource::GetInstance()) { 172 scoped_ptr<ActivationChangeObserverImpl> observer;
163 dispatcher_override = 173 MenuEventFilter::Delegate* previous_delegate =
164 ui::PlatformEventSource::GetInstance()->OverrideDispatcher( 174 menu_event_filter_->GetCurrentDelegate();
165 &event_dispatcher); 175
166 }
167 if (root) { 176 if (root) {
168 scoped_ptr<ActivationChangeObserverImpl> observer;
169 if (!nested_menu) 177 if (!nested_menu)
170 observer.reset(new ActivationChangeObserverImpl(controller, root)); 178 observer.reset(new ActivationChangeObserverImpl(controller, root));
171 aura::client::DispatcherRunLoop run_loop( 179 MenuEventFilter::Delegate* filter_delegate =
172 aura::client::GetDispatcherClient(root), NULL); 180 MenuEventFilter::GetMenuEventFilterDelegate(root);
173 message_loop_quit_ = run_loop.QuitClosure(); 181 if (filter_delegate)
174 run_loop.Run(); 182 menu_event_filter_->SetDelegate(filter_delegate);
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 } 183 }
182 #endif 184
185 aura::Env::GetInstanceDontCreate()->PrependPreTargetHandler(
186 menu_event_filter_.get());
187
188 base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
189 base::MessageLoop::ScopedNestableTaskAllower allow(loop);
190 base::RunLoop run_loop;
191 message_loop_quit_ = run_loop.QuitClosure();
192
193 run_loop.Run();
194
195 // Restore the previous delegate in the case of nested run loops.
196 menu_event_filter_->SetDelegate(previous_delegate);
pkotwicz 2015/08/27 17:19:55 Can we remove the MenuEventFilter pretarget handle
afakhry 2015/08/28 01:24:41 This is an awesome idea! I don't even need to care
197 #endif // defined(OS_WIN)
198
199 // Now erase this run_loop ID to mark it as returned.
200 run_loops_status_.erase(current_run_loop_id);
201 run_loop_ids_stack_.pop();
183 } 202 }
184 203
185 void MenuMessageLoopAura::QuitNow() { 204 void MenuMessageLoopAura::QuitNow() {
186 CHECK(!message_loop_quit_.is_null()); 205 CHECK(!message_loop_quit_.is_null());
206
207 if (run_loop_ids_stack_.empty())
208 return;
209
210 // This RunLoop has already returned? We should not call QuitNow() again.
211 int current_run_loop_id = run_loop_ids_stack_.top();
212 auto itr = run_loops_status_.find(current_run_loop_id);
213 if (itr == run_loops_status_.end())
214 return;
215
216 // QuitNow() has already been called?
217 if (itr->second)
218 return;
219
220 // Overwrite the status so that QuitNow() is marked as called for this
221 // RunLoop.
222 run_loops_status_[current_run_loop_id] = true;
223
187 message_loop_quit_.Run(); 224 message_loop_quit_.Run();
188 225
189 #if !defined(OS_WIN) 226 #if !defined(OS_WIN)
227 aura::Env::GetInstanceDontCreate()->RemovePreTargetHandler(
228 menu_event_filter_.get());
229 menu_event_filter_->ClearDelegate();
230
190 // Ask PlatformEventSource to stop dispatching events in this message loop 231 // Ask PlatformEventSource to stop dispatching events in this message loop
191 // iteration. We want our menu's loop to return before the next event. 232 // iteration. We want our menu's loop to return before the next event.
192 if (ui::PlatformEventSource::GetInstance()) 233 if (ui::PlatformEventSource::GetInstance())
193 ui::PlatformEventSource::GetInstance()->StopCurrentEventStream(); 234 ui::PlatformEventSource::GetInstance()->StopCurrentEventStream();
194 #endif 235 #endif
195 } 236 }
196 237
197 void MenuMessageLoopAura::ClearOwner() { 238 void MenuMessageLoopAura::ClearOwner() {
198 owner_ = NULL; 239 owner_ = NULL;
199 } 240 }
200 241
201 } // namespace views 242 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698