1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_VIEWS_TOOLBAR_WRENCH_MENU_H_
#define CHROME_BROWSER_UI_VIEWS_TOOLBAR_WRENCH_MENU_H_
#include <map>
#include <utility>
#include "base/memory/scoped_ptr.h"
#include "base/observer_list.h"
#include "base/time/time.h"
#include "base/timer/elapsed_timer.h"
#include "components/bookmarks/browser/base_bookmark_model_observer.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "ui/base/models/menu_model.h"
#include "ui/views/controls/menu/menu_delegate.h"
class BookmarkMenuDelegate;
class Browser;
class WrenchMenuObserver;
namespace ui {
class NativeTheme;
}
namespace views {
class MenuButton;
struct MenuConfig;
class MenuItemView;
class MenuRunner;
class View;
} // namespace views
// WrenchMenu adapts the WrenchMenuModel to view's menu related classes.
class WrenchMenu : public views::MenuDelegate,
public BaseBookmarkModelObserver,
public content::NotificationObserver {
public:
enum RunFlags {
// Indicates that the menu was opened for a drag-and-drop operation.
FOR_DROP = 1 << 0,
};
WrenchMenu(Browser* browser, int run_flags);
~WrenchMenu() override;
void Init(ui::MenuModel* model);
// Shows the menu relative to the specified view.
void RunMenu(views::MenuButton* host);
// Closes the menu if it is open, otherwise does nothing.
void CloseMenu();
// Whether the menu is currently visible to the user.
bool IsShowing();
bool for_drop() const { return (run_flags_ & FOR_DROP) != 0; }
void AddObserver(WrenchMenuObserver* observer);
void RemoveObserver(WrenchMenuObserver* observer);
// MenuDelegate overrides:
const gfx::FontList* GetLabelFontList(int command_id) const override;
bool GetShouldUseDisabledEmphasizedForegroundColor(
int command_id) const override;
base::string16 GetTooltipText(int command_id,
const gfx::Point& p) const override;
bool IsTriggerableEvent(views::MenuItemView* menu,
const ui::Event& e) override;
bool GetDropFormats(
views::MenuItemView* menu,
int* formats,
std::set<ui::OSExchangeData::CustomFormat>* custom_formats) override;
bool AreDropTypesRequired(views::MenuItemView* menu) override;
bool CanDrop(views::MenuItemView* menu,
const ui::OSExchangeData& data) override;
int GetDropOperation(views::MenuItemView* item,
const ui::DropTargetEvent& event,
DropPosition* position) override;
int OnPerformDrop(views::MenuItemView* menu,
DropPosition position,
const ui::DropTargetEvent& event) override;
bool ShowContextMenu(views::MenuItemView* source,
int command_id,
const gfx::Point& p,
ui::MenuSourceType source_type) override;
bool CanDrag(views::MenuItemView* menu) override;
void WriteDragData(views::MenuItemView* sender,
ui::OSExchangeData* data) override;
int GetDragOperations(views::MenuItemView* sender) override;
int GetMaxWidthForMenu(views::MenuItemView* menu) override;
bool IsItemChecked(int command_id) const override;
bool IsCommandEnabled(int command_id) const override;
void ExecuteCommand(int command_id, int mouse_event_flags) override;
bool GetAccelerator(int command_id,
ui::Accelerator* accelerator) const override;
void WillShowMenu(views::MenuItemView* menu) override;
void WillHideMenu(views::MenuItemView* menu) override;
bool ShouldCloseOnDragComplete() override;
// BaseBookmarkModelObserver overrides:
void BookmarkModelChanged() override;
// content::NotificationObserver overrides:
void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) override;
private:
class CutCopyPasteView;
class RecentTabsMenuModelDelegate;
class ZoomView;
typedef std::pair<ui::MenuModel*,int> Entry;
typedef std::map<int,Entry> CommandIDToEntry;
// Populates |parent| with all the child menus in |model|. Recursively invokes
// |PopulateMenu| for any submenu.
void PopulateMenu(views::MenuItemView* parent,
ui::MenuModel* model);
// Adds a new menu item to |parent| at |menu_index| to represent the item in
// |model| at |model_index|:
// - |menu_index|: position in |parent| to add the new item.
// - |model_index|: position in |model| to retrieve information about the
// new menu item.
// The returned item's MenuItemView::GetCommand() is the same as that of
// |model|->GetCommandIdAt(|model_index|).
views::MenuItemView* AddMenuItem(views::MenuItemView* parent,
int menu_index,
ui::MenuModel* model,
int model_index,
ui::MenuModel::ItemType menu_type);
// Invoked from the cut/copy/paste menus. Cancels the current active menu and
// activates the menu item in |model| at |index|.
void CancelAndEvaluate(ui::MenuModel* model, int index);
// Creates the bookmark menu if necessary. Does nothing if already created or
// the bookmark model isn't loaded.
void CreateBookmarkMenu();
// Returns the index of the MenuModel/index pair representing the |command_id|
// in |command_id_to_entry_|.
int ModelIndexFromCommandId(int command_id) const;
// The views menu. Owned by |menu_runner_|.
views::MenuItemView* root_;
scoped_ptr<views::MenuRunner> menu_runner_;
// Maps from the command ID in model to the model/index pair the item came
// from.
CommandIDToEntry command_id_to_entry_;
// Browser the menu is being shown for.
Browser* browser_;
// |CancelAndEvaluate| sets |selected_menu_model_| and |selected_index_|.
// If |selected_menu_model_| is non-null after the menu completes
// ActivatedAt is invoked. This is done so that ActivatedAt isn't invoked
// while the message loop is nested.
ui::MenuModel* selected_menu_model_;
int selected_index_;
// Used for managing the bookmark menu items.
scoped_ptr<BookmarkMenuDelegate> bookmark_menu_delegate_;
// Menu corresponding to IDC_BOOKMARKS_MENU.
views::MenuItemView* bookmark_menu_;
// Menu corresponding to IDC_FEEDBACK.
views::MenuItemView* feedback_menu_item_;
// Used for managing "Recent tabs" menu items.
scoped_ptr<RecentTabsMenuModelDelegate> recent_tabs_menu_model_delegate_;
content::NotificationRegistrar registrar_;
// The bit mask of RunFlags.
const int run_flags_;
ObserverList<WrenchMenuObserver> observer_list_;
// Records the time from when menu opens to when the user selects a menu item.
base::ElapsedTimer menu_opened_timer_;
DISALLOW_COPY_AND_ASSIGN(WrenchMenu);
};
#endif // CHROME_BROWSER_UI_VIEWS_TOOLBAR_WRENCH_MENU_H_
|