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