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/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" 26 27 class BrowserActionButton; 28 class ExtensionKeybindingRegistryViews; 29 class ExtensionPopup; 30 31 namespace extensions { 32 class ActiveTabPermissionGranter; 33 class Command; 34 class Extension; 35 } 36 37 namespace gfx { 38 class SlideAnimation; 39 } 40 41 namespace views { 42 class ResizeArea; 43 } 44 45 //////////////////////////////////////////////////////////////////////////////// 46 // 47 // The BrowserActionsContainer is a container view, responsible for drawing the 48 // browser action icons (extensions that add icons to the toolbar). 49 // 50 // The container is placed flush against the omnibox and wrench menu, and its 51 // layout looks like: 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. 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 62 // menu). 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.) 68 // 69 // The BrowserActionsContainer follows a few rules, in terms of user experience: 70 // 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 85 // menu in this case. 86 // 87 // Resizing the BrowserActionsContainer: 88 // 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. 94 // 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. 99 // 100 // Animating the BrowserActionsContainer: 101 // 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. 105 // 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. 109 // 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. 115 // 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 { 127 public: 128 BrowserActionsContainer(Browser* browser, views::View* owner_view); 129 virtual ~BrowserActionsContainer(); 130 131 void Init(); 132 133 // Get the number of browser actions being displayed. 134 int num_browser_actions() const { return browser_action_views_.size(); } 135 136 // Whether we are performing resize animation on the container. 137 bool animating() const { return animation_target_size_ > 0; } 138 139 // Returns the chevron, if any. 140 views::View* chevron() { return chevron_; } 141 const views::View* chevron() const { return chevron_; } 142 143 // Returns the profile this container is associated with. 144 Profile* profile() const { return profile_; } 145 146 // Get a particular browser action view. 147 BrowserActionView* GetBrowserActionViewAt(int index) { 148 return browser_action_views_[index]; 149 } 150 151 // Retrieve the BrowserActionView for a certain extension |action|. 152 BrowserActionView* GetBrowserActionView(ExtensionAction* action); 153 154 // Update the views to reflect the state of the browser action icons. 155 void RefreshBrowserActionViews(); 156 157 // Sets up the browser action view vector. 158 void CreateBrowserActionViews(); 159 160 // Delete all browser action views. 161 void DeleteBrowserActionViews(); 162 163 // Returns how many browser actions are currently visible. If the intent is 164 // to find how many are visible once the container finishes animation, see 165 // VisibleBrowserActionsAfterAnimation() below. 166 size_t VisibleBrowserActions() const; 167 168 // Returns how many browser actions will be visible once the container 169 // finishes animating to a new size, or (if not animating) the currently 170 // visible icons. 171 size_t VisibleBrowserActionsAfterAnimation() const; 172 173 // Executes |command| registered by |extension|. 174 void ExecuteExtensionCommand(const extensions::Extension* extension, 175 const extensions::Command& command); 176 177 // Add or remove an observer. 178 void AddObserver(BrowserActionsContainerObserver* observer); 179 void RemoveObserver(BrowserActionsContainerObserver* observer); 180 181 // Overridden from views::View: 182 virtual gfx::Size GetPreferredSize() const OVERRIDE; 183 virtual gfx::Size GetMinimumSize() const OVERRIDE; 184 virtual void Layout() OVERRIDE; 185 virtual bool GetDropFormats(int* formats, 186 std::set<ui::OSExchangeData::CustomFormat>* custom_formats) OVERRIDE; 187 virtual bool AreDropTypesRequired() OVERRIDE; 188 virtual bool CanDrop(const ui::OSExchangeData& data) OVERRIDE; 189 virtual void OnDragEntered(const ui::DropTargetEvent& event) OVERRIDE; 190 virtual int OnDragUpdated(const ui::DropTargetEvent& event) OVERRIDE; 191 virtual void OnDragExited() OVERRIDE; 192 virtual int OnPerformDrop(const ui::DropTargetEvent& event) OVERRIDE; 193 virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE; 194 195 // Overridden from views::MenuButtonListener: 196 virtual void OnMenuButtonClicked(views::View* source, 197 const gfx::Point& point) OVERRIDE; 198 199 // Overridden from views::DragController: 200 virtual void WriteDragDataForView(View* sender, 201 const gfx::Point& press_pt, 202 ui::OSExchangeData* data) OVERRIDE; 203 virtual int GetDragOperationsForView(View* sender, 204 const gfx::Point& p) OVERRIDE; 205 virtual bool CanStartDragForView(View* sender, 206 const gfx::Point& press_pt, 207 const gfx::Point& p) OVERRIDE; 208 209 // Overridden from views::ResizeAreaDelegate: 210 virtual void OnResize(int resize_amount, bool done_resizing) OVERRIDE; 211 212 // Overridden from gfx::AnimationDelegate: 213 virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE; 214 virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE; 215 216 // Overridden from BrowserActionOverflowMenuController::Observer: 217 virtual void NotifyMenuDeleted( 218 BrowserActionOverflowMenuController* controller) OVERRIDE; 219 220 // Overridden from views::WidgetObserver: 221 virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE; 222 223 // Overridden from BrowserActionView::Delegate: 224 virtual void InspectPopup(ExtensionAction* action) OVERRIDE; 225 virtual int GetCurrentTabId() const OVERRIDE; 226 virtual void OnBrowserActionExecuted(BrowserActionButton* button) OVERRIDE; 227 virtual void OnBrowserActionVisibilityChanged() OVERRIDE; 228 229 // Overridden from extension::ExtensionKeybindingRegistry::Delegate: 230 virtual extensions::ActiveTabPermissionGranter* 231 GetActiveTabPermissionGranter() OVERRIDE; 232 233 // Moves a browser action with |id| to |new_index|. 234 void MoveBrowserAction(const std::string& extension_id, size_t new_index); 235 236 // Shows the popup for |extension| if possible. Returns true if a new popup 237 // was shown. Showing the popup will grant tab permissions if 238 // |grant_tab_permissions| is true. Only pass true for this argument for 239 // popups triggered interactively, not popups triggered by an API. 240 bool ShowPopup(const extensions::Extension* extension, 241 bool grant_tab_permissions); 242 243 // Hide the current popup. 244 void HidePopup(); 245 246 // Simulate a click on a browser action button. This should only be 247 // used by unit tests. 248 void TestExecuteBrowserAction(int index); 249 250 // Retrieve the current popup. This should only be used by unit tests. 251 ExtensionPopup* TestGetPopup() { return popup_; } 252 253 // Set how many icons the container should show. This should only be used by 254 // unit tests. 255 void TestSetIconVisibilityCount(size_t icons); 256 257 // During testing we can disable animations by setting this flag to true, 258 // so that the bar resizes instantly, instead of having to poll it while it 259 // animates to open/closed status. 260 static bool disable_animations_during_testing_; 261 262 protected: 263 // Overridden from views::View: 264 virtual void ViewHierarchyChanged( 265 const ViewHierarchyChangedDetails& details) OVERRIDE; 266 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; 267 virtual void OnThemeChanged() OVERRIDE; 268 269 private: 270 friend class BrowserActionView; // So it can access IconWidth(). 271 friend class ShowFolderMenuTask; 272 273 typedef std::vector<BrowserActionView*> BrowserActionViews; 274 275 // Returns the width of an icon, optionally with its padding. 276 static int IconWidth(bool include_padding); 277 278 // Returns the height of an icon. 279 static int IconHeight(); 280 281 // extensions::ExtensionToolbarModel::Observer implementation. 282 virtual void BrowserActionAdded(const extensions::Extension* extension, 283 int index) OVERRIDE; 284 virtual void BrowserActionRemoved( 285 const extensions::Extension* extension) OVERRIDE; 286 virtual void BrowserActionMoved(const extensions::Extension* extension, 287 int index) OVERRIDE; 288 virtual bool BrowserActionShowPopup( 289 const extensions::Extension* extension) OVERRIDE; 290 virtual void VisibleCountChanged() OVERRIDE; 291 virtual void HighlightModeChanged(bool is_highlighting) OVERRIDE; 292 293 void LoadImages(); 294 295 // Sets the initial container width. 296 void SetContainerWidth(); 297 298 // Closes the overflow menu if open. 299 void CloseOverflowMenu(); 300 301 // Cancels the timer for showing the drop down menu. 302 void StopShowFolderDropMenuTimer(); 303 304 // Show the drop down folder after a slight delay. 305 void StartShowFolderDropMenuTimer(); 306 307 // Show the overflow menu. 308 void ShowDropFolder(); 309 310 // Sets the drop indicator position (and schedules paint if the position has 311 // changed). 312 void SetDropIndicator(int x_pos); 313 314 // Given a number of |icons| and whether to |display_chevron|, returns the 315 // amount of pixels needed to draw the entire container. For convenience, 316 // callers can set |icons| to -1 to mean "all icons". 317 int IconCountToWidth(int icons, bool display_chevron) const; 318 319 // Given a pixel width, returns the number of icons that fit. (This 320 // automatically determines whether a chevron will be needed and includes it 321 // in the calculation.) 322 size_t WidthToIconCount(int pixels) const; 323 324 // Returns the absolute minimum size you can shrink the container down to and 325 // still show it. This assumes a visible chevron because the only way we 326 // would not have a chevron when shrinking down this far is if there were no 327 // icons, in which case the container wouldn't be shown at all. 328 int MinimumNonemptyWidth() const; 329 330 // Animate to the target size (unless testing, in which case we go straight to 331 // the target size). This also saves the target number of visible icons in 332 // the pref if we're not incognito. 333 void SaveDesiredSizeAndAnimate(gfx::Tween::Type type, 334 size_t num_visible_icons); 335 336 // Returns true if this extension should be shown in this toolbar. This can 337 // return false if we are in an incognito window and the extension is disabled 338 // for incognito. 339 bool ShouldDisplayBrowserAction(const extensions::Extension* extension); 340 341 // Show a popup. Returns true if a new popup was shown. Showing the popup will 342 // grant tab permissions if |grant_tab_permissions| is true. Only pass true 343 // for this argument for popups triggered interactively, not popups triggered 344 // by an API. 345 bool ShowPopup(BrowserActionButton* button, 346 ExtensionPopup::ShowAction show_action, 347 bool grant_tab_permissions); 348 349 // The vector of browser actions (icons/image buttons for each action). Note 350 // that not every BrowserAction in the ToolbarModel will necessarily be in 351 // this collection. Some extensions may be disabled in incognito windows. 352 BrowserActionViews browser_action_views_; 353 354 Profile* profile_; 355 356 // The Browser object the container is associated with. 357 Browser* browser_; 358 359 // The view that owns us. 360 views::View* owner_view_; 361 362 // The current popup and the button it came from. NULL if no popup. 363 ExtensionPopup* popup_; 364 365 // The button that triggered the current popup (just a reference to a button 366 // from browser_action_views_). 367 BrowserActionButton* popup_button_; 368 369 // The model that tracks the order of the toolbar icons. 370 extensions::ExtensionToolbarModel* model_; 371 372 // The current width of the container. 373 int container_width_; 374 375 // The resize area for the container. 376 views::ResizeArea* resize_area_; 377 378 // The chevron for accessing the overflow items. 379 views::MenuButton* chevron_; 380 381 // The painter used when we are highlighting a subset of extensions. 382 scoped_ptr<views::Painter> highlight_painter_; 383 384 // The menu to show for the overflow button (chevron). This class manages its 385 // own lifetime so that it can stay alive during drag and drop operations. 386 BrowserActionOverflowMenuController* overflow_menu_; 387 388 // The animation that happens when the container snaps to place. 389 scoped_ptr<gfx::SlideAnimation> resize_animation_; 390 391 // Don't show the chevron while animating. 392 bool suppress_chevron_; 393 394 // This is used while the user is resizing (and when the animations are in 395 // progress) to know how wide the delta is between the current state and what 396 // we should draw. 397 int resize_amount_; 398 399 // Keeps track of the absolute pixel width the container should have when we 400 // are done animating. 401 int animation_target_size_; 402 403 // The x position for where to draw the drop indicator. -1 if no indicator. 404 int drop_indicator_position_; 405 406 // The class that registers for keyboard shortcuts for extension commands. 407 scoped_ptr<ExtensionKeybindingRegistryViews> extension_keybinding_registry_; 408 409 base::WeakPtrFactory<BrowserActionsContainer> task_factory_; 410 411 // Handles delayed showing of the overflow menu when hovering. 412 base::WeakPtrFactory<BrowserActionsContainer> show_menu_task_factory_; 413 414 ObserverList<BrowserActionsContainerObserver> observers_; 415 416 DISALLOW_COPY_AND_ASSIGN(BrowserActionsContainer); 417 }; 418 419 #endif // CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTIONS_CONTAINER_H_ 420