browser_actions_container.h revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
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. 4 5#ifndef CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTIONS_CONTAINER_H_ 6#define CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTIONS_CONTAINER_H_ 7 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" 20 21class BrowserActionsContainerObserver; 22class ExtensionKeybindingRegistryViews; 23class ExtensionPopup; 24 25namespace extensions { 26class ActiveTabPermissionGranter; 27class Command; 28class Extension; 29} 30 31namespace gfx { 32class SlideAnimation; 33} 34 35namespace views { 36class ResizeArea; 37} 38 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). 43// 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. 51// 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: 55// rI_I_IcCs 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 59// container. 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 69// menu). 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.) 75// 76// The BrowserActionsContainer in 'main' mode follows a few rules, in terms of 77// user experience: 78// 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 93// menu in this case. 94// 95// Resizing the BrowserActionsContainer: 96// 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. 102// 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. 107// 108// Animating the BrowserActionsContainer: 109// 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. 113// 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. 117// 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. 123// 124//////////////////////////////////////////////////////////////////////////////// 125class 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 { 134 public: 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(); 142 143 void Init(); 144 145 // Get the number of browser actions being displayed. 146 size_t num_browser_actions() const { return browser_action_views_.size(); } 147 148 // Whether we are performing resize animation on the container. 149 bool animating() const { return animation_target_size_ > 0; } 150 151 // Returns the chevron, if any. 152 views::View* chevron() { return chevron_; } 153 const views::View* chevron() const { return chevron_; } 154 155 // Returns the profile this container is associated with. 156 Profile* profile() const { return profile_; } 157 158 // Get a particular browser action view. 159 BrowserActionView* GetBrowserActionViewAt(int index) { 160 return browser_action_views_[index]; 161 } 162 163 // Retrieve the BrowserActionView for a certain extension |action|. 164 BrowserActionView* GetBrowserActionView(ExtensionAction* action); 165 166 // Update the views to reflect the state of the browser action icons. 167 void RefreshBrowserActionViews(); 168 169 // Sets up the browser action view vector. 170 void CreateBrowserActionViews(); 171 172 // Delete all browser action views. 173 void DeleteBrowserActionViews(); 174 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; 179 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 182 // visible icons. 183 size_t VisibleBrowserActionsAfterAnimation() const; 184 185 // Executes |command| registered by |extension|. 186 void ExecuteExtensionCommand(const extensions::Extension* extension, 187 const extensions::Command& command); 188 189 // Add or remove an observer. 190 void AddObserver(BrowserActionsContainerObserver* observer); 191 void RemoveObserver(BrowserActionsContainerObserver* observer); 192 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; 206 207 // Overridden from views::MenuButtonListener: 208 virtual void OnMenuButtonClicked(views::View* source, 209 const gfx::Point& point) OVERRIDE; 210 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; 220 221 // Overridden from views::ResizeAreaDelegate: 222 virtual void OnResize(int resize_amount, bool done_resizing) OVERRIDE; 223 224 // Overridden from gfx::AnimationDelegate: 225 virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE; 226 virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE; 227 228 // Overridden from BrowserActionOverflowMenuController::Observer: 229 virtual void NotifyMenuDeleted( 230 BrowserActionOverflowMenuController* controller) OVERRIDE; 231 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; 239 240 // Overridden from extension::ExtensionKeybindingRegistry::Delegate: 241 virtual extensions::ActiveTabPermissionGranter* 242 GetActiveTabPermissionGranter() OVERRIDE; 243 244 // Moves a browser action with |id| to |new_index|. 245 void MoveBrowserAction(const std::string& extension_id, size_t new_index); 246 247 // Shows the popup for |extension| if possible. Returns true if a new popup 248 // was shown. Showing the popup will grant active tab permissions if 249 // |grant_tab_permissions| is true. Only pass true for this argument for 250 // popups triggered interactively, not popups triggered by an API. 251 // If |can_override| is true, this popup can override other popups (hiding 252 // them) and does not have to be in the active window. 253 bool ShowPopupForExtension(const extensions::Extension* extension, 254 bool grant_tab_permissions, 255 bool can_override); 256 257 // Retrieve the current popup. This should only be used by unit tests. 258 ExtensionPopup* TestGetPopup(); 259 260 // Set how many icons the container should show. This should only be used by 261 // unit tests. 262 void TestSetIconVisibilityCount(size_t icons); 263 264 // During testing we can disable animations by setting this flag to true, 265 // so that the bar resizes instantly, instead of having to poll it while it 266 // animates to open/closed status. 267 static bool disable_animations_during_testing_; 268 269 protected: 270 // Overridden from views::View: 271 virtual void ViewHierarchyChanged( 272 const ViewHierarchyChangedDetails& details) OVERRIDE; 273 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; 274 virtual void OnThemeChanged() OVERRIDE; 275 276 private: 277 friend class BrowserActionView; // So it can access IconWidth(). 278 friend class ShowFolderMenuTask; 279 280 // A struct representing the position at which an action will be dropped. 281 struct DropPosition; 282 283 typedef std::vector<BrowserActionView*> BrowserActionViews; 284 285 // Returns the width of an icon, optionally with its padding. 286 static int IconWidth(bool include_padding); 287 288 // Returns the height of an icon. 289 static int IconHeight(); 290 291 // extensions::ExtensionToolbarModel::Observer implementation. 292 virtual void ToolbarExtensionAdded(const extensions::Extension* extension, 293 int index) OVERRIDE; 294 virtual void ToolbarExtensionRemoved( 295 const extensions::Extension* extension) OVERRIDE; 296 virtual void ToolbarExtensionMoved(const extensions::Extension* extension, 297 int index) OVERRIDE; 298 virtual void ToolbarExtensionUpdated( 299 const extensions::Extension* extension) OVERRIDE; 300 virtual bool ShowExtensionActionPopup( 301 const extensions::Extension* extension) OVERRIDE; 302 virtual void ToolbarVisibleCountChanged() OVERRIDE; 303 virtual void ToolbarHighlightModeChanged(bool is_highlighting) OVERRIDE; 304 305 void LoadImages(); 306 307 // Called when a browser action's visibility may have changed. 308 void OnBrowserActionVisibilityChanged(); 309 310 // Sets the initial container width. 311 void SetContainerWidth(); 312 313 // Closes the overflow menu if open. 314 void CloseOverflowMenu(); 315 316 // Cancels the timer for showing the drop down menu. 317 void StopShowFolderDropMenuTimer(); 318 319 // Show the drop down folder after a slight delay. 320 void StartShowFolderDropMenuTimer(); 321 322 // Show the overflow menu. 323 void ShowDropFolder(); 324 325 // Given a number of |icons| and whether to |display_chevron|, returns the 326 // amount of pixels needed to draw the entire container. For convenience, 327 // callers can set |icons| to -1 to mean "all icons". 328 int IconCountToWidth(int icons, bool display_chevron) const; 329 330 // Given a pixel width, returns the number of icons that fit. (This 331 // automatically determines whether a chevron will be needed and includes it 332 // in the calculation.) 333 size_t WidthToIconCount(int pixels) const; 334 335 // Returns the absolute minimum size you can shrink the container down to and 336 // still show it. This assumes a visible chevron because the only way we 337 // would not have a chevron when shrinking down this far is if there were no 338 // icons, in which case the container wouldn't be shown at all. 339 int MinimumNonemptyWidth() const; 340 341 // Animate to the target size (unless testing, in which case we go straight to 342 // the target size). 343 void Animate(gfx::Tween::Type type, size_t num_visible_icons); 344 345 // Returns true if this extension should be shown in this toolbar. This can 346 // return false if we are in an incognito window and the extension is disabled 347 // for incognito. 348 bool ShouldDisplayBrowserAction(const extensions::Extension* extension) const; 349 350 // Return the index of the first visible icon. 351 size_t GetFirstVisibleIconIndex() const; 352 353 // Returns the BrowserActionView* associated with the given |extension|, or 354 // NULL if none exists. 355 BrowserActionView* GetViewForExtension( 356 const extensions::Extension* extension); 357 358 // Returns the number of icons that this container should draw. This differs 359 // from the model's GetVisibleIconCount if this container is for the overflow. 360 size_t GetIconCount() const; 361 362 // Whether this container is in overflow mode (as opposed to in 'main' 363 // mode). See class comments for details on the difference. 364 bool in_overflow_mode() const { return main_container_ != NULL; } 365 366 // The vector of browser actions (icons/image buttons for each action). Note 367 // that not every BrowserAction in the ToolbarModel will necessarily be in 368 // this collection. Some extensions may be disabled in incognito windows. 369 BrowserActionViews browser_action_views_; 370 371 Profile* profile_; 372 373 // The Browser object the container is associated with. 374 Browser* browser_; 375 376 // The view that owns us. 377 views::View* owner_view_; 378 379 // The main container we are serving as overflow for, or NULL if this 380 // class is the the main container. See class comments for details on 381 // the difference between main and overflow. 382 BrowserActionsContainer* main_container_; 383 384 // The view that triggered the current popup (just a reference to a view 385 // from browser_action_views_). 386 BrowserActionView* popup_owner_; 387 388 // The model that tracks the order of the toolbar icons. 389 extensions::ExtensionToolbarModel* model_; 390 391 // The current width of the container. 392 int container_width_; 393 394 // The resize area for the container. 395 views::ResizeArea* resize_area_; 396 397 // The chevron for accessing the overflow items. Can be NULL when in overflow 398 // mode or if the toolbar is permanently suppressing the chevron menu. 399 views::MenuButton* chevron_; 400 401 // The painter used when we are highlighting a subset of extensions. 402 scoped_ptr<views::Painter> highlight_painter_; 403 404 // The menu to show for the overflow button (chevron). This class manages its 405 // own lifetime so that it can stay alive during drag and drop operations. 406 BrowserActionOverflowMenuController* overflow_menu_; 407 408 // The animation that happens when the container snaps to place. 409 scoped_ptr<gfx::SlideAnimation> resize_animation_; 410 411 // Don't show the chevron while animating. 412 bool suppress_chevron_; 413 414 // This is used while the user is resizing (and when the animations are in 415 // progress) to know how wide the delta is between the current state and what 416 // we should draw. 417 int resize_amount_; 418 419 // Keeps track of the absolute pixel width the container should have when we 420 // are done animating. 421 int animation_target_size_; 422 423 // The DropPosition for the current drag-and-drop operation, or NULL if there 424 // is none. 425 scoped_ptr<DropPosition> drop_position_; 426 427 // The class that registers for keyboard shortcuts for extension commands. 428 scoped_ptr<ExtensionKeybindingRegistryViews> extension_keybinding_registry_; 429 430 base::WeakPtrFactory<BrowserActionsContainer> task_factory_; 431 432 // Handles delayed showing of the overflow menu when hovering. 433 base::WeakPtrFactory<BrowserActionsContainer> show_menu_task_factory_; 434 435 ObserverList<BrowserActionsContainerObserver> observers_; 436 437 DISALLOW_COPY_AND_ASSIGN(BrowserActionsContainer); 438}; 439 440#endif // CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTIONS_CONTAINER_H_ 441