bookmark_bar_view.h revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Copyright (c) 2012 The Chromium Authors. All rights reserved.
247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Use of this source code is governed by a BSD-style license that can be
347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// found in the LICENSE file.
447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#ifndef CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_BAR_VIEW_H_
647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#define CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_BAR_VIEW_H_
747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include <set>
947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include <string>
1047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "base/basictypes.h"
1247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "base/compiler_specific.h"
1347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "base/gtest_prod_util.h"
14d505b21170e209f07dea4ff52bcbecdcf03b7ed4henrike@webrtc.org#include "base/memory/weak_ptr.h"
15d505b21170e209f07dea4ff52bcbecdcf03b7ed4henrike@webrtc.org#include "base/prefs/pref_change_registrar.h"
16d505b21170e209f07dea4ff52bcbecdcf03b7ed4henrike@webrtc.org#include "chrome/browser/bookmarks/bookmark_model_observer.h"
17d505b21170e209f07dea4ff52bcbecdcf03b7ed4henrike@webrtc.org#include "chrome/browser/bookmarks/bookmark_node_data.h"
1847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "chrome/browser/bookmarks/bookmark_stats.h"
1947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "chrome/browser/ui/bookmarks/bookmark_bar.h"
2047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "chrome/browser/ui/bookmarks/bookmark_bar_instructions_delegate.h"
2147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "chrome/browser/ui/views/bookmarks/bookmark_bubble_view_observer.h"
2247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "chrome/browser/ui/views/bookmarks/bookmark_menu_controller_observer.h"
2347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "chrome/browser/ui/views/detachable_toolbar_view.h"
2447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "ui/gfx/animation/animation_delegate.h"
2547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "ui/views/context_menu_controller.h"
2647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "ui/views/controls/button/button.h"
2747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "ui/views/controls/button/menu_button_listener.h"
2847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "ui/views/controls/menu/menu_item_view.h"
29d505b21170e209f07dea4ff52bcbecdcf03b7ed4henrike@webrtc.org#include "ui/views/drag_controller.h"
3047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
3147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgclass BookmarkContextMenu;
3247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgclass Browser;
3347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgclass BrowserView;
3447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
3547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgnamespace content {
3647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgclass PageNavigator;
3747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
3847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
39d505b21170e209f07dea4ff52bcbecdcf03b7ed4henrike@webrtc.orgnamespace gfx {
4047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgclass SlideAnimation;
4147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
4247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
4347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgnamespace views {
4447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgclass CustomButton;
4547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgclass MenuButton;
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 gfx::AnimationDelegate,
63                        public BookmarkMenuControllerObserver,
64                        public 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(views::MenuButton* button,
122                                  views::MenuItemView::AnchorPosition* anchor);
123
124  // Returns the button responsible for showing bookmarks in the other bookmark
125  // folder.
126  views::MenuButton* other_bookmarked_button() const {
127    return other_bookmarked_button_;
128  }
129
130  // Returns the button used when not all the items on the bookmark bar fit.
131  views::MenuButton* overflow_button() const { return overflow_button_; }
132
133  // Returns the active MenuItemView, or NULL if a menu isn't showing.
134  views::MenuItemView* GetMenu();
135
136  // Returns the context menu, or null if one isn't showing.
137  views::MenuItemView* GetContextMenu();
138
139  // Returns the drop MenuItemView, or NULL if a menu isn't showing.
140  views::MenuItemView* GetDropMenu();
141
142  // If a button is currently throbbing, it is stopped. If immediate is true
143  // the throb stops immediately, otherwise it stops after a couple more
144  // throbs.
145  void StopThrobbing(bool immediate);
146
147  // Returns the tooltip text for the specified url and title. The returned
148  // text is clipped to fit within the bounds of the monitor. |context| is
149  // used to determine which gfx::Screen is used to retrieve bounds.
150  //
151  // Note that we adjust the direction of both the URL and the title based on
152  // the locale so that pure LTR strings are displayed properly in RTL locales.
153  static string16 CreateToolTipForURLAndTitle(const views::Widget* widget,
154                                              const gfx::Point& screen_loc,
155                                              const GURL& url,
156                                              const string16& title,
157                                              Profile* profile);
158
159  // DetachableToolbarView methods:
160  virtual bool IsDetached() const OVERRIDE;
161  virtual double GetAnimationValue() const OVERRIDE;
162  virtual int GetToolbarOverlap() const OVERRIDE;
163
164  // View methods:
165  virtual gfx::Size GetPreferredSize() OVERRIDE;
166  virtual gfx::Size GetMinimumSize() OVERRIDE;
167  virtual bool HitTestRect(const gfx::Rect& rect) const OVERRIDE;
168  virtual void Layout() OVERRIDE;
169  virtual void ViewHierarchyChanged(
170      const ViewHierarchyChangedDetails& details) OVERRIDE;
171  virtual void PaintChildren(gfx::Canvas* canvas) OVERRIDE;
172  virtual bool GetDropFormats(
173      int* formats,
174      std::set<ui::OSExchangeData::CustomFormat>* custom_formats) OVERRIDE;
175  virtual bool AreDropTypesRequired() OVERRIDE;
176  virtual bool CanDrop(const ui::OSExchangeData& data) OVERRIDE;
177  virtual void OnDragEntered(const ui::DropTargetEvent& event) OVERRIDE;
178  virtual int OnDragUpdated(const ui::DropTargetEvent& event) OVERRIDE;
179  virtual void OnDragExited() OVERRIDE;
180  virtual int OnPerformDrop(const ui::DropTargetEvent& event) OVERRIDE;
181  virtual void OnThemeChanged() OVERRIDE;
182  virtual const char* GetClassName() const OVERRIDE;
183
184  // AccessiblePaneView:
185  virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
186
187  // gfx::AnimationDelegate:
188  virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE;
189  virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE;
190
191  // BookmarkMenuControllerObserver:
192  virtual void BookmarkMenuControllerDeleted(
193      BookmarkMenuController* controller) OVERRIDE;
194
195  // BookmarkBarInstructionsDelegate:
196  virtual void ShowImportDialog() OVERRIDE;
197
198  // BookmarkBubbleViewObserver:
199  virtual void OnBookmarkBubbleShown(const GURL& url) OVERRIDE;
200  virtual void OnBookmarkBubbleHidden() OVERRIDE;
201
202  // BookmarkModelObserver:
203  virtual void Loaded(BookmarkModel* model, bool ids_reassigned) OVERRIDE;
204  virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE;
205  virtual void BookmarkNodeMoved(BookmarkModel* model,
206                                 const BookmarkNode* old_parent,
207                                 int old_index,
208                                 const BookmarkNode* new_parent,
209                                 int new_index) OVERRIDE;
210  virtual void BookmarkNodeAdded(BookmarkModel* model,
211                                 const BookmarkNode* parent,
212                                 int index) OVERRIDE;
213  virtual void BookmarkNodeRemoved(BookmarkModel* model,
214                                   const BookmarkNode* parent,
215                                   int old_index,
216                                   const BookmarkNode* node) OVERRIDE;
217  virtual void BookmarkAllNodesRemoved(BookmarkModel* model) OVERRIDE;
218  virtual void BookmarkNodeChanged(BookmarkModel* model,
219                                   const BookmarkNode* node) OVERRIDE;
220  virtual void BookmarkNodeChildrenReordered(BookmarkModel* model,
221                                             const BookmarkNode* node) OVERRIDE;
222  virtual void BookmarkNodeFaviconChanged(BookmarkModel* model,
223                                          const BookmarkNode* node) OVERRIDE;
224
225  // views::DragController:
226  virtual void WriteDragDataForView(views::View* sender,
227                                    const gfx::Point& press_pt,
228                                    ui::OSExchangeData* data) OVERRIDE;
229  virtual int GetDragOperationsForView(views::View* sender,
230                                       const gfx::Point& p) OVERRIDE;
231  virtual bool CanStartDragForView(views::View* sender,
232                                   const gfx::Point& press_pt,
233                                   const gfx::Point& p) OVERRIDE;
234
235  // views::MenuButtonListener:
236  virtual void OnMenuButtonClicked(views::View* view,
237                                   const gfx::Point& point) OVERRIDE;
238
239  // views::ButtonListener:
240  virtual void ButtonPressed(views::Button* sender,
241                             const ui::Event& event) OVERRIDE;
242
243  // views::ContextMenuController:
244  virtual void ShowContextMenuForView(views::View* source,
245                                      const gfx::Point& point,
246                                      ui::MenuSourceType source_type) OVERRIDE;
247
248 private:
249  class ButtonSeparatorView;
250  struct DropInfo;
251  struct DropLocation;
252
253  friend class BookmarkBarViewEventTestBase;
254  FRIEND_TEST_ALL_PREFIXES(BookmarkBarViewTest, SwitchProfile);
255  FRIEND_TEST_ALL_PREFIXES(BookmarkBarViewTest,
256                           NoAppsShortcutWithoutInstantExtended);
257  FRIEND_TEST_ALL_PREFIXES(BookmarkBarViewInstantExtendedTest,
258                           AppsShortcutVisibility);
259
260  // Used to identify what the user is dropping onto.
261  enum DropButtonType {
262    DROP_BOOKMARK,
263    DROP_OTHER_FOLDER,
264    DROP_OVERFLOW
265  };
266
267  // Creates recent bookmark button and when visible button as well as
268  // calculating the preferred height.
269  void Init();
270
271  // NOTE: unless otherwise stated all methods that take an int for an index are
272  // in terms of the bookmark bar view. Typically the view index and model index
273  // are the same, but they may differ during animations or drag and drop.
274  //
275  // It's easy to get the mapping wrong. For this reason all these methods are
276  // private.
277
278  // Returns the number of buttons corresponding to starred urls/folders. This
279  // is equivalent to the number of children the bookmark bar node from the
280  // bookmark bar model has.
281  int GetBookmarkButtonCount();
282
283  // Returns the button at the specified index.
284  views::TextButton* GetBookmarkButton(int index);
285
286  // Returns BOOKMARK_LAUNCH_LOCATION_DETACHED_BAR or
287  // BOOKMARK_LAUNCH_LOCATION_ATTACHED_BAR based on detached state.
288  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<gfx::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