Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / toolbar / browser_actions_container.h
blobcb5cd316aebc83783a626c05e82b1ce51fe6fcec
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/ui/toolbar/toolbar_actions_bar.h"
11 #include "chrome/browser/ui/toolbar/toolbar_actions_bar_delegate.h"
12 #include "chrome/browser/ui/views/extensions/extension_keybinding_registry_views.h"
13 #include "chrome/browser/ui/views/toolbar/chevron_menu_button.h"
14 #include "chrome/browser/ui/views/toolbar/toolbar_action_view.h"
15 #include "ui/gfx/animation/animation_delegate.h"
16 #include "ui/gfx/animation/slide_animation.h"
17 #include "ui/gfx/animation/tween.h"
18 #include "ui/views/controls/resize_area_delegate.h"
19 #include "ui/views/drag_controller.h"
20 #include "ui/views/view.h"
21 #include "ui/views/widget/widget_observer.h"
23 class BrowserActionsContainerObserver;
24 class ExtensionPopup;
26 namespace extensions {
27 class ActiveTabPermissionGranter;
28 class Command;
29 class Extension;
32 namespace views {
33 class BubbleDelegateView;
34 class ResizeArea;
37 // The BrowserActionsContainer is a container view, responsible for drawing the
38 // toolbar action icons (including extension icons and icons for component
39 // toolbar actions). It comes intwo flavors, a main container (when residing on
40 // the toolbar) and an overflow container (that resides in the main application
41 // menu, aka the Chrome menu).
43 // When in 'main' mode, the container supports the full functionality of a
44 // BrowserActionContainer, but in 'overflow' mode the container is effectively
45 // just an overflow for the 'main' toolbar (shows only the icons that the main
46 // toolbar does not) and as such does not have an overflow itself. The overflow
47 // container also does not support resizing. Since the main container only shows
48 // icons in the Chrome toolbar, it is limited to a single row of icons. The
49 // overflow container, however, is allowed to display icons in multiple rows.
51 // The main container is placed flush against the omnibox and hot dog menu,
52 // whereas the overflow container is placed within the hot dog menu. The
53 // layout is similar to this:
54 // rI_I_IcCs
55 // Where the letters are as follows:
56 // r: An invisible resize area. This is ToolbarView::kStandardSpacing pixels
57 // wide and directly adjacent to the omnibox. Only shown for the main
58 // container.
59 // I: An icon. This is as wide as the IDR_BROWSER_ACTION image.
60 // _: kItemSpacing pixels of empty space.
61 // c: kChevronSpacing pixels of empty space. Only present if C is present.
62 // C: An optional chevron, as wide as the IDR_BROWSER_ACTIONS_OVERFLOW image,
63 // and visible only when both of the following statements are true:
64 // - The container is set to a width smaller than needed to show all icons.
65 // - There is no other container in 'overflow' mode to handle the
66 // non-visible icons for this container.
67 // s: ToolbarView::kStandardSpacing pixels of empty space (before the wrench
68 // menu).
69 // The reason the container contains the trailing space "s", rather than having
70 // it be handled by the parent view, is so that when the chevron is invisible
71 // and the user starts dragging an icon around, we have the space to draw the
72 // ultimate drop indicator. (Otherwise, we'd be trying to draw it into the
73 // padding beyond our right edge, and it wouldn't appear.)
75 // The BrowserActionsContainer in 'main' mode follows a few rules, in terms of
76 // user experience:
78 // 1) The container can never grow beyond the space needed to show all icons
79 // (hereby referred to as the max width).
80 // 2) The container can never shrink below the space needed to show just the
81 // initial padding and the chevron (ignoring the case where there are no icons
82 // to show, in which case the container won't be visible anyway).
83 // 3) The container snaps into place (to the pixel count that fits the visible
84 // icons) to make sure there is no wasted space at the edges of the container.
85 // 4) If the user adds or removes icons (read: installs/uninstalls browser
86 // actions) we grow and shrink the container as needed - but ONLY if the
87 // container was at max width to begin with.
88 // 5) If the container is NOT at max width (has an overflow menu), we respect
89 // that size when adding and removing icons and DON'T grow/shrink the container.
90 // This means that new icons (which always appear at the far right) will show up
91 // in the overflow. The install bubble for extensions points to the chevron
92 // menu in this case.
94 // Resizing the BrowserActionsContainer:
96 // The ResizeArea view sends OnResize messages to the BrowserActionsContainer
97 // class as the user drags it. This modifies the value for |resize_amount_|.
98 // That indicates to the container that a resize is in progress and is used to
99 // calculate the size in GetPreferredSize(), though that function never exceeds
100 // the defined minimum and maximum size of the container.
102 // When the user releases the mouse (ends the resize), we calculate a target
103 // size for the container (animation_target_size_), clamp that value to the
104 // containers min and max and then animate from the *current* position (that the
105 // user has dragged the view to) to the target size.
107 // Animating the BrowserActionsContainer:
109 // Animations are used when snapping the container to a value that fits all
110 // visible icons. This can be triggered when the user finishes resizing the
111 // container or when Browser Actions are added/removed.
113 // We always animate from the current width (container_width_) to the target
114 // size (animation_target_size_), using |resize_amount| to keep track of the
115 // animation progress.
117 // NOTE: When adding Browser Actions to a maximum width container (no overflow)
118 // we make sure to suppress the chevron menu if it wasn't visible. This is
119 // because we won't have enough space to show the new Browser Action until the
120 // animation ends and we don't want the chevron to flash into view while we are
121 // growing the container.
123 ////////////////////////////////////////////////////////////////////////////////
124 class BrowserActionsContainer
125 : public views::View,
126 public ToolbarActionsBarDelegate,
127 public views::ResizeAreaDelegate,
128 public gfx::AnimationDelegate,
129 public ToolbarActionView::Delegate,
130 public views::WidgetObserver,
131 public extensions::ExtensionKeybindingRegistry::Delegate {
132 public:
133 // Constructs a BrowserActionContainer for a particular |browser| object. For
134 // documentation of |main_container|, see class comments.
135 BrowserActionsContainer(Browser* browser,
136 BrowserActionsContainer* main_container);
137 ~BrowserActionsContainer() override;
139 void Init();
141 // Get the number of toolbar actions being displayed.
142 size_t num_toolbar_actions() const { return toolbar_action_views_.size(); }
144 // Returns the chevron, if any.
145 views::View* chevron() { return chevron_; }
146 const views::View* chevron() const { return chevron_; }
148 // Returns the browser this container is associated with.
149 Browser* browser() const { return browser_; }
151 ToolbarActionsBar* toolbar_actions_bar() {
152 return toolbar_actions_bar_.get();
155 // The class that registers for keyboard shortcuts for extension commands.
156 extensions::ExtensionKeybindingRegistry* extension_keybinding_registry() {
157 return extension_keybinding_registry_.get();
160 // Get a particular toolbar action view.
161 ToolbarActionView* GetToolbarActionViewAt(int index) {
162 return toolbar_action_views_[index];
165 // Whether we are performing resize animation on the container.
166 bool animating() const {
167 return resize_animation_ && resize_animation_->is_animating();
170 // Returns the ID of the action represented by the view at |index|.
171 std::string GetIdAt(size_t index) const;
173 // Returns the ToolbarActionView* associated with the given |extension|, or
174 // NULL if none exists.
175 ToolbarActionView* GetViewForId(const std::string& id);
177 // Update the views to reflect the state of the toolbar actions.
178 void RefreshToolbarActionViews();
180 // Returns how many actions are currently visible. If the intent is to find
181 // how many are visible once the container finishes animation, see
182 // VisibleBrowserActionsAfterAnimation() below.
183 size_t VisibleBrowserActions() const;
185 // Returns how many actions will be visible once the container finishes
186 // animating to a new size, or (if not animating) the currently visible icons.
187 size_t VisibleBrowserActionsAfterAnimation() const;
189 // Executes |command| registered by |extension|.
190 void ExecuteExtensionCommand(const extensions::Extension* extension,
191 const extensions::Command& command);
193 // Add or remove an observer.
194 void AddObserver(BrowserActionsContainerObserver* observer);
195 void RemoveObserver(BrowserActionsContainerObserver* observer);
197 // Overridden from views::View:
198 gfx::Size GetPreferredSize() const override;
199 int GetHeightForWidth(int width) const override;
200 gfx::Size GetMinimumSize() const override;
201 void Layout() override;
202 void OnMouseEntered(const ui::MouseEvent& event) override;
203 bool GetDropFormats(
204 int* formats,
205 std::set<ui::OSExchangeData::CustomFormat>* custom_formats) override;
206 bool AreDropTypesRequired() override;
207 bool CanDrop(const ui::OSExchangeData& data) override;
208 int OnDragUpdated(const ui::DropTargetEvent& event) override;
209 void OnDragExited() override;
210 int OnPerformDrop(const ui::DropTargetEvent& event) override;
211 void GetAccessibleState(ui::AXViewState* state) override;
213 // Overridden from views::DragController:
214 void WriteDragDataForView(View* sender,
215 const gfx::Point& press_pt,
216 ui::OSExchangeData* data) override;
217 int GetDragOperationsForView(View* sender, const gfx::Point& p) override;
218 bool CanStartDragForView(View* sender,
219 const gfx::Point& press_pt,
220 const gfx::Point& p) override;
222 // Overridden from views::ResizeAreaDelegate:
223 void OnResize(int resize_amount, bool done_resizing) override;
225 // Overridden from gfx::AnimationDelegate:
226 void AnimationProgressed(const gfx::Animation* animation) override;
227 void AnimationCanceled(const gfx::Animation* animation) override;
228 void AnimationEnded(const gfx::Animation* animation) override;
230 // Overridden from ToolbarActionView::Delegate:
231 content::WebContents* GetCurrentWebContents() override;
232 bool ShownInsideMenu() const override;
233 void OnToolbarActionViewDragDone() override;
234 views::MenuButton* GetOverflowReferenceView() override;
235 void OnMouseEnteredToolbarActionView() override;
237 // ToolbarActionsBarDelegate:
238 void AddViewForAction(ToolbarActionViewController* action,
239 size_t index) override;
240 void RemoveViewForAction(ToolbarActionViewController* action) override;
241 void RemoveAllViews() override;
242 void Redraw(bool order_changed) override;
243 void ResizeAndAnimate(gfx::Tween::Type tween_type,
244 int target_width,
245 bool suppress_chevron) override;
246 void SetChevronVisibility(bool chevron_visible) override;
247 int GetWidth(GetWidthTime get_width_time) const override;
248 bool IsAnimating() const override;
249 void StopAnimating() override;
250 int GetChevronWidth() const override;
251 void ShowExtensionMessageBubble(
252 scoped_ptr<extensions::ExtensionMessageBubbleController> controller,
253 ToolbarActionViewController* anchor_action) override;
255 // views::WidgetObserver:
256 void OnWidgetClosing(views::Widget* widget) override;
257 void OnWidgetDestroying(views::Widget* widget) override;
259 // Overridden from extension::ExtensionKeybindingRegistry::Delegate:
260 extensions::ActiveTabPermissionGranter* GetActiveTabPermissionGranter()
261 override;
263 views::BubbleDelegateView* active_bubble() { return active_bubble_; }
265 protected:
266 // Overridden from views::View:
267 void ViewHierarchyChanged(
268 const ViewHierarchyChangedDetails& details) override;
269 void OnPaint(gfx::Canvas* canvas) override;
270 void OnThemeChanged() override;
272 private:
273 // A struct representing the position at which an action will be dropped.
274 struct DropPosition;
276 typedef std::vector<ToolbarActionView*> ToolbarActionViews;
278 void LoadImages();
280 // Clears the |active_bubble_|, and unregisters the container as an observer.
281 void ClearActiveBubble(views::Widget* widget);
283 const ToolbarActionsBar::PlatformSettings& platform_settings() const {
284 return toolbar_actions_bar_->platform_settings();
287 // Whether this container is in overflow mode (as opposed to in 'main'
288 // mode). See class comments for details on the difference.
289 bool in_overflow_mode() const { return main_container_ != NULL; }
291 // The controlling ToolbarActionsBar, which handles most non-view logic.
292 scoped_ptr<ToolbarActionsBar> toolbar_actions_bar_;
294 // The vector of toolbar actions (icons/image buttons for each action).
295 ToolbarActionViews toolbar_action_views_;
297 // The Browser object the container is associated with.
298 Browser* browser_;
300 // The main container we are serving as overflow for, or NULL if this
301 // class is the the main container. See class comments for details on
302 // the difference between main and overflow.
303 BrowserActionsContainer* main_container_;
305 // The current width of the container.
306 int container_width_;
308 // The resize area for the container.
309 views::ResizeArea* resize_area_;
311 // The chevron for accessing the overflow items. Can be NULL when in overflow
312 // mode or if the toolbar is permanently suppressing the chevron menu.
313 ChevronMenuButton* chevron_;
315 // The painter used when we are highlighting a subset of extensions.
316 scoped_ptr<views::Painter> info_highlight_painter_;
317 scoped_ptr<views::Painter> warning_highlight_painter_;
319 // The animation that happens when the container snaps to place.
320 scoped_ptr<gfx::SlideAnimation> resize_animation_;
322 // Don't show the chevron while animating.
323 bool suppress_chevron_;
325 // True if the container has been added to the parent view.
326 bool added_to_view_;
328 // Whether or not the info bubble has been shown, if it should be.
329 bool shown_bubble_;
331 // This is used while the user is resizing (and when the animations are in
332 // progress) to know how wide the delta is between the current state and what
333 // we should draw.
334 int resize_amount_;
336 // Keeps track of the absolute pixel width the container should have when we
337 // are done animating.
338 int animation_target_size_;
340 // The DropPosition for the current drag-and-drop operation, or NULL if there
341 // is none.
342 scoped_ptr<DropPosition> drop_position_;
344 // The class that registers for keyboard shortcuts for extension commands.
345 scoped_ptr<ExtensionKeybindingRegistryViews> extension_keybinding_registry_;
347 // The extension bubble that is actively showing, if any.
348 views::BubbleDelegateView* active_bubble_;
350 base::ObserverList<BrowserActionsContainerObserver> observers_;
352 DISALLOW_COPY_AND_ASSIGN(BrowserActionsContainer);
355 #endif // CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTIONS_CONTAINER_H_