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
{
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
{
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.
48 // The padding following the final icon in the container.
50 // The spacing between each of the icons.
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
59 // The type of drag that occurred in a drag-and-drop operation.
61 // The icon was dragged to the same container it started in.
63 // The icon was dragged from the main container to the overflow.
65 // The icon was dragged from the overflow container to the main.
75 ToolbarActionsBar(ToolbarActionsBarDelegate
* delegate
,
77 ToolbarActionsBar
* main_bar
);
78 ~ToolbarActionsBar() override
;
80 // Returns the width of a browser action icon, optionally including the
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.
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
,
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
)
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
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()
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 static void set_send_overflowed_action_changes_for_testing(
201 bool send_overflowed_action_changes
) {
202 send_overflowed_action_changes_
= send_overflowed_action_changes
;
205 // During testing we can disable animations by setting this flag to true,
206 // so that the bar resizes instantly, instead of having to poll it while it
207 // animates to open/closed status.
208 static bool disable_animations_for_testing_
;
211 using ToolbarActions
= ScopedVector
<ToolbarActionViewController
>;
213 // ToolbarActionsModel::Observer:
214 void OnToolbarActionAdded(const std::string
& action_id
, int index
) override
;
215 void OnToolbarActionRemoved(const std::string
& action_id
) override
;
216 void OnToolbarActionMoved(const std::string
& action_id
, int index
) override
;
217 void OnToolbarActionUpdated(const std::string
& action_id
) override
;
218 void OnToolbarVisibleCountChanged() override
;
219 void OnToolbarHighlightModeChanged(bool is_highlighting
) override
;
220 void OnToolbarModelInitialized() override
;
222 // Resizes the delegate (if necessary) to the preferred size using the given
223 // |tween_type| and optionally suppressing the chevron.
224 void ResizeDelegate(gfx::Tween::Type tween_type
, bool suppress_chevron
);
226 // Returns the action for the given |id|, if one exists.
227 ToolbarActionViewController
* GetActionForId(const std::string
& action_id
);
229 // Returns the current web contents.
230 content::WebContents
* GetCurrentWebContents();
232 // Reorders the toolbar actions to reflect the model and, optionally, to
233 // "pop out" any overflowed actions that want to run (depending on the
234 // value of |pop_out_actions_to_run|.
235 void ReorderActions();
237 // Sets |overflowed_action_wants_to_run_| to the proper value.
238 void SetOverflowedActionWantsToRun();
240 // Shows an extension message bubble, if any should be shown.
241 void MaybeShowExtensionBubble(
242 scoped_ptr
<extensions::ExtensionMessageBubbleController
> controller
);
244 // The delegate for this object (in a real build, this is the view).
245 ToolbarActionsBarDelegate
* delegate_
;
247 // The associated browser.
250 // The observed toolbar model.
251 ToolbarActionsModel
* model_
;
253 // The controller for the main toolbar actions bar. This will be null if this
255 ToolbarActionsBar
* main_bar_
;
257 // Platform-specific settings for dimensions and the overflow chevron.
258 PlatformSettings platform_settings_
;
260 // The toolbar actions.
261 ToolbarActions toolbar_actions_
;
263 // The action that triggered the current popup (just a reference to an action
264 // from toolbar_actions_).
265 ToolbarActionViewController
* popup_owner_
;
267 ScopedObserver
<ToolbarActionsModel
, ToolbarActionsModel::Observer
>
270 // True if we should suppress layout, such as when we are creating or
271 // adjusting a lot of actions at once.
272 bool suppress_layout_
;
274 // True if we should suppress animation; we do this when first creating the
275 // toolbar, and also when switching tabs changes the state of the icons.
276 bool suppress_animation_
;
278 // If this is true, actions that want to run (e.g., an extension's page
279 // action) will pop out of overflow to draw more attention.
280 // See also TabOrderHelper in the .cc file.
281 static bool pop_out_actions_to_run_
;
283 // If set to false, notifications for OnOverflowedActionWantsToRunChanged()
284 // will not be sent. Used because in unit tests there is no wrench menu to
286 static bool send_overflowed_action_changes_
;
288 // True if an action in the overflow menu wants to run.
289 bool overflowed_action_wants_to_run_
;
291 // True if we have checked to see if there is an extension bubble that should
292 // be displayed, and, if there is, shown that bubble.
293 bool checked_extension_bubble_
;
295 // The action, if any, which is currently "popped out" of the overflow in
296 // order to show a popup.
297 ToolbarActionViewController
* popped_out_action_
;
299 // The task to alert the |popped_out_action_| that animation has finished, and
300 // it is fully popped out.
301 base::Closure popped_out_closure_
;
303 // The controller of the bubble to show once animation finishes, if any.
304 scoped_ptr
<extensions::ExtensionMessageBubbleController
>
305 pending_extension_bubble_controller_
;
307 base::WeakPtrFactory
<ToolbarActionsBar
> weak_ptr_factory_
;
309 DISALLOW_COPY_AND_ASSIGN(ToolbarActionsBar
);
312 #endif // CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_ACTIONS_BAR_H_