[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / ui / views / controls / menu / menu_runner.cc
blob4adaed724421caddcd0ba30932dfb4ef421c2297
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
3 // found in the LICENSE file.
5 #include "ui/views/controls/menu/menu_runner.h"
7 #include <set>
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"
18 #include "ui/views/widget/widget.h"
20 #if defined(OS_WIN)
21 #include "base/win/win_util.h"
22 #endif
24 namespace views {
26 namespace internal {
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);
36 bool running() const { return running_; }
38 // See description above class for details.
39 void Release();
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;
48 void Cancel();
50 // Returns the time from the event which closed the menu - or 0.
51 base::TimeDelta closing_event_time() const;
53 // MenuControllerDelegate:
54 virtual void DropMenuClosed(NotifyType type, MenuItemView* menu) OVERRIDE;
55 virtual void SiblingMenuCreated(MenuItemView* menu) OVERRIDE;
57 private:
58 virtual ~MenuRunnerImpl();
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);
64 // Returns true if mnemonics should be shown in the menu.
65 bool ShouldShowMnemonics(MenuButton* button);
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_;
71 // Any sibling menus. Does not include |menu_|. We own these too.
72 std::set<MenuItemView*> sibling_menus_;
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_;
80 // Are we in run waiting for it to return?
81 bool running_;
83 // Set if |running_| and Release() has been invoked.
84 bool delete_after_run_;
86 // Are we running for a drop?
87 bool for_drop_;
89 // The controller.
90 MenuController* controller_;
92 // Do we own the controller?
93 bool owns_controller_;
95 // The timestamp of the event which closed the menu - or 0.
96 base::TimeDelta closing_event_time_;
98 // Used to detect deletion of |this| when notifying delegate of success.
99 base::WeakPtrFactory<MenuRunnerImpl> weak_factory_;
101 DISALLOW_COPY_AND_ASSIGN(MenuRunnerImpl);
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) {
115 void MenuRunnerImpl::Release() {
116 if (running_) {
117 if (delete_after_run_)
118 return; // We already canceled.
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;
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());
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;
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;
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;
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;
170 // Drop menus don't block the message loop, so it's ok to create a new
171 // MenuController.
172 controller = NULL;
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;
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));
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;
205 return MenuDone(result, mouse_event_flags);
208 void MenuRunnerImpl::Cancel() {
209 if (running_)
210 controller_->Cancel(MenuController::EXIT_ALL);
213 base::TimeDelta MenuRunnerImpl::closing_event_time() const {
214 return closing_event_time_;
217 void MenuRunnerImpl::DropMenuClosed(NotifyType type, MenuItemView* menu) {
218 MenuDone(NULL, 0);
220 if (type == NOTIFY_DELEGATE && menu->GetDelegate()) {
221 // Delegate is null when invoked from the destructor.
222 menu->GetDelegate()->DropMenuClosed(menu);
226 void MenuRunnerImpl::SiblingMenuCreated(MenuItemView* menu) {
227 if (menu != menu_ && sibling_menus_.count(menu) == 0)
228 sibling_menus_.insert(menu);
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;
238 MenuRunner::RunResult MenuRunnerImpl::MenuDone(MenuItemView* result,
239 int mouse_event_flags) {
240 menu_->RemoveEmptyMenus();
241 menu_->set_controller(NULL);
243 if (owns_controller_) {
244 // We created the controller and need to delete it.
245 delete controller_;
246 owns_controller_ = false;
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;
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;
265 return MenuRunner::NORMAL_EXIT;
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;
279 } // namespace internal
281 MenuRunner::MenuRunner(ui::MenuModel* menu_model, int32 run_types)
282 : run_types_(run_types),
283 menu_model_adapter_(new MenuModelAdapter(menu_model)),
284 holder_(new internal::MenuRunnerImpl(menu_model_adapter_->CreateMenu())) {
287 MenuRunner::MenuRunner(MenuItemView* menu_view, int32 run_types)
288 : run_types_(run_types), holder_(new internal::MenuRunnerImpl(menu_view)) {
291 MenuRunner::~MenuRunner() {
292 holder_->Release();
295 MenuRunner::RunResult MenuRunner::RunMenuAt(Widget* parent,
296 MenuButton* button,
297 const gfx::Rect& bounds,
298 MenuAnchorPosition anchor,
299 ui::MenuSourceType source_type) {
300 if (runner_handler_.get()) {
301 return runner_handler_->RunMenuAt(
302 parent, button, bounds, anchor, source_type, run_types_);
305 // The parent of the nested menu will have created a DisplayChangeListener, so
306 // we avoid creating a DisplayChangeListener if nested. Drop menus are
307 // transient, so we don't cancel in that case.
308 if ((run_types_ & (IS_NESTED | FOR_DROP)) == 0 && parent) {
309 display_change_listener_.reset(
310 internal::DisplayChangeListener::Create(parent, this));
313 if (run_types_ & CONTEXT_MENU) {
314 switch (source_type) {
315 case ui::MENU_SOURCE_NONE:
316 case ui::MENU_SOURCE_KEYBOARD:
317 case ui::MENU_SOURCE_MOUSE:
318 anchor = MENU_ANCHOR_TOPLEFT;
319 break;
320 case ui::MENU_SOURCE_TOUCH:
321 case ui::MENU_SOURCE_TOUCH_EDIT_MENU:
322 anchor = MENU_ANCHOR_BOTTOMCENTER;
323 break;
324 default:
325 break;
329 return holder_->RunMenuAt(parent, button, bounds, anchor, run_types_);
332 bool MenuRunner::IsRunning() const {
333 return holder_->running();
336 void MenuRunner::Cancel() {
337 holder_->Cancel();
340 base::TimeDelta MenuRunner::closing_event_time() const {
341 return holder_->closing_event_time();
344 void MenuRunner::SetRunnerHandler(
345 scoped_ptr<MenuRunnerHandler> runner_handler) {
346 runner_handler_ = runner_handler.Pass();
349 } // namespace views