browser_actions_container.h revision f2477e01787aa58f445919b809d89e252beef54f
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.
4
5#ifndef CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTIONS_CONTAINER_H_
6#define CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTIONS_CONTAINER_H_
7
8#include "chrome/browser/extensions/extension_keybinding_registry.h"
9#include "chrome/browser/extensions/extension_toolbar_model.h"
10#include "chrome/browser/ui/views/chrome_views_export.h"
11#include "chrome/browser/ui/views/extensions/browser_action_overflow_menu_controller.h"
12#include "chrome/browser/ui/views/extensions/extension_keybinding_registry_views.h"
13#include "chrome/browser/ui/views/extensions/extension_popup.h"
14#include "chrome/browser/ui/views/toolbar/browser_action_view.h"
15#include "content/public/browser/notification_observer.h"
16#include "ui/gfx/animation/animation_delegate.h"
17#include "ui/gfx/animation/tween.h"
18#include "ui/views/controls/button/menu_button.h"
19#include "ui/views/controls/button/menu_button_listener.h"
20#include "ui/views/controls/resize_area_delegate.h"
21#include "ui/views/drag_controller.h"
22#include "ui/views/view.h"
23#include "ui/views/widget/widget_observer.h"
24
25class BrowserActionButton;
26class ExtensionKeybindingRegistryViews;
27class ExtensionPopup;
28
29namespace extensions {
30class ActiveTabPermissionGranter;
31}
32
33namespace gfx {
34class SlideAnimation;
35}
36
37namespace views {
38class ResizeArea;
39}
40
41////////////////////////////////////////////////////////////////////////////////
42//
43// The BrowserActionsContainer is a container view, responsible for drawing the
44// browser action icons (extensions that add icons to the toolbar).
45//
46// The container is placed flush against the omnibox and wrench menu, and its
47// layout looks like:
48//   rI_I_IcCs
49// Where the letters are as follows:
50//   r: An invisible resize area.  This is ToolbarView::kStandardSpacing pixels
51//      wide and directly adjacent to the omnibox.
52//   I: An icon.  This is as wide as the IDR_BROWSER_ACTION image.
53//   _: kItemSpacing pixels of empty space.
54//   c: kChevronSpacing pixels of empty space.  Only present if C is present.
55//   C: An optional chevron, visible for overflow.  As wide as the
56//      IDR_BROWSER_ACTIONS_OVERFLOW image.
57//   s: ToolbarView::kStandardSpacing pixels of empty space (before the wrench
58//      menu).
59// The reason the container contains the trailing space "s", rather than having
60// it be handled by the parent view, is so that when the chevron is invisible
61// and the user starts dragging an icon around, we have the space to draw the
62// ultimate drop indicator.  (Otherwise, we'd be trying to draw it into the
63// padding beyond our right edge, and it wouldn't appear.)
64//
65// The BrowserActionsContainer follows a few rules, in terms of user experience:
66//
67// 1) The container can never grow beyond the space needed to show all icons
68// (hereby referred to as the max width).
69// 2) The container can never shrink below the space needed to show just the
70// initial padding and the chevron (ignoring the case where there are no icons
71// to show, in which case the container won't be visible anyway).
72// 3) The container snaps into place (to the pixel count that fits the visible
73// icons) to make sure there is no wasted space at the edges of the container.
74// 4) If the user adds or removes icons (read: installs/uninstalls browser
75// actions) we grow and shrink the container as needed - but ONLY if the
76// container was at max width to begin with.
77// 5) If the container is NOT at max width (has an overflow menu), we respect
78// that size when adding and removing icons and DON'T grow/shrink the container.
79// This means that new icons (which always appear at the far right) will show up
80// in the overflow menu. The install bubble for extensions points to the chevron
81// menu in this case.
82//
83// Resizing the BrowserActionsContainer:
84//
85// The ResizeArea view sends OnResize messages to the BrowserActionsContainer
86// class as the user drags it. This modifies the value for |resize_amount_|.
87// That indicates to the container that a resize is in progress and is used to
88// calculate the size in GetPreferredSize(), though that function never exceeds
89// the defined minimum and maximum size of the container.
90//
91// When the user releases the mouse (ends the resize), we calculate a target
92// size for the container (animation_target_size_), clamp that value to the
93// containers min and max and then animate from the *current* position (that the
94// user has dragged the view to) to the target size.
95//
96// Animating the BrowserActionsContainer:
97//
98// Animations are used when snapping the container to a value that fits all
99// visible icons. This can be triggered when the user finishes resizing the
100// container or when Browser Actions are added/removed.
101//
102// We always animate from the current width (container_width_) to the target
103// size (animation_target_size_), using |resize_amount| to keep track of the
104// animation progress.
105//
106// NOTE: When adding Browser Actions to a maximum width container (no overflow)
107// we make sure to suppress the chevron menu if it wasn't visible. This is
108// because we won't have enough space to show the new Browser Action until the
109// animation ends and we don't want the chevron to flash into view while we are
110// growing the container.
111//
112////////////////////////////////////////////////////////////////////////////////
113class BrowserActionsContainer
114    : public views::View,
115      public views::MenuButtonListener,
116      public views::ResizeAreaDelegate,
117      public gfx::AnimationDelegate,
118      public ExtensionToolbarModel::Observer,
119      public BrowserActionOverflowMenuController::Observer,
120      public views::WidgetObserver,
121      public BrowserActionView::Delegate,
122      public extensions::ExtensionKeybindingRegistry::Delegate {
123 public:
124  BrowserActionsContainer(Browser* browser, views::View* owner_view);
125  virtual ~BrowserActionsContainer();
126
127  void Init();
128
129  // Get the number of browser actions being displayed.
130  int num_browser_actions() const { return browser_action_views_.size(); }
131
132  // Whether we are performing resize animation on the container.
133  bool animating() const { return animation_target_size_ > 0; }
134
135  // Returns the chevron, if any.
136  views::View* chevron() { return chevron_; }
137  const views::View* chevron() const { return chevron_; }
138
139  // Returns the profile this container is associated with.
140  Profile* profile() const { return profile_; }
141
142  // Get a particular browser action view.
143  BrowserActionView* GetBrowserActionViewAt(int index) {
144    return browser_action_views_[index];
145  }
146
147  // Retrieve the BrowserActionView for |extension|.
148  BrowserActionView* GetBrowserActionView(ExtensionAction* action);
149
150  // Update the views to reflect the state of the browser action icons.
151  void RefreshBrowserActionViews();
152
153  // Sets up the browser action view vector.
154  void CreateBrowserActionViews();
155
156  // Delete all browser action views.
157  void DeleteBrowserActionViews();
158
159  // Returns how many browser actions are visible.
160  size_t VisibleBrowserActions() const;
161
162  // Overridden from views::View:
163  virtual gfx::Size GetPreferredSize() OVERRIDE;
164  virtual void Layout() OVERRIDE;
165  virtual bool GetDropFormats(int* formats,
166      std::set<ui::OSExchangeData::CustomFormat>* custom_formats) OVERRIDE;
167  virtual bool AreDropTypesRequired() OVERRIDE;
168  virtual bool CanDrop(const ui::OSExchangeData& data) OVERRIDE;
169  virtual void OnDragEntered(const ui::DropTargetEvent& event) OVERRIDE;
170  virtual int OnDragUpdated(const ui::DropTargetEvent& event) OVERRIDE;
171  virtual void OnDragExited() OVERRIDE;
172  virtual int OnPerformDrop(const ui::DropTargetEvent& event) OVERRIDE;
173  virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
174
175  // Overridden from views::MenuButtonListener:
176  virtual void OnMenuButtonClicked(views::View* source,
177                                   const gfx::Point& point) OVERRIDE;
178
179  // Overridden from views::DragController:
180  virtual void WriteDragDataForView(View* sender,
181                                    const gfx::Point& press_pt,
182                                    ui::OSExchangeData* data) OVERRIDE;
183  virtual int GetDragOperationsForView(View* sender,
184                                       const gfx::Point& p) OVERRIDE;
185  virtual bool CanStartDragForView(View* sender,
186                                   const gfx::Point& press_pt,
187                                   const gfx::Point& p) OVERRIDE;
188
189  // Overridden from views::ResizeAreaDelegate:
190  virtual void OnResize(int resize_amount, bool done_resizing) OVERRIDE;
191
192  // Overridden from gfx::AnimationDelegate:
193  virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE;
194  virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE;
195
196  // Overridden from BrowserActionOverflowMenuController::Observer:
197  virtual void NotifyMenuDeleted(
198      BrowserActionOverflowMenuController* controller) OVERRIDE;
199
200  // Overridden from views::WidgetObserver:
201  virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE;
202
203  // Overridden from BrowserActionView::Delegate:
204  virtual void InspectPopup(ExtensionAction* action) OVERRIDE;
205  virtual int GetCurrentTabId() const OVERRIDE;
206  virtual void OnBrowserActionExecuted(BrowserActionButton* button) OVERRIDE;
207  virtual void OnBrowserActionVisibilityChanged() OVERRIDE;
208  virtual gfx::Point GetViewContentOffset() const OVERRIDE;
209
210  // Overridden from extension::ExtensionKeybindingRegistry::Delegate:
211  virtual extensions::ActiveTabPermissionGranter*
212      GetActiveTabPermissionGranter() OVERRIDE;
213
214  // Moves a browser action with |id| to |new_index|.
215  void MoveBrowserAction(const std::string& extension_id, size_t new_index);
216
217  // Hide the current popup.
218  void HidePopup();
219
220  // Simulate a click on a browser action button.  This should only be
221  // used by unit tests.
222  void TestExecuteBrowserAction(int index);
223
224  // Retrieve the current popup.  This should only be used by unit tests.
225  ExtensionPopup* TestGetPopup() { return popup_; }
226
227  // Set how many icons the container should show. This should only be used by
228  // unit tests.
229  void TestSetIconVisibilityCount(size_t icons);
230
231  // During testing we can disable animations by setting this flag to true,
232  // so that the bar resizes instantly, instead of having to poll it while it
233  // animates to open/closed status.
234  static bool disable_animations_during_testing_;
235
236 protected:
237  // Overridden from views::View:
238  virtual void ViewHierarchyChanged(
239      const ViewHierarchyChangedDetails& details) OVERRIDE;
240  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
241  virtual void OnThemeChanged() OVERRIDE;
242
243 private:
244  friend class BrowserActionView;  // So it can access IconHeight().
245  friend class ShowFolderMenuTask;
246
247  typedef std::vector<BrowserActionView*> BrowserActionViews;
248
249  // Returns the width of an icon, optionally with its padding.
250  static int IconWidth(bool include_padding);
251
252  // Returns the height of an icon.
253  static int IconHeight();
254
255  // ExtensionToolbarModel::Observer implementation.
256  virtual void BrowserActionAdded(const extensions::Extension* extension,
257                                  int index) OVERRIDE;
258  virtual void BrowserActionRemoved(
259      const extensions::Extension* extension) OVERRIDE;
260  virtual void BrowserActionMoved(const extensions::Extension* extension,
261                                  int index) OVERRIDE;
262  virtual bool BrowserActionShowPopup(
263      const extensions::Extension* extension) OVERRIDE;
264  virtual void ModelLoaded() OVERRIDE;
265
266  void LoadImages();
267
268  // Sets the initial container width.
269  void SetContainerWidth();
270
271  // Closes the overflow menu if open.
272  void CloseOverflowMenu();
273
274  // Cancels the timer for showing the drop down menu.
275  void StopShowFolderDropMenuTimer();
276
277  // Show the drop down folder after a slight delay.
278  void StartShowFolderDropMenuTimer();
279
280  // Show the overflow menu.
281  void ShowDropFolder();
282
283  // Sets the drop indicator position (and schedules paint if the position has
284  // changed).
285  void SetDropIndicator(int x_pos);
286
287  // Given a number of |icons| and whether to |display_chevron|, returns the
288  // amount of pixels needed to draw the entire container.  For convenience,
289  // callers can set |icons| to -1 to mean "all icons".
290  int IconCountToWidth(int icons, bool display_chevron) const;
291
292  // Given a pixel width, returns the number of icons that fit.  (This
293  // automatically determines whether a chevron will be needed and includes it
294  // in the calculation.)
295  size_t WidthToIconCount(int pixels) const;
296
297  // Returns the absolute minimum size you can shrink the container down to and
298  // still show it.  This assumes a visible chevron because the only way we
299  // would not have a chevron when shrinking down this far is if there were no
300  // icons, in which case the container wouldn't be shown at all.
301  int ContainerMinSize() const;
302
303  // Animate to the target size (unless testing, in which case we go straight to
304  // the target size).  This also saves the target number of visible icons in
305  // the pref if we're not incognito.
306  void SaveDesiredSizeAndAnimate(gfx::Tween::Type type,
307                                 size_t num_visible_icons);
308
309  // Returns true if this extension should be shown in this toolbar. This can
310  // return false if we are in an incognito window and the extension is disabled
311  // for incognito.
312  bool ShouldDisplayBrowserAction(const extensions::Extension* extension);
313
314  // Show a popup. Returns true if a new popup was shown. Showing the popup will
315  // grant tab permissions if |should_grant| is true. Popup's shown via an API
316  // should not grant permissions.
317  bool ShowPopup(BrowserActionButton* button,
318                 ExtensionPopup::ShowAction show_action,
319                 bool should_grant);
320
321  // The vector of browser actions (icons/image buttons for each action). Note
322  // that not every BrowserAction in the ToolbarModel will necessarily be in
323  // this collection. Some extensions may be disabled in incognito windows.
324  BrowserActionViews browser_action_views_;
325
326  Profile* profile_;
327
328  // The Browser object the container is associated with.
329  Browser* browser_;
330
331  // The view that owns us.
332  views::View* owner_view_;
333
334  // The current popup and the button it came from.  NULL if no popup.
335  ExtensionPopup* popup_;
336
337  // The button that triggered the current popup (just a reference to a button
338  // from browser_action_views_).
339  BrowserActionButton* popup_button_;
340
341  // The model that tracks the order of the toolbar icons.
342  ExtensionToolbarModel* model_;
343
344  // The current width of the container.
345  int container_width_;
346
347  // The resize area for the container.
348  views::ResizeArea* resize_area_;
349
350  // The chevron for accessing the overflow items.
351  views::MenuButton* chevron_;
352
353  // The menu to show for the overflow button (chevron). This class manages its
354  // own lifetime so that it can stay alive during drag and drop operations.
355  BrowserActionOverflowMenuController* overflow_menu_;
356
357  // The animation that happens when the container snaps to place.
358  scoped_ptr<gfx::SlideAnimation> resize_animation_;
359
360  // Don't show the chevron while animating.
361  bool suppress_chevron_;
362
363  // This is used while the user is resizing (and when the animations are in
364  // progress) to know how wide the delta is between the current state and what
365  // we should draw.
366  int resize_amount_;
367
368  // Keeps track of the absolute pixel width the container should have when we
369  // are done animating.
370  int animation_target_size_;
371
372  // The x position for where to draw the drop indicator. -1 if no indicator.
373  int drop_indicator_position_;
374
375  // The class that registers for keyboard shortcuts for extension commands.
376  scoped_ptr<ExtensionKeybindingRegistryViews> extension_keybinding_registry_;
377
378  base::WeakPtrFactory<BrowserActionsContainer> task_factory_;
379
380  // Handles delayed showing of the overflow menu when hovering.
381  base::WeakPtrFactory<BrowserActionsContainer> show_menu_task_factory_;
382
383  DISALLOW_COPY_AND_ASSIGN(BrowserActionsContainer);
384};
385
386#endif  // CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTIONS_CONTAINER_H_
387