OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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.h" | 5 #include "ui/views/controls/menu/menu_runner.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
| 9 #include "base/metrics/histogram.h" |
9 #include "ui/views/controls/button/menu_button.h" | 10 #include "ui/views/controls/button/menu_button.h" |
10 #include "ui/views/controls/menu/menu_controller.h" | 11 #include "ui/views/controls/menu/menu_controller.h" |
11 #include "ui/views/controls/menu/menu_controller_delegate.h" | 12 #include "ui/views/controls/menu/menu_controller_delegate.h" |
12 #include "ui/views/controls/menu/menu_delegate.h" | 13 #include "ui/views/controls/menu/menu_delegate.h" |
| 14 #include "ui/views/controls/menu/submenu_view.h" |
13 #include "ui/views/widget/widget.h" | 15 #include "ui/views/widget/widget.h" |
14 | 16 |
15 #if defined(OS_WIN) | 17 #if defined(OS_WIN) |
16 #include "base/win/win_util.h" | 18 #include "base/win/win_util.h" |
17 #endif | 19 #endif |
18 | 20 |
19 namespace views { | 21 namespace views { |
20 | 22 |
21 namespace internal { | 23 namespace internal { |
22 | 24 |
| 25 void RecordSelectedIndexes(const MenuItemView* menu_item) { |
| 26 if (!menu_item) |
| 27 return; |
| 28 const MenuItemView* parent = menu_item->GetParentMenuItem(); |
| 29 if (!parent) |
| 30 return; |
| 31 |
| 32 SubmenuView* submenu = parent->GetSubmenu(); |
| 33 for (int i = 0; i < submenu->GetMenuItemCount(); ++i) { |
| 34 if (submenu->GetMenuItemAt(i) == menu_item) { |
| 35 HISTOGRAM_COUNTS_100("MenuSelection.Index", i); |
| 36 break; |
| 37 } |
| 38 } |
| 39 |
| 40 RecordSelectedIndexes(parent); |
| 41 } |
| 42 |
| 43 void RecordMenuStats(MenuItemView* result, base::TimeDelta time_elapsed) { |
| 44 // Report if user made a selection. |
| 45 HISTOGRAM_BOOLEAN("MenuSelection", result != NULL); |
| 46 |
| 47 if (result) { |
| 48 // Report how much time it took to make a selection. |
| 49 HISTOGRAM_TIMES("MenuSelection.Time", time_elapsed); |
| 50 RecordSelectedIndexes(result); |
| 51 } |
| 52 } |
| 53 |
23 // Manages the menu. To destroy a MenuRunnerImpl invoke Release(). Release() | 54 // Manages the menu. To destroy a MenuRunnerImpl invoke Release(). Release() |
24 // deletes immediately if the menu isn't showing. If the menu is showing | 55 // deletes immediately if the menu isn't showing. If the menu is showing |
25 // Release() cancels the menu and when the nested RunMenuAt() call returns | 56 // Release() cancels the menu and when the nested RunMenuAt() call returns |
26 // deletes itself and the menu. | 57 // deletes itself and the menu. |
27 class MenuRunnerImpl : public internal::MenuControllerDelegate { | 58 class MenuRunnerImpl : public internal::MenuControllerDelegate { |
28 public: | 59 public: |
29 explicit MenuRunnerImpl(MenuItemView* menu); | 60 explicit MenuRunnerImpl(MenuItemView* menu); |
30 | 61 |
31 MenuItemView* menu() { return menu_; } | 62 MenuItemView* menu() { return menu_; } |
32 | 63 |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 // We can't open another menu, otherwise the message loop would become | 182 // We can't open another menu, otherwise the message loop would become |
152 // twice nested. This isn't necessarily a problem, but generally isn't | 183 // twice nested. This isn't necessarily a problem, but generally isn't |
153 // expected. | 184 // expected. |
154 return MenuRunner::NORMAL_EXIT; | 185 return MenuRunner::NORMAL_EXIT; |
155 } | 186 } |
156 // Drop menus don't block the message loop, so it's ok to create a new | 187 // Drop menus don't block the message loop, so it's ok to create a new |
157 // MenuController. | 188 // MenuController. |
158 controller = NULL; | 189 controller = NULL; |
159 } | 190 } |
160 } | 191 } |
| 192 |
161 running_ = true; | 193 running_ = true; |
162 for_drop_ = (types & MenuRunner::FOR_DROP) != 0; | 194 for_drop_ = (types & MenuRunner::FOR_DROP) != 0; |
163 bool has_mnemonics = (types & MenuRunner::HAS_MNEMONICS) != 0 && !for_drop_; | 195 bool has_mnemonics = (types & MenuRunner::HAS_MNEMONICS) != 0 && !for_drop_; |
164 owns_controller_ = false; | 196 owns_controller_ = false; |
165 if (!controller) { | 197 if (!controller) { |
166 // No menus are showing, show one. | 198 // No menus are showing, show one. |
167 ui::NativeTheme* theme = parent ? parent->GetNativeTheme() : | 199 ui::NativeTheme* theme = parent ? parent->GetNativeTheme() : |
168 ui::NativeTheme::instance(); | 200 ui::NativeTheme::instance(); |
169 controller = new MenuController(theme, !for_drop_, this); | 201 controller = new MenuController(theme, !for_drop_, this); |
170 owns_controller_ = true; | 202 owns_controller_ = true; |
171 } | 203 } |
172 controller_ = controller; | 204 controller_ = controller; |
173 menu_->set_controller(controller_); | 205 menu_->set_controller(controller_); |
174 menu_->PrepareForRun(owns_controller_, | 206 menu_->PrepareForRun(owns_controller_, |
175 has_mnemonics, | 207 has_mnemonics, |
176 !for_drop_ && ShouldShowMnemonics(button)); | 208 !for_drop_ && ShouldShowMnemonics(button)); |
177 | 209 |
178 // Run the loop. | 210 // Run the loop. |
| 211 base::TimeTicks start_time = base::TimeTicks::Now(); |
179 int mouse_event_flags = 0; | 212 int mouse_event_flags = 0; |
180 MenuItemView* result = controller->Run(parent, button, menu_, bounds, anchor, | 213 MenuItemView* result = controller->Run(parent, button, menu_, bounds, anchor, |
181 (types & MenuRunner::CONTEXT_MENU) != 0, | 214 (types & MenuRunner::CONTEXT_MENU) != 0, |
182 &mouse_event_flags); | 215 &mouse_event_flags); |
183 | 216 |
184 if (for_drop_) { | 217 if (for_drop_) { |
185 // Drop menus return immediately. We finish processing in DropMenuClosed. | 218 // Drop menus return immediately. We finish processing in DropMenuClosed. |
186 return MenuRunner::NORMAL_EXIT; | 219 return MenuRunner::NORMAL_EXIT; |
187 } | 220 } |
188 | 221 RecordMenuStats(result, base::TimeTicks::Now() - start_time); |
189 return MenuDone(result, mouse_event_flags); | 222 return MenuDone(result, mouse_event_flags); |
190 } | 223 } |
191 | 224 |
192 void MenuRunnerImpl::Cancel() { | 225 void MenuRunnerImpl::Cancel() { |
193 if (running_) | 226 if (running_) |
194 controller_->Cancel(MenuController::EXIT_ALL); | 227 controller_->Cancel(MenuController::EXIT_ALL); |
195 } | 228 } |
196 | 229 |
197 void MenuRunnerImpl::DropMenuClosed(NotifyType type, MenuItemView* menu) { | 230 void MenuRunnerImpl::DropMenuClosed(NotifyType type, MenuItemView* menu) { |
198 MenuDone(NULL, 0); | 231 MenuDone(NULL, 0); |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 | 333 |
301 bool MenuRunner::IsRunning() const { | 334 bool MenuRunner::IsRunning() const { |
302 return holder_->running(); | 335 return holder_->running(); |
303 } | 336 } |
304 | 337 |
305 void MenuRunner::Cancel() { | 338 void MenuRunner::Cancel() { |
306 holder_->Cancel(); | 339 holder_->Cancel(); |
307 } | 340 } |
308 | 341 |
309 } // namespace views | 342 } // namespace views |
OLD | NEW |