1 // Copyright 2013 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 #ifndef CHROME_BROWSER_UI_VIEWS_TOOLBAR_WRENCH_MENU_H_
6 #define CHROME_BROWSER_UI_VIEWS_TOOLBAR_WRENCH_MENU_H_
11 #include "base/memory/scoped_ptr.h"
12 #include "base/observer_list.h"
13 #include "base/time/time.h"
14 #include "base/timer/elapsed_timer.h"
15 #include "components/bookmarks/browser/base_bookmark_model_observer.h"
16 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
18 #include "ui/base/models/menu_model.h"
19 #include "ui/views/controls/menu/menu_delegate.h"
21 class ExtensionToolbarMenuView
;
22 class BookmarkMenuDelegate
;
24 class WrenchMenuObserver
;
38 // WrenchMenu adapts the WrenchMenuModel to view's menu related classes.
39 class WrenchMenu
: public views::MenuDelegate
,
40 public bookmarks::BaseBookmarkModelObserver
,
41 public content::NotificationObserver
{
44 // Indicates that the menu was opened for a drag-and-drop operation.
48 WrenchMenu(Browser
* browser
, int run_flags
);
49 ~WrenchMenu() override
;
51 void Init(ui::MenuModel
* model
);
53 // Shows the menu relative to the specified view.
54 void RunMenu(views::MenuButton
* host
);
56 // Closes the menu if it is open, otherwise does nothing.
59 // Whether the menu is currently visible to the user.
62 bool for_drop() const { return (run_flags_
& FOR_DROP
) != 0; }
64 void AddObserver(WrenchMenuObserver
* observer
);
65 void RemoveObserver(WrenchMenuObserver
* observer
);
67 // MenuDelegate overrides:
68 const gfx::FontList
* GetLabelFontList(int command_id
) const override
;
69 bool GetShouldUseDisabledEmphasizedForegroundColor(
70 int command_id
) const override
;
71 base::string16
GetTooltipText(int command_id
,
72 const gfx::Point
& p
) const override
;
73 bool IsTriggerableEvent(views::MenuItemView
* menu
,
74 const ui::Event
& e
) override
;
76 views::MenuItemView
* menu
,
78 std::set
<ui::OSExchangeData::CustomFormat
>* custom_formats
) override
;
79 bool AreDropTypesRequired(views::MenuItemView
* menu
) override
;
80 bool CanDrop(views::MenuItemView
* menu
,
81 const ui::OSExchangeData
& data
) override
;
82 int GetDropOperation(views::MenuItemView
* item
,
83 const ui::DropTargetEvent
& event
,
84 DropPosition
* position
) override
;
85 int OnPerformDrop(views::MenuItemView
* menu
,
86 DropPosition position
,
87 const ui::DropTargetEvent
& event
) override
;
88 bool ShowContextMenu(views::MenuItemView
* source
,
91 ui::MenuSourceType source_type
) override
;
92 bool CanDrag(views::MenuItemView
* menu
) override
;
93 void WriteDragData(views::MenuItemView
* sender
,
94 ui::OSExchangeData
* data
) override
;
95 int GetDragOperations(views::MenuItemView
* sender
) override
;
96 int GetMaxWidthForMenu(views::MenuItemView
* menu
) override
;
97 bool IsItemChecked(int command_id
) const override
;
98 bool IsCommandEnabled(int command_id
) const override
;
99 void ExecuteCommand(int command_id
, int mouse_event_flags
) override
;
100 bool GetAccelerator(int command_id
,
101 ui::Accelerator
* accelerator
) const override
;
102 void WillShowMenu(views::MenuItemView
* menu
) override
;
103 void WillHideMenu(views::MenuItemView
* menu
) override
;
104 bool ShouldCloseOnDragComplete() override
;
106 // bookmarks::BaseBookmarkModelObserver overrides:
107 void BookmarkModelChanged() override
;
109 // content::NotificationObserver overrides:
110 void Observe(int type
,
111 const content::NotificationSource
& source
,
112 const content::NotificationDetails
& details
) override
;
114 ExtensionToolbarMenuView
* extension_toolbar_for_testing() {
115 return extension_toolbar_
;
119 class CutCopyPasteView
;
120 class RecentTabsMenuModelDelegate
;
123 typedef std::pair
<ui::MenuModel
*,int> Entry
;
124 typedef std::map
<int,Entry
> CommandIDToEntry
;
126 // Populates |parent| with all the child menus in |model|. Recursively invokes
127 // |PopulateMenu| for any submenu.
128 void PopulateMenu(views::MenuItemView
* parent
,
129 ui::MenuModel
* model
);
131 // Adds a new menu item to |parent| at |menu_index| to represent the item in
132 // |model| at |model_index|:
133 // - |menu_index|: position in |parent| to add the new item.
134 // - |model_index|: position in |model| to retrieve information about the
136 // The returned item's MenuItemView::GetCommand() is the same as that of
137 // |model|->GetCommandIdAt(|model_index|).
138 views::MenuItemView
* AddMenuItem(views::MenuItemView
* parent
,
140 ui::MenuModel
* model
,
142 ui::MenuModel::ItemType menu_type
);
144 // Invoked from the cut/copy/paste menus. Cancels the current active menu and
145 // activates the menu item in |model| at |index|.
146 void CancelAndEvaluate(ui::ButtonMenuItemModel
* model
, int index
);
148 // Creates the bookmark menu if necessary. Does nothing if already created or
149 // the bookmark model isn't loaded.
150 void CreateBookmarkMenu();
152 // Returns the index of the MenuModel/index pair representing the |command_id|
153 // in |command_id_to_entry_|.
154 int ModelIndexFromCommandId(int command_id
) const;
156 // The views menu. Owned by |menu_runner_|.
157 views::MenuItemView
* root_
;
159 scoped_ptr
<views::MenuRunner
> menu_runner_
;
161 // Maps from the command ID in model to the model/index pair the item came
163 CommandIDToEntry command_id_to_entry_
;
165 // Browser the menu is being shown for.
168 // |CancelAndEvaluate| sets |selected_menu_model_| and |selected_index_|.
169 // If |selected_menu_model_| is non-null after the menu completes
170 // ActivatedAt is invoked. This is done so that ActivatedAt isn't invoked
171 // while the message loop is nested.
172 ui::ButtonMenuItemModel
* selected_menu_model_
;
175 // Used for managing the bookmark menu items.
176 scoped_ptr
<BookmarkMenuDelegate
> bookmark_menu_delegate_
;
178 // Menu corresponding to IDC_BOOKMARKS_MENU.
179 views::MenuItemView
* bookmark_menu_
;
181 // Menu corresponding to IDC_FEEDBACK.
182 views::MenuItemView
* feedback_menu_item_
;
184 // Menu corresponding to IDC_TAKE_SCREENSHOT.
185 views::MenuItemView
* screenshot_menu_item_
;
187 // The view within the IDC_EXTENSIONS_OVERFLOW_MENU item (only present with
188 // the toolbar action redesign enabled).
189 ExtensionToolbarMenuView
* extension_toolbar_
;
191 // Used for managing "Recent tabs" menu items.
192 scoped_ptr
<RecentTabsMenuModelDelegate
> recent_tabs_menu_model_delegate_
;
194 content::NotificationRegistrar registrar_
;
196 // The bit mask of RunFlags.
197 const int run_flags_
;
199 base::ObserverList
<WrenchMenuObserver
> observer_list_
;
201 // Records the time from when menu opens to when the user selects a menu item.
202 base::ElapsedTimer menu_opened_timer_
;
204 DISALLOW_COPY_AND_ASSIGN(WrenchMenu
);
207 #endif // CHROME_BROWSER_UI_VIEWS_TOOLBAR_WRENCH_MENU_H_