1 // Copyright (c) 2012 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_BROWSER_ACTIONS_CONTAINER_H_ 6 #define CHROME_BROWSER_UI_VIEWS_BROWSER_ACTIONS_CONTAINER_H_ 7 8 #include "chrome/browser/extensions/extension_keybinding_registry.h" 9 #include "chrome/browser/extensions/extension_toolbar_model.h" 10 #include "chrome/browser/ui/views/browser_action_view.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 "content/public/browser/notification_observer.h" 16 #include "ui/base/animation/animation_delegate.h" 17 #include "ui/base/animation/tween.h" 18 #include "ui/views/controls/button/menu_button.h" 19 #include "ui/views/controls/button/menu_button_listener.h" 20 #include "ui/views/controls/resize_area_delegate.h" 21 #include "ui/views/drag_controller.h" 22 #include "ui/views/view.h" 23 #include "ui/views/widget/widget_observer.h" 24 25 class BrowserActionButton; 26 class ExtensionKeybindingRegistryViews; 27 class ExtensionPopup; 28 29 namespace extensions { 30 class ActiveTabPermissionGranter; 31 } 32 33 namespace ui { 34 class SlideAnimation; 35 } 36 37 namespace views { 38 class ResizeArea; 39 } 40 41 //////////////////////////////////////////////////////////////////////////////// 42 // 43 // The BrowserActionsContainer is a container view, responsible for drawing the 44 // browser action icons (extensions that add icons to the toolbar). 45 // 46 // The container is placed flush against the omnibox and wrench menu, and its 47 // layout looks like: 48 // rI_I_IcCs 49 // Where the letters are as follows: 50 // r: An invisible resize area. This is ToolbarView::kStandardSpacing pixels 51 // wide and directly adjacent to the omnibox. 52 // I: An icon. This is as wide as the IDR_BROWSER_ACTION image. 53 // _: kItemSpacing pixels of empty space. 54 // c: kChevronSpacing pixels of empty space. Only present if C is present. 55 // C: An optional chevron, visible for overflow. As wide as the 56 // IDR_BROWSER_ACTIONS_OVERFLOW image. 57 // s: ToolbarView::kStandardSpacing pixels of empty space (before the wrench 58 // menu). 59 // The reason the container contains the trailing space "s", rather than having 60 // it be handled by the parent view, is so that when the chevron is invisible 61 // and the user starts dragging an icon around, we have the space to draw the 62 // ultimate drop indicator. (Otherwise, we'd be trying to draw it into the 63 // padding beyond our right edge, and it wouldn't appear.) 64 // 65 // The BrowserActionsContainer follows a few rules, in terms of user experience: 66 // 67 // 1) The container can never grow beyond the space needed to show all icons 68 // (hereby referred to as the max width). 69 // 2) The container can never shrink below the space needed to show just the 70 // initial padding and the chevron (ignoring the case where there are no icons 71 // to show, in which case the container won't be visible anyway). 72 // 3) The container snaps into place (to the pixel count that fits the visible 73 // icons) to make sure there is no wasted space at the edges of the container. 74 // 4) If the user adds or removes icons (read: installs/uninstalls browser 75 // actions) we grow and shrink the container as needed - but ONLY if the 76 // container was at max width to begin with. 77 // 5) If the container is NOT at max width (has an overflow menu), we respect 78 // that size when adding and removing icons and DON'T grow/shrink the container. 79 // This means that new icons (which always appear at the far right) will show up 80 // in the overflow menu. The install bubble for extensions points to the chevron 81 // menu in this case. 82 // 83 // Resizing the BrowserActionsContainer: 84 // 85 // The ResizeArea view sends OnResize messages to the BrowserActionsContainer 86 // class as the user drags it. This modifies the value for |resize_amount_|. 87 // That indicates to the container that a resize is in progress and is used to 88 // calculate the size in GetPreferredSize(), though that function never exceeds 89 // the defined minimum and maximum size of the container. 90 // 91 // When the user releases the mouse (ends the resize), we calculate a target 92 // size for the container (animation_target_size_), clamp that value to the 93 // containers min and max and then animate from the *current* position (that the 94 // user has dragged the view to) to the target size. 95 // 96 // Animating the BrowserActionsContainer: 97 // 98 // Animations are used when snapping the container to a value that fits all 99 // visible icons. This can be triggered when the user finishes resizing the 100 // container or when Browser Actions are added/removed. 101 // 102 // We always animate from the current width (container_width_) to the target 103 // size (animation_target_size_), using |resize_amount| to keep track of the 104 // animation progress. 105 // 106 // NOTE: When adding Browser Actions to a maximum width container (no overflow) 107 // we make sure to suppress the chevron menu if it wasn't visible. This is 108 // because we won't have enough space to show the new Browser Action until the 109 // animation ends and we don't want the chevron to flash into view while we are 110 // growing the container. 111 // 112 //////////////////////////////////////////////////////////////////////////////// 113 class BrowserActionsContainer 114 : public views::View, 115 public views::MenuButtonListener, 116 public views::ResizeAreaDelegate, 117 public ui::AnimationDelegate, 118 public ExtensionToolbarModel::Observer, 119 public BrowserActionOverflowMenuController::Observer, 120 public views::WidgetObserver, 121 public BrowserActionView::Delegate, 122 public extensions::ExtensionKeybindingRegistry::Delegate { 123 public: 124 BrowserActionsContainer(Browser* browser, views::View* owner_view); 125 virtual ~BrowserActionsContainer(); 126 127 void Init(); 128 129 // Get the number of browser actions being displayed. 130 int num_browser_actions() const { return browser_action_views_.size(); } 131 132 // Whether we are performing resize animation on the container. 133 bool animating() const { return animation_target_size_ > 0; } 134 135 // Returns the chevron, if any. 136 views::View* chevron() { return chevron_; } 137 const views::View* chevron() const { return chevron_; } 138 139 // Returns the profile this container is associated with. 140 Profile* profile() const { return profile_; } 141 142 // Get a particular browser action view. 143 BrowserActionView* GetBrowserActionViewAt(int index) { 144 return browser_action_views_[index]; 145 } 146 147 // Retrieve the BrowserActionView for |extension|. 148 BrowserActionView* GetBrowserActionView(ExtensionAction* action); 149 150 // Update the views to reflect the state of the browser action icons. 151 void RefreshBrowserActionViews(); 152 153 // Sets up the browser action view vector. 154 void CreateBrowserActionViews(); 155 156 // Delete all browser action views. 157 void DeleteBrowserActionViews(); 158 159 // Returns how many browser actions are visible. 160 size_t VisibleBrowserActions() const; 161 162 // Overridden from views::View: 163 virtual gfx::Size GetPreferredSize() OVERRIDE; 164 virtual void Layout() OVERRIDE; 165 virtual bool GetDropFormats(int* formats, 166 std::set<ui::OSExchangeData::CustomFormat>* custom_formats) OVERRIDE; 167 virtual bool AreDropTypesRequired() OVERRIDE; 168 virtual bool CanDrop(const ui::OSExchangeData& data) OVERRIDE; 169 virtual void OnDragEntered(const ui::DropTargetEvent& event) OVERRIDE; 170 virtual int OnDragUpdated(const ui::DropTargetEvent& event) OVERRIDE; 171 virtual void OnDragExited() OVERRIDE; 172 virtual int OnPerformDrop(const ui::DropTargetEvent& event) OVERRIDE; 173 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; 174 175 // Overridden from views::MenuButtonListener: 176 virtual void OnMenuButtonClicked(views::View* source, 177 const gfx::Point& point) OVERRIDE; 178 179 // Overridden from views::DragController: 180 virtual void WriteDragDataForView(View* sender, 181 const gfx::Point& press_pt, 182 ui::OSExchangeData* data) OVERRIDE; 183 virtual int GetDragOperationsForView(View* sender, 184 const gfx::Point& p) OVERRIDE; 185 virtual bool CanStartDragForView(View* sender, 186 const gfx::Point& press_pt, 187 const gfx::Point& p) OVERRIDE; 188 189 // Overridden from views::ResizeAreaDelegate: 190 virtual void OnResize(int resize_amount, bool done_resizing) OVERRIDE; 191 192 // Overridden from ui::AnimationDelegate: 193 virtual void AnimationProgressed(const ui::Animation* animation) OVERRIDE; 194 virtual void AnimationEnded(const ui::Animation* animation) OVERRIDE; 195 196 // Overridden from BrowserActionOverflowMenuController::Observer: 197 virtual void NotifyMenuDeleted( 198 BrowserActionOverflowMenuController* controller) OVERRIDE; 199 200 // Overridden from views::WidgetObserver: 201 virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE; 202 203 // Overridden from BrowserActionView::Delegate: 204 virtual void InspectPopup(ExtensionAction* action) OVERRIDE; 205 virtual int GetCurrentTabId() const OVERRIDE; 206 virtual void OnBrowserActionExecuted(BrowserActionButton* button) OVERRIDE; 207 virtual void OnBrowserActionVisibilityChanged() OVERRIDE; 208 virtual gfx::Point GetViewContentOffset() const OVERRIDE; 209 210 // Overridden from extension::ExtensionKeybindingRegistry::Delegate: 211 virtual extensions::ActiveTabPermissionGranter* 212 GetActiveTabPermissionGranter() OVERRIDE; 213 214 // Moves a browser action with |id| to |new_index|. 215 void MoveBrowserAction(const std::string& extension_id, size_t new_index); 216 217 // Hide the current popup. 218 void HidePopup(); 219 220 // Simulate a click on a browser action button. This should only be 221 // used by unit tests. 222 void TestExecuteBrowserAction(int index); 223 224 // Retrieve the current popup. This should only be used by unit tests. 225 ExtensionPopup* TestGetPopup() { return popup_; } 226 227 // Set how many icons the container should show. This should only be used by 228 // unit tests. 229 void TestSetIconVisibilityCount(size_t icons); 230 231 // During testing we can disable animations by setting this flag to true, 232 // so that the bar resizes instantly, instead of having to poll it while it 233 // animates to open/closed status. 234 static bool disable_animations_during_testing_; 235 236 protected: 237 // Overridden from views::View: 238 virtual void ViewHierarchyChanged( 239 const ViewHierarchyChangedDetails& details) OVERRIDE; 240 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; 241 virtual void OnThemeChanged() OVERRIDE; 242 243 private: 244 friend class BrowserActionView; // So it can access IconHeight(). 245 friend class ShowFolderMenuTask; 246 247 typedef std::vector<BrowserActionView*> BrowserActionViews; 248 249 // Returns the width of an icon, optionally with its padding. 250 static int IconWidth(bool include_padding); 251 252 // Returns the height of an icon. 253 static int IconHeight(); 254 255 // ExtensionToolbarModel::Observer implementation. 256 virtual void BrowserActionAdded(const extensions::Extension* extension, 257 int index) OVERRIDE; 258 virtual void BrowserActionRemoved( 259 const extensions::Extension* extension) OVERRIDE; 260 virtual void BrowserActionMoved(const extensions::Extension* extension, 261 int index) OVERRIDE; 262 virtual void ModelLoaded() OVERRIDE; 263 264 void LoadImages(); 265 266 // Sets the initial container width. 267 void SetContainerWidth(); 268 269 // Closes the overflow menu if open. 270 void CloseOverflowMenu(); 271 272 // Cancels the timer for showing the drop down menu. 273 void StopShowFolderDropMenuTimer(); 274 275 // Show the drop down folder after a slight delay. 276 void StartShowFolderDropMenuTimer(); 277 278 // Show the overflow menu. 279 void ShowDropFolder(); 280 281 // Sets the drop indicator position (and schedules paint if the position has 282 // changed). 283 void SetDropIndicator(int x_pos); 284 285 // Given a number of |icons| and whether to |display_chevron|, returns the 286 // amount of pixels needed to draw the entire container. For convenience, 287 // callers can set |icons| to -1 to mean "all icons". 288 int IconCountToWidth(int icons, bool display_chevron) const; 289 290 // Given a pixel width, returns the number of icons that fit. (This 291 // automatically determines whether a chevron will be needed and includes it 292 // in the calculation.) 293 size_t WidthToIconCount(int pixels) const; 294 295 // Returns the absolute minimum size you can shrink the container down to and 296 // still show it. This assumes a visible chevron because the only way we 297 // would not have a chevron when shrinking down this far is if there were no 298 // icons, in which case the container wouldn't be shown at all. 299 int ContainerMinSize() const; 300 301 // Animate to the target size (unless testing, in which case we go straight to 302 // the target size). This also saves the target number of visible icons in 303 // the pref if we're not incognito. 304 void SaveDesiredSizeAndAnimate(ui::Tween::Type type, 305 size_t num_visible_icons); 306 307 // Returns true if this extension should be shown in this toolbar. This can 308 // return false if we are in an incognito window and the extension is disabled 309 // for incognito. 310 bool ShouldDisplayBrowserAction(const extensions::Extension* extension); 311 312 // Show a popup. 313 void ShowPopup(BrowserActionButton* button, 314 ExtensionPopup::ShowAction show_action); 315 316 // The vector of browser actions (icons/image buttons for each action). Note 317 // that not every BrowserAction in the ToolbarModel will necessarily be in 318 // this collection. Some extensions may be disabled in incognito windows. 319 BrowserActionViews browser_action_views_; 320 321 Profile* profile_; 322 323 // The Browser object the container is associated with. 324 Browser* browser_; 325 326 // The view that owns us. 327 views::View* owner_view_; 328 329 // The current popup and the button it came from. NULL if no popup. 330 ExtensionPopup* popup_; 331 332 // The button that triggered the current popup (just a reference to a button 333 // from browser_action_views_). 334 BrowserActionButton* popup_button_; 335 336 // The model that tracks the order of the toolbar icons. 337 ExtensionToolbarModel* model_; 338 339 // The current width of the container. 340 int container_width_; 341 342 // The resize area for the container. 343 views::ResizeArea* resize_area_; 344 345 // The chevron for accessing the overflow items. 346 views::MenuButton* chevron_; 347 348 // The menu to show for the overflow button (chevron). This class manages its 349 // own lifetime so that it can stay alive during drag and drop operations. 350 BrowserActionOverflowMenuController* overflow_menu_; 351 352 // The animation that happens when the container snaps to place. 353 scoped_ptr<ui::SlideAnimation> resize_animation_; 354 355 // Don't show the chevron while animating. 356 bool suppress_chevron_; 357 358 // This is used while the user is resizing (and when the animations are in 359 // progress) to know how wide the delta is between the current state and what 360 // we should draw. 361 int resize_amount_; 362 363 // Keeps track of the absolute pixel width the container should have when we 364 // are done animating. 365 int animation_target_size_; 366 367 // The x position for where to draw the drop indicator. -1 if no indicator. 368 int drop_indicator_position_; 369 370 // The class that registers for keyboard shortcuts for extension commands. 371 scoped_ptr<ExtensionKeybindingRegistryViews> extension_keybinding_registry_; 372 373 base::WeakPtrFactory<BrowserActionsContainer> task_factory_; 374 375 // Handles delayed showing of the overflow menu when hovering. 376 base::WeakPtrFactory<BrowserActionsContainer> show_menu_task_factory_; 377 378 DISALLOW_COPY_AND_ASSIGN(BrowserActionsContainer); 379 }; 380 381 #endif // CHROME_BROWSER_UI_VIEWS_BROWSER_ACTIONS_CONTAINER_H_ 382