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/browser_action_overflow_menu_controller.h"
12 #include "chrome/browser/ui/views/toolbar/browser_action_view.h"
13 #include "content/public/browser/notification_observer.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
;
22 class ExtensionKeybindingRegistryViews
;
25 namespace extensions
{
26 class ActiveTabPermissionGranter
;
39 // The BrowserActionsContainer is a container view, responsible for drawing the
40 // browser action icons (extensions that add icons to the toolbar). It comes in
41 // two flavors, a main container (when residing on the toolbar) and an overflow
42 // container (that resides in the main application 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::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
{
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();
145 // Get the number of browser actions being displayed.
146 size_t num_browser_actions() const { return browser_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 // Get a particular browser action view.
159 BrowserActionView
* GetBrowserActionViewAt(int index
) {
160 return browser_action_views_
[index
];
163 // Retrieve the BrowserActionView for a certain extension |action|.
164 BrowserActionView
* GetBrowserActionView(ExtensionAction
* action
);
166 // Update the views to reflect the state of the browser action icons.
167 void RefreshBrowserActionViews();
169 // Sets up the browser action view vector.
170 void CreateBrowserActionViews();
172 // Delete all browser action views.
173 void DeleteBrowserActionViews();
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;
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
183 size_t VisibleBrowserActionsAfterAnimation() const;
185 // Executes |command| registered by |extension|.
186 void ExecuteExtensionCommand(const extensions::Extension
* extension
,
187 const extensions::Command
& command
);
189 // Add or remove an observer.
190 void AddObserver(BrowserActionsContainerObserver
* observer
);
191 void RemoveObserver(BrowserActionsContainerObserver
* observer
);
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
;
207 // Overridden from views::MenuButtonListener:
208 virtual void OnMenuButtonClicked(views::View
* source
,
209 const gfx::Point
& point
) OVERRIDE
;
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
;
221 // Overridden from views::ResizeAreaDelegate:
222 virtual void OnResize(int resize_amount
, bool done_resizing
) OVERRIDE
;
224 // Overridden from gfx::AnimationDelegate:
225 virtual void AnimationProgressed(const gfx::Animation
* animation
) OVERRIDE
;
226 virtual void AnimationEnded(const gfx::Animation
* animation
) OVERRIDE
;
228 // Overridden from BrowserActionOverflowMenuController::Observer:
229 virtual void NotifyMenuDeleted(
230 BrowserActionOverflowMenuController
* controller
) OVERRIDE
;
232 // Overridden from BrowserActionView::Delegate:
233 virtual content::WebContents
* GetCurrentWebContents() OVERRIDE
;
234 virtual bool ShownInsideMenu() const OVERRIDE
;
235 virtual void OnBrowserActionViewDragDone() OVERRIDE
;
236 virtual views::View
* GetOverflowReferenceView() OVERRIDE
;
237 virtual void SetPopupOwner(BrowserActionView
* popup_owner
) OVERRIDE
;
238 virtual void HideActivePopup() OVERRIDE
;
240 // Overridden from extension::ExtensionKeybindingRegistry::Delegate:
241 virtual extensions::ActiveTabPermissionGranter
*
242 GetActiveTabPermissionGranter() OVERRIDE
;
244 // Moves a browser action with |id| to |new_index|.
245 void MoveBrowserAction(const std::string
& extension_id
, size_t new_index
);
247 // Retrieve the current popup. This should only be used by unit tests.
248 ExtensionPopup
* TestGetPopup();
250 // Set how many icons the container should show. This should only be used by
252 void TestSetIconVisibilityCount(size_t icons
);
254 // During testing we can disable animations by setting this flag to true,
255 // so that the bar resizes instantly, instead of having to poll it while it
256 // animates to open/closed status.
257 static bool disable_animations_during_testing_
;
260 // Overridden from views::View:
261 virtual void ViewHierarchyChanged(
262 const ViewHierarchyChangedDetails
& details
) OVERRIDE
;
263 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
;
264 virtual void OnThemeChanged() OVERRIDE
;
267 friend class BrowserActionView
; // So it can access IconWidth().
268 friend class ShowFolderMenuTask
;
270 // A struct representing the position at which an action will be dropped.
273 typedef std::vector
<BrowserActionView
*> BrowserActionViews
;
275 // Returns the width of an icon, optionally with its padding.
276 static int IconWidth(bool include_padding
);
278 // Returns the height of an icon.
279 static int IconHeight();
281 // extensions::ExtensionToolbarModel::Observer implementation.
282 virtual void ToolbarExtensionAdded(const extensions::Extension
* extension
,
284 virtual void ToolbarExtensionRemoved(
285 const extensions::Extension
* extension
) OVERRIDE
;
286 virtual void ToolbarExtensionMoved(const extensions::Extension
* extension
,
288 virtual void ToolbarExtensionUpdated(
289 const extensions::Extension
* extension
) OVERRIDE
;
290 virtual bool ShowExtensionActionPopup(
291 const extensions::Extension
* extension
,
292 bool grant_active_tab
) OVERRIDE
;
293 virtual void ToolbarVisibleCountChanged() OVERRIDE
;
294 virtual void ToolbarHighlightModeChanged(bool is_highlighting
) OVERRIDE
;
295 virtual Browser
* GetBrowser() OVERRIDE
;
299 // Called when a browser action's visibility may have changed.
300 void OnBrowserActionVisibilityChanged();
302 // Sets the initial container width.
303 void SetContainerWidth();
305 // Closes the overflow menu if open.
306 void CloseOverflowMenu();
308 // Cancels the timer for showing the drop down menu.
309 void StopShowFolderDropMenuTimer();
311 // Show the drop down folder after a slight delay.
312 void StartShowFolderDropMenuTimer();
314 // Show the overflow menu.
315 void ShowDropFolder();
317 // Given a number of |icons| and whether to |display_chevron|, returns the
318 // amount of pixels needed to draw the entire container. For convenience,
319 // callers can set |icons| to -1 to mean "all icons".
320 int IconCountToWidth(int icons
, bool display_chevron
) const;
322 // Given a pixel width, returns the number of icons that fit. (This
323 // automatically determines whether a chevron will be needed and includes it
324 // in the calculation.)
325 size_t WidthToIconCount(int pixels
) const;
327 // Returns the absolute minimum size you can shrink the container down to and
328 // still show it. This assumes a visible chevron because the only way we
329 // would not have a chevron when shrinking down this far is if there were no
330 // icons, in which case the container wouldn't be shown at all.
331 int MinimumNonemptyWidth() const;
333 // Animate to the target size (unless testing, in which case we go straight to
335 void Animate(gfx::Tween::Type type
, size_t num_visible_icons
);
337 // Returns true if this extension should be shown in this toolbar. This can
338 // return false if we are in an incognito window and the extension is disabled
340 bool ShouldDisplayBrowserAction(const extensions::Extension
* extension
) const;
342 // Return the index of the first visible icon.
343 size_t GetFirstVisibleIconIndex() const;
345 // Returns the BrowserActionView* associated with the given |extension|, or
346 // NULL if none exists.
347 BrowserActionView
* GetViewForExtension(
348 const extensions::Extension
* extension
);
350 // Returns the number of icons that this container should draw. This differs
351 // from the model's GetVisibleIconCount if this container is for the overflow.
352 size_t GetIconCount() const;
354 // Whether this container is in overflow mode (as opposed to in 'main'
355 // mode). See class comments for details on the difference.
356 bool in_overflow_mode() const { return main_container_
!= NULL
; }
358 // The vector of browser actions (icons/image buttons for each action). Note
359 // that not every BrowserAction in the ToolbarModel will necessarily be in
360 // this collection. Some extensions may be disabled in incognito windows.
361 BrowserActionViews browser_action_views_
;
365 // The Browser object the container is associated with.
368 // The view that owns us.
369 views::View
* owner_view_
;
371 // The main container we are serving as overflow for, or NULL if this
372 // class is the the main container. See class comments for details on
373 // the difference between main and overflow.
374 BrowserActionsContainer
* main_container_
;
376 // The view that triggered the current popup (just a reference to a view
377 // from browser_action_views_).
378 BrowserActionView
* popup_owner_
;
380 // The model that tracks the order of the toolbar icons.
381 extensions::ExtensionToolbarModel
* model_
;
383 // The current width of the container.
384 int container_width_
;
386 // The resize area for the container.
387 views::ResizeArea
* resize_area_
;
389 // The chevron for accessing the overflow items. Can be NULL when in overflow
390 // mode or if the toolbar is permanently suppressing the chevron menu.
391 views::MenuButton
* chevron_
;
393 // The painter used when we are highlighting a subset of extensions.
394 scoped_ptr
<views::Painter
> highlight_painter_
;
396 // The menu to show for the overflow button (chevron). This class manages its
397 // own lifetime so that it can stay alive during drag and drop operations.
398 BrowserActionOverflowMenuController
* overflow_menu_
;
400 // The animation that happens when the container snaps to place.
401 scoped_ptr
<gfx::SlideAnimation
> resize_animation_
;
403 // Don't show the chevron while animating.
404 bool suppress_chevron_
;
406 // This is used while the user is resizing (and when the animations are in
407 // progress) to know how wide the delta is between the current state and what
411 // Keeps track of the absolute pixel width the container should have when we
412 // are done animating.
413 int animation_target_size_
;
415 // The DropPosition for the current drag-and-drop operation, or NULL if there
417 scoped_ptr
<DropPosition
> drop_position_
;
419 // The class that registers for keyboard shortcuts for extension commands.
420 scoped_ptr
<ExtensionKeybindingRegistryViews
> extension_keybinding_registry_
;
422 base::WeakPtrFactory
<BrowserActionsContainer
> task_factory_
;
424 // Handles delayed showing of the overflow menu when hovering.
425 base::WeakPtrFactory
<BrowserActionsContainer
> show_menu_task_factory_
;
427 ObserverList
<BrowserActionsContainerObserver
> observers_
;
429 DISALLOW_COPY_AND_ASSIGN(BrowserActionsContainer
);
432 #endif // CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTIONS_CONTAINER_H_