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 BookmarkMenuDelegate
;
23 class WrenchMenuObserver
;
37 // WrenchMenu adapts the WrenchMenuModel to view's menu related classes.
38 class WrenchMenu
: public views::MenuDelegate
,
39 public bookmarks::BaseBookmarkModelObserver
,
40 public content::NotificationObserver
{
43 // Indicates that the menu was opened for a drag-and-drop operation.
47 WrenchMenu(Browser
* browser
, int run_flags
);
48 ~WrenchMenu() override
;
50 void Init(ui::MenuModel
* model
);
52 // Shows the menu relative to the specified view.
53 void RunMenu(views::MenuButton
* host
);
55 // Closes the menu if it is open, otherwise does nothing.
58 // Whether the menu is currently visible to the user.
61 bool for_drop() const { return (run_flags_
& FOR_DROP
) != 0; }
63 void AddObserver(WrenchMenuObserver
* observer
);
64 void RemoveObserver(WrenchMenuObserver
* observer
);
66 // MenuDelegate overrides:
67 const gfx::FontList
* GetLabelFontList(int command_id
) const override
;
68 bool GetShouldUseDisabledEmphasizedForegroundColor(
69 int command_id
) const override
;
70 base::string16
GetTooltipText(int command_id
,
71 const gfx::Point
& p
) const override
;
72 bool IsTriggerableEvent(views::MenuItemView
* menu
,
73 const ui::Event
& e
) override
;
75 views::MenuItemView
* menu
,
77 std::set
<ui::OSExchangeData::CustomFormat
>* custom_formats
) override
;
78 bool AreDropTypesRequired(views::MenuItemView
* menu
) override
;
79 bool CanDrop(views::MenuItemView
* menu
,
80 const ui::OSExchangeData
& data
) override
;
81 int GetDropOperation(views::MenuItemView
* item
,
82 const ui::DropTargetEvent
& event
,
83 DropPosition
* position
) override
;
84 int OnPerformDrop(views::MenuItemView
* menu
,
85 DropPosition position
,
86 const ui::DropTargetEvent
& event
) override
;
87 bool ShowContextMenu(views::MenuItemView
* source
,
90 ui::MenuSourceType source_type
) override
;
91 bool CanDrag(views::MenuItemView
* menu
) override
;
92 void WriteDragData(views::MenuItemView
* sender
,
93 ui::OSExchangeData
* data
) override
;
94 int GetDragOperations(views::MenuItemView
* sender
) override
;
95 int GetMaxWidthForMenu(views::MenuItemView
* menu
) override
;
96 bool IsItemChecked(int command_id
) const override
;
97 bool IsCommandEnabled(int command_id
) const override
;
98 void ExecuteCommand(int command_id
, int mouse_event_flags
) override
;
99 bool GetAccelerator(int command_id
,
100 ui::Accelerator
* accelerator
) const override
;
101 void WillShowMenu(views::MenuItemView
* menu
) override
;
102 void WillHideMenu(views::MenuItemView
* menu
) override
;
103 bool ShouldCloseOnDragComplete() override
;
105 // bookmarks::BaseBookmarkModelObserver overrides:
106 void BookmarkModelChanged() override
;
108 // content::NotificationObserver overrides:
109 void Observe(int type
,
110 const content::NotificationSource
& source
,
111 const content::NotificationDetails
& details
) override
;
114 class CutCopyPasteView
;
115 class RecentTabsMenuModelDelegate
;
118 typedef std::pair
<ui::MenuModel
*,int> Entry
;
119 typedef std::map
<int,Entry
> CommandIDToEntry
;
121 // Populates |parent| with all the child menus in |model|. Recursively invokes
122 // |PopulateMenu| for any submenu.
123 void PopulateMenu(views::MenuItemView
* parent
,
124 ui::MenuModel
* model
);
126 // Adds a new menu item to |parent| at |menu_index| to represent the item in
127 // |model| at |model_index|:
128 // - |menu_index|: position in |parent| to add the new item.
129 // - |model_index|: position in |model| to retrieve information about the
131 // The returned item's MenuItemView::GetCommand() is the same as that of
132 // |model|->GetCommandIdAt(|model_index|).
133 views::MenuItemView
* AddMenuItem(views::MenuItemView
* parent
,
135 ui::MenuModel
* model
,
137 ui::MenuModel::ItemType menu_type
);
139 // Invoked from the cut/copy/paste menus. Cancels the current active menu and
140 // activates the menu item in |model| at |index|.
141 void CancelAndEvaluate(ui::ButtonMenuItemModel
* model
, int index
);
143 // Creates the bookmark menu if necessary. Does nothing if already created or
144 // the bookmark model isn't loaded.
145 void CreateBookmarkMenu();
147 // Returns the index of the MenuModel/index pair representing the |command_id|
148 // in |command_id_to_entry_|.
149 int ModelIndexFromCommandId(int command_id
) const;
151 // The views menu. Owned by |menu_runner_|.
152 views::MenuItemView
* root_
;
154 scoped_ptr
<views::MenuRunner
> menu_runner_
;
156 // Maps from the command ID in model to the model/index pair the item came
158 CommandIDToEntry command_id_to_entry_
;
160 // Browser the menu is being shown for.
163 // |CancelAndEvaluate| sets |selected_menu_model_| and |selected_index_|.
164 // If |selected_menu_model_| is non-null after the menu completes
165 // ActivatedAt is invoked. This is done so that ActivatedAt isn't invoked
166 // while the message loop is nested.
167 ui::ButtonMenuItemModel
* selected_menu_model_
;
170 // Used for managing the bookmark menu items.
171 scoped_ptr
<BookmarkMenuDelegate
> bookmark_menu_delegate_
;
173 // Menu corresponding to IDC_BOOKMARKS_MENU.
174 views::MenuItemView
* bookmark_menu_
;
176 // Menu corresponding to IDC_FEEDBACK.
177 views::MenuItemView
* feedback_menu_item_
;
179 // Menu corresponding to IDC_TAKE_SCREENSHOT.
180 views::MenuItemView
* screenshot_menu_item_
;
182 // Used for managing "Recent tabs" menu items.
183 scoped_ptr
<RecentTabsMenuModelDelegate
> recent_tabs_menu_model_delegate_
;
185 content::NotificationRegistrar registrar_
;
187 // The bit mask of RunFlags.
188 const int run_flags_
;
190 ObserverList
<WrenchMenuObserver
> observer_list_
;
192 // Records the time from when menu opens to when the user selects a menu item.
193 base::ElapsedTimer menu_opened_timer_
;
195 DISALLOW_COPY_AND_ASSIGN(WrenchMenu
);
198 #endif // CHROME_BROWSER_UI_VIEWS_TOOLBAR_WRENCH_MENU_H_