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_BOOKMARKS_BOOKMARK_BAR_VIEW_H_ 6 #define CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_BAR_VIEW_H_ 7 8 #include <set> 9 10 #include "base/basictypes.h" 11 #include "base/compiler_specific.h" 12 #include "base/gtest_prod_util.h" 13 #include "base/memory/weak_ptr.h" 14 #include "base/prefs/pref_change_registrar.h" 15 #include "chrome/browser/bookmarks/bookmark_stats.h" 16 #include "chrome/browser/ui/bookmarks/bookmark_bar.h" 17 #include "chrome/browser/ui/bookmarks/bookmark_bar_instructions_delegate.h" 18 #include "chrome/browser/ui/views/bookmarks/bookmark_bubble_view_observer.h" 19 #include "chrome/browser/ui/views/bookmarks/bookmark_menu_controller_observer.h" 20 #include "chrome/browser/ui/views/detachable_toolbar_view.h" 21 #include "components/bookmarks/browser/bookmark_model_observer.h" 22 #include "components/bookmarks/browser/bookmark_node_data.h" 23 #include "ui/gfx/animation/animation_delegate.h" 24 #include "ui/views/context_menu_controller.h" 25 #include "ui/views/controls/button/button.h" 26 #include "ui/views/controls/button/menu_button_listener.h" 27 #include "ui/views/controls/menu/menu_types.h" 28 #include "ui/views/drag_controller.h" 29 30 class BookmarkContextMenu; 31 class BookmarkModel; 32 class Browser; 33 class BrowserView; 34 class ChromeBookmarkClient; 35 class Profile; 36 37 namespace content { 38 class PageNavigator; 39 } 40 41 namespace gfx { 42 class SlideAnimation; 43 } 44 45 namespace views { 46 class CustomButton; 47 class MenuButton; 48 class MenuItemView; 49 class LabelButton; 50 } 51 52 // BookmarkBarView renders the BookmarkModel. Each starred entry on the 53 // BookmarkBar is rendered as a MenuButton. An additional MenuButton aligned to 54 // the right allows the user to quickly see recently starred entries. 55 // 56 // BookmarkBarView shows the bookmarks from a specific Profile. BookmarkBarView 57 // waits until the HistoryService for the profile has been loaded before 58 // creating the BookmarkModel. 59 class BookmarkBarView : public DetachableToolbarView, 60 public BookmarkModelObserver, 61 public views::MenuButtonListener, 62 public views::ButtonListener, 63 public views::ContextMenuController, 64 public views::DragController, 65 public gfx::AnimationDelegate, 66 public BookmarkMenuControllerObserver, 67 public BookmarkBarInstructionsDelegate, 68 public BookmarkBubbleViewObserver { 69 public: 70 // The internal view class name. 71 static const char kViewClassName[]; 72 73 // Constant used in Browser View, as well as here. 74 // How inset the bookmarks bar is when displayed on the new tab page. 75 static const int kNewtabHorizontalPadding; 76 77 // Maximum size of buttons on the bookmark bar. 78 static const int kMaxButtonWidth; 79 80 // Number of pixels the attached bookmark bar overlaps with the toolbar. 81 static const int kToolbarAttachedBookmarkBarOverlap; 82 83 // |browser_view| can be NULL during tests. 84 BookmarkBarView(Browser* browser, BrowserView* browser_view); 85 virtual ~BookmarkBarView(); 86 87 static void DisableAnimationsForTesting(bool disabled); 88 89 // Returns the current browser. 90 Browser* browser() const { return browser_; } 91 92 // Sets the PageNavigator that is used when the user selects an entry on 93 // the bookmark bar. 94 void SetPageNavigator(content::PageNavigator* navigator); 95 96 // Sets whether the containing browser is showing an infobar. This affects 97 // layout during animation. 98 void set_infobar_visible(bool infobar_visible) { 99 infobar_visible_ = infobar_visible; 100 } 101 102 // Changes the state of the bookmark bar. 103 void SetBookmarkBarState(BookmarkBar::State state, 104 BookmarkBar::AnimateChangeType animate_type); 105 106 // Returns the toolbar overlap when fully detached. 107 int GetFullyDetachedToolbarOverlap() const; 108 109 // Whether or not we are animating. 110 bool is_animating(); 111 112 // If |loc| is over a bookmark button the node is returned corresponding to 113 // the button and |model_start_index| is set to 0. If a overflow button is 114 // showing and |loc| is over the overflow button, the bookmark bar node is 115 // returned and |model_start_index| is set to the index of the first node 116 // contained in the overflow menu. 117 const BookmarkNode* GetNodeForButtonAtModelIndex(const gfx::Point& loc, 118 int* model_start_index); 119 120 // Returns the MenuButton for node. 121 views::MenuButton* GetMenuButtonForNode(const BookmarkNode* node); 122 123 // Returns the position to anchor the menu for |button| at. 124 void GetAnchorPositionForButton(views::MenuButton* button, 125 views::MenuAnchorPosition* anchor); 126 127 // Returns the button responsible for showing bookmarks in the other bookmark 128 // folder. 129 views::MenuButton* other_bookmarked_button() const { 130 return other_bookmarked_button_; 131 } 132 133 // Returns the button used when not all the items on the bookmark bar fit. 134 views::MenuButton* overflow_button() const { return overflow_button_; } 135 136 // Returns the active MenuItemView, or NULL if a menu isn't showing. 137 views::MenuItemView* GetMenu(); 138 139 // Returns the context menu, or null if one isn't showing. 140 views::MenuItemView* GetContextMenu(); 141 142 // Returns the drop MenuItemView, or NULL if a menu isn't showing. 143 views::MenuItemView* GetDropMenu(); 144 145 // If a button is currently throbbing, it is stopped. If immediate is true 146 // the throb stops immediately, otherwise it stops after a couple more 147 // throbs. 148 void StopThrobbing(bool immediate); 149 150 // Returns the tooltip text for the specified url and title. The returned 151 // text is clipped to fit within the bounds of the monitor. |context| is 152 // used to determine which gfx::Screen is used to retrieve bounds. 153 // 154 // Note that we adjust the direction of both the URL and the title based on 155 // the locale so that pure LTR strings are displayed properly in RTL locales. 156 static base::string16 CreateToolTipForURLAndTitle(const views::Widget* widget, 157 const gfx::Point& screen_loc, 158 const GURL& url, 159 const base::string16& title, 160 Profile* profile); 161 162 // DetachableToolbarView methods: 163 virtual bool IsDetached() const OVERRIDE; 164 virtual double GetAnimationValue() const OVERRIDE; 165 virtual int GetToolbarOverlap() const OVERRIDE; 166 167 // View methods: 168 virtual gfx::Size GetPreferredSize() const OVERRIDE; 169 virtual gfx::Size GetMinimumSize() const OVERRIDE; 170 virtual bool CanProcessEventsWithinSubtree() const OVERRIDE; 171 virtual void Layout() OVERRIDE; 172 virtual void ViewHierarchyChanged( 173 const ViewHierarchyChangedDetails& details) OVERRIDE; 174 virtual void PaintChildren(gfx::Canvas* canvas, 175 const views::CullSet& cull_set) OVERRIDE; 176 virtual bool GetDropFormats( 177 int* formats, 178 std::set<ui::OSExchangeData::CustomFormat>* custom_formats) OVERRIDE; 179 virtual bool AreDropTypesRequired() OVERRIDE; 180 virtual bool CanDrop(const ui::OSExchangeData& data) OVERRIDE; 181 virtual void OnDragEntered(const ui::DropTargetEvent& event) OVERRIDE; 182 virtual int OnDragUpdated(const ui::DropTargetEvent& event) OVERRIDE; 183 virtual void OnDragExited() OVERRIDE; 184 virtual int OnPerformDrop(const ui::DropTargetEvent& event) OVERRIDE; 185 virtual void OnThemeChanged() OVERRIDE; 186 virtual const char* GetClassName() const OVERRIDE; 187 188 // AccessiblePaneView: 189 virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE; 190 191 // gfx::AnimationDelegate: 192 virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE; 193 virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE; 194 195 // BookmarkMenuControllerObserver: 196 virtual void BookmarkMenuControllerDeleted( 197 BookmarkMenuController* controller) OVERRIDE; 198 199 // BookmarkBarInstructionsDelegate: 200 virtual void ShowImportDialog() OVERRIDE; 201 202 // BookmarkBubbleViewObserver: 203 virtual void OnBookmarkBubbleShown(const GURL& url) OVERRIDE; 204 virtual void OnBookmarkBubbleHidden() OVERRIDE; 205 206 // BookmarkModelObserver: 207 virtual void BookmarkModelLoaded(BookmarkModel* model, 208 bool ids_reassigned) OVERRIDE; 209 virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE; 210 virtual void BookmarkNodeMoved(BookmarkModel* model, 211 const BookmarkNode* old_parent, 212 int old_index, 213 const BookmarkNode* new_parent, 214 int new_index) OVERRIDE; 215 virtual void BookmarkNodeAdded(BookmarkModel* model, 216 const BookmarkNode* parent, 217 int index) OVERRIDE; 218 virtual void BookmarkNodeRemoved(BookmarkModel* model, 219 const BookmarkNode* parent, 220 int old_index, 221 const BookmarkNode* node, 222 const std::set<GURL>& removed_urls) OVERRIDE; 223 virtual void BookmarkAllUserNodesRemoved( 224 BookmarkModel* model, 225 const std::set<GURL>& removed_urls) OVERRIDE; 226 virtual void BookmarkNodeChanged(BookmarkModel* model, 227 const BookmarkNode* node) OVERRIDE; 228 virtual void BookmarkNodeChildrenReordered(BookmarkModel* model, 229 const BookmarkNode* node) OVERRIDE; 230 virtual void BookmarkNodeFaviconChanged(BookmarkModel* model, 231 const BookmarkNode* node) OVERRIDE; 232 233 // views::DragController: 234 virtual void WriteDragDataForView(views::View* sender, 235 const gfx::Point& press_pt, 236 ui::OSExchangeData* data) OVERRIDE; 237 virtual int GetDragOperationsForView(views::View* sender, 238 const gfx::Point& p) OVERRIDE; 239 virtual bool CanStartDragForView(views::View* sender, 240 const gfx::Point& press_pt, 241 const gfx::Point& p) OVERRIDE; 242 243 // views::MenuButtonListener: 244 virtual void OnMenuButtonClicked(views::View* view, 245 const gfx::Point& point) OVERRIDE; 246 247 // views::ButtonListener: 248 virtual void ButtonPressed(views::Button* sender, 249 const ui::Event& event) OVERRIDE; 250 251 // views::ContextMenuController: 252 virtual void ShowContextMenuForView(views::View* source, 253 const gfx::Point& point, 254 ui::MenuSourceType source_type) OVERRIDE; 255 256 private: 257 class ButtonSeparatorView; 258 struct DropInfo; 259 struct DropLocation; 260 261 friend class BookmarkBarViewEventTestBase; 262 FRIEND_TEST_ALL_PREFIXES(BookmarkBarViewTest, SwitchProfile); 263 FRIEND_TEST_ALL_PREFIXES(BookmarkBarViewTest, 264 ManagedShowAppsShortcutInBookmarksBar); 265 FRIEND_TEST_ALL_PREFIXES(BookmarkBarViewInstantExtendedTest, 266 AppsShortcutVisibility); 267 268 // Used to identify what the user is dropping onto. 269 enum DropButtonType { 270 DROP_BOOKMARK, 271 DROP_OTHER_FOLDER, 272 DROP_OVERFLOW 273 }; 274 275 // Creates recent bookmark button and when visible button as well as 276 // calculating the preferred height. 277 void Init(); 278 279 // NOTE: unless otherwise stated all methods that take an int for an index are 280 // in terms of the bookmark bar view. Typically the view index and model index 281 // are the same, but they may differ during animations or drag and drop. 282 // 283 // It's easy to get the mapping wrong. For this reason all these methods are 284 // private. 285 286 // Returns the number of buttons corresponding to starred urls/folders. This 287 // is equivalent to the number of children the bookmark bar node from the 288 // bookmark bar model has. 289 int GetBookmarkButtonCount() const; 290 291 // Returns the button at the specified index. 292 views::LabelButton* GetBookmarkButton(int index); 293 294 // Returns BOOKMARK_LAUNCH_LOCATION_DETACHED_BAR or 295 // BOOKMARK_LAUNCH_LOCATION_ATTACHED_BAR based on detached state. 296 BookmarkLaunchLocation GetBookmarkLaunchLocation() const; 297 298 // Returns the index of the first hidden bookmark button. If all buttons are 299 // visible, this returns GetBookmarkButtonCount(). 300 int GetFirstHiddenNodeIndex(); 301 302 // Creates the button showing the other bookmarked items. 303 views::MenuButton* CreateOtherBookmarkedButton(); 304 305 // Creates the button showing the managed bookmarks items. 306 views::MenuButton* CreateManagedBookmarksButton(); 307 308 // Creates the button used when not all bookmark buttons fit. 309 views::MenuButton* CreateOverflowButton(); 310 311 // Creates the button for rendering the specified bookmark node. 312 views::View* CreateBookmarkButton(const BookmarkNode* node); 313 314 // Creates the button for rendering the apps page shortcut. 315 views::LabelButton* CreateAppsPageShortcutButton(); 316 317 // Configures the button from the specified node. This sets the text, 318 // and icon. 319 void ConfigureButton(const BookmarkNode* node, views::LabelButton* button); 320 321 // Implementation for BookmarkNodeAddedImpl. 322 void BookmarkNodeAddedImpl(BookmarkModel* model, 323 const BookmarkNode* parent, 324 int index); 325 326 // Implementation for BookmarkNodeRemoved. 327 void BookmarkNodeRemovedImpl(BookmarkModel* model, 328 const BookmarkNode* parent, 329 int index); 330 331 // If the node is a child of the root node, the button is updated 332 // appropriately. 333 void BookmarkNodeChangedImpl(BookmarkModel* model, const BookmarkNode* node); 334 335 // Shows the menu used during drag and drop for the specified node. 336 void ShowDropFolderForNode(const BookmarkNode* node); 337 338 // Cancels the timer used to show a drop menu. 339 void StopShowFolderDropMenuTimer(); 340 341 // Stars the timer used to show a drop menu for node. 342 void StartShowFolderDropMenuTimer(const BookmarkNode* node); 343 344 // Calculates the location for the drop in |location|. 345 void CalculateDropLocation(const ui::DropTargetEvent& event, 346 const BookmarkNodeData& data, 347 DropLocation* location); 348 349 // Writes a BookmarkNodeData for node to data. 350 void WriteBookmarkDragData(const BookmarkNode* node, 351 ui::OSExchangeData* data); 352 353 // This determines which view should throb and starts it 354 // throbbing (e.g when the bookmark bubble is showing). 355 // If |overflow_only| is true, start throbbing only if |node| is hidden in 356 // the overflow menu. 357 void StartThrobbing(const BookmarkNode* node, bool overflow_only); 358 359 // Returns the view to throb when a node is removed. |parent| is the parent of 360 // the node that was removed, and |old_index| the index of the node that was 361 // removed. 362 views::CustomButton* DetermineViewToThrobFromRemove( 363 const BookmarkNode* parent, 364 int old_index); 365 366 // Updates the colors for all the child objects in the bookmarks bar. 367 void UpdateColors(); 368 369 // Updates the visibility of |other_bookmarked_button_| and 370 // |managed_bookmarks_button_|. Also shows or hides the separator if required. 371 void UpdateButtonsVisibility(); 372 373 // Updates the visibility of |bookmarks_separator_view_|. 374 void UpdateBookmarksSeparatorVisibility(); 375 376 // This method computes the bounds for the bookmark bar items. 377 void LayoutItems(); 378 379 // Updates the visibility of the apps shortcut based on the pref value. 380 void OnAppsPageShortcutVisibilityPrefChanged(); 381 382 // Needed to react to kShowAppsShortcutInBookmarkBar changes. 383 PrefChangeRegistrar profile_pref_registrar_; 384 385 // Used for opening urls. 386 content::PageNavigator* page_navigator_; 387 388 // BookmarkModel that owns the entries and folders that are shown in this 389 // view. This is owned by the Profile. 390 BookmarkModel* model_; 391 392 // ChromeBookmarkClient. This is owned by the Profile. 393 ChromeBookmarkClient* client_; 394 395 // Used to manage showing a Menu, either for the most recently bookmarked 396 // entries, or for the starred folder. 397 BookmarkMenuController* bookmark_menu_; 398 399 // Used when showing a menu for drag and drop. That is, if the user drags 400 // over a folder this becomes non-null and manages the menu showing the 401 // contents of the node. 402 BookmarkMenuController* bookmark_drop_menu_; 403 404 // If non-NULL we're showing a context menu for one of the items on the 405 // bookmark bar. 406 scoped_ptr<BookmarkContextMenu> context_menu_; 407 408 // Shows the other bookmark entries. 409 views::MenuButton* other_bookmarked_button_; 410 411 // Shows the managed bookmarks entries. 412 views::MenuButton* managed_bookmarks_button_; 413 414 // Shows the Apps page shortcut. 415 views::LabelButton* apps_page_shortcut_; 416 417 // Task used to delay showing of the drop menu. 418 base::WeakPtrFactory<BookmarkBarView> show_folder_method_factory_; 419 420 // Used to track drops on the bookmark bar view. 421 scoped_ptr<DropInfo> drop_info_; 422 423 // Visible if not all the bookmark buttons fit. 424 views::MenuButton* overflow_button_; 425 426 // Shows a text and a link to import bookmarks if there are no bookmarks in 427 // the Bookmarks Bar. 428 views::View* instructions_; 429 430 ButtonSeparatorView* bookmarks_separator_view_; 431 432 Browser* browser_; 433 BrowserView* browser_view_; 434 435 // True if the owning browser is showing an infobar. 436 bool infobar_visible_; 437 438 // Animation controlling showing and hiding of the bar. 439 scoped_ptr<gfx::SlideAnimation> size_animation_; 440 441 // If the bookmark bubble is showing, this is the visible ancestor of the URL. 442 // The visible ancestor is either the other_bookmarked_button_, 443 // overflow_button_ or a button on the bar. 444 views::CustomButton* throbbing_view_; 445 446 BookmarkBar::State bookmark_bar_state_; 447 448 // Are we animating to or from the detached state? 449 bool animating_detached_; 450 451 DISALLOW_COPY_AND_ASSIGN(BookmarkBarView); 452 }; 453 454 #endif // CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_BAR_VIEW_H_ 455