| 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> | |
| 8 | |
| 9 #include "base/memory/weak_ptr.h" | |
| 10 #include "ui/base/models/menu_model.h" | |
| 11 #include "ui/views/controls/button/menu_button.h" | |
| 12 #include "ui/views/controls/menu/menu_controller.h" | |
| 13 #include "ui/views/controls/menu/menu_controller_delegate.h" | |
| 14 #include "ui/views/controls/menu/menu_delegate.h" | |
| 15 #include "ui/views/controls/menu/menu_item_view.h" | |
| 16 #include "ui/views/controls/menu/menu_model_adapter.h" | |
| 17 #include "ui/views/controls/menu/menu_runner_handler.h" | 7 #include "ui/views/controls/menu/menu_runner_handler.h" |
| 18 #include "ui/views/widget/widget.h" | 8 #include "ui/views/controls/menu/menu_runner_impl.h" |
| 19 | |
| 20 #if defined(OS_WIN) | |
| 21 #include "base/win/win_util.h" | |
| 22 #endif | |
| 23 | 9 |
| 24 namespace views { | 10 namespace views { |
| 25 | 11 |
| 26 namespace internal { | |
| 27 | |
| 28 // Manages the menu. To destroy a MenuRunnerImpl invoke Release(). Release() | |
| 29 // deletes immediately if the menu isn't showing. If the menu is showing | |
| 30 // Release() cancels the menu and when the nested RunMenuAt() call returns | |
| 31 // deletes itself and the menu. | |
| 32 class MenuRunnerImpl : public internal::MenuControllerDelegate { | |
| 33 public: | |
| 34 explicit MenuRunnerImpl(MenuItemView* menu); | |
| 35 | |
| 36 bool running() const { return running_; } | |
| 37 | |
| 38 // See description above class for details. | |
| 39 void Release(); | |
| 40 | |
| 41 // Runs the menu. | |
| 42 MenuRunner::RunResult RunMenuAt(Widget* parent, | |
| 43 MenuButton* button, | |
| 44 const gfx::Rect& bounds, | |
| 45 MenuAnchorPosition anchor, | |
| 46 int32 types) WARN_UNUSED_RESULT; | |
| 47 | |
| 48 void Cancel(); | |
| 49 | |
| 50 // Returns the time from the event which closed the menu - or 0. | |
| 51 base::TimeDelta closing_event_time() const; | |
| 52 | |
| 53 // MenuControllerDelegate: | |
| 54 virtual void DropMenuClosed(NotifyType type, MenuItemView* menu) OVERRIDE; | |
| 55 virtual void SiblingMenuCreated(MenuItemView* menu) OVERRIDE; | |
| 56 | |
| 57 private: | |
| 58 virtual ~MenuRunnerImpl(); | |
| 59 | |
| 60 // Cleans up after the menu is no longer showing. |result| is the menu that | |
| 61 // the user selected, or NULL if nothing was selected. | |
| 62 MenuRunner::RunResult MenuDone(MenuItemView* result, int mouse_event_flags); | |
| 63 | |
| 64 // Returns true if mnemonics should be shown in the menu. | |
| 65 bool ShouldShowMnemonics(MenuButton* button); | |
| 66 | |
| 67 // The menu. We own this. We don't use scoped_ptr as the destructor is | |
| 68 // protected and we're a friend. | |
| 69 MenuItemView* menu_; | |
| 70 | |
| 71 // Any sibling menus. Does not include |menu_|. We own these too. | |
| 72 std::set<MenuItemView*> sibling_menus_; | |
| 73 | |
| 74 // Created and set as the delegate of the MenuItemView if Release() is | |
| 75 // invoked. This is done to make sure the delegate isn't notified after | |
| 76 // Release() is invoked. We do this as we assume the delegate is no longer | |
| 77 // valid if MenuRunner has been deleted. | |
| 78 scoped_ptr<MenuDelegate> empty_delegate_; | |
| 79 | |
| 80 // Are we in run waiting for it to return? | |
| 81 bool running_; | |
| 82 | |
| 83 // Set if |running_| and Release() has been invoked. | |
| 84 bool delete_after_run_; | |
| 85 | |
| 86 // Are we running for a drop? | |
| 87 bool for_drop_; | |
| 88 | |
| 89 // The controller. | |
| 90 MenuController* controller_; | |
| 91 | |
| 92 // Do we own the controller? | |
| 93 bool owns_controller_; | |
| 94 | |
| 95 // The timestamp of the event which closed the menu - or 0. | |
| 96 base::TimeDelta closing_event_time_; | |
| 97 | |
| 98 // Used to detect deletion of |this| when notifying delegate of success. | |
| 99 base::WeakPtrFactory<MenuRunnerImpl> weak_factory_; | |
| 100 | |
| 101 DISALLOW_COPY_AND_ASSIGN(MenuRunnerImpl); | |
| 102 }; | |
| 103 | |
| 104 MenuRunnerImpl::MenuRunnerImpl(MenuItemView* menu) | |
| 105 : menu_(menu), | |
| 106 running_(false), | |
| 107 delete_after_run_(false), | |
| 108 for_drop_(false), | |
| 109 controller_(NULL), | |
| 110 owns_controller_(false), | |
| 111 closing_event_time_(base::TimeDelta()), | |
| 112 weak_factory_(this) { | |
| 113 } | |
| 114 | |
| 115 void MenuRunnerImpl::Release() { | |
| 116 if (running_) { | |
| 117 if (delete_after_run_) | |
| 118 return; // We already canceled. | |
| 119 | |
| 120 // The menu is running a nested message loop, we can't delete it now | |
| 121 // otherwise the stack would be in a really bad state (many frames would | |
| 122 // have deleted objects on them). Instead cancel the menu, when it returns | |
| 123 // Holder will delete itself. | |
| 124 delete_after_run_ = true; | |
| 125 | |
| 126 // Swap in a different delegate. That way we know the original MenuDelegate | |
| 127 // won't be notified later on (when it's likely already been deleted). | |
| 128 if (!empty_delegate_.get()) | |
| 129 empty_delegate_.reset(new MenuDelegate()); | |
| 130 menu_->set_delegate(empty_delegate_.get()); | |
| 131 | |
| 132 DCHECK(controller_); | |
| 133 // Release is invoked when MenuRunner is destroyed. Assume this is happening | |
| 134 // because the object referencing the menu has been destroyed and the menu | |
| 135 // button is no longer valid. | |
| 136 controller_->Cancel(MenuController::EXIT_DESTROYED); | |
| 137 } else { | |
| 138 delete this; | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 MenuRunner::RunResult MenuRunnerImpl::RunMenuAt(Widget* parent, | |
| 143 MenuButton* button, | |
| 144 const gfx::Rect& bounds, | |
| 145 MenuAnchorPosition anchor, | |
| 146 int32 types) { | |
| 147 closing_event_time_ = base::TimeDelta(); | |
| 148 if (running_) { | |
| 149 // Ignore requests to show the menu while it's already showing. MenuItemView | |
| 150 // doesn't handle this very well (meaning it crashes). | |
| 151 return MenuRunner::NORMAL_EXIT; | |
| 152 } | |
| 153 | |
| 154 MenuController* controller = MenuController::GetActiveInstance(); | |
| 155 if (controller) { | |
| 156 if ((types & MenuRunner::IS_NESTED) != 0) { | |
| 157 if (!controller->IsBlockingRun()) { | |
| 158 controller->CancelAll(); | |
| 159 controller = NULL; | |
| 160 } | |
| 161 } else { | |
| 162 // There's some other menu open and we're not nested. Cancel the menu. | |
| 163 controller->CancelAll(); | |
| 164 if ((types & MenuRunner::FOR_DROP) == 0) { | |
| 165 // We can't open another menu, otherwise the message loop would become | |
| 166 // twice nested. This isn't necessarily a problem, but generally isn't | |
| 167 // expected. | |
| 168 return MenuRunner::NORMAL_EXIT; | |
| 169 } | |
| 170 // Drop menus don't block the message loop, so it's ok to create a new | |
| 171 // MenuController. | |
| 172 controller = NULL; | |
| 173 } | |
| 174 } | |
| 175 | |
| 176 running_ = true; | |
| 177 for_drop_ = (types & MenuRunner::FOR_DROP) != 0; | |
| 178 bool has_mnemonics = (types & MenuRunner::HAS_MNEMONICS) != 0 && !for_drop_; | |
| 179 owns_controller_ = false; | |
| 180 if (!controller) { | |
| 181 // No menus are showing, show one. | |
| 182 ui::NativeTheme* theme = parent ? parent->GetNativeTheme() : | |
| 183 ui::NativeTheme::instance(); | |
| 184 controller = new MenuController(theme, !for_drop_, this); | |
| 185 owns_controller_ = true; | |
| 186 } | |
| 187 controller->set_is_combobox((types & MenuRunner::COMBOBOX) != 0); | |
| 188 controller_ = controller; | |
| 189 menu_->set_controller(controller_); | |
| 190 menu_->PrepareForRun(owns_controller_, | |
| 191 has_mnemonics, | |
| 192 !for_drop_ && ShouldShowMnemonics(button)); | |
| 193 | |
| 194 // Run the loop. | |
| 195 int mouse_event_flags = 0; | |
| 196 MenuItemView* result = controller->Run(parent, button, menu_, bounds, anchor, | |
| 197 (types & MenuRunner::CONTEXT_MENU) != 0, | |
| 198 &mouse_event_flags); | |
| 199 // Get the time of the event which closed this menu. | |
| 200 closing_event_time_ = controller->closing_event_time(); | |
| 201 if (for_drop_) { | |
| 202 // Drop menus return immediately. We finish processing in DropMenuClosed. | |
| 203 return MenuRunner::NORMAL_EXIT; | |
| 204 } | |
| 205 return MenuDone(result, mouse_event_flags); | |
| 206 } | |
| 207 | |
| 208 void MenuRunnerImpl::Cancel() { | |
| 209 if (running_) | |
| 210 controller_->Cancel(MenuController::EXIT_ALL); | |
| 211 } | |
| 212 | |
| 213 base::TimeDelta MenuRunnerImpl::closing_event_time() const { | |
| 214 return closing_event_time_; | |
| 215 } | |
| 216 | |
| 217 void MenuRunnerImpl::DropMenuClosed(NotifyType type, MenuItemView* menu) { | |
| 218 MenuDone(NULL, 0); | |
| 219 | |
| 220 if (type == NOTIFY_DELEGATE && menu->GetDelegate()) { | |
| 221 // Delegate is null when invoked from the destructor. | |
| 222 menu->GetDelegate()->DropMenuClosed(menu); | |
| 223 } | |
| 224 } | |
| 225 | |
| 226 void MenuRunnerImpl::SiblingMenuCreated(MenuItemView* menu) { | |
| 227 if (menu != menu_ && sibling_menus_.count(menu) == 0) | |
| 228 sibling_menus_.insert(menu); | |
| 229 } | |
| 230 | |
| 231 MenuRunnerImpl::~MenuRunnerImpl() { | |
| 232 delete menu_; | |
| 233 for (std::set<MenuItemView*>::iterator i = sibling_menus_.begin(); | |
| 234 i != sibling_menus_.end(); ++i) | |
| 235 delete *i; | |
| 236 } | |
| 237 | |
| 238 MenuRunner::RunResult MenuRunnerImpl::MenuDone(MenuItemView* result, | |
| 239 int mouse_event_flags) { | |
| 240 menu_->RemoveEmptyMenus(); | |
| 241 menu_->set_controller(NULL); | |
| 242 | |
| 243 if (owns_controller_) { | |
| 244 // We created the controller and need to delete it. | |
| 245 delete controller_; | |
| 246 owns_controller_ = false; | |
| 247 } | |
| 248 controller_ = NULL; | |
| 249 // Make sure all the windows we created to show the menus have been | |
| 250 // destroyed. | |
| 251 menu_->DestroyAllMenuHosts(); | |
| 252 if (delete_after_run_) { | |
| 253 delete this; | |
| 254 return MenuRunner::MENU_DELETED; | |
| 255 } | |
| 256 running_ = false; | |
| 257 if (result && menu_->GetDelegate()) { | |
| 258 // Executing the command may also delete this. | |
| 259 base::WeakPtr<MenuRunnerImpl> ref(weak_factory_.GetWeakPtr()); | |
| 260 menu_->GetDelegate()->ExecuteCommand(result->GetCommand(), | |
| 261 mouse_event_flags); | |
| 262 if (!ref) | |
| 263 return MenuRunner::MENU_DELETED; | |
| 264 } | |
| 265 return MenuRunner::NORMAL_EXIT; | |
| 266 } | |
| 267 | |
| 268 bool MenuRunnerImpl::ShouldShowMnemonics(MenuButton* button) { | |
| 269 // Show mnemonics if the button has focus or alt is pressed. | |
| 270 bool show_mnemonics = button ? button->HasFocus() : false; | |
| 271 #if defined(OS_WIN) | |
| 272 // This is only needed on Windows. | |
| 273 if (!show_mnemonics) | |
| 274 show_mnemonics = base::win::IsAltPressed(); | |
| 275 #endif | |
| 276 return show_mnemonics; | |
| 277 } | |
| 278 | |
| 279 } // namespace internal | |
| 280 | |
| 281 MenuRunner::MenuRunner(ui::MenuModel* menu_model, int32 run_types) | 12 MenuRunner::MenuRunner(ui::MenuModel* menu_model, int32 run_types) |
| 282 : run_types_(run_types), | 13 : run_types_(run_types), |
| 283 menu_model_adapter_(new MenuModelAdapter(menu_model)), | 14 impl_(internal::MenuRunnerImplInterface::Create(menu_model, run_types)) { |
| 284 holder_(new internal::MenuRunnerImpl(menu_model_adapter_->CreateMenu())) { | |
| 285 } | 15 } |
| 286 | 16 |
| 287 MenuRunner::MenuRunner(MenuItemView* menu_view, int32 run_types) | 17 MenuRunner::MenuRunner(MenuItemView* menu_view, int32 run_types) |
| 288 : run_types_(run_types), holder_(new internal::MenuRunnerImpl(menu_view)) { | 18 : run_types_(run_types), impl_(new internal::MenuRunnerImpl(menu_view)) { |
| 289 } | 19 } |
| 290 | 20 |
| 291 MenuRunner::~MenuRunner() { | 21 MenuRunner::~MenuRunner() { |
| 292 holder_->Release(); | 22 impl_->Release(); |
| 293 } | 23 } |
| 294 | 24 |
| 295 MenuRunner::RunResult MenuRunner::RunMenuAt(Widget* parent, | 25 MenuRunner::RunResult MenuRunner::RunMenuAt(Widget* parent, |
| 296 MenuButton* button, | 26 MenuButton* button, |
| 297 const gfx::Rect& bounds, | 27 const gfx::Rect& bounds, |
| 298 MenuAnchorPosition anchor, | 28 MenuAnchorPosition anchor, |
| 299 ui::MenuSourceType source_type) { | 29 ui::MenuSourceType source_type) { |
| 300 if (runner_handler_.get()) { | 30 if (runner_handler_.get()) { |
| 301 return runner_handler_->RunMenuAt( | 31 return runner_handler_->RunMenuAt( |
| 302 parent, button, bounds, anchor, source_type, run_types_); | 32 parent, button, bounds, anchor, source_type, run_types_); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 319 break; | 49 break; |
| 320 case ui::MENU_SOURCE_TOUCH: | 50 case ui::MENU_SOURCE_TOUCH: |
| 321 case ui::MENU_SOURCE_TOUCH_EDIT_MENU: | 51 case ui::MENU_SOURCE_TOUCH_EDIT_MENU: |
| 322 anchor = MENU_ANCHOR_BOTTOMCENTER; | 52 anchor = MENU_ANCHOR_BOTTOMCENTER; |
| 323 break; | 53 break; |
| 324 default: | 54 default: |
| 325 break; | 55 break; |
| 326 } | 56 } |
| 327 } | 57 } |
| 328 | 58 |
| 329 return holder_->RunMenuAt(parent, button, bounds, anchor, run_types_); | 59 return impl_->RunMenuAt(parent, button, bounds, anchor, run_types_); |
| 330 } | 60 } |
| 331 | 61 |
| 332 bool MenuRunner::IsRunning() const { | 62 bool MenuRunner::IsRunning() const { |
| 333 return holder_->running(); | 63 return impl_->IsRunning(); |
| 334 } | 64 } |
| 335 | 65 |
| 336 void MenuRunner::Cancel() { | 66 void MenuRunner::Cancel() { |
| 337 holder_->Cancel(); | 67 impl_->Cancel(); |
| 338 } | 68 } |
| 339 | 69 |
| 340 base::TimeDelta MenuRunner::closing_event_time() const { | 70 base::TimeDelta MenuRunner::closing_event_time() const { |
| 341 return holder_->closing_event_time(); | 71 return impl_->GetClosingEventTime(); |
| 342 } | 72 } |
| 343 | 73 |
| 344 void MenuRunner::SetRunnerHandler( | 74 void MenuRunner::SetRunnerHandler( |
| 345 scoped_ptr<MenuRunnerHandler> runner_handler) { | 75 scoped_ptr<MenuRunnerHandler> runner_handler) { |
| 346 runner_handler_ = runner_handler.Pass(); | 76 runner_handler_ = runner_handler.Pass(); |
| 347 } | 77 } |
| 348 | 78 |
| 349 } // namespace views | 79 } // namespace views |
| OLD | NEW |