OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/views/controls/menu/menu_runner_impl.h" |
| 6 |
| 7 #include "ui/native_theme/native_theme.h" |
| 8 #include "ui/views/controls/button/menu_button.h" |
| 9 #include "ui/views/controls/menu/menu_controller.h" |
| 10 #include "ui/views/controls/menu/menu_delegate.h" |
| 11 #include "ui/views/controls/menu/menu_item_view.h" |
| 12 #include "ui/views/controls/menu/menu_runner_impl_adapter.h" |
| 13 #include "ui/views/widget/widget.h" |
| 14 |
| 15 #if defined(OS_WIN) |
| 16 #include "base/win/win_util.h" |
| 17 #endif |
| 18 |
| 19 namespace views { |
| 20 namespace internal { |
| 21 |
| 22 #if !defined(OS_MACOSX) |
| 23 MenuRunnerImplInterface* MenuRunnerImplInterface::Create( |
| 24 ui::MenuModel* menu_model, |
| 25 int32 run_types) { |
| 26 return new MenuRunnerImplAdapter(menu_model); |
| 27 } |
| 28 #endif |
| 29 |
| 30 MenuRunnerImpl::MenuRunnerImpl(MenuItemView* menu) |
| 31 : menu_(menu), |
| 32 running_(false), |
| 33 delete_after_run_(false), |
| 34 for_drop_(false), |
| 35 controller_(NULL), |
| 36 owns_controller_(false), |
| 37 closing_event_time_(base::TimeDelta()), |
| 38 weak_factory_(this) { |
| 39 } |
| 40 |
| 41 MenuItemView* MenuRunnerImpl::GetMenu() const { |
| 42 return menu_; |
| 43 } |
| 44 |
| 45 bool MenuRunnerImpl::IsRunning() const { |
| 46 return running_; |
| 47 } |
| 48 |
| 49 void MenuRunnerImpl::Release() { |
| 50 if (running_) { |
| 51 if (delete_after_run_) |
| 52 return; // We already canceled. |
| 53 |
| 54 // The menu is running a nested message loop, we can't delete it now |
| 55 // otherwise the stack would be in a really bad state (many frames would |
| 56 // have deleted objects on them). Instead cancel the menu, when it returns |
| 57 // Holder will delete itself. |
| 58 delete_after_run_ = true; |
| 59 |
| 60 // Swap in a different delegate. That way we know the original MenuDelegate |
| 61 // won't be notified later on (when it's likely already been deleted). |
| 62 if (!empty_delegate_.get()) |
| 63 empty_delegate_.reset(new MenuDelegate()); |
| 64 menu_->set_delegate(empty_delegate_.get()); |
| 65 |
| 66 DCHECK(controller_); |
| 67 // Release is invoked when MenuRunner is destroyed. Assume this is happening |
| 68 // because the object referencing the menu has been destroyed and the menu |
| 69 // button is no longer valid. |
| 70 controller_->Cancel(MenuController::EXIT_DESTROYED); |
| 71 } else { |
| 72 delete this; |
| 73 } |
| 74 } |
| 75 |
| 76 MenuRunner::RunResult MenuRunnerImpl::RunMenuAt(Widget* parent, |
| 77 MenuButton* button, |
| 78 const gfx::Rect& bounds, |
| 79 MenuAnchorPosition anchor, |
| 80 int32 run_types) { |
| 81 closing_event_time_ = base::TimeDelta(); |
| 82 if (running_) { |
| 83 // Ignore requests to show the menu while it's already showing. MenuItemView |
| 84 // doesn't handle this very well (meaning it crashes). |
| 85 return MenuRunner::NORMAL_EXIT; |
| 86 } |
| 87 |
| 88 MenuController* controller = MenuController::GetActiveInstance(); |
| 89 if (controller) { |
| 90 if ((run_types & MenuRunner::IS_NESTED) != 0) { |
| 91 if (!controller->IsBlockingRun()) { |
| 92 controller->CancelAll(); |
| 93 controller = NULL; |
| 94 } |
| 95 } else { |
| 96 // There's some other menu open and we're not nested. Cancel the menu. |
| 97 controller->CancelAll(); |
| 98 if ((run_types & MenuRunner::FOR_DROP) == 0) { |
| 99 // We can't open another menu, otherwise the message loop would become |
| 100 // twice nested. This isn't necessarily a problem, but generally isn't |
| 101 // expected. |
| 102 return MenuRunner::NORMAL_EXIT; |
| 103 } |
| 104 // Drop menus don't block the message loop, so it's ok to create a new |
| 105 // MenuController. |
| 106 controller = NULL; |
| 107 } |
| 108 } |
| 109 |
| 110 running_ = true; |
| 111 for_drop_ = (run_types & MenuRunner::FOR_DROP) != 0; |
| 112 bool has_mnemonics = |
| 113 (run_types & MenuRunner::HAS_MNEMONICS) != 0 && !for_drop_; |
| 114 owns_controller_ = false; |
| 115 if (!controller) { |
| 116 // No menus are showing, show one. |
| 117 ui::NativeTheme* theme = |
| 118 parent ? parent->GetNativeTheme() : ui::NativeTheme::instance(); |
| 119 controller = new MenuController(theme, !for_drop_, this); |
| 120 owns_controller_ = true; |
| 121 } |
| 122 controller->set_is_combobox((run_types & MenuRunner::COMBOBOX) != 0); |
| 123 controller_ = controller; |
| 124 menu_->set_controller(controller_); |
| 125 menu_->PrepareForRun(owns_controller_, |
| 126 has_mnemonics, |
| 127 !for_drop_ && ShouldShowMnemonics(button)); |
| 128 |
| 129 // Run the loop. |
| 130 int mouse_event_flags = 0; |
| 131 MenuItemView* result = |
| 132 controller->Run(parent, |
| 133 button, |
| 134 menu_, |
| 135 bounds, |
| 136 anchor, |
| 137 (run_types & MenuRunner::CONTEXT_MENU) != 0, |
| 138 &mouse_event_flags); |
| 139 // Get the time of the event which closed this menu. |
| 140 closing_event_time_ = controller->closing_event_time(); |
| 141 if (for_drop_) { |
| 142 // Drop menus return immediately. We finish processing in DropMenuClosed. |
| 143 return MenuRunner::NORMAL_EXIT; |
| 144 } |
| 145 return MenuDone(result, mouse_event_flags); |
| 146 } |
| 147 |
| 148 void MenuRunnerImpl::Cancel() { |
| 149 if (running_) |
| 150 controller_->Cancel(MenuController::EXIT_ALL); |
| 151 } |
| 152 |
| 153 base::TimeDelta MenuRunnerImpl::GetClosingEventTime() const { |
| 154 return closing_event_time_; |
| 155 } |
| 156 |
| 157 void MenuRunnerImpl::DropMenuClosed(NotifyType type, MenuItemView* menu) { |
| 158 MenuDone(NULL, 0); |
| 159 |
| 160 if (type == NOTIFY_DELEGATE && menu->GetDelegate()) { |
| 161 // Delegate is null when invoked from the destructor. |
| 162 menu->GetDelegate()->DropMenuClosed(menu); |
| 163 } |
| 164 } |
| 165 |
| 166 void MenuRunnerImpl::SiblingMenuCreated(MenuItemView* menu) { |
| 167 if (menu != menu_ && sibling_menus_.count(menu) == 0) |
| 168 sibling_menus_.insert(menu); |
| 169 } |
| 170 |
| 171 MenuRunnerImpl::~MenuRunnerImpl() { |
| 172 delete menu_; |
| 173 for (std::set<MenuItemView*>::iterator i = sibling_menus_.begin(); |
| 174 i != sibling_menus_.end(); |
| 175 ++i) |
| 176 delete *i; |
| 177 } |
| 178 |
| 179 MenuRunner::RunResult MenuRunnerImpl::MenuDone(MenuItemView* result, |
| 180 int mouse_event_flags) { |
| 181 menu_->RemoveEmptyMenus(); |
| 182 menu_->set_controller(NULL); |
| 183 |
| 184 if (owns_controller_) { |
| 185 // We created the controller and need to delete it. |
| 186 delete controller_; |
| 187 owns_controller_ = false; |
| 188 } |
| 189 controller_ = NULL; |
| 190 // Make sure all the windows we created to show the menus have been |
| 191 // destroyed. |
| 192 menu_->DestroyAllMenuHosts(); |
| 193 if (delete_after_run_) { |
| 194 delete this; |
| 195 return MenuRunner::MENU_DELETED; |
| 196 } |
| 197 running_ = false; |
| 198 if (result && menu_->GetDelegate()) { |
| 199 // Executing the command may also delete this. |
| 200 base::WeakPtr<MenuRunnerImpl> ref(weak_factory_.GetWeakPtr()); |
| 201 menu_->GetDelegate()->ExecuteCommand(result->GetCommand(), |
| 202 mouse_event_flags); |
| 203 if (!ref) |
| 204 return MenuRunner::MENU_DELETED; |
| 205 } |
| 206 return MenuRunner::NORMAL_EXIT; |
| 207 } |
| 208 |
| 209 bool MenuRunnerImpl::ShouldShowMnemonics(MenuButton* button) { |
| 210 // Show mnemonics if the button has focus or alt is pressed. |
| 211 bool show_mnemonics = button ? button->HasFocus() : false; |
| 212 #if defined(OS_WIN) |
| 213 // This is only needed on Windows. |
| 214 if (!show_mnemonics) |
| 215 show_mnemonics = base::win::IsAltPressed(); |
| 216 #endif |
| 217 return show_mnemonics; |
| 218 } |
| 219 |
| 220 } // namespace internal |
| 221 } // namespace views |
OLD | NEW |