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 ////////////////////////////////////////////////////////////////////////////////
47 // The BrowserActionsContainer is a container view, responsible for drawing the
48 // browser action icons (extensions that add icons to the toolbar).
50 // The container is placed flush against the omnibox and wrench menu, and its
53 // Where the letters are as follows:
54 // r: An invisible resize area. This is ToolbarView::kStandardSpacing pixels
55 // wide and directly adjacent to the omnibox.
56 // I: An icon. This is as wide as the IDR_BROWSER_ACTION image.
57 // _: kItemSpacing pixels of empty space.
58 // c: kChevronSpacing pixels of empty space. Only present if C is present.
59 // C: An optional chevron, visible for overflow. As wide as the
60 // IDR_BROWSER_ACTIONS_OVERFLOW image.
61 // s: ToolbarView::kStandardSpacing pixels of empty space (before the wrench
63 // The reason the container contains the trailing space "s", rather than having
64 // it be handled by the parent view, is so that when the chevron is invisible
65 // and the user starts dragging an icon around, we have the space to draw the
66 // ultimate drop indicator. (Otherwise, we'd be trying to draw it into the
67 // padding beyond our right edge, and it wouldn't appear.)
69 // The BrowserActionsContainer follows a few rules, in terms of user experience:
71 // 1) The container can never grow beyond the space needed to show all icons
72 // (hereby referred to as the max width).
73 // 2) The container can never shrink below the space needed to show just the
74 // initial padding and the chevron (ignoring the case where there are no icons
75 // to show, in which case the container won't be visible anyway).
76 // 3) The container snaps into place (to the pixel count that fits the visible
77 // icons) to make sure there is no wasted space at the edges of the container.
78 // 4) If the user adds or removes icons (read: installs/uninstalls browser
79 // actions) we grow and shrink the container as needed - but ONLY if the
80 // container was at max width to begin with.
81 // 5) If the container is NOT at max width (has an overflow menu), we respect
82 // that size when adding and removing icons and DON'T grow/shrink the container.
83 // This means that new icons (which always appear at the far right) will show up
84 // in the overflow menu. The install bubble for extensions points to the chevron
87 // Resizing the BrowserActionsContainer:
89 // The ResizeArea view sends OnResize messages to the BrowserActionsContainer
90 // class as the user drags it. This modifies the value for |resize_amount_|.
91 // That indicates to the container that a resize is in progress and is used to
92 // calculate the size in GetPreferredSize(), though that function never exceeds
93 // the defined minimum and maximum size of the container.
95 // When the user releases the mouse (ends the resize), we calculate a target
96 // size for the container (animation_target_size_), clamp that value to the
97 // containers min and max and then animate from the *current* position (that the
98 // user has dragged the view to) to the target size.
100 // Animating the BrowserActionsContainer:
102 // Animations are used when snapping the container to a value that fits all
103 // visible icons. This can be triggered when the user finishes resizing the
104 // container or when Browser Actions are added/removed.
106 // We always animate from the current width (container_width_) to the target
107 // size (animation_target_size_), using |resize_amount| to keep track of the
108 // animation progress.
110 // NOTE: When adding Browser Actions to a maximum width container (no overflow)
111 // we make sure to suppress the chevron menu if it wasn't visible. This is
112 // because we won't have enough space to show the new Browser Action until the
113 // animation ends and we don't want the chevron to flash into view while we are
114 // growing the container.
116 ////////////////////////////////////////////////////////////////////////////////
117 class BrowserActionsContainer
118 : public views::View
,
119 public views::MenuButtonListener
,
120 public views::ResizeAreaDelegate
,
121 public gfx::AnimationDelegate
,
122 public extensions::ExtensionToolbarModel::Observer
,
123 public BrowserActionOverflowMenuController::Observer
,
124 public views::WidgetObserver
,
125 public BrowserActionView::Delegate
,
126 public extensions::ExtensionKeybindingRegistry::Delegate
{
128 BrowserActionsContainer(Browser
* browser
, views::View
* owner_view
);
129 virtual ~BrowserActionsContainer();
133 // Get the number of browser actions being displayed.
134 int num_browser_actions() const { return browser_action_views_
.size(); }
136 // Whether we are performing resize animation on the container.
137 bool animating() const { return animation_target_size_
> 0; }
139 // Returns the chevron, if any.
140 views::View
* chevron() { return chevron_
; }
141 const views::View
* chevron() const { return chevron_
; }
143 // Returns the profile this container is associated with.
144 Profile
* profile() const { return profile_
; }
146 // Get a particular browser action view.
147 BrowserActionView
* GetBrowserActionViewAt(int index
) {
148 return browser_action_views_
[index
];
151 // Retrieve the BrowserActionView for a certain extension |action|.
152 BrowserActionView
* GetBrowserActionView(ExtensionAction
* action
);
154 // Update the views to reflect the state of the browser action icons.
155 void RefreshBrowserActionViews();
157 // Sets up the browser action view vector.
158 void CreateBrowserActionViews();
160 // Delete all browser action views.
161 void DeleteBrowserActionViews();
163 // Returns how many browser actions are visible.
164 size_t VisibleBrowserActions() const;
166 // Executes |command| registered by |extension|.
167 void ExecuteExtensionCommand(const extensions::Extension
* extension
,
168 const extensions::Command
& command
);
170 // Add or remove an observer.
171 void AddObserver(BrowserActionsContainerObserver
* observer
);
172 void RemoveObserver(BrowserActionsContainerObserver
* observer
);
174 // Overridden from views::View:
175 virtual gfx::Size
GetPreferredSize() const OVERRIDE
;
176 virtual gfx::Size
GetMinimumSize() const OVERRIDE
;
177 virtual void Layout() OVERRIDE
;
178 virtual bool GetDropFormats(int* formats
,
179 std::set
<ui::OSExchangeData::CustomFormat
>* custom_formats
) OVERRIDE
;
180 virtual bool AreDropTypesRequired() OVERRIDE
;
181 virtual bool CanDrop(const ui::OSExchangeData
& data
) OVERRIDE
;
182 virtual void OnDragEntered(const ui::DropTargetEvent
& event
) OVERRIDE
;
183 virtual int OnDragUpdated(const ui::DropTargetEvent
& event
) OVERRIDE
;
184 virtual void OnDragExited() OVERRIDE
;
185 virtual int OnPerformDrop(const ui::DropTargetEvent
& event
) OVERRIDE
;
186 virtual void GetAccessibleState(ui::AXViewState
* state
) OVERRIDE
;
188 // Overridden from views::MenuButtonListener:
189 virtual void OnMenuButtonClicked(views::View
* source
,
190 const gfx::Point
& point
) OVERRIDE
;
192 // Overridden from views::DragController:
193 virtual void WriteDragDataForView(View
* sender
,
194 const gfx::Point
& press_pt
,
195 ui::OSExchangeData
* data
) OVERRIDE
;
196 virtual int GetDragOperationsForView(View
* sender
,
197 const gfx::Point
& p
) OVERRIDE
;
198 virtual bool CanStartDragForView(View
* sender
,
199 const gfx::Point
& press_pt
,
200 const gfx::Point
& p
) OVERRIDE
;
202 // Overridden from views::ResizeAreaDelegate:
203 virtual void OnResize(int resize_amount
, bool done_resizing
) OVERRIDE
;
205 // Overridden from gfx::AnimationDelegate:
206 virtual void AnimationProgressed(const gfx::Animation
* animation
) OVERRIDE
;
207 virtual void AnimationEnded(const gfx::Animation
* animation
) OVERRIDE
;
209 // Overridden from BrowserActionOverflowMenuController::Observer:
210 virtual void NotifyMenuDeleted(
211 BrowserActionOverflowMenuController
* controller
) OVERRIDE
;
213 // Overridden from views::WidgetObserver:
214 virtual void OnWidgetDestroying(views::Widget
* widget
) OVERRIDE
;
216 // Overridden from BrowserActionView::Delegate:
217 virtual void InspectPopup(ExtensionAction
* action
) OVERRIDE
;
218 virtual int GetCurrentTabId() const OVERRIDE
;
219 virtual void OnBrowserActionExecuted(BrowserActionButton
* button
) OVERRIDE
;
220 virtual void OnBrowserActionVisibilityChanged() OVERRIDE
;
222 // Overridden from extension::ExtensionKeybindingRegistry::Delegate:
223 virtual extensions::ActiveTabPermissionGranter
*
224 GetActiveTabPermissionGranter() OVERRIDE
;
226 // Moves a browser action with |id| to |new_index|.
227 void MoveBrowserAction(const std::string
& extension_id
, size_t new_index
);
229 // Shows the popup for |extension| if possible. Returns true if a new popup
230 // was shown. Showing the popup will grant tab permissions if
231 // |grant_tab_permissions| is true. Only pass true for this argument for
232 // popups triggered interactively, not popups triggered by an API.
233 bool ShowPopup(const extensions::Extension
* extension
,
234 bool grant_tab_permissions
);
236 // Hide the current popup.
239 // Simulate a click on a browser action button. This should only be
240 // used by unit tests.
241 void TestExecuteBrowserAction(int index
);
243 // Retrieve the current popup. This should only be used by unit tests.
244 ExtensionPopup
* TestGetPopup() { return popup_
; }
246 // Set how many icons the container should show. This should only be used by
248 void TestSetIconVisibilityCount(size_t icons
);
250 // During testing we can disable animations by setting this flag to true,
251 // so that the bar resizes instantly, instead of having to poll it while it
252 // animates to open/closed status.
253 static bool disable_animations_during_testing_
;
256 // Overridden from views::View:
257 virtual void ViewHierarchyChanged(
258 const ViewHierarchyChangedDetails
& details
) OVERRIDE
;
259 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
;
260 virtual void OnThemeChanged() OVERRIDE
;
263 friend class BrowserActionView
; // So it can access IconWidth().
264 friend class ShowFolderMenuTask
;
266 typedef std::vector
<BrowserActionView
*> BrowserActionViews
;
268 // Returns the width of an icon, optionally with its padding.
269 static int IconWidth(bool include_padding
);
271 // Returns the height of an icon.
272 static int IconHeight();
274 // extensions::ExtensionToolbarModel::Observer implementation.
275 virtual void BrowserActionAdded(const extensions::Extension
* extension
,
277 virtual void BrowserActionRemoved(
278 const extensions::Extension
* extension
) OVERRIDE
;
279 virtual void BrowserActionMoved(const extensions::Extension
* extension
,
281 virtual bool BrowserActionShowPopup(
282 const extensions::Extension
* extension
) OVERRIDE
;
283 virtual void VisibleCountChanged() OVERRIDE
;
284 virtual void HighlightModeChanged(bool is_highlighting
) OVERRIDE
;
288 // Sets the initial container width.
289 void SetContainerWidth();
291 // Closes the overflow menu if open.
292 void CloseOverflowMenu();
294 // Cancels the timer for showing the drop down menu.
295 void StopShowFolderDropMenuTimer();
297 // Show the drop down folder after a slight delay.
298 void StartShowFolderDropMenuTimer();
300 // Show the overflow menu.
301 void ShowDropFolder();
303 // Sets the drop indicator position (and schedules paint if the position has
305 void SetDropIndicator(int x_pos
);
307 // Given a number of |icons| and whether to |display_chevron|, returns the
308 // amount of pixels needed to draw the entire container. For convenience,
309 // callers can set |icons| to -1 to mean "all icons".
310 int IconCountToWidth(int icons
, bool display_chevron
) const;
312 // Given a pixel width, returns the number of icons that fit. (This
313 // automatically determines whether a chevron will be needed and includes it
314 // in the calculation.)
315 size_t WidthToIconCount(int pixels
) const;
317 // Returns the absolute minimum size you can shrink the container down to and
318 // still show it. This assumes a visible chevron because the only way we
319 // would not have a chevron when shrinking down this far is if there were no
320 // icons, in which case the container wouldn't be shown at all.
321 int MinimumNonemptyWidth() const;
323 // Animate to the target size (unless testing, in which case we go straight to
324 // the target size). This also saves the target number of visible icons in
325 // the pref if we're not incognito.
326 void SaveDesiredSizeAndAnimate(gfx::Tween::Type type
,
327 size_t num_visible_icons
);
329 // Returns true if this extension should be shown in this toolbar. This can
330 // return false if we are in an incognito window and the extension is disabled
332 bool ShouldDisplayBrowserAction(const extensions::Extension
* extension
);
334 // Show a popup. Returns true if a new popup was shown. Showing the popup will
335 // grant tab permissions if |grant_tab_permissions| is true. Only pass true
336 // for this argument for popups triggered interactively, not popups triggered
338 bool ShowPopup(BrowserActionButton
* button
,
339 ExtensionPopup::ShowAction show_action
,
340 bool grant_tab_permissions
);
342 // The vector of browser actions (icons/image buttons for each action). Note
343 // that not every BrowserAction in the ToolbarModel will necessarily be in
344 // this collection. Some extensions may be disabled in incognito windows.
345 BrowserActionViews browser_action_views_
;
349 // The Browser object the container is associated with.
352 // The view that owns us.
353 views::View
* owner_view_
;
355 // The current popup and the button it came from. NULL if no popup.
356 ExtensionPopup
* popup_
;
358 // The button that triggered the current popup (just a reference to a button
359 // from browser_action_views_).
360 BrowserActionButton
* popup_button_
;
362 // The model that tracks the order of the toolbar icons.
363 extensions::ExtensionToolbarModel
* model_
;
365 // The current width of the container.
366 int container_width_
;
368 // The resize area for the container.
369 views::ResizeArea
* resize_area_
;
371 // The chevron for accessing the overflow items.
372 views::MenuButton
* chevron_
;
374 // The painter used when we are highlighting a subset of extensions.
375 scoped_ptr
<views::Painter
> highlight_painter_
;
377 // The menu to show for the overflow button (chevron). This class manages its
378 // own lifetime so that it can stay alive during drag and drop operations.
379 BrowserActionOverflowMenuController
* overflow_menu_
;
381 // The animation that happens when the container snaps to place.
382 scoped_ptr
<gfx::SlideAnimation
> resize_animation_
;
384 // Don't show the chevron while animating.
385 bool suppress_chevron_
;
387 // This is used while the user is resizing (and when the animations are in
388 // progress) to know how wide the delta is between the current state and what
392 // Keeps track of the absolute pixel width the container should have when we
393 // are done animating.
394 int animation_target_size_
;
396 // The x position for where to draw the drop indicator. -1 if no indicator.
397 int drop_indicator_position_
;
399 // The class that registers for keyboard shortcuts for extension commands.
400 scoped_ptr
<ExtensionKeybindingRegistryViews
> extension_keybinding_registry_
;
402 base::WeakPtrFactory
<BrowserActionsContainer
> task_factory_
;
404 // Handles delayed showing of the overflow menu when hovering.
405 base::WeakPtrFactory
<BrowserActionsContainer
> show_menu_task_factory_
;
407 ObserverList
<BrowserActionsContainerObserver
> observers_
;
409 DISALLOW_COPY_AND_ASSIGN(BrowserActionsContainer
);
412 #endif // CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTIONS_CONTAINER_H_