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