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/macros.h"
9 #include "base/memory/scoped_vector.h"
10 #include "base/scoped_observer.h"
11 #include "chrome/browser/extensions/extension_toolbar_model.h"
12 #include "ui/gfx/geometry/size.h"
14 namespace extensions
{
18 class ToolbarActionsBarDelegate
;
19 class ToolbarActionViewController
;
21 // A platform-independent version of the container for toolbar actions,
22 // including extension actions and component actions.
23 // This class manages the order of the actions, the actions' state, and owns the
24 // action controllers, in addition to (for extensions) interfacing with the
25 // extension toolbar model. Further, it manages dimensions for the bar,
26 // excluding animations.
27 // This can come in two flavors, main and "overflow". The main bar is visible
28 // next to the omnibox, and the overflow bar is visible inside the chrome
29 // (fka wrench) menu. The main bar can have only a single row of icons with
30 // flexible width, whereas the overflow bar has multiple rows of icons with a
31 // fixed width (the width of the menu).
32 class ToolbarActionsBar
: public extensions::ExtensionToolbarModel::Observer
{
34 // A struct to contain the platform settings.
35 struct PlatformSettings
{
36 explicit PlatformSettings(bool in_overflow_mode
);
38 // The padding that comes before the first icon in the container.
40 // The padding following the final icon in the container.
42 // The spacing between each of the icons.
44 // The number of icons per row in the overflow menu.
45 int icons_per_overflow_menu_row
;
46 // Whether or not the overflow menu is displayed as a chevron (this is being
51 // The type of drag that occurred in a drag-and-drop operation.
53 // The icon was dragged to the same container it started in.
55 // The icon was dragged from the main container to the overflow.
57 // The icon was dragged from the overflow container to the main.
61 ToolbarActionsBar(ToolbarActionsBarDelegate
* delegate
,
63 bool in_overflow_mode
);
64 ~ToolbarActionsBar() override
;
66 // Returns the width of a browser action icon, optionally including the
68 static int IconWidth(bool include_padding
);
70 // Returns the height of a browser action icon.
71 static int IconHeight();
73 // Returns the preferred size for the toolbar; this does *not* reflect any
74 // animations that may be running.
75 gfx::Size
GetPreferredSize() const;
77 // Returns the [minimum|maximum] possible width for the toolbar.
78 int GetMinimumWidth() const;
79 int GetMaximumWidth() const;
81 // Returns the width for the given number of icons.
82 int IconCountToWidth(int icons
) const;
84 // Returns the number of icons that can fit within the given width.
85 size_t WidthToIconCount(int width
) const;
87 // Returns the number of icons that should be displayed.
88 size_t GetIconCount() const;
90 // Creates the toolbar actions.
93 // Deletes all toolbar actions.
96 // Updates all the toolbar actions.
99 // Sets the width for the overflow menu rows.
100 void SetOverflowRowWidth(int width
);
102 // Notifies the ToolbarActionsBar that a user completed a resize gesture, and
103 // the new width is |width|.
104 void OnResizeComplete(int width
);
106 // Notifies the ToolbarActionsBar that a user completed a drag and drop event,
107 // and dragged the view from |dragged_index| to |dropped_index|.
108 // |drag_type| indicates whether or not the icon was dragged between the
109 // overflow and main containers.
110 void OnDragDrop(int dragged_index
,
114 const std::vector
<ToolbarActionViewController
*>& toolbar_actions() {
115 return toolbar_actions_
.get();
117 bool enabled() const { return model_
!= nullptr; }
118 bool suppress_layout() const { return suppress_layout_
; }
119 bool suppress_animation() const { return suppress_animation_
; }
120 bool is_highlighting() const { return model_
&& model_
->is_highlighting(); }
121 const PlatformSettings
& platform_settings() const {
122 return platform_settings_
;
126 using ToolbarActions
= ScopedVector
<ToolbarActionViewController
>;
128 // ExtensionToolbarModel::Observer:
129 void ToolbarExtensionAdded(const extensions::Extension
* extension
,
131 void ToolbarExtensionRemoved(const extensions::Extension
* extension
) override
;
132 void ToolbarExtensionMoved(const extensions::Extension
* extension
,
134 void ToolbarExtensionUpdated(const extensions::Extension
* extension
) override
;
135 bool ShowExtensionActionPopup(const extensions::Extension
* extension
,
136 bool grant_active_tab
) override
;
137 void ToolbarVisibleCountChanged() override
;
138 void ToolbarHighlightModeChanged(bool is_highlighting
) override
;
139 void OnToolbarReorderNecessary(content::WebContents
* web_contents
) override
;
140 Browser
* GetBrowser() override
;
142 // Returns the action for the given |id|, if one exists.
143 ToolbarActionViewController
* GetActionForId(const std::string
& id
);
145 // Returns the current web contents.
146 content::WebContents
* GetCurrentWebContents();
148 // Reorders the toolbar actions to reflect any that need to pop out.
149 void ReorderActions();
151 // The delegate for this object (in a real build, this is the view).
152 ToolbarActionsBarDelegate
* delegate_
;
154 // The associated browser.
157 // The observed toolbar model.
158 extensions::ExtensionToolbarModel
* model_
;
160 // True if this bar is for the overflow menu.
161 bool in_overflow_mode_
;
163 // Platform-specific settings for dimensions and the overflow chevron.
164 PlatformSettings platform_settings_
;
166 // The toolbar actions.
167 ToolbarActions toolbar_actions_
;
169 ScopedObserver
<extensions::ExtensionToolbarModel
,
170 extensions::ExtensionToolbarModel::Observer
> model_observer_
;
172 // True if we should suppress layout, such as when we are creating or
173 // adjusting a lot of actions at once.
174 bool suppress_layout_
;
176 // True if we should suppress animation; we typically do this e.g. when
177 // switching tabs changes the state of the icons.
178 bool suppress_animation_
;
180 DISALLOW_COPY_AND_ASSIGN(ToolbarActionsBar
);
183 #endif // CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_ACTIONS_BAR_H_