Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / toolbar / toolbar_actions_bar.h
blob2e78ef387ce2eea9ee5504ecc69c2f9f54453679
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.
5 #ifndef CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_ACTIONS_BAR_H_
6 #define CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_ACTIONS_BAR_H_
8 #include "base/callback.h"
9 #include "base/macros.h"
10 #include "base/memory/scoped_vector.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/scoped_observer.h"
13 #include "chrome/browser/ui/toolbar/toolbar_actions_bar_bubble_delegate.h"
14 #include "chrome/browser/ui/toolbar/toolbar_actions_model.h"
15 #include "ui/gfx/animation/tween.h"
16 #include "ui/gfx/geometry/size.h"
18 namespace extensions {
19 class Extension;
20 class ExtensionMessageBubbleController;
23 namespace user_prefs {
24 class PrefRegistrySyncable;
27 class ToolbarActionsBarDelegate;
28 class ToolbarActionViewController;
30 // A platform-independent version of the container for toolbar actions,
31 // including extension actions and component actions.
32 // This class manages the order of the actions, the actions' state, and owns the
33 // action controllers, in addition to interfacing with the toolbar actions
34 // model. Further, it manages dimensions for the bar, excluding animations.
35 // This can come in two flavors, main and "overflow". The main bar is visible
36 // next to the omnibox, and the overflow bar is visible inside the chrome
37 // (fka wrench) menu. The main bar can have only a single row of icons with
38 // flexible width, whereas the overflow bar has multiple rows of icons with a
39 // fixed width (the width of the menu).
40 class ToolbarActionsBar : public ToolbarActionsModel::Observer {
41 public:
42 // A struct to contain the platform settings.
43 struct PlatformSettings {
44 explicit PlatformSettings(bool in_overflow_mode);
46 // The padding that comes before the first icon in the container.
47 int left_padding;
48 // The padding following the final icon in the container.
49 int right_padding;
50 // The spacing between each of the icons.
51 int item_spacing;
52 // The number of icons per row in the overflow menu.
53 int icons_per_overflow_menu_row;
54 // Whether or not the overflow menu is displayed as a chevron (this is being
55 // phased out).
56 bool chevron_enabled;
59 // The type of drag that occurred in a drag-and-drop operation.
60 enum DragType {
61 // The icon was dragged to the same container it started in.
62 DRAG_TO_SAME,
63 // The icon was dragged from the main container to the overflow.
64 DRAG_TO_OVERFLOW,
65 // The icon was dragged from the overflow container to the main.
66 DRAG_TO_MAIN,
69 enum HighlightType {
70 HIGHLIGHT_NONE,
71 HIGHLIGHT_INFO,
72 HIGHLIGHT_WARNING,
75 ToolbarActionsBar(ToolbarActionsBarDelegate* delegate,
76 Browser* browser,
77 ToolbarActionsBar* main_bar);
78 ~ToolbarActionsBar() override;
80 // Returns the width of a browser action icon, optionally including the
81 // following padding.
82 static int IconWidth(bool include_padding);
84 // Returns the height of a browser action icon.
85 static int IconHeight();
87 // Registers profile preferences.
88 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
90 // Returns the preferred size for the toolbar; this does *not* reflect any
91 // animations that may be running.
92 gfx::Size GetPreferredSize() const;
94 // Returns the [minimum|maximum] possible width for the toolbar.
95 int GetMinimumWidth() const;
96 int GetMaximumWidth() const;
98 // Returns the width for the given number of icons.
99 int IconCountToWidth(int icons) const;
101 // Returns the number of icons that can fit within the given width.
102 size_t WidthToIconCount(int width) const;
104 // Returns the number of icons that should be displayed.
105 size_t GetIconCount() const;
107 // Returns the frame (bounds) that the specified index should have, taking
108 // into account if this is the main or overflow bar. If this is the overflow
109 // bar and the index should not be displayed (i.e., it is shown on the main
110 // bar), returns an empty rect.
111 gfx::Rect GetFrameForIndex(size_t index) const;
113 // Returns the actions in the proper order; this may differ from the
114 // underlying order in the case of actions being popped out to show a popup.
115 std::vector<ToolbarActionViewController*> GetActions() const;
117 // Creates the toolbar actions.
118 void CreateActions();
120 // Deletes all toolbar actions.
121 void DeleteActions();
123 // Updates all the toolbar actions.
124 void Update();
126 // Shows the popup for the action with |id|, returning true if a popup is
127 // shown. If |grant_active_tab| is true, then active tab permissions should
128 // be given to the action (only do this if this is through a user action).
129 bool ShowToolbarActionPopup(const std::string& id, bool grant_active_tab);
131 // Sets the width for the overflow menu rows.
132 void SetOverflowRowWidth(int width);
134 // Notifies the ToolbarActionsBar that a user completed a resize gesture, and
135 // the new width is |width|.
136 void OnResizeComplete(int width);
138 // Notifies the ToolbarActionsBar that a user completed a drag and drop event,
139 // and dragged the view from |dragged_index| to |dropped_index|.
140 // |drag_type| indicates whether or not the icon was dragged between the
141 // overflow and main containers.
142 // The main container should handle all drag/drop notifications.
143 void OnDragDrop(int dragged_index,
144 int dropped_index,
145 DragType drag_type);
147 // Notifies the ToolbarActionsBar that the delegate finished animating.
148 void OnAnimationEnded();
150 // Returns true if the given |action| is visible on the main toolbar.
151 bool IsActionVisibleOnMainBar(const ToolbarActionViewController* action)
152 const;
154 // Pops out a given |action|, ensuring it is visible.
155 // |closure| will be called once any animation is complete.
156 void PopOutAction(ToolbarActionViewController* action,
157 const base::Closure& closure);
159 // Undoes the current "pop out"; i.e., moves the popped out action back into
160 // overflow.
161 void UndoPopOut();
163 // Sets the active popup owner to be |popup_owner|.
164 void SetPopupOwner(ToolbarActionViewController* popup_owner);
166 // Hides the actively showing popup, if any.
167 void HideActivePopup();
169 // Returns the main (i.e., not overflow) controller for the given action.
170 ToolbarActionViewController* GetMainControllerForAction(
171 ToolbarActionViewController* action);
173 // Returns the underlying toolbar actions, but does not order them. Primarily
174 // for use in testing.
175 const std::vector<ToolbarActionViewController*>& toolbar_actions_unordered()
176 const {
177 return toolbar_actions_.get();
179 bool enabled() const { return model_ != nullptr; }
180 bool suppress_layout() const { return suppress_layout_; }
181 bool suppress_animation() const {
182 return suppress_animation_ || disable_animations_for_testing_;
184 bool is_highlighting() const { return model_ && model_->is_highlighting(); }
185 ToolbarActionsModel::HighlightType highlight_type() const {
186 return model_ ? model_->highlight_type()
187 : ToolbarActionsModel::HIGHLIGHT_NONE;
189 const PlatformSettings& platform_settings() const {
190 return platform_settings_;
192 ToolbarActionViewController* popup_owner() { return popup_owner_; }
193 ToolbarActionViewController* popped_out_action() {
194 return popped_out_action_;
196 bool in_overflow_mode() const { return main_bar_ != nullptr; }
198 ToolbarActionsBarDelegate* delegate_for_test() { return delegate_; }
200 // During testing we can disable animations by setting this flag to true,
201 // so that the bar resizes instantly, instead of having to poll it while it
202 // animates to open/closed status.
203 static bool disable_animations_for_testing_;
205 private:
206 using ToolbarActions = ScopedVector<ToolbarActionViewController>;
208 // ToolbarActionsModel::Observer:
209 void OnToolbarActionAdded(const std::string& action_id, int index) override;
210 void OnToolbarActionRemoved(const std::string& action_id) override;
211 void OnToolbarActionMoved(const std::string& action_id, int index) override;
212 void OnToolbarActionUpdated(const std::string& action_id) override;
213 void OnToolbarVisibleCountChanged() override;
214 void OnToolbarHighlightModeChanged(bool is_highlighting) override;
215 void OnToolbarModelInitialized() override;
217 // Resizes the delegate (if necessary) to the preferred size using the given
218 // |tween_type| and optionally suppressing the chevron.
219 void ResizeDelegate(gfx::Tween::Type tween_type, bool suppress_chevron);
221 // Returns the action for the given |id|, if one exists.
222 ToolbarActionViewController* GetActionForId(const std::string& action_id);
224 // Returns the current web contents.
225 content::WebContents* GetCurrentWebContents();
227 // Reorders the toolbar actions to reflect the model and, optionally, to
228 // "pop out" any overflowed actions that want to run (depending on the
229 // value of |pop_out_actions_to_run|.
230 void ReorderActions();
232 // Shows an extension message bubble, if any should be shown.
233 void MaybeShowExtensionBubble(
234 scoped_ptr<extensions::ExtensionMessageBubbleController> controller);
236 // The delegate for this object (in a real build, this is the view).
237 ToolbarActionsBarDelegate* delegate_;
239 // The associated browser.
240 Browser* browser_;
242 // The observed toolbar model.
243 ToolbarActionsModel* model_;
245 // The controller for the main toolbar actions bar. This will be null if this
246 // is the main bar.
247 ToolbarActionsBar* main_bar_;
249 // Platform-specific settings for dimensions and the overflow chevron.
250 PlatformSettings platform_settings_;
252 // The toolbar actions.
253 ToolbarActions toolbar_actions_;
255 // The action that triggered the current popup (just a reference to an action
256 // from toolbar_actions_).
257 ToolbarActionViewController* popup_owner_;
259 ScopedObserver<ToolbarActionsModel, ToolbarActionsModel::Observer>
260 model_observer_;
262 // True if we should suppress layout, such as when we are creating or
263 // adjusting a lot of actions at once.
264 bool suppress_layout_;
266 // True if we should suppress animation; we do this when first creating the
267 // toolbar, and also when switching tabs changes the state of the icons.
268 bool suppress_animation_;
270 // If this is true, actions that want to run (e.g., an extension's page
271 // action) will pop out of overflow to draw more attention.
272 // See also TabOrderHelper in the .cc file.
273 static bool pop_out_actions_to_run_;
275 // True if we have checked to see if there is an extension bubble that should
276 // be displayed, and, if there is, shown that bubble.
277 bool checked_extension_bubble_;
279 // The action, if any, which is currently "popped out" of the overflow in
280 // order to show a popup.
281 ToolbarActionViewController* popped_out_action_;
283 // The task to alert the |popped_out_action_| that animation has finished, and
284 // it is fully popped out.
285 base::Closure popped_out_closure_;
287 // The controller of the bubble to show once animation finishes, if any.
288 scoped_ptr<extensions::ExtensionMessageBubbleController>
289 pending_extension_bubble_controller_;
291 base::WeakPtrFactory<ToolbarActionsBar> weak_ptr_factory_;
293 DISALLOW_COPY_AND_ASSIGN(ToolbarActionsBar);
296 #endif // CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_ACTIONS_BAR_H_