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_BROWSER_ACTIONS_CONTAINER_H_
6 #define CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTIONS_CONTAINER_H_
8 #include "base/observer_list.h"
9 #include "chrome/browser/extensions/extension_keybinding_registry.h"
10 #include "chrome/browser/extensions/extension_toolbar_model.h"
11 #include "chrome/browser/ui/views/extensions/extension_keybinding_registry_views.h"
12 #include "chrome/browser/ui/views/toolbar/chevron_menu_button.h"
13 #include "chrome/browser/ui/views/toolbar/toolbar_action_view.h"
14 #include "ui/gfx/animation/animation_delegate.h"
15 #include "ui/gfx/animation/tween.h"
16 #include "ui/views/controls/button/menu_button_listener.h"
17 #include "ui/views/controls/resize_area_delegate.h"
18 #include "ui/views/drag_controller.h"
19 #include "ui/views/view.h"
21 class BrowserActionsContainerObserver;
24 namespace extensions {
25 class ActiveTabPermissionGranter;
38 // The BrowserActionsContainer is a container view, responsible for drawing the
39 // toolbar action icons (including extension icons and icons for component
40 // toolbar actions). It comes intwo flavors, a main container (when residing on
41 // the toolbar) and an overflow container (that resides in the main application
42 // menu, aka the Chrome menu).
44 // When in 'main' mode, the container supports the full functionality of a
45 // BrowserActionContainer, but in 'overflow' mode the container is effectively
46 // just an overflow for the 'main' toolbar (shows only the icons that the main
47 // toolbar does not) and as such does not have an overflow itself. The overflow
48 // container also does not support resizing. Since the main container only shows
49 // icons in the Chrome toolbar, it is limited to a single row of icons. The
50 // overflow container, however, is allowed to display icons in multiple rows.
52 // The main container is placed flush against the omnibox and hot dog menu,
53 // whereas the overflow container is placed within the hot dog menu. The
54 // layout is similar to this:
56 // Where the letters are as follows:
57 // r: An invisible resize area. This is ToolbarView::kStandardSpacing pixels
58 // wide and directly adjacent to the omnibox. Only shown for the main
60 // I: An icon. This is as wide as the IDR_BROWSER_ACTION image.
61 // _: kItemSpacing pixels of empty space.
62 // c: kChevronSpacing pixels of empty space. Only present if C is present.
63 // C: An optional chevron, as wide as the IDR_BROWSER_ACTIONS_OVERFLOW image,
64 // and visible only when both of the following statements are true:
65 // - The container is set to a width smaller than needed to show all icons.
66 // - There is no other container in 'overflow' mode to handle the
67 // non-visible icons for this container.
68 // s: ToolbarView::kStandardSpacing pixels of empty space (before the wrench
70 // The reason the container contains the trailing space "s", rather than having
71 // it be handled by the parent view, is so that when the chevron is invisible
72 // and the user starts dragging an icon around, we have the space to draw the
73 // ultimate drop indicator. (Otherwise, we'd be trying to draw it into the
74 // padding beyond our right edge, and it wouldn't appear.)
76 // The BrowserActionsContainer in 'main' mode follows a few rules, in terms of
79 // 1) The container can never grow beyond the space needed to show all icons
80 // (hereby referred to as the max width).
81 // 2) The container can never shrink below the space needed to show just the
82 // initial padding and the chevron (ignoring the case where there are no icons
83 // to show, in which case the container won't be visible anyway).
84 // 3) The container snaps into place (to the pixel count that fits the visible
85 // icons) to make sure there is no wasted space at the edges of the container.
86 // 4) If the user adds or removes icons (read: installs/uninstalls browser
87 // actions) we grow and shrink the container as needed - but ONLY if the
88 // container was at max width to begin with.
89 // 5) If the container is NOT at max width (has an overflow menu), we respect
90 // that size when adding and removing icons and DON'T grow/shrink the container.
91 // This means that new icons (which always appear at the far right) will show up
92 // in the overflow. The install bubble for extensions points to the chevron
95 // Resizing the BrowserActionsContainer:
97 // The ResizeArea view sends OnResize messages to the BrowserActionsContainer
98 // class as the user drags it. This modifies the value for |resize_amount_|.
99 // That indicates to the container that a resize is in progress and is used to
100 // calculate the size in GetPreferredSize(), though that function never exceeds
101 // the defined minimum and maximum size of the container.
103 // When the user releases the mouse (ends the resize), we calculate a target
104 // size for the container (animation_target_size_), clamp that value to the
105 // containers min and max and then animate from the *current* position (that the
106 // user has dragged the view to) to the target size.
108 // Animating the BrowserActionsContainer:
110 // Animations are used when snapping the container to a value that fits all
111 // visible icons. This can be triggered when the user finishes resizing the
112 // container or when Browser Actions are added/removed.
114 // We always animate from the current width (container_width_) to the target
115 // size (animation_target_size_), using |resize_amount| to keep track of the
116 // animation progress.
118 // NOTE: When adding Browser Actions to a maximum width container (no overflow)
119 // we make sure to suppress the chevron menu if it wasn't visible. This is
120 // because we won't have enough space to show the new Browser Action until the
121 // animation ends and we don't want the chevron to flash into view while we are
122 // growing the container.
124 ////////////////////////////////////////////////////////////////////////////////
125 class BrowserActionsContainer
126 : public views::View,
127 public views::ResizeAreaDelegate,
128 public gfx::AnimationDelegate,
129 public extensions::ExtensionToolbarModel::Observer,
130 public ToolbarActionView::Delegate,
131 public extensions::ExtensionKeybindingRegistry::Delegate {
133 // Horizontal spacing between most items in the container, as well as after
134 // the last item or chevron (if visible).
135 static const int kItemSpacing;
137 // Constructs a BrowserActionContainer for a particular |browser| object. For
138 // documentation of |main_container|, see class comments.
139 BrowserActionsContainer(Browser* browser,
140 BrowserActionsContainer* main_container);
141 ~BrowserActionsContainer() override;
145 // Get the number of toolbar actions being displayed.
146 size_t num_toolbar_actions() const { return toolbar_action_views_.size(); }
148 // Whether we are performing resize animation on the container.
149 bool animating() const { return animation_target_size_ > 0; }
151 // Returns the chevron, if any.
152 views::View* chevron() { return chevron_; }
153 const views::View* chevron() const { return chevron_; }
155 // Returns the profile this container is associated with.
156 Profile* profile() const { return profile_; }
158 // The class that registers for keyboard shortcuts for extension commands.
159 extensions::ExtensionKeybindingRegistry* extension_keybinding_registry() {
160 return extension_keybinding_registry_.get();
163 // Get a particular toolbar action view.
164 ToolbarActionView* GetToolbarActionViewAt(int index) {
165 return toolbar_action_views_[index];
168 // Returns the ID of the action represented by the view at |index|.
169 const std::string& GetIdAt(size_t index);
171 // Returns the ToolbarActionView* associated with the given |extension|, or
172 // NULL if none exists.
173 ToolbarActionView* GetViewForExtension(
174 const extensions::Extension* extension);
176 // Update the views to reflect the state of the toolbar actions.
177 void RefreshToolbarActionViews();
179 // Sets up the toolbar action view vector.
180 void CreateToolbarActionViews();
182 // Delete all toolbar action views.
183 void DeleteToolbarActionViews();
185 // Returns how many actions are currently visible. If the intent is to find
186 // how many are visible once the container finishes animation, see
187 // VisibleBrowserActionsAfterAnimation() below.
188 size_t VisibleBrowserActions() const;
190 // Returns how many actions will be visible once the container finishes
191 // animating to a new size, or (if not animating) the currently visible icons.
192 size_t VisibleBrowserActionsAfterAnimation() const;
194 // Executes |command| registered by |extension|.
195 void ExecuteExtensionCommand(const extensions::Extension* extension,
196 const extensions::Command& command);
198 // Notify the container that an extension has been moved to the overflow
200 void NotifyActionMovedToOverflow();
202 // Add or remove an observer.
203 void AddObserver(BrowserActionsContainerObserver* observer);
204 void RemoveObserver(BrowserActionsContainerObserver* observer);
206 // Overridden from views::View:
207 gfx::Size GetPreferredSize() const override;
208 int GetHeightForWidth(int width) const override;
209 gfx::Size GetMinimumSize() const override;
210 void Layout() override;
213 std::set<ui::OSExchangeData::CustomFormat>* custom_formats) override;
214 bool AreDropTypesRequired() override;
215 bool CanDrop(const ui::OSExchangeData& data) override;
216 int OnDragUpdated(const ui::DropTargetEvent& event) override;
217 void OnDragExited() override;
218 int OnPerformDrop(const ui::DropTargetEvent& event) override;
219 void GetAccessibleState(ui::AXViewState* state) override;
221 // Overridden from views::DragController:
222 void WriteDragDataForView(View* sender,
223 const gfx::Point& press_pt,
224 ui::OSExchangeData* data) override;
225 int GetDragOperationsForView(View* sender, const gfx::Point& p) override;
226 bool CanStartDragForView(View* sender,
227 const gfx::Point& press_pt,
228 const gfx::Point& p) override;
230 // Overridden from views::ResizeAreaDelegate:
231 void OnResize(int resize_amount, bool done_resizing) override;
233 // Overridden from gfx::AnimationDelegate:
234 void AnimationProgressed(const gfx::Animation* animation) override;
235 void AnimationEnded(const gfx::Animation* animation) override;
237 // Overridden from ToolbarActionView::Delegate:
238 content::WebContents* GetCurrentWebContents() override;
239 bool ShownInsideMenu() const override;
240 void OnToolbarActionViewDragDone() override;
241 views::MenuButton* GetOverflowReferenceView() override;
242 void SetPopupOwner(ToolbarActionView* popup_owner) override;
243 void HideActivePopup() override;
244 ToolbarActionView* GetMainViewForAction(ToolbarActionView* view) override;
246 // Overridden from extension::ExtensionKeybindingRegistry::Delegate:
247 extensions::ActiveTabPermissionGranter* GetActiveTabPermissionGranter()
250 // Retrieve the current popup. This should only be used by unit tests.
251 gfx::NativeView TestGetPopup();
253 // Returns the width of an icon, optionally with its padding.
254 static int IconWidth(bool include_padding);
256 // Returns the height of an icon.
257 static int IconHeight();
259 // During testing we can disable animations by setting this flag to true,
260 // so that the bar resizes instantly, instead of having to poll it while it
261 // animates to open/closed status.
262 static bool disable_animations_during_testing_;
265 // Overridden from views::View:
266 void ViewHierarchyChanged(
267 const ViewHierarchyChangedDetails& details) override;
268 void OnPaint(gfx::Canvas* canvas) override;
269 void OnThemeChanged() override;
272 // A struct representing the position at which an action will be dropped.
275 typedef std::vector<ToolbarActionView*> ToolbarActionViews;
277 // extensions::ExtensionToolbarModel::Observer implementation.
278 void ToolbarExtensionAdded(const extensions::Extension* extension,
280 void ToolbarExtensionRemoved(const extensions::Extension* extension) override;
281 void ToolbarExtensionMoved(const extensions::Extension* extension,
283 void ToolbarExtensionUpdated(const extensions::Extension* extension) override;
284 bool ShowExtensionActionPopup(const extensions::Extension* extension,
285 bool grant_active_tab) override;
286 void ToolbarVisibleCountChanged() override;
287 void ToolbarHighlightModeChanged(bool is_highlighting) override;
288 void OnToolbarReorderNecessary(content::WebContents* web_contents) override;
289 Browser* GetBrowser() override;
293 // Called when an action's visibility may have changed.
294 void OnBrowserActionVisibilityChanged();
296 // Returns the preferred width of the container in order to show all icons
297 // that should be visible and, optionally, the chevron.
298 int GetPreferredWidth();
300 // Sets the chevron to be visible or not based on whether all actions are
302 void SetChevronVisibility();
304 // Given a number of |icons|, returns the pixels needed to draw the entire
305 // container (including the chevron if the number of icons is not all the
306 // icons and there's not a separate overflow container). For convenience,
307 // callers can set |icons| to -1 to mean "all icons".
308 int IconCountToWidth(int icons) const;
310 // Given a pixel width, returns the number of icons that fit. (This
311 // automatically determines whether a chevron will be needed and includes it
312 // in the calculation.)
313 size_t WidthToIconCount(int pixels) const;
315 // Returns the absolute minimum size you can shrink the container down to and
316 // still show it. If there's no chevron (i.e., there's a separate overflow
317 // container), this leaves only enough room for the resize area; otherwise,
318 // this assumes a visible chevron because the only way we would not have a
319 // chevron when shrinking down this far is if there were no icons, in which
320 // case the container wouldn't be shown at all.
321 int MinimumNonemptyWidth() const;
323 // Animates to the target size (unless testing, in which case we go straight
324 // to the target size).
325 void Animate(gfx::Tween::Type type, size_t num_visible_icons);
327 // Reorders the views to match the toolbar model for the active tab.
330 // Returns the number of icons that this container should draw. This differs
331 // from the model's visible_icon_count if this container is for the overflow.
332 size_t GetIconCount() const;
334 // Whether this container is in overflow mode (as opposed to in 'main'
335 // mode). See class comments for details on the difference.
336 bool in_overflow_mode() const { return main_container_ != NULL; }
338 // Whether or not the container has been initialized.
341 // The vector of toolbar actions (icons/image buttons for each action).
342 ToolbarActionViews toolbar_action_views_;
346 // The Browser object the container is associated with.
349 // The main container we are serving as overflow for, or NULL if this
350 // class is the the main container. See class comments for details on
351 // the difference between main and overflow.
352 BrowserActionsContainer* main_container_;
354 // The view that triggered the current popup (just a reference to a view
355 // from toolbar_action_views_).
356 ToolbarActionView* popup_owner_;
358 // The model that tracks the order of the toolbar icons.
359 extensions::ExtensionToolbarModel* model_;
361 // The current width of the container.
362 int container_width_;
364 // The resize area for the container.
365 views::ResizeArea* resize_area_;
367 // The chevron for accessing the overflow items. Can be NULL when in overflow
368 // mode or if the toolbar is permanently suppressing the chevron menu.
369 ChevronMenuButton* chevron_;
371 // The painter used when we are highlighting a subset of extensions.
372 scoped_ptr<views::Painter> highlight_painter_;
374 // The animation that happens when the container snaps to place.
375 scoped_ptr<gfx::SlideAnimation> resize_animation_;
377 // Don't show the chevron while animating.
378 bool suppress_chevron_;
380 // True if we should suppress animation; we typically do this e.g. when
381 // switching tabs changes the state of the icons.
382 bool suppress_animation_;
384 // True if we should suppress layout, such as when we are creating or
385 // adjusting a lot of views.
386 bool suppress_layout_;
388 // This is used while the user is resizing (and when the animations are in
389 // progress) to know how wide the delta is between the current state and what
393 // Keeps track of the absolute pixel width the container should have when we
394 // are done animating.
395 int animation_target_size_;
397 // The DropPosition for the current drag-and-drop operation, or NULL if there
399 scoped_ptr<DropPosition> drop_position_;
401 // The class that registers for keyboard shortcuts for extension commands.
402 scoped_ptr<ExtensionKeybindingRegistryViews> extension_keybinding_registry_;
404 ObserverList<BrowserActionsContainerObserver> observers_;
406 // The maximum number of icons to show per row when in overflow mode (showing
407 // icons in the application menu).
408 static int icons_per_overflow_menu_row_;
410 DISALLOW_COPY_AND_ASSIGN(BrowserActionsContainer);
413 #endif // CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTIONS_CONTAINER_H_