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_runner_impl.h" | 5 #include "ui/views/controls/menu/menu_runner_impl.h" |
6 | 6 |
7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
8 #include "ui/native_theme/native_theme.h" | 8 #include "ui/native_theme/native_theme.h" |
9 #include "ui/views/controls/button/menu_button.h" | 9 #include "ui/views/controls/button/menu_button.h" |
10 #include "ui/views/controls/menu/menu_controller.h" | 10 #include "ui/views/controls/menu/menu_controller.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 int32_t run_types, | 26 int32_t run_types, |
27 const base::Closure& on_menu_closed_callback) { | 27 const base::Closure& on_menu_closed_callback) { |
28 return new MenuRunnerImplAdapter(menu_model, on_menu_closed_callback); | 28 return new MenuRunnerImplAdapter(menu_model, on_menu_closed_callback); |
29 } | 29 } |
30 #endif | 30 #endif |
31 | 31 |
32 MenuRunnerImpl::MenuRunnerImpl(MenuItemView* menu) | 32 MenuRunnerImpl::MenuRunnerImpl(MenuItemView* menu) |
33 : menu_(menu), | 33 : menu_(menu), |
34 running_(false), | 34 running_(false), |
35 delete_after_run_(false), | 35 delete_after_run_(false), |
36 async_(false), | |
37 for_drop_(false), | 36 for_drop_(false), |
38 controller_(NULL), | 37 controller_(NULL), |
39 owns_controller_(false), | 38 owns_controller_(false), |
40 weak_factory_(this) {} | 39 weak_factory_(this) {} |
41 | 40 |
42 bool MenuRunnerImpl::IsRunning() const { | 41 bool MenuRunnerImpl::IsRunning() const { |
43 return running_; | 42 return running_; |
44 } | 43 } |
45 | 44 |
46 void MenuRunnerImpl::Release() { | 45 void MenuRunnerImpl::Release() { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 // expected. | 106 // expected. |
108 return MenuRunner::NORMAL_EXIT; | 107 return MenuRunner::NORMAL_EXIT; |
109 } | 108 } |
110 // Drop menus don't block the message loop, so it's ok to create a new | 109 // Drop menus don't block the message loop, so it's ok to create a new |
111 // MenuController. | 110 // MenuController. |
112 controller = NULL; | 111 controller = NULL; |
113 } | 112 } |
114 } | 113 } |
115 | 114 |
116 running_ = true; | 115 running_ = true; |
117 async_ = (run_types & MenuRunner::ASYNC) != 0; | |
118 for_drop_ = (run_types & MenuRunner::FOR_DROP) != 0; | 116 for_drop_ = (run_types & MenuRunner::FOR_DROP) != 0; |
119 bool has_mnemonics = (run_types & MenuRunner::HAS_MNEMONICS) != 0; | 117 bool has_mnemonics = (run_types & MenuRunner::HAS_MNEMONICS) != 0; |
120 owns_controller_ = false; | 118 owns_controller_ = false; |
121 if (!controller) { | 119 if (!controller) { |
122 // No menus are showing, show one. | 120 // No menus are showing, show one. |
123 controller = new MenuController(!for_drop_, this); | 121 controller = new MenuController(!for_drop_, this); |
124 owns_controller_ = true; | 122 owns_controller_ = true; |
125 } | 123 } |
126 controller->SetAsyncRun(async_); | |
127 controller->set_is_combobox((run_types & MenuRunner::COMBOBOX) != 0); | 124 controller->set_is_combobox((run_types & MenuRunner::COMBOBOX) != 0); |
128 controller_ = controller->AsWeakPtr(); | 125 controller_ = controller->AsWeakPtr(); |
129 menu_->set_controller(controller_.get()); | 126 menu_->set_controller(controller_.get()); |
130 menu_->PrepareForRun(owns_controller_, | 127 menu_->PrepareForRun(owns_controller_, |
131 has_mnemonics, | 128 has_mnemonics, |
132 !for_drop_ && ShouldShowMnemonics(button)); | 129 !for_drop_ && ShouldShowMnemonics(button)); |
133 | 130 |
134 // Run the loop. | |
135 int mouse_event_flags = 0; | 131 int mouse_event_flags = 0; |
136 MenuItemView* result = | 132 controller->Run(parent, button, menu_, bounds, anchor, |
137 controller->Run(parent, | 133 (run_types & MenuRunner::CONTEXT_MENU) != 0, |
138 button, | 134 (run_types & MenuRunner::NESTED_DRAG) != 0, |
139 menu_, | 135 &mouse_event_flags); |
140 bounds, | 136 // We finish processing results in OnMenuClosed. |
141 anchor, | 137 return MenuRunner::NORMAL_EXIT; |
142 (run_types & MenuRunner::CONTEXT_MENU) != 0, | |
143 (run_types & MenuRunner::NESTED_DRAG) != 0, | |
144 &mouse_event_flags); | |
145 // Get the time of the event which closed this menu. | |
146 closing_event_time_ = controller->closing_event_time(); | |
147 if (for_drop_ || async_) { | |
148 // Drop and asynchronous menus return immediately. We finish processing in | |
149 // OnMenuClosed. | |
150 return MenuRunner::NORMAL_EXIT; | |
151 } | |
152 return MenuDone(NOTIFY_DELEGATE, result, mouse_event_flags); | |
153 } | 138 } |
154 | 139 |
155 void MenuRunnerImpl::Cancel() { | 140 void MenuRunnerImpl::Cancel() { |
156 if (running_) | 141 if (running_) |
157 controller_->Cancel(MenuController::EXIT_ALL); | 142 controller_->Cancel(MenuController::EXIT_ALL); |
158 } | 143 } |
159 | 144 |
160 base::TimeTicks MenuRunnerImpl::GetClosingEventTime() const { | 145 base::TimeTicks MenuRunnerImpl::GetClosingEventTime() const { |
161 return closing_event_time_; | 146 return closing_event_time_; |
162 } | 147 } |
163 | 148 |
164 void MenuRunnerImpl::OnMenuClosed(NotifyType type, | 149 void MenuRunnerImpl::OnMenuClosed(NotifyType type, |
165 MenuItemView* menu, | 150 MenuItemView* menu, |
166 int mouse_event_flags) { | 151 int mouse_event_flags) { |
167 MenuDone(type, menu, mouse_event_flags); | 152 if (controller_) |
168 } | 153 closing_event_time_ = controller_->closing_event_time(); |
169 | |
170 void MenuRunnerImpl::SiblingMenuCreated(MenuItemView* menu) { | |
171 if (menu != menu_ && sibling_menus_.count(menu) == 0) | |
172 sibling_menus_.insert(menu); | |
173 } | |
174 | |
175 MenuRunnerImpl::~MenuRunnerImpl() { | |
176 delete menu_; | |
177 for (std::set<MenuItemView*>::iterator i = sibling_menus_.begin(); | |
178 i != sibling_menus_.end(); | |
179 ++i) | |
180 delete *i; | |
181 } | |
182 | |
183 MenuRunner::RunResult MenuRunnerImpl::MenuDone(NotifyType type, | |
184 MenuItemView* result, | |
185 int mouse_event_flags) { | |
186 menu_->RemoveEmptyMenus(); | 154 menu_->RemoveEmptyMenus(); |
187 menu_->set_controller(nullptr); | 155 menu_->set_controller(nullptr); |
188 | 156 |
189 if (owns_controller_ && controller_) { | 157 if (owns_controller_ && controller_) { |
190 // We created the controller and need to delete it. | 158 // We created the controller and need to delete it. |
191 delete controller_.get(); | 159 delete controller_.get(); |
192 owns_controller_ = false; | 160 owns_controller_ = false; |
193 } | 161 } |
194 controller_ = nullptr; | 162 controller_ = nullptr; |
195 // Make sure all the windows we created to show the menus have been | 163 // Make sure all the windows we created to show the menus have been |
196 // destroyed. | 164 // destroyed. |
197 menu_->DestroyAllMenuHosts(); | 165 menu_->DestroyAllMenuHosts(); |
198 if (delete_after_run_) { | 166 if (delete_after_run_) { |
199 delete this; | 167 delete this; |
200 return MenuRunner::MENU_DELETED; | 168 return; |
201 } | 169 } |
202 running_ = false; | 170 running_ = false; |
203 if (menu_->GetDelegate()) { | 171 if (menu_->GetDelegate()) { |
204 // Executing the command may also delete this. | 172 // Executing the command may also delete this. |
205 base::WeakPtr<MenuRunnerImpl> ref(weak_factory_.GetWeakPtr()); | 173 base::WeakPtr<MenuRunnerImpl> ref(weak_factory_.GetWeakPtr()); |
206 if (result && !for_drop_) { | 174 if (menu && !for_drop_) { |
207 // Do not execute the menu that was dragged/dropped. | 175 // Do not execute the menu that was dragged/dropped. |
208 menu_->GetDelegate()->ExecuteCommand(result->GetCommand(), | 176 menu_->GetDelegate()->ExecuteCommand(menu->GetCommand(), |
209 mouse_event_flags); | 177 mouse_event_flags); |
210 } | 178 } |
211 // Only notify the delegate if it did not delete this. | 179 // Only notify the delegate if it did not delete this. |
212 if (!ref) | 180 if (!ref) |
213 return MenuRunner::MENU_DELETED; | 181 return; |
214 else if (type == NOTIFY_DELEGATE) | 182 else if (type == NOTIFY_DELEGATE) |
215 menu_->GetDelegate()->OnMenuClosed(result, MenuRunner::NORMAL_EXIT); | 183 menu_->GetDelegate()->OnMenuClosed(menu, MenuRunner::NORMAL_EXIT); |
216 } | 184 } |
217 return MenuRunner::NORMAL_EXIT; | 185 } |
| 186 |
| 187 void MenuRunnerImpl::SiblingMenuCreated(MenuItemView* menu) { |
| 188 if (menu != menu_ && sibling_menus_.count(menu) == 0) |
| 189 sibling_menus_.insert(menu); |
| 190 } |
| 191 |
| 192 MenuRunnerImpl::~MenuRunnerImpl() { |
| 193 delete menu_; |
| 194 for (std::set<MenuItemView*>::iterator i = sibling_menus_.begin(); |
| 195 i != sibling_menus_.end(); ++i) |
| 196 delete *i; |
218 } | 197 } |
219 | 198 |
220 bool MenuRunnerImpl::ShouldShowMnemonics(MenuButton* button) { | 199 bool MenuRunnerImpl::ShouldShowMnemonics(MenuButton* button) { |
221 // Show mnemonics if the button has focus or alt is pressed. | 200 // Show mnemonics if the button has focus or alt is pressed. |
222 bool show_mnemonics = button ? button->HasFocus() : false; | 201 bool show_mnemonics = button ? button->HasFocus() : false; |
223 #if defined(OS_WIN) | 202 #if defined(OS_WIN) |
224 // This is only needed on Windows. | 203 // This is only needed on Windows. |
225 if (!show_mnemonics) | 204 if (!show_mnemonics) |
226 show_mnemonics = ui::win::IsAltPressed(); | 205 show_mnemonics = ui::win::IsAltPressed(); |
227 #endif | 206 #endif |
228 return show_mnemonics; | 207 return show_mnemonics; |
229 } | 208 } |
230 | 209 |
231 } // namespace internal | 210 } // namespace internal |
232 } // namespace views | 211 } // namespace views |
OLD | NEW |