browser_actions_container.h revision 5f1c94371a64b3196d4be9466099bb892df9b88e
1cfd74d65d832137e20e193c960802afba73b5d38sm// Copyright 2013 The Chromium Authors. All rights reserved.
23c1e67e433728684b5f228c5d4f3e5b1457bb271sm// Use of this source code is governed by a BSD-style license that can be
3cfd74d65d832137e20e193c960802afba73b5d38sm// found in the LICENSE file.
4cfd74d65d832137e20e193c960802afba73b5d38sm
5cfd74d65d832137e20e193c960802afba73b5d38sm#ifndef CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTIONS_CONTAINER_H_
6cfd74d65d832137e20e193c960802afba73b5d38sm#define CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTIONS_CONTAINER_H_
7cfd74d65d832137e20e193c960802afba73b5d38sm
8cfd74d65d832137e20e193c960802afba73b5d38sm#include "base/observer_list.h"
9cfd74d65d832137e20e193c960802afba73b5d38sm#include "chrome/browser/extensions/extension_keybinding_registry.h"
10cfd74d65d832137e20e193c960802afba73b5d38sm#include "chrome/browser/extensions/extension_toolbar_model.h"
11cfd74d65d832137e20e193c960802afba73b5d38sm#include "chrome/browser/ui/views/extensions/browser_action_overflow_menu_controller.h"
12cfd74d65d832137e20e193c960802afba73b5d38sm#include "chrome/browser/ui/views/toolbar/browser_action_view.h"
13cfd74d65d832137e20e193c960802afba73b5d38sm#include "content/public/browser/notification_observer.h"
14cfd74d65d832137e20e193c960802afba73b5d38sm#include "ui/gfx/animation/animation_delegate.h"
15cfd74d65d832137e20e193c960802afba73b5d38sm#include "ui/gfx/animation/tween.h"
16cfd74d65d832137e20e193c960802afba73b5d38sm#include "ui/views/controls/button/menu_button_listener.h"
17cfd74d65d832137e20e193c960802afba73b5d38sm#include "ui/views/controls/resize_area_delegate.h"
18cfd74d65d832137e20e193c960802afba73b5d38sm#include "ui/views/drag_controller.h"
19cfd74d65d832137e20e193c960802afba73b5d38sm#include "ui/views/view.h"
20cfd74d65d832137e20e193c960802afba73b5d38sm
21cfd74d65d832137e20e193c960802afba73b5d38smclass BrowserActionsContainerObserver;
22cfd74d65d832137e20e193c960802afba73b5d38smclass ExtensionKeybindingRegistryViews;
232b1168acefb6a4104bb7f008df6ac51fcd1de7ecsmclass ExtensionPopup;
24cfd74d65d832137e20e193c960802afba73b5d38sm
25cfd74d65d832137e20e193c960802afba73b5d38smnamespace extensions {
26cfd74d65d832137e20e193c960802afba73b5d38smclass ActiveTabPermissionGranter;
27cfd74d65d832137e20e193c960802afba73b5d38smclass Command;
28cfd74d65d832137e20e193c960802afba73b5d38smclass Extension;
29cfd74d65d832137e20e193c960802afba73b5d38sm}
30cfd74d65d832137e20e193c960802afba73b5d38sm
31cfd74d65d832137e20e193c960802afba73b5d38smnamespace gfx {
32cfd74d65d832137e20e193c960802afba73b5d38smclass SlideAnimation;
33cfd74d65d832137e20e193c960802afba73b5d38sm}
342b1168acefb6a4104bb7f008df6ac51fcd1de7ecsm
35cfd74d65d832137e20e193c960802afba73b5d38smnamespace views {
36cfd74d65d832137e20e193c960802afba73b5d38smclass ResizeArea;
37cfd74d65d832137e20e193c960802afba73b5d38sm}
38cfd74d65d832137e20e193c960802afba73b5d38sm
39cfd74d65d832137e20e193c960802afba73b5d38sm// The BrowserActionsContainer is a container view, responsible for drawing the
40cfd74d65d832137e20e193c960802afba73b5d38sm// browser action icons (extensions that add icons to the toolbar). It comes in
41cfd74d65d832137e20e193c960802afba73b5d38sm// two flavors, a main container (when residing on the toolbar) and an overflow
42cfd74d65d832137e20e193c960802afba73b5d38sm// container (that resides in the main application menu, aka the Chrome menu).
43cfd74d65d832137e20e193c960802afba73b5d38sm//
443c1e67e433728684b5f228c5d4f3e5b1457bb271sm// When in 'main' mode, the container supports the full functionality of a
453c1e67e433728684b5f228c5d4f3e5b1457bb271sm// BrowserActionContainer, but in 'overflow' mode the container is effectively
463c1e67e433728684b5f228c5d4f3e5b1457bb271sm// just an overflow for the 'main' toolbar (shows only the icons that the main
473c1e67e433728684b5f228c5d4f3e5b1457bb271sm// toolbar does not) and as such does not have an overflow itself. The overflow
482b1168acefb6a4104bb7f008df6ac51fcd1de7ecsm// container also does not support resizing. Since the main container only shows
493c1e67e433728684b5f228c5d4f3e5b1457bb271sm// icons in the Chrome toolbar, it is limited to a single row of icons. The
503c1e67e433728684b5f228c5d4f3e5b1457bb271sm// overflow container, however, is allowed to display icons in multiple rows.
512b1168acefb6a4104bb7f008df6ac51fcd1de7ecsm//
522b1168acefb6a4104bb7f008df6ac51fcd1de7ecsm// The main container is placed flush against the omnibox and hot dog menu,
532b1168acefb6a4104bb7f008df6ac51fcd1de7ecsm// whereas the overflow container is placed within the hot dog menu. The
542b1168acefb6a4104bb7f008df6ac51fcd1de7ecsm// layout is similar to this:
552b1168acefb6a4104bb7f008df6ac51fcd1de7ecsm//   rI_I_IcCs
562b1168acefb6a4104bb7f008df6ac51fcd1de7ecsm// Where the letters are as follows:
572b1168acefb6a4104bb7f008df6ac51fcd1de7ecsm//   r: An invisible resize area.  This is ToolbarView::kStandardSpacing pixels
58cfd74d65d832137e20e193c960802afba73b5d38sm//      wide and directly adjacent to the omnibox. Only shown for the main
59cfd74d65d832137e20e193c960802afba73b5d38sm//      container.
60cfd74d65d832137e20e193c960802afba73b5d38sm//   I: An icon.  This is as wide as the IDR_BROWSER_ACTION image.
61cfd74d65d832137e20e193c960802afba73b5d38sm//   _: kItemSpacing pixels of empty space.
622b1168acefb6a4104bb7f008df6ac51fcd1de7ecsm//   c: kChevronSpacing pixels of empty space.  Only present if C is present.
63cfd74d65d832137e20e193c960802afba73b5d38sm//   C: An optional chevron, as wide as the IDR_BROWSER_ACTIONS_OVERFLOW image,
642b1168acefb6a4104bb7f008df6ac51fcd1de7ecsm//      and visible only when both of the following statements are true:
652b1168acefb6a4104bb7f008df6ac51fcd1de7ecsm//      - The container is set to a width smaller than needed to show all icons.
662b1168acefb6a4104bb7f008df6ac51fcd1de7ecsm//      - There is no other container in 'overflow' mode to handle the
672b1168acefb6a4104bb7f008df6ac51fcd1de7ecsm//        non-visible icons for this container.
682b1168acefb6a4104bb7f008df6ac51fcd1de7ecsm//   s: ToolbarView::kStandardSpacing pixels of empty space (before the wrench
692b1168acefb6a4104bb7f008df6ac51fcd1de7ecsm//      menu).
702b1168acefb6a4104bb7f008df6ac51fcd1de7ecsm// The reason the container contains the trailing space "s", rather than having
712b1168acefb6a4104bb7f008df6ac51fcd1de7ecsm// it be handled by the parent view, is so that when the chevron is invisible
722b1168acefb6a4104bb7f008df6ac51fcd1de7ecsm// and the user starts dragging an icon around, we have the space to draw the
732b1168acefb6a4104bb7f008df6ac51fcd1de7ecsm// ultimate drop indicator.  (Otherwise, we'd be trying to draw it into the
74cfd74d65d832137e20e193c960802afba73b5d38sm// padding beyond our right edge, and it wouldn't appear.)
75cfd74d65d832137e20e193c960802afba73b5d38sm//
76cfd74d65d832137e20e193c960802afba73b5d38sm// The BrowserActionsContainer in 'main' mode follows a few rules, in terms of
77cfd74d65d832137e20e193c960802afba73b5d38sm// user experience:
78cfd74d65d832137e20e193c960802afba73b5d38sm//
79cfd74d65d832137e20e193c960802afba73b5d38sm// 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
93// menu in this case.
94//
95// Resizing the BrowserActionsContainer:
96//
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.
102//
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.
107//
108// Animating the BrowserActionsContainer:
109//
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.
113//
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.
117//
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.
123//
124////////////////////////////////////////////////////////////////////////////////
125class BrowserActionsContainer
126    : public views::View,
127      public views::MenuButtonListener,
128      public views::ResizeAreaDelegate,
129      public gfx::AnimationDelegate,
130      public extensions::ExtensionToolbarModel::Observer,
131      public BrowserActionOverflowMenuController::Observer,
132      public BrowserActionView::Delegate,
133      public extensions::ExtensionKeybindingRegistry::Delegate {
134 public:
135  // Constructs a BrowserActionContainer for a particular |browser| object, and
136  // specifies which view is the |owner_view|. For documentation of
137  // |main_container|, see class comments.
138  BrowserActionsContainer(Browser* browser,
139                          views::View* owner_view,
140                          BrowserActionsContainer* main_container);
141  virtual ~BrowserActionsContainer();
142
143  void Init();
144
145  // Get the number of browser actions being displayed.
146  int num_browser_actions() const { return browser_action_views_.size(); }
147
148  // Whether we are performing resize animation on the container.
149  bool animating() const { return animation_target_size_ > 0; }
150
151  // Returns the chevron, if any.
152  views::View* chevron() { return chevron_; }
153  const views::View* chevron() const { return chevron_; }
154
155  // Returns the profile this container is associated with.
156  Profile* profile() const { return profile_; }
157
158  // Get a particular browser action view.
159  BrowserActionView* GetBrowserActionViewAt(int index) {
160    return browser_action_views_[index];
161  }
162
163  // Retrieve the BrowserActionView for a certain extension |action|.
164  BrowserActionView* GetBrowserActionView(ExtensionAction* action);
165
166  // Update the views to reflect the state of the browser action icons.
167  void RefreshBrowserActionViews();
168
169  // Sets up the browser action view vector.
170  void CreateBrowserActionViews();
171
172  // Delete all browser action views.
173  void DeleteBrowserActionViews();
174
175  // Returns how many browser actions are currently visible. If the intent is
176  // to find how many are visible once the container finishes animation, see
177  // VisibleBrowserActionsAfterAnimation() below.
178  size_t VisibleBrowserActions() const;
179
180  // Returns how many browser actions will be visible once the container
181  // finishes animating to a new size, or (if not animating) the currently
182  // visible icons.
183  size_t VisibleBrowserActionsAfterAnimation() const;
184
185  // Executes |command| registered by |extension|.
186  void ExecuteExtensionCommand(const extensions::Extension* extension,
187                               const extensions::Command& command);
188
189  // Add or remove an observer.
190  void AddObserver(BrowserActionsContainerObserver* observer);
191  void RemoveObserver(BrowserActionsContainerObserver* observer);
192
193  // Overridden from views::View:
194  virtual gfx::Size GetPreferredSize() const OVERRIDE;
195  virtual gfx::Size GetMinimumSize() const OVERRIDE;
196  virtual void Layout() OVERRIDE;
197  virtual bool GetDropFormats(int* formats,
198      std::set<ui::OSExchangeData::CustomFormat>* custom_formats) OVERRIDE;
199  virtual bool AreDropTypesRequired() OVERRIDE;
200  virtual bool CanDrop(const ui::OSExchangeData& data) OVERRIDE;
201  virtual void OnDragEntered(const ui::DropTargetEvent& event) OVERRIDE;
202  virtual int OnDragUpdated(const ui::DropTargetEvent& event) OVERRIDE;
203  virtual void OnDragExited() OVERRIDE;
204  virtual int OnPerformDrop(const ui::DropTargetEvent& event) OVERRIDE;
205  virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE;
206
207  // Overridden from views::MenuButtonListener:
208  virtual void OnMenuButtonClicked(views::View* source,
209                                   const gfx::Point& point) OVERRIDE;
210
211  // Overridden from views::DragController:
212  virtual void WriteDragDataForView(View* sender,
213                                    const gfx::Point& press_pt,
214                                    ui::OSExchangeData* data) OVERRIDE;
215  virtual int GetDragOperationsForView(View* sender,
216                                       const gfx::Point& p) OVERRIDE;
217  virtual bool CanStartDragForView(View* sender,
218                                   const gfx::Point& press_pt,
219                                   const gfx::Point& p) OVERRIDE;
220
221  // Overridden from views::ResizeAreaDelegate:
222  virtual void OnResize(int resize_amount, bool done_resizing) OVERRIDE;
223
224  // Overridden from gfx::AnimationDelegate:
225  virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE;
226  virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE;
227
228  // Overridden from BrowserActionOverflowMenuController::Observer:
229  virtual void NotifyMenuDeleted(
230      BrowserActionOverflowMenuController* controller) OVERRIDE;
231
232  // Overridden from BrowserActionView::Delegate:
233  virtual content::WebContents* GetCurrentWebContents() OVERRIDE;
234  virtual void OnBrowserActionVisibilityChanged() OVERRIDE;
235  virtual bool ShownInsideMenu() const OVERRIDE;
236  virtual void OnBrowserActionViewDragDone() OVERRIDE;
237  virtual views::View* GetOverflowReferenceView() OVERRIDE;
238  virtual void SetPopupOwner(BrowserActionButton* popup_owner) OVERRIDE;
239  virtual void HideActivePopup() OVERRIDE;
240
241  // Overridden from extension::ExtensionKeybindingRegistry::Delegate:
242  virtual extensions::ActiveTabPermissionGranter*
243      GetActiveTabPermissionGranter() OVERRIDE;
244
245  // Moves a browser action with |id| to |new_index|.
246  void MoveBrowserAction(const std::string& extension_id, size_t new_index);
247
248  // Shows the popup for |extension| if possible. Returns true if a new popup
249  // was shown. Showing the popup will grant active tab permissions if
250  // |grant_tab_permissions| is true. Only pass true for this argument for
251  // popups triggered interactively, not popups triggered by an API.
252  // If |can_override| is true, this popup can override other popups (hiding
253  // them) and does not have to be in the active window.
254  bool ShowPopupForExtension(const extensions::Extension* extension,
255                             bool grant_tab_permissions,
256                             bool can_override);
257
258  // Retrieve the current popup.  This should only be used by unit tests.
259  ExtensionPopup* TestGetPopup();
260
261  // Set how many icons the container should show. This should only be used by
262  // unit tests.
263  void TestSetIconVisibilityCount(size_t icons);
264
265  // During testing we can disable animations by setting this flag to true,
266  // so that the bar resizes instantly, instead of having to poll it while it
267  // animates to open/closed status.
268  static bool disable_animations_during_testing_;
269
270 protected:
271  // Overridden from views::View:
272  virtual void ViewHierarchyChanged(
273      const ViewHierarchyChangedDetails& details) OVERRIDE;
274  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
275  virtual void OnThemeChanged() OVERRIDE;
276
277 private:
278  friend class BrowserActionView;  // So it can access IconWidth().
279  friend class ShowFolderMenuTask;
280
281  // A struct representing the position at which an action will be dropped.
282  struct DropPosition;
283
284  typedef std::vector<BrowserActionView*> BrowserActionViews;
285
286  // Returns the width of an icon, optionally with its padding.
287  static int IconWidth(bool include_padding);
288
289  // Returns the height of an icon.
290  static int IconHeight();
291
292  // extensions::ExtensionToolbarModel::Observer implementation.
293  virtual void BrowserActionAdded(const extensions::Extension* extension,
294                                  int index) OVERRIDE;
295  virtual void BrowserActionRemoved(
296      const extensions::Extension* extension) OVERRIDE;
297  virtual void BrowserActionMoved(const extensions::Extension* extension,
298                                  int index) OVERRIDE;
299  virtual bool BrowserActionShowPopup(
300      const extensions::Extension* extension) OVERRIDE;
301  virtual void VisibleCountChanged() OVERRIDE;
302  virtual void HighlightModeChanged(bool is_highlighting) OVERRIDE;
303
304  void LoadImages();
305
306  // Sets the initial container width.
307  void SetContainerWidth();
308
309  // Closes the overflow menu if open.
310  void CloseOverflowMenu();
311
312  // Cancels the timer for showing the drop down menu.
313  void StopShowFolderDropMenuTimer();
314
315  // Show the drop down folder after a slight delay.
316  void StartShowFolderDropMenuTimer();
317
318  // Show the overflow menu.
319  void ShowDropFolder();
320
321  // Given a number of |icons| and whether to |display_chevron|, returns the
322  // amount of pixels needed to draw the entire container.  For convenience,
323  // callers can set |icons| to -1 to mean "all icons".
324  int IconCountToWidth(int icons, bool display_chevron) const;
325
326  // Given a pixel width, returns the number of icons that fit.  (This
327  // automatically determines whether a chevron will be needed and includes it
328  // in the calculation.)
329  size_t WidthToIconCount(int pixels) const;
330
331  // Returns the absolute minimum size you can shrink the container down to and
332  // still show it.  This assumes a visible chevron because the only way we
333  // would not have a chevron when shrinking down this far is if there were no
334  // icons, in which case the container wouldn't be shown at all.
335  int MinimumNonemptyWidth() const;
336
337  // Animate to the target size (unless testing, in which case we go straight to
338  // the target size).  This also saves the target number of visible icons in
339  // the pref if we're not incognito.
340  void SaveDesiredSizeAndAnimate(gfx::Tween::Type type,
341                                 size_t num_visible_icons);
342
343  // Returns true if this extension should be shown in this toolbar. This can
344  // return false if we are in an incognito window and the extension is disabled
345  // for incognito.
346  bool ShouldDisplayBrowserAction(const extensions::Extension* extension);
347
348  // Return the index of the first visible icon.
349  size_t GetFirstVisibleIconIndex() const;
350
351  // Whether this container is in overflow mode (as opposed to in 'main'
352  // mode). See class comments for details on the difference.
353  bool in_overflow_mode() const { return main_container_ != NULL; }
354
355  // The vector of browser actions (icons/image buttons for each action). Note
356  // that not every BrowserAction in the ToolbarModel will necessarily be in
357  // this collection. Some extensions may be disabled in incognito windows.
358  BrowserActionViews browser_action_views_;
359
360  Profile* profile_;
361
362  // The Browser object the container is associated with.
363  Browser* browser_;
364
365  // The view that owns us.
366  views::View* owner_view_;
367
368  // The main container we are serving as overflow for, or NULL if this
369  // class is the the main container. See class comments for details on
370  // the difference between main and overflow.
371  BrowserActionsContainer* main_container_;
372
373  // The button that triggered the current popup (just a reference to a button
374  // from browser_action_views_).
375  BrowserActionButton* popup_owner_;
376
377  // The model that tracks the order of the toolbar icons.
378  extensions::ExtensionToolbarModel* model_;
379
380  // The current width of the container.
381  int container_width_;
382
383  // The resize area for the container.
384  views::ResizeArea* resize_area_;
385
386  // The chevron for accessing the overflow items. Can be NULL when in overflow
387  // mode or if the toolbar is permanently suppressing the chevron menu.
388  views::MenuButton* chevron_;
389
390  // The painter used when we are highlighting a subset of extensions.
391  scoped_ptr<views::Painter> highlight_painter_;
392
393  // The menu to show for the overflow button (chevron). This class manages its
394  // own lifetime so that it can stay alive during drag and drop operations.
395  BrowserActionOverflowMenuController* overflow_menu_;
396
397  // The animation that happens when the container snaps to place.
398  scoped_ptr<gfx::SlideAnimation> resize_animation_;
399
400  // Don't show the chevron while animating.
401  bool suppress_chevron_;
402
403  // This is used while the user is resizing (and when the animations are in
404  // progress) to know how wide the delta is between the current state and what
405  // we should draw.
406  int resize_amount_;
407
408  // Keeps track of the absolute pixel width the container should have when we
409  // are done animating.
410  int animation_target_size_;
411
412  // The DropPosition for the current drag-and-drop operation, or NULL if there
413  // is none.
414  scoped_ptr<DropPosition> drop_position_;
415
416  // The class that registers for keyboard shortcuts for extension commands.
417  scoped_ptr<ExtensionKeybindingRegistryViews> extension_keybinding_registry_;
418
419  base::WeakPtrFactory<BrowserActionsContainer> task_factory_;
420
421  // Handles delayed showing of the overflow menu when hovering.
422  base::WeakPtrFactory<BrowserActionsContainer> show_menu_task_factory_;
423
424  ObserverList<BrowserActionsContainerObserver> observers_;
425
426  DISALLOW_COPY_AND_ASSIGN(BrowserActionsContainer);
427};
428
429#endif  // CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTIONS_CONTAINER_H_
430