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/chrome_views_export.h"
12 #include "chrome/browser/ui/views/extensions/browser_action_overflow_menu_controller.h"
13 #include "chrome/browser/ui/views/extensions/extension_keybinding_registry_views.h"
14 #include "chrome/browser/ui/views/extensions/extension_popup.h"
15 #include "chrome/browser/ui/views/toolbar/browser_action_view.h"
16 #include "chrome/browser/ui/views/toolbar/browser_actions_container_observer.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "ui/gfx/animation/animation_delegate.h"
19 #include "ui/gfx/animation/tween.h"
20 #include "ui/views/controls/button/menu_button.h"
21 #include "ui/views/controls/button/menu_button_listener.h"
22 #include "ui/views/controls/resize_area_delegate.h"
23 #include "ui/views/drag_controller.h"
24 #include "ui/views/view.h"
25 #include "ui/views/widget/widget_observer.h"
27 class BrowserActionButton
;
28 class ExtensionKeybindingRegistryViews
;
31 namespace extensions
{
32 class ActiveTabPermissionGranter
;
45 // The BrowserActionsContainer is a container view, responsible for drawing the
46 // browser action icons (extensions that add icons to the toolbar). It comes in
47 // two flavors, a main container (when residing on the toolbar) and an overflow
48 // container (that resides in the main application menu, aka the Chrome menu).
50 // When in 'main' mode, the container supports the full functionality of a
51 // BrowserActionContainer, but in 'overflow' mode the container is effectively
52 // just an overflow for the 'main' toolbar (shows only the icons that the main
53 // toolbar does not) and as such does not have an overflow itself. The overflow
54 // container also does not support resizing. Since the main container only shows
55 // icons in the Chrome toolbar, it is limited to a single row of icons. The
56 // overflow container, however, is allowed to display icons in multiple rows.
58 // The main container is placed flush against the omnibox and hot dog menu,
59 // whereas the overflow container is placed within the hot dog menu. The
60 // layout is similar to this:
62 // Where the letters are as follows:
63 // r: An invisible resize area. This is ToolbarView::kStandardSpacing pixels
64 // wide and directly adjacent to the omnibox. Only shown for the main
66 // I: An icon. This is as wide as the IDR_BROWSER_ACTION image.
67 // _: kItemSpacing pixels of empty space.
68 // c: kChevronSpacing pixels of empty space. Only present if C is present.
69 // C: An optional chevron, as wide as the IDR_BROWSER_ACTIONS_OVERFLOW image,
70 // and visible only when both of the following statements are true:
71 // - The container is set to a width smaller than needed to show all icons.
72 // - There is no other container in 'overflow' mode to handle the
73 // non-visible icons for this container.
74 // s: ToolbarView::kStandardSpacing pixels of empty space (before the wrench
76 // The reason the container contains the trailing space "s", rather than having
77 // it be handled by the parent view, is so that when the chevron is invisible
78 // and the user starts dragging an icon around, we have the space to draw the
79 // ultimate drop indicator. (Otherwise, we'd be trying to draw it into the
80 // padding beyond our right edge, and it wouldn't appear.)
82 // The BrowserActionsContainer in 'main' mode follows a few rules, in terms of
85 // 1) The container can never grow beyond the space needed to show all icons
86 // (hereby referred to as the max width).
87 // 2) The container can never shrink below the space needed to show just the
88 // initial padding and the chevron (ignoring the case where there are no icons
89 // to show, in which case the container won't be visible anyway).
90 // 3) The container snaps into place (to the pixel count that fits the visible
91 // icons) to make sure there is no wasted space at the edges of the container.
92 // 4) If the user adds or removes icons (read: installs/uninstalls browser
93 // actions) we grow and shrink the container as needed - but ONLY if the
94 // container was at max width to begin with.
95 // 5) If the container is NOT at max width (has an overflow menu), we respect
96 // that size when adding and removing icons and DON'T grow/shrink the container.
97 // This means that new icons (which always appear at the far right) will show up
98 // in the overflow. The install bubble for extensions points to the chevron
101 // Resizing the BrowserActionsContainer:
103 // The ResizeArea view sends OnResize messages to the BrowserActionsContainer
104 // class as the user drags it. This modifies the value for |resize_amount_|.
105 // That indicates to the container that a resize is in progress and is used to
106 // calculate the size in GetPreferredSize(), though that function never exceeds
107 // the defined minimum and maximum size of the container.
109 // When the user releases the mouse (ends the resize), we calculate a target
110 // size for the container (animation_target_size_), clamp that value to the
111 // containers min and max and then animate from the *current* position (that the
112 // user has dragged the view to) to the target size.
114 // Animating the BrowserActionsContainer:
116 // Animations are used when snapping the container to a value that fits all
117 // visible icons. This can be triggered when the user finishes resizing the
118 // container or when Browser Actions are added/removed.
120 // We always animate from the current width (container_width_) to the target
121 // size (animation_target_size_), using |resize_amount| to keep track of the
122 // animation progress.
124 // NOTE: When adding Browser Actions to a maximum width container (no overflow)
125 // we make sure to suppress the chevron menu if it wasn't visible. This is
126 // because we won't have enough space to show the new Browser Action until the
127 // animation ends and we don't want the chevron to flash into view while we are
128 // growing the container.
130 ////////////////////////////////////////////////////////////////////////////////
131 class BrowserActionsContainer
132 : public views::View
,
133 public views::MenuButtonListener
,
134 public views::ResizeAreaDelegate
,
135 public gfx::AnimationDelegate
,
136 public extensions::ExtensionToolbarModel::Observer
,
137 public BrowserActionOverflowMenuController::Observer
,
138 public views::WidgetObserver
,
139 public BrowserActionView::Delegate
,
140 public extensions::ExtensionKeybindingRegistry::Delegate
{
142 // Constructs a BrowserActionContainer for a particular |browser| object, and
143 // specifies which view is the |owner_view|. For documentation of
144 // |main_container|, see class comments.
145 BrowserActionsContainer(Browser
* browser
,
146 views::View
* owner_view
,
147 BrowserActionsContainer
* main_container
);
148 virtual ~BrowserActionsContainer();
152 // Get the number of browser actions being displayed.
153 int num_browser_actions() const { return browser_action_views_
.size(); }
155 // Whether we are performing resize animation on the container.
156 bool animating() const { return animation_target_size_
> 0; }
158 // Returns the chevron, if any.
159 views::View
* chevron() { return chevron_
; }
160 const views::View
* chevron() const { return chevron_
; }
162 // Returns the profile this container is associated with.
163 Profile
* profile() const { return profile_
; }
165 // Get a particular browser action view.
166 BrowserActionView
* GetBrowserActionViewAt(int index
) {
167 return browser_action_views_
[index
];
170 // Retrieve the BrowserActionView for a certain extension |action|.
171 BrowserActionView
* GetBrowserActionView(ExtensionAction
* action
);
173 // Update the views to reflect the state of the browser action icons.
174 void RefreshBrowserActionViews();
176 // Sets up the browser action view vector.
177 void CreateBrowserActionViews();
179 // Delete all browser action views.
180 void DeleteBrowserActionViews();
182 // Returns how many browser actions are currently visible. If the intent is
183 // to find how many are visible once the container finishes animation, see
184 // VisibleBrowserActionsAfterAnimation() below.
185 size_t VisibleBrowserActions() const;
187 // Returns how many browser actions will be visible once the container
188 // finishes animating to a new size, or (if not animating) the currently
190 size_t VisibleBrowserActionsAfterAnimation() const;
192 // Executes |command| registered by |extension|.
193 void ExecuteExtensionCommand(const extensions::Extension
* extension
,
194 const extensions::Command
& command
);
196 // Add or remove an observer.
197 void AddObserver(BrowserActionsContainerObserver
* observer
);
198 void RemoveObserver(BrowserActionsContainerObserver
* observer
);
200 // Overridden from views::View:
201 virtual gfx::Size
GetPreferredSize() const OVERRIDE
;
202 virtual gfx::Size
GetMinimumSize() const OVERRIDE
;
203 virtual void Layout() OVERRIDE
;
204 virtual bool GetDropFormats(int* formats
,
205 std::set
<ui::OSExchangeData::CustomFormat
>* custom_formats
) OVERRIDE
;
206 virtual bool AreDropTypesRequired() OVERRIDE
;
207 virtual bool CanDrop(const ui::OSExchangeData
& data
) OVERRIDE
;
208 virtual void OnDragEntered(const ui::DropTargetEvent
& event
) OVERRIDE
;
209 virtual int OnDragUpdated(const ui::DropTargetEvent
& event
) OVERRIDE
;
210 virtual void OnDragExited() OVERRIDE
;
211 virtual int OnPerformDrop(const ui::DropTargetEvent
& event
) OVERRIDE
;
212 virtual void GetAccessibleState(ui::AXViewState
* state
) OVERRIDE
;
214 // Overridden from views::MenuButtonListener:
215 virtual void OnMenuButtonClicked(views::View
* source
,
216 const gfx::Point
& point
) OVERRIDE
;
218 // Overridden from views::DragController:
219 virtual void WriteDragDataForView(View
* sender
,
220 const gfx::Point
& press_pt
,
221 ui::OSExchangeData
* data
) OVERRIDE
;
222 virtual int GetDragOperationsForView(View
* sender
,
223 const gfx::Point
& p
) OVERRIDE
;
224 virtual bool CanStartDragForView(View
* sender
,
225 const gfx::Point
& press_pt
,
226 const gfx::Point
& p
) OVERRIDE
;
228 // Overridden from views::ResizeAreaDelegate:
229 virtual void OnResize(int resize_amount
, bool done_resizing
) OVERRIDE
;
231 // Overridden from gfx::AnimationDelegate:
232 virtual void AnimationProgressed(const gfx::Animation
* animation
) OVERRIDE
;
233 virtual void AnimationEnded(const gfx::Animation
* animation
) OVERRIDE
;
235 // Overridden from BrowserActionOverflowMenuController::Observer:
236 virtual void NotifyMenuDeleted(
237 BrowserActionOverflowMenuController
* controller
) OVERRIDE
;
239 // Overridden from views::WidgetObserver:
240 virtual void OnWidgetDestroying(views::Widget
* widget
) OVERRIDE
;
242 // Overridden from BrowserActionView::Delegate:
243 virtual void InspectPopup(ExtensionAction
* action
) OVERRIDE
;
244 virtual int GetCurrentTabId() const OVERRIDE
;
245 virtual void OnBrowserActionExecuted(BrowserActionButton
* button
) OVERRIDE
;
246 virtual void OnBrowserActionVisibilityChanged() OVERRIDE
;
247 virtual bool ShownInsideMenu() const OVERRIDE
;
249 // Overridden from extension::ExtensionKeybindingRegistry::Delegate:
250 virtual extensions::ActiveTabPermissionGranter
*
251 GetActiveTabPermissionGranter() OVERRIDE
;
253 // Moves a browser action with |id| to |new_index|.
254 void MoveBrowserAction(const std::string
& extension_id
, size_t new_index
);
256 // Shows the popup for |extension| if possible. Returns true if a new popup
257 // was shown. Showing the popup will grant tab permissions if
258 // |grant_tab_permissions| is true. Only pass true for this argument for
259 // popups triggered interactively, not popups triggered by an API.
260 bool ShowPopup(const extensions::Extension
* extension
,
261 bool grant_tab_permissions
);
263 // Hide the current popup.
266 // Simulate a click on a browser action button. This should only be
267 // used by unit tests.
268 void TestExecuteBrowserAction(int index
);
270 // Retrieve the current popup. This should only be used by unit tests.
271 ExtensionPopup
* TestGetPopup() { return popup_
; }
273 // Set how many icons the container should show. This should only be used by
275 void TestSetIconVisibilityCount(size_t icons
);
277 // During testing we can disable animations by setting this flag to true,
278 // so that the bar resizes instantly, instead of having to poll it while it
279 // animates to open/closed status.
280 static bool disable_animations_during_testing_
;
283 // Overridden from views::View:
284 virtual void ViewHierarchyChanged(
285 const ViewHierarchyChangedDetails
& details
) OVERRIDE
;
286 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
;
287 virtual void OnThemeChanged() OVERRIDE
;
290 friend class BrowserActionView
; // So it can access IconWidth().
291 friend class ShowFolderMenuTask
;
293 typedef std::vector
<BrowserActionView
*> BrowserActionViews
;
295 // Returns the width of an icon, optionally with its padding.
296 static int IconWidth(bool include_padding
);
298 // Returns the height of an icon.
299 static int IconHeight();
301 // extensions::ExtensionToolbarModel::Observer implementation.
302 virtual void BrowserActionAdded(const extensions::Extension
* extension
,
304 virtual void BrowserActionRemoved(
305 const extensions::Extension
* extension
) OVERRIDE
;
306 virtual void BrowserActionMoved(const extensions::Extension
* extension
,
308 virtual bool BrowserActionShowPopup(
309 const extensions::Extension
* extension
) OVERRIDE
;
310 virtual void VisibleCountChanged() OVERRIDE
;
311 virtual void HighlightModeChanged(bool is_highlighting
) OVERRIDE
;
315 // Sets the initial container width.
316 void SetContainerWidth();
318 // Closes the overflow menu if open.
319 void CloseOverflowMenu();
321 // Cancels the timer for showing the drop down menu.
322 void StopShowFolderDropMenuTimer();
324 // Show the drop down folder after a slight delay.
325 void StartShowFolderDropMenuTimer();
327 // Show the overflow menu.
328 void ShowDropFolder();
330 // Sets the drop indicator position (and schedules paint if the position has
332 void SetDropIndicator(int x_pos
);
334 // Given a number of |icons| and whether to |display_chevron|, returns the
335 // amount of pixels needed to draw the entire container. For convenience,
336 // callers can set |icons| to -1 to mean "all icons".
337 int IconCountToWidth(int icons
, bool display_chevron
) const;
339 // Given a pixel width, returns the number of icons that fit. (This
340 // automatically determines whether a chevron will be needed and includes it
341 // in the calculation.)
342 size_t WidthToIconCount(int pixels
) const;
344 // Returns the absolute minimum size you can shrink the container down to and
345 // still show it. This assumes a visible chevron because the only way we
346 // would not have a chevron when shrinking down this far is if there were no
347 // icons, in which case the container wouldn't be shown at all.
348 int MinimumNonemptyWidth() const;
350 // Animate to the target size (unless testing, in which case we go straight to
351 // the target size). This also saves the target number of visible icons in
352 // the pref if we're not incognito.
353 void SaveDesiredSizeAndAnimate(gfx::Tween::Type type
,
354 size_t num_visible_icons
);
356 // Returns true if this extension should be shown in this toolbar. This can
357 // return false if we are in an incognito window and the extension is disabled
359 bool ShouldDisplayBrowserAction(const extensions::Extension
* extension
);
361 // Show a popup. Returns true if a new popup was shown. Showing the popup will
362 // grant tab permissions if |grant_tab_permissions| is true. Only pass true
363 // for this argument for popups triggered interactively, not popups triggered
365 bool ShowPopup(BrowserActionButton
* button
,
366 ExtensionPopup::ShowAction show_action
,
367 bool grant_tab_permissions
);
369 // Whether this container is in overflow mode (as opposed to in 'main'
370 // mode). See class comments for details on the difference.
371 bool in_overflow_mode() const { return main_container_
!= NULL
; }
373 // The vector of browser actions (icons/image buttons for each action). Note
374 // that not every BrowserAction in the ToolbarModel will necessarily be in
375 // this collection. Some extensions may be disabled in incognito windows.
376 BrowserActionViews browser_action_views_
;
380 // The Browser object the container is associated with.
383 // The view that owns us.
384 views::View
* owner_view_
;
386 // The main container we are serving as overflow for, or NULL if this
387 // class is the the main container. See class comments for details on
388 // the difference between main and overflow.
389 BrowserActionsContainer
* main_container_
;
391 // The current popup and the button it came from. NULL if no popup.
392 ExtensionPopup
* popup_
;
394 // The button that triggered the current popup (just a reference to a button
395 // from browser_action_views_).
396 BrowserActionButton
* popup_button_
;
398 // The model that tracks the order of the toolbar icons.
399 extensions::ExtensionToolbarModel
* model_
;
401 // The current width of the container.
402 int container_width_
;
404 // The resize area for the container.
405 views::ResizeArea
* resize_area_
;
407 // The chevron for accessing the overflow items. Can be NULL when in overflow
408 // mode or if the toolbar is permanently suppressing the chevron menu.
409 views::MenuButton
* chevron_
;
411 // The painter used when we are highlighting a subset of extensions.
412 scoped_ptr
<views::Painter
> highlight_painter_
;
414 // The menu to show for the overflow button (chevron). This class manages its
415 // own lifetime so that it can stay alive during drag and drop operations.
416 BrowserActionOverflowMenuController
* overflow_menu_
;
418 // The animation that happens when the container snaps to place.
419 scoped_ptr
<gfx::SlideAnimation
> resize_animation_
;
421 // Don't show the chevron while animating.
422 bool suppress_chevron_
;
424 // This is used while the user is resizing (and when the animations are in
425 // progress) to know how wide the delta is between the current state and what
429 // Keeps track of the absolute pixel width the container should have when we
430 // are done animating.
431 int animation_target_size_
;
433 // The x position for where to draw the drop indicator. -1 if no indicator.
434 int drop_indicator_position_
;
436 // The class that registers for keyboard shortcuts for extension commands.
437 scoped_ptr
<ExtensionKeybindingRegistryViews
> extension_keybinding_registry_
;
439 base::WeakPtrFactory
<BrowserActionsContainer
> task_factory_
;
441 // Handles delayed showing of the overflow menu when hovering.
442 base::WeakPtrFactory
<BrowserActionsContainer
> show_menu_task_factory_
;
444 ObserverList
<BrowserActionsContainerObserver
> observers_
;
446 DISALLOW_COPY_AND_ASSIGN(BrowserActionsContainer
);
449 #endif // CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTIONS_CONTAINER_H_