Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / toolbar / toolbar_actions_bar.h
blob8bea70d04804b35c48d152e4fe808f94348222ca
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/extensions/extension_toolbar_model.h"
14 #include "chrome/browser/ui/toolbar/toolbar_actions_bar_bubble_delegate.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 (for extensions) interfacing with the
34 // extension toolbar model. Further, it manages dimensions for the bar,
35 // excluding animations.
36 // This can come in two flavors, main and "overflow". The main bar is visible
37 // next to the omnibox, and the overflow bar is visible inside the chrome
38 // (fka wrench) menu. The main bar can have only a single row of icons with
39 // flexible width, whereas the overflow bar has multiple rows of icons with a
40 // fixed width (the width of the menu).
41 class ToolbarActionsBar : public extensions::ExtensionToolbarModel::Observer {
42 public:
43 // A struct to contain the platform settings.
44 struct PlatformSettings {
45 explicit PlatformSettings(bool in_overflow_mode);
47 // The padding that comes before the first icon in the container.
48 int left_padding;
49 // The padding following the final icon in the container.
50 int right_padding;
51 // The spacing between each of the icons.
52 int item_spacing;
53 // The number of icons per row in the overflow menu.
54 int icons_per_overflow_menu_row;
55 // Whether or not the overflow menu is displayed as a chevron (this is being
56 // phased out).
57 bool chevron_enabled;
60 // The type of drag that occurred in a drag-and-drop operation.
61 enum DragType {
62 // The icon was dragged to the same container it started in.
63 DRAG_TO_SAME,
64 // The icon was dragged from the main container to the overflow.
65 DRAG_TO_OVERFLOW,
66 // The icon was dragged from the overflow container to the main.
67 DRAG_TO_MAIN,
70 ToolbarActionsBar(ToolbarActionsBarDelegate* delegate,
71 Browser* browser,
72 ToolbarActionsBar* main_bar);
73 ~ToolbarActionsBar() override;
75 // Returns the width of a browser action icon, optionally including the
76 // following padding.
77 static int IconWidth(bool include_padding);
79 // Returns the height of a browser action icon.
80 static int IconHeight();
82 // Registers profile preferences.
83 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
85 // Returns the preferred size for the toolbar; this does *not* reflect any
86 // animations that may be running.
87 gfx::Size GetPreferredSize() const;
89 // Returns the [minimum|maximum] possible width for the toolbar.
90 int GetMinimumWidth() const;
91 int GetMaximumWidth() const;
93 // Returns the width for the given number of icons.
94 int IconCountToWidth(int icons) const;
96 // Returns the number of icons that can fit within the given width.
97 size_t WidthToIconCount(int width) const;
99 // Returns the number of icons that should be displayed.
100 size_t GetIconCount() const;
102 // Returns the actions in the proper order; this may differ from the
103 // underlying order in the case of actions being popped out to show a popup.
104 std::vector<ToolbarActionViewController*> GetActions() const;
106 // Creates the toolbar actions.
107 void CreateActions();
109 // Deletes all toolbar actions.
110 void DeleteActions();
112 // Updates all the toolbar actions.
113 void Update();
115 // Sets the width for the overflow menu rows.
116 void SetOverflowRowWidth(int width);
118 // Notifies the ToolbarActionsBar that a user completed a resize gesture, and
119 // the new width is |width|.
120 void OnResizeComplete(int width);
122 // Notifies the ToolbarActionsBar that a user completed a drag and drop event,
123 // and dragged the view from |dragged_index| to |dropped_index|.
124 // |drag_type| indicates whether or not the icon was dragged between the
125 // overflow and main containers.
126 // The main container should handle all drag/drop notifications.
127 void OnDragDrop(int dragged_index,
128 int dropped_index,
129 DragType drag_type);
131 // Notifies the ToolbarActionsBar that the delegate finished animating.
132 void OnAnimationEnded();
134 // Returns true if the given |action| is visible.
135 bool IsActionVisible(const ToolbarActionViewController* action) const;
137 // Pops out a given |action|, ensuring it is visible.
138 // |closure| will be called once any animation is complete.
139 void PopOutAction(ToolbarActionViewController* action,
140 const base::Closure& closure);
142 // Undoes the current "pop out"; i.e., moves the popped out action back into
143 // overflow.
144 void UndoPopOut();
146 // Sets the active popup owner to be |popup_owner|.
147 void SetPopupOwner(ToolbarActionViewController* popup_owner);
149 // Hides the actively showing popup, if any.
150 void HideActivePopup();
152 // Returns the main (i.e., not overflow) controller for the given action.
153 ToolbarActionViewController* GetMainControllerForAction(
154 ToolbarActionViewController* action);
156 // Returns the underlying toolbar actions, but does not order them. Primarily
157 // for use in testing.
158 const std::vector<ToolbarActionViewController*>& toolbar_actions_unordered()
159 const {
160 return toolbar_actions_.get();
162 bool enabled() const { return model_ != nullptr; }
163 bool suppress_layout() const { return suppress_layout_; }
164 bool suppress_animation() const {
165 return suppress_animation_ || disable_animations_for_testing_;
167 bool is_highlighting() const { return model_ && model_->is_highlighting(); }
168 const PlatformSettings& platform_settings() const {
169 return platform_settings_;
171 ToolbarActionViewController* popup_owner() { return popup_owner_; }
172 ToolbarActionViewController* popped_out_action() {
173 return popped_out_action_;
175 bool in_overflow_mode() const { return main_bar_ != nullptr; }
177 ToolbarActionsBarDelegate* delegate_for_test() { return delegate_; }
179 static void set_send_overflowed_action_changes_for_testing(
180 bool send_overflowed_action_changes) {
181 send_overflowed_action_changes_ = send_overflowed_action_changes;
184 // During testing we can disable animations by setting this flag to true,
185 // so that the bar resizes instantly, instead of having to poll it while it
186 // animates to open/closed status.
187 static bool disable_animations_for_testing_;
189 private:
190 using ToolbarActions = ScopedVector<ToolbarActionViewController>;
192 // ExtensionToolbarModel::Observer:
193 void OnToolbarExtensionAdded(const extensions::Extension* extension,
194 int index) override;
195 void OnToolbarExtensionRemoved(
196 const extensions::Extension* extension) override;
197 void OnToolbarExtensionMoved(const extensions::Extension* extension,
198 int index) override;
199 void OnToolbarExtensionUpdated(
200 const extensions::Extension* extension) override;
201 bool ShowExtensionActionPopup(const extensions::Extension* extension,
202 bool grant_active_tab) override;
203 void OnToolbarVisibleCountChanged() override;
204 void OnToolbarHighlightModeChanged(bool is_highlighting) override;
205 void OnToolbarModelInitialized() override;
206 Browser* GetBrowser() override;
208 // Resizes the delegate (if necessary) to the preferred size using the given
209 // |tween_type| and optionally suppressing the chevron.
210 void ResizeDelegate(gfx::Tween::Type tween_type, bool suppress_chevron);
212 // Returns the action for the given |id|, if one exists.
213 ToolbarActionViewController* GetActionForId(const std::string& id);
215 // Returns the current web contents.
216 content::WebContents* GetCurrentWebContents();
218 // Reorders the toolbar actions to reflect the model and, optionally, to
219 // "pop out" any overflowed actions that want to run (depending on the
220 // value of |pop_out_actions_to_run|.
221 void ReorderActions();
223 // Sets |overflowed_action_wants_to_run_| to the proper value.
224 void SetOverflowedActionWantsToRun();
226 // Shows an extension message bubble, if any should be shown.
227 void MaybeShowExtensionBubble(
228 scoped_ptr<extensions::ExtensionMessageBubbleController> controller);
230 // The delegate for this object (in a real build, this is the view).
231 ToolbarActionsBarDelegate* delegate_;
233 // The associated browser.
234 Browser* browser_;
236 // The observed toolbar model.
237 extensions::ExtensionToolbarModel* model_;
239 // The controller for the main toolbar actions bar. This will be null if this
240 // is the main bar.
241 ToolbarActionsBar* main_bar_;
243 // Platform-specific settings for dimensions and the overflow chevron.
244 PlatformSettings platform_settings_;
246 // The toolbar actions.
247 ToolbarActions toolbar_actions_;
249 // The action that triggered the current popup (just a reference to an action
250 // from toolbar_actions_).
251 ToolbarActionViewController* popup_owner_;
253 ScopedObserver<extensions::ExtensionToolbarModel,
254 extensions::ExtensionToolbarModel::Observer> model_observer_;
256 // True if we should suppress layout, such as when we are creating or
257 // adjusting a lot of actions at once.
258 bool suppress_layout_;
260 // True if we should suppress animation; we do this when first creating the
261 // toolbar, and also when switching tabs changes the state of the icons.
262 bool suppress_animation_;
264 // If this is true, actions that want to run (e.g., an extension's page
265 // action) will pop out of overflow to draw more attention.
266 // See also TabOrderHelper in the .cc file.
267 static bool pop_out_actions_to_run_;
269 // If set to false, notifications for OnOverflowedActionWantsToRunChanged()
270 // will not be sent. Used because in unit tests there is no wrench menu to
271 // alter.
272 static bool send_overflowed_action_changes_;
274 // True if an action in the overflow menu wants to run.
275 bool overflowed_action_wants_to_run_;
277 // True if we have checked to see if there is an extension bubble that should
278 // be displayed, and, if there is, shown that bubble.
279 bool checked_extension_bubble_;
281 // The action, if any, which is currently "popped out" of the overflow in
282 // order to show a popup.
283 ToolbarActionViewController* popped_out_action_;
285 // The task to alert the |popped_out_action_| that animation has finished, and
286 // it is fully popped out.
287 base::Closure popped_out_closure_;
289 // The controller of the bubble to show once animation finishes, if any.
290 scoped_ptr<extensions::ExtensionMessageBubbleController>
291 pending_extension_bubble_controller_;
293 base::WeakPtrFactory<ToolbarActionsBar> weak_ptr_factory_;
295 DISALLOW_COPY_AND_ASSIGN(ToolbarActionsBar);
298 #endif // CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_ACTIONS_BAR_H_