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.
5 #ifndef CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_BAR_VIEW_H_
6 #define CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_BAR_VIEW_H_
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/core/browser/bookmark_model_observer.h"
23 #include "components/bookmarks/core/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"
31 class BookmarkContextMenu;
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.
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.
58 class 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 {
69 // The internal view class name.
70 static const char kViewClassName[];
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;
76 // Maximum size of buttons on the bookmark bar.
77 static const int kMaxButtonWidth;
79 // Number of pixels the attached bookmark bar overlaps with the toolbar.
80 static const int kToolbarAttachedBookmarkBarOverlap;
82 // |browser_view| can be NULL during tests.
83 BookmarkBarView(Browser* browser, BrowserView* browser_view);
84 virtual ~BookmarkBarView();
86 static void DisableAnimationsForTesting(bool disabled);
88 // Returns the current browser.
89 Browser* browser() const { return browser_; }
91 // Sets the PageNavigator that is used when the user selects an entry on
93 void SetPageNavigator(content::PageNavigator* navigator);
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;
101 // Changes the state of the bookmark bar.
102 void SetBookmarkBarState(BookmarkBar::State state,
103 BookmarkBar::AnimateChangeType animate_type);
105 // Returns the toolbar overlap when fully detached.
106 int GetFullyDetachedToolbarOverlap() const;
108 // Whether or not we are animating.
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);
119 // Returns the MenuButton for node.
120 views::MenuButton* GetMenuButtonForNode(const BookmarkNode* node);
122 // Returns the position to anchor the menu for |button| at.
123 void GetAnchorPositionForButton(views::MenuButton* button,
124 views::MenuAnchorPosition* anchor);
126 // Returns the button responsible for showing bookmarks in the other bookmark
128 views::MenuButton* other_bookmarked_button() const {
129 return other_bookmarked_button_;
132 // Returns the button used when not all the items on the bookmark bar fit.
133 views::MenuButton* overflow_button() const { return overflow_button_; }
135 // Returns the active MenuItemView, or NULL if a menu isn't showing.
136 views::MenuItemView* GetMenu();
138 // Returns the context menu, or null if one isn't showing.
139 views::MenuItemView* GetContextMenu();
141 // Returns the drop MenuItemView, or NULL if a menu isn't showing.
142 views::MenuItemView* GetDropMenu();
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
147 void StopThrobbing(bool immediate);
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.
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,
158 const base::string16& title,
161 // DetachableToolbarView methods:
162 virtual bool IsDetached() const OVERRIDE;
163 virtual double GetAnimationValue() const OVERRIDE;
164 virtual int GetToolbarOverlap() const OVERRIDE;
167 virtual gfx::Size GetPreferredSize() OVERRIDE;
168 virtual gfx::Size GetMinimumSize() OVERRIDE;
169 virtual bool HitTestRect(const gfx::Rect& rect) const OVERRIDE;
170 virtual void Layout() OVERRIDE;
171 virtual void ViewHierarchyChanged(
172 const ViewHierarchyChangedDetails& details) OVERRIDE;
173 virtual void PaintChildren(gfx::Canvas* canvas) OVERRIDE;
174 virtual bool GetDropFormats(
176 std::set<ui::OSExchangeData::CustomFormat>* custom_formats) OVERRIDE;
177 virtual bool AreDropTypesRequired() OVERRIDE;
178 virtual bool CanDrop(const ui::OSExchangeData& data) OVERRIDE;
179 virtual void OnDragEntered(const ui::DropTargetEvent& event) OVERRIDE;
180 virtual int OnDragUpdated(const ui::DropTargetEvent& event) OVERRIDE;
181 virtual void OnDragExited() OVERRIDE;
182 virtual int OnPerformDrop(const ui::DropTargetEvent& event) OVERRIDE;
183 virtual void OnThemeChanged() OVERRIDE;
184 virtual const char* GetClassName() const OVERRIDE;
186 // AccessiblePaneView:
187 virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE;
189 // gfx::AnimationDelegate:
190 virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE;
191 virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE;
193 // BookmarkMenuControllerObserver:
194 virtual void BookmarkMenuControllerDeleted(
195 BookmarkMenuController* controller) OVERRIDE;
197 // BookmarkBarInstructionsDelegate:
198 virtual void ShowImportDialog() OVERRIDE;
200 // BookmarkBubbleViewObserver:
201 virtual void OnBookmarkBubbleShown(const GURL& url) OVERRIDE;
202 virtual void OnBookmarkBubbleHidden() OVERRIDE;
204 // BookmarkModelObserver:
205 virtual void BookmarkModelLoaded(BookmarkModel* model,
206 bool ids_reassigned) OVERRIDE;
207 virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE;
208 virtual void BookmarkNodeMoved(BookmarkModel* model,
209 const BookmarkNode* old_parent,
211 const BookmarkNode* new_parent,
212 int new_index) OVERRIDE;
213 virtual void BookmarkNodeAdded(BookmarkModel* model,
214 const BookmarkNode* parent,
216 virtual void BookmarkNodeRemoved(BookmarkModel* model,
217 const BookmarkNode* parent,
219 const BookmarkNode* node,
220 const std::set<GURL>& removed_urls) OVERRIDE;
221 virtual void BookmarkAllNodesRemoved(
222 BookmarkModel* model,
223 const std::set<GURL>& removed_urls) OVERRIDE;
224 virtual void BookmarkNodeChanged(BookmarkModel* model,
225 const BookmarkNode* node) OVERRIDE;
226 virtual void BookmarkNodeChildrenReordered(BookmarkModel* model,
227 const BookmarkNode* node) OVERRIDE;
228 virtual void BookmarkNodeFaviconChanged(BookmarkModel* model,
229 const BookmarkNode* node) OVERRIDE;
231 // views::DragController:
232 virtual void WriteDragDataForView(views::View* sender,
233 const gfx::Point& press_pt,
234 ui::OSExchangeData* data) OVERRIDE;
235 virtual int GetDragOperationsForView(views::View* sender,
236 const gfx::Point& p) OVERRIDE;
237 virtual bool CanStartDragForView(views::View* sender,
238 const gfx::Point& press_pt,
239 const gfx::Point& p) OVERRIDE;
241 // views::MenuButtonListener:
242 virtual void OnMenuButtonClicked(views::View* view,
243 const gfx::Point& point) OVERRIDE;
245 // views::ButtonListener:
246 virtual void ButtonPressed(views::Button* sender,
247 const ui::Event& event) OVERRIDE;
249 // views::ContextMenuController:
250 virtual void ShowContextMenuForView(views::View* source,
251 const gfx::Point& point,
252 ui::MenuSourceType source_type) OVERRIDE;
255 class ButtonSeparatorView;
259 friend class BookmarkBarViewEventTestBase;
260 FRIEND_TEST_ALL_PREFIXES(BookmarkBarViewTest, SwitchProfile);
261 FRIEND_TEST_ALL_PREFIXES(BookmarkBarViewTest,
262 NoAppsShortcutWithoutInstantExtended);
263 FRIEND_TEST_ALL_PREFIXES(BookmarkBarViewInstantExtendedTest,
264 AppsShortcutVisibility);
266 // Used to identify what the user is dropping onto.
267 enum DropButtonType {
273 // Creates recent bookmark button and when visible button as well as
274 // calculating the preferred height.
277 // NOTE: unless otherwise stated all methods that take an int for an index are
278 // in terms of the bookmark bar view. Typically the view index and model index
279 // are the same, but they may differ during animations or drag and drop.
281 // It's easy to get the mapping wrong. For this reason all these methods are
284 // Returns the number of buttons corresponding to starred urls/folders. This
285 // is equivalent to the number of children the bookmark bar node from the
286 // bookmark bar model has.
287 int GetBookmarkButtonCount();
289 // Returns the button at the specified index.
290 views::TextButton* GetBookmarkButton(int index);
292 // Returns BOOKMARK_LAUNCH_LOCATION_DETACHED_BAR or
293 // BOOKMARK_LAUNCH_LOCATION_ATTACHED_BAR based on detached state.
294 BookmarkLaunchLocation GetBookmarkLaunchLocation() const;
296 // Returns the index of the first hidden bookmark button. If all buttons are
297 // visible, this returns GetBookmarkButtonCount().
298 int GetFirstHiddenNodeIndex();
300 // Creates the button showing the other bookmarked items.
301 views::MenuButton* CreateOtherBookmarkedButton();
303 // Creates the button used when not all bookmark buttons fit.
304 views::MenuButton* CreateOverflowButton();
306 // Creates the button for rendering the specified bookmark node.
307 views::View* CreateBookmarkButton(const BookmarkNode* node);
309 // Creates the button for rendering the apps page shortcut.
310 views::TextButton* CreateAppsPageShortcutButton();
312 // Configures the button from the specified node. This sets the text,
314 void ConfigureButton(const BookmarkNode* node, views::TextButton* button);
316 // Implementation for BookmarkNodeAddedImpl.
317 void BookmarkNodeAddedImpl(BookmarkModel* model,
318 const BookmarkNode* parent,
321 // Implementation for BookmarkNodeRemoved.
322 void BookmarkNodeRemovedImpl(BookmarkModel* model,
323 const BookmarkNode* parent,
326 // If the node is a child of the root node, the button is updated
328 void BookmarkNodeChangedImpl(BookmarkModel* model, const BookmarkNode* node);
330 // Shows the menu used during drag and drop for the specified node.
331 void ShowDropFolderForNode(const BookmarkNode* node);
333 // Cancels the timer used to show a drop menu.
334 void StopShowFolderDropMenuTimer();
336 // Stars the timer used to show a drop menu for node.
337 void StartShowFolderDropMenuTimer(const BookmarkNode* node);
339 // Calculates the location for the drop in |location|.
340 void CalculateDropLocation(const ui::DropTargetEvent& event,
341 const BookmarkNodeData& data,
342 DropLocation* location);
344 // Writes a BookmarkNodeData for node to data.
345 void WriteBookmarkDragData(const BookmarkNode* node,
346 ui::OSExchangeData* data);
348 // This determines which view should throb and starts it
349 // throbbing (e.g when the bookmark bubble is showing).
350 // If |overflow_only| is true, start throbbing only if |node| is hidden in
351 // the overflow menu.
352 void StartThrobbing(const BookmarkNode* node, bool overflow_only);
354 // Returns the view to throb when a node is removed. |parent| is the parent of
355 // the node that was removed, and |old_index| the index of the node that was
357 views::CustomButton* DetermineViewToThrobFromRemove(
358 const BookmarkNode* parent,
361 // Updates the colors for all the child objects in the bookmarks bar.
364 // Updates the visibility of |other_bookmarked_button_|. Also shows or hide
365 // the separator if required.
366 void UpdateOtherBookmarksVisibility();
368 // Updates the visibility of |bookmarks_separator_view_|.
369 void UpdateBookmarksSeparatorVisibility();
371 // This method computes the bounds for the bookmark bar items. If
372 // |compute_bounds_only| = TRUE, the bounds for the items are just computed,
373 // but are not set. This mode is used by GetPreferredSize() to obtain the
374 // desired bounds. If |compute_bounds_only| = FALSE, the bounds are set.
375 gfx::Size LayoutItems(bool compute_bounds_only);
377 // Updates the visibility of the apps shortcut based on the pref value.
378 void OnAppsPageShortcutVisibilityPrefChanged();
380 // Needed to react to kShowAppsShortcutInBookmarkBar changes.
381 PrefChangeRegistrar profile_pref_registrar_;
383 // Used for opening urls.
384 content::PageNavigator* page_navigator_;
386 // Model providing details as to the starred entries/folders that should be
387 // shown. This is owned by the Profile.
388 BookmarkModel* model_;
390 // Used to manage showing a Menu, either for the most recently bookmarked
391 // entries, or for the starred folder.
392 BookmarkMenuController* bookmark_menu_;
394 // Used when showing a menu for drag and drop. That is, if the user drags
395 // over a folder this becomes non-null and manages the menu showing the
396 // contents of the node.
397 BookmarkMenuController* bookmark_drop_menu_;
399 // If non-NULL we're showing a context menu for one of the items on the
401 scoped_ptr<BookmarkContextMenu> context_menu_;
403 // Shows the other bookmark entries.
404 views::MenuButton* other_bookmarked_button_;
406 // Shows the Apps page shortcut.
407 views::TextButton* apps_page_shortcut_;
409 // Task used to delay showing of the drop menu.
410 base::WeakPtrFactory<BookmarkBarView> show_folder_method_factory_;
412 // Used to track drops on the bookmark bar view.
413 scoped_ptr<DropInfo> drop_info_;
415 // Visible if not all the bookmark buttons fit.
416 views::MenuButton* overflow_button_;
418 // Shows a text and a link to import bookmarks if there are no bookmarks in
419 // the Bookmarks Bar.
420 views::View* instructions_;
422 ButtonSeparatorView* bookmarks_separator_view_;
425 BrowserView* browser_view_;
427 // True if the owning browser is showing an infobar.
428 bool infobar_visible_;
430 // Animation controlling showing and hiding of the bar.
431 scoped_ptr<gfx::SlideAnimation> size_animation_;
433 // If the bookmark bubble is showing, this is the visible ancestor of the URL.
434 // The visible ancestor is either the other_bookmarked_button_,
435 // overflow_button_ or a button on the bar.
436 views::CustomButton* throbbing_view_;
438 BookmarkBar::State bookmark_bar_state_;
440 // Are we animating to or from the detached state?
441 bool animating_detached_;
443 DISALLOW_COPY_AND_ASSIGN(BookmarkBarView);
446 #endif // CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_BAR_VIEW_H_