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#ifndef UI_VIEWS_CONTROLS_MENU_MENU_RUNNER_IMPL_H_
6#define UI_VIEWS_CONTROLS_MENU_MENU_RUNNER_IMPL_H_
7
8#include "ui/views/controls/menu/menu_runner_impl_interface.h"
9
10#include <set>
11
12#include "base/memory/weak_ptr.h"
13#include "base/time/time.h"
14#include "ui/views/controls/menu/menu_controller_delegate.h"
15
16namespace views {
17
18class MenuController;
19class MenuDelegate;
20class MenuItemView;
21
22namespace internal {
23
24// A menu runner implementation that uses views::MenuItemView to show a menu.
25class MenuRunnerImpl : public MenuRunnerImplInterface,
26                       public MenuControllerDelegate {
27 public:
28  explicit MenuRunnerImpl(MenuItemView* menu);
29
30  virtual bool IsRunning() const OVERRIDE;
31  virtual void Release() OVERRIDE;
32  virtual MenuRunner::RunResult RunMenuAt(Widget* parent,
33                                          MenuButton* button,
34                                          const gfx::Rect& bounds,
35                                          MenuAnchorPosition anchor,
36                                          int32 run_types) OVERRIDE;
37  virtual void Cancel() OVERRIDE;
38  virtual base::TimeDelta GetClosingEventTime() const OVERRIDE;
39
40  // MenuControllerDelegate:
41  virtual void DropMenuClosed(NotifyType type, MenuItemView* menu) OVERRIDE;
42  virtual void SiblingMenuCreated(MenuItemView* menu) OVERRIDE;
43
44 private:
45  virtual ~MenuRunnerImpl();
46
47  // Cleans up after the menu is no longer showing. |result| is the menu that
48  // the user selected, or NULL if nothing was selected.
49  MenuRunner::RunResult MenuDone(MenuItemView* result, int mouse_event_flags);
50
51  // Returns true if mnemonics should be shown in the menu.
52  bool ShouldShowMnemonics(MenuButton* button);
53
54  // The menu. We own this. We don't use scoped_ptr as the destructor is
55  // protected and we're a friend.
56  MenuItemView* menu_;
57
58  // Any sibling menus. Does not include |menu_|. We own these too.
59  std::set<MenuItemView*> sibling_menus_;
60
61  // Created and set as the delegate of the MenuItemView if Release() is
62  // invoked.  This is done to make sure the delegate isn't notified after
63  // Release() is invoked. We do this as we assume the delegate is no longer
64  // valid if MenuRunner has been deleted.
65  scoped_ptr<MenuDelegate> empty_delegate_;
66
67  // Are we in run waiting for it to return?
68  bool running_;
69
70  // Set if |running_| and Release() has been invoked.
71  bool delete_after_run_;
72
73  // Are we running for a drop?
74  bool for_drop_;
75
76  // The controller.
77  MenuController* controller_;
78
79  // Do we own the controller?
80  bool owns_controller_;
81
82  // The timestamp of the event which closed the menu - or 0.
83  base::TimeDelta closing_event_time_;
84
85  // Used to detect deletion of |this| when notifying delegate of success.
86  base::WeakPtrFactory<MenuRunnerImpl> weak_factory_;
87
88  DISALLOW_COPY_AND_ASSIGN(MenuRunnerImpl);
89};
90
91}  // namespace internal
92}  // namespace views
93
94#endif  // UI_VIEWS_CONTROLS_MENU_MENU_RUNNER_IMPL_H_
95