Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / views / toolbar / browser_actions_container.h
blobab40e10dccbd61eefaeba677c9e4c62faf4bc539
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"
22 class BrowserActionsContainerObserver;
23 class ExtensionPopup;
25 namespace extensions {
26 class ActiveTabPermissionGranter;
27 class Command;
28 class Extension;
31 namespace views {
32 class ResizeArea;
35 // The BrowserActionsContainer is a container view, responsible for drawing the
36 // toolbar action icons (including extension icons and icons for component
37 // toolbar actions). It comes intwo flavors, a main container (when residing on
38 // the toolbar) and an overflow container (that resides in the main application
39 // menu, aka the Chrome menu).
41 // When in 'main' mode, the container supports the full functionality of a
42 // BrowserActionContainer, but in 'overflow' mode the container is effectively
43 // just an overflow for the 'main' toolbar (shows only the icons that the main
44 // toolbar does not) and as such does not have an overflow itself. The overflow
45 // container also does not support resizing. Since the main container only shows
46 // icons in the Chrome toolbar, it is limited to a single row of icons. The
47 // overflow container, however, is allowed to display icons in multiple rows.
49 // The main container is placed flush against the omnibox and hot dog menu,
50 // whereas the overflow container is placed within the hot dog menu. The
51 // layout is similar to this:
52 // rI_I_IcCs
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. Only shown for the main
56 // container.
57 // I: An icon. This is as wide as the IDR_BROWSER_ACTION image.
58 // _: kItemSpacing pixels of empty space.
59 // c: kChevronSpacing pixels of empty space. Only present if C is present.
60 // C: An optional chevron, as wide as the IDR_BROWSER_ACTIONS_OVERFLOW image,
61 // and visible only when both of the following statements are true:
62 // - The container is set to a width smaller than needed to show all icons.
63 // - There is no other container in 'overflow' mode to handle the
64 // non-visible icons for this container.
65 // s: ToolbarView::kStandardSpacing pixels of empty space (before the wrench
66 // menu).
67 // The reason the container contains the trailing space "s", rather than having
68 // it be handled by the parent view, is so that when the chevron is invisible
69 // and the user starts dragging an icon around, we have the space to draw the
70 // ultimate drop indicator. (Otherwise, we'd be trying to draw it into the
71 // padding beyond our right edge, and it wouldn't appear.)
73 // The BrowserActionsContainer in 'main' mode follows a few rules, in terms of
74 // user experience:
76 // 1) The container can never grow beyond the space needed to show all icons
77 // (hereby referred to as the max width).
78 // 2) The container can never shrink below the space needed to show just the
79 // initial padding and the chevron (ignoring the case where there are no icons
80 // to show, in which case the container won't be visible anyway).
81 // 3) The container snaps into place (to the pixel count that fits the visible
82 // icons) to make sure there is no wasted space at the edges of the container.
83 // 4) If the user adds or removes icons (read: installs/uninstalls browser
84 // actions) we grow and shrink the container as needed - but ONLY if the
85 // container was at max width to begin with.
86 // 5) If the container is NOT at max width (has an overflow menu), we respect
87 // that size when adding and removing icons and DON'T grow/shrink the container.
88 // This means that new icons (which always appear at the far right) will show up
89 // in the overflow. The install bubble for extensions points to the chevron
90 // menu in this case.
92 // Resizing the BrowserActionsContainer:
94 // The ResizeArea view sends OnResize messages to the BrowserActionsContainer
95 // class as the user drags it. This modifies the value for |resize_amount_|.
96 // That indicates to the container that a resize is in progress and is used to
97 // calculate the size in GetPreferredSize(), though that function never exceeds
98 // the defined minimum and maximum size of the container.
100 // When the user releases the mouse (ends the resize), we calculate a target
101 // size for the container (animation_target_size_), clamp that value to the
102 // containers min and max and then animate from the *current* position (that the
103 // user has dragged the view to) to the target size.
105 // Animating the BrowserActionsContainer:
107 // Animations are used when snapping the container to a value that fits all
108 // visible icons. This can be triggered when the user finishes resizing the
109 // container or when Browser Actions are added/removed.
111 // We always animate from the current width (container_width_) to the target
112 // size (animation_target_size_), using |resize_amount| to keep track of the
113 // animation progress.
115 // NOTE: When adding Browser Actions to a maximum width container (no overflow)
116 // we make sure to suppress the chevron menu if it wasn't visible. This is
117 // because we won't have enough space to show the new Browser Action until the
118 // animation ends and we don't want the chevron to flash into view while we are
119 // growing the container.
121 ////////////////////////////////////////////////////////////////////////////////
122 class BrowserActionsContainer
123 : public views::View,
124 public ToolbarActionsBarDelegate,
125 public views::ResizeAreaDelegate,
126 public gfx::AnimationDelegate,
127 public ToolbarActionView::Delegate,
128 public extensions::ExtensionKeybindingRegistry::Delegate {
129 public:
130 // Constructs a BrowserActionContainer for a particular |browser| object. For
131 // documentation of |main_container|, see class comments.
132 BrowserActionsContainer(Browser* browser,
133 BrowserActionsContainer* main_container);
134 ~BrowserActionsContainer() override;
136 void Init();
138 // Get the number of toolbar actions being displayed.
139 size_t num_toolbar_actions() const { return toolbar_action_views_.size(); }
141 // Returns the chevron, if any.
142 views::View* chevron() { return chevron_; }
143 const views::View* chevron() const { return chevron_; }
145 // Returns the browser this container is associated with.
146 Browser* browser() const { return browser_; }
148 ToolbarActionsBar* toolbar_actions_bar() {
149 return toolbar_actions_bar_.get();
152 // The class that registers for keyboard shortcuts for extension commands.
153 extensions::ExtensionKeybindingRegistry* extension_keybinding_registry() {
154 return extension_keybinding_registry_.get();
157 // Get a particular toolbar action view.
158 ToolbarActionView* GetToolbarActionViewAt(int index) {
159 return toolbar_action_views_[index];
162 // Whether we are performing resize animation on the container.
163 bool animating() const {
164 return resize_animation_ && resize_animation_->is_animating();
167 // Returns the ID of the action represented by the view at |index|.
168 const std::string& GetIdAt(size_t index) const;
170 // Returns the ToolbarActionView* associated with the given |extension|, or
171 // NULL if none exists.
172 ToolbarActionView* GetViewForId(const std::string& id);
174 // Update the views to reflect the state of the toolbar actions.
175 void RefreshToolbarActionViews();
177 // Returns how many actions are currently visible. If the intent is to find
178 // how many are visible once the container finishes animation, see
179 // VisibleBrowserActionsAfterAnimation() below.
180 size_t VisibleBrowserActions() const;
182 // Returns how many actions will be visible once the container finishes
183 // animating to a new size, or (if not animating) the currently visible icons.
184 size_t VisibleBrowserActionsAfterAnimation() const;
186 // Executes |command| registered by |extension|.
187 void ExecuteExtensionCommand(const extensions::Extension* extension,
188 const extensions::Command& command);
190 // Hides the currently-active popup, if there is one.
191 void HideActivePopup();
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 SetPopupOwner(ToolbarActionView* popup_owner) override;
236 ToolbarActionView* GetMainViewForAction(ToolbarActionView* view) override;
238 // ToolbarActionsBarDelegate:
239 void AddViewForAction(ToolbarActionViewController* action,
240 size_t index) override;
241 void RemoveViewForAction(ToolbarActionViewController* action) override;
242 void RemoveAllViews() override;
243 void Redraw(bool order_changed) override;
244 void ResizeAndAnimate(gfx::Tween::Type tween_type,
245 int target_width,
246 bool suppress_chevron) override;
247 void SetChevronVisibility(bool chevron_visible) override;
248 int GetWidth() const override;
249 bool IsAnimating() const override;
250 void StopAnimating() override;
251 int GetChevronWidth() const override;
252 bool IsPopupRunning() const override;
253 void OnOverflowedActionWantsToRunChanged(
254 bool overflowed_action_wants_to_run) override;
256 // Overridden from extension::ExtensionKeybindingRegistry::Delegate:
257 extensions::ActiveTabPermissionGranter* GetActiveTabPermissionGranter()
258 override;
260 // Retrieve the current popup. This should only be used by unit tests.
261 gfx::NativeView TestGetPopup();
263 protected:
264 // Overridden from views::View:
265 void ViewHierarchyChanged(
266 const ViewHierarchyChangedDetails& details) override;
267 void OnPaint(gfx::Canvas* canvas) override;
268 void OnThemeChanged() override;
270 private:
271 // A struct representing the position at which an action will be dropped.
272 struct DropPosition;
274 typedef std::vector<ToolbarActionView*> ToolbarActionViews;
276 void LoadImages();
278 const ToolbarActionsBar::PlatformSettings& platform_settings() const {
279 return toolbar_actions_bar_->platform_settings();
282 // Whether this container is in overflow mode (as opposed to in 'main'
283 // mode). See class comments for details on the difference.
284 bool in_overflow_mode() const { return main_container_ != NULL; }
286 // The controlling ToolbarActionsBar, which handles most non-view logic.
287 scoped_ptr<ToolbarActionsBar> toolbar_actions_bar_;
289 // The vector of toolbar actions (icons/image buttons for each action).
290 ToolbarActionViews toolbar_action_views_;
292 // The Browser object the container is associated with.
293 Browser* browser_;
295 // The main container we are serving as overflow for, or NULL if this
296 // class is the the main container. See class comments for details on
297 // the difference between main and overflow.
298 BrowserActionsContainer* main_container_;
300 // The view that triggered the current popup (just a reference to a view
301 // from toolbar_action_views_).
302 ToolbarActionView* popup_owner_;
304 // The current width of the container.
305 int container_width_;
307 // The resize area for the container.
308 views::ResizeArea* resize_area_;
310 // The chevron for accessing the overflow items. Can be NULL when in overflow
311 // mode or if the toolbar is permanently suppressing the chevron menu.
312 ChevronMenuButton* chevron_;
314 // The painter used when we are highlighting a subset of extensions.
315 scoped_ptr<views::Painter> highlight_painter_;
317 // The animation that happens when the container snaps to place.
318 scoped_ptr<gfx::SlideAnimation> resize_animation_;
320 // Don't show the chevron while animating.
321 bool suppress_chevron_;
323 // True if the container has been added to the parent view.
324 bool added_to_view_;
326 // Whether or not the info bubble has been shown, if it should be.
327 bool shown_bubble_;
329 // This is used while the user is resizing (and when the animations are in
330 // progress) to know how wide the delta is between the current state and what
331 // we should draw.
332 int resize_amount_;
334 // Keeps track of the absolute pixel width the container should have when we
335 // are done animating.
336 int animation_target_size_;
338 // The DropPosition for the current drag-and-drop operation, or NULL if there
339 // is none.
340 scoped_ptr<DropPosition> drop_position_;
342 // The class that registers for keyboard shortcuts for extension commands.
343 scoped_ptr<ExtensionKeybindingRegistryViews> extension_keybinding_registry_;
345 ObserverList<BrowserActionsContainerObserver> observers_;
347 DISALLOW_COPY_AND_ASSIGN(BrowserActionsContainer);
350 #endif // CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTIONS_CONTAINER_H_