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_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_observer.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/controls/menu/menu_item_view.h"
29 #include "ui/views/drag_controller.h"
31 class BookmarkContextMenu
;
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.
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.
56 class 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 BookmarkMenuControllerObserver
,
64 public chrome::BookmarkBarInstructionsDelegate
,
65 public BookmarkBubbleViewObserver
{
67 // The internal view class name.
68 static const char kViewClassName
[];
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
;
74 // Maximum size of buttons on the bookmark bar.
75 static const int kMaxButtonWidth
;
77 // Number of pixels the attached bookmark bar overlaps with the toolbar.
78 static const int kToolbarAttachedBookmarkBarOverlap
;
80 // |browser_view| can be NULL during tests.
81 BookmarkBarView(Browser
* browser
, BrowserView
* browser_view
);
82 virtual ~BookmarkBarView();
84 static void DisableAnimationsForTesting(bool disabled
);
86 // Returns the current browser.
87 Browser
* browser() const { return browser_
; }
89 // Sets the PageNavigator that is used when the user selects an entry on
91 void SetPageNavigator(content::PageNavigator
* navigator
);
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
;
99 // Changes the state of the bookmark bar.
100 void SetBookmarkBarState(BookmarkBar::State state
,
101 BookmarkBar::AnimateChangeType animate_type
);
103 // Returns the toolbar overlap when fully detached.
104 int GetFullyDetachedToolbarOverlap() const;
106 // Whether or not we are animating.
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
);
117 // Returns the MenuButton for node.
118 views::MenuButton
* GetMenuButtonForNode(const BookmarkNode
* node
);
120 // Returns the position to anchor the menu for |button| at.
121 void GetAnchorPositionForButton(views::MenuButton
* button
,
122 views::MenuItemView::AnchorPosition
* anchor
);
124 // Returns the button responsible for showing bookmarks in the other bookmark
126 views::MenuButton
* other_bookmarked_button() const {
127 return other_bookmarked_button_
;
130 // Returns the button used when not all the items on the bookmark bar fit.
131 views::MenuButton
* overflow_button() const { return overflow_button_
; }
133 // Returns the active MenuItemView, or NULL if a menu isn't showing.
134 views::MenuItemView
* GetMenu();
136 // Returns the context menu, or null if one isn't showing.
137 views::MenuItemView
* GetContextMenu();
139 // Returns the drop MenuItemView, or NULL if a menu isn't showing.
140 views::MenuItemView
* GetDropMenu();
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
145 void StopThrobbing(bool immediate
);
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.
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 gfx::Point
& screen_loc
,
155 const string16
& title
,
157 gfx::NativeView context
);
159 // DetachableToolbarView methods:
160 virtual bool IsDetached() const OVERRIDE
;
161 virtual double GetAnimationValue() const OVERRIDE
;
162 virtual int GetToolbarOverlap() const OVERRIDE
;
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(
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
;
184 // AccessiblePaneView:
185 virtual void GetAccessibleState(ui::AccessibleViewState
* state
) OVERRIDE
;
187 // ui::AnimationDelegate:
188 virtual void AnimationProgressed(const ui::Animation
* animation
) OVERRIDE
;
189 virtual void AnimationEnded(const ui::Animation
* animation
) OVERRIDE
;
191 // BookmarkMenuControllerObserver:
192 virtual void BookmarkMenuControllerDeleted(
193 BookmarkMenuController
* controller
) OVERRIDE
;
195 // chrome::BookmarkBarInstructionsDelegate:
196 virtual void ShowImportDialog() OVERRIDE
;
198 // BookmarkBubbleViewObserver:
199 virtual void OnBookmarkBubbleShown(const GURL
& url
) OVERRIDE
;
200 virtual void OnBookmarkBubbleHidden() OVERRIDE
;
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
,
208 const BookmarkNode
* new_parent
,
209 int new_index
) OVERRIDE
;
210 virtual void BookmarkNodeAdded(BookmarkModel
* model
,
211 const BookmarkNode
* parent
,
213 virtual void BookmarkNodeRemoved(BookmarkModel
* model
,
214 const BookmarkNode
* parent
,
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
;
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
;
235 // views::MenuButtonListener:
236 virtual void OnMenuButtonClicked(views::View
* view
,
237 const gfx::Point
& point
) OVERRIDE
;
239 // views::ButtonListener:
240 virtual void ButtonPressed(views::Button
* sender
,
241 const ui::Event
& event
) OVERRIDE
;
243 // views::ContextMenuController:
244 virtual void ShowContextMenuForView(views::View
* source
,
245 const gfx::Point
& point
,
246 ui::MenuSourceType source_type
) OVERRIDE
;
249 class ButtonSeparatorView
;
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
);
260 // Used to identify what the user is dropping onto.
261 enum DropButtonType
{
267 // Creates recent bookmark button and when visible button as well as
268 // calculating the preferred height.
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.
275 // It's easy to get the mapping wrong. For this reason all these methods are
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();
283 // Returns the button at the specified index.
284 views::TextButton
* GetBookmarkButton(int index
);
286 // Returns LAUNCH_DETACHED_BAR or LAUNCH_ATTACHED_BAR based on detached state.
287 bookmark_utils::BookmarkLaunchLocation
GetBookmarkLaunchLocation() const;
289 // Returns the index of the first hidden bookmark button. If all buttons are
290 // visible, this returns GetBookmarkButtonCount().
291 int GetFirstHiddenNodeIndex();
293 // Creates the button showing the other bookmarked items.
294 views::MenuButton
* CreateOtherBookmarkedButton();
296 // Creates the button used when not all bookmark buttons fit.
297 views::MenuButton
* CreateOverflowButton();
299 // Creates the button for rendering the specified bookmark node.
300 views::View
* CreateBookmarkButton(const BookmarkNode
* node
);
302 // Creates the button for rendering the apps page shortcut.
303 views::TextButton
* CreateAppsPageShortcutButton();
305 // Configures the button from the specified node. This sets the text,
307 void ConfigureButton(const BookmarkNode
* node
, views::TextButton
* button
);
309 // Implementation for BookmarkNodeAddedImpl.
310 void BookmarkNodeAddedImpl(BookmarkModel
* model
,
311 const BookmarkNode
* parent
,
314 // Implementation for BookmarkNodeRemoved.
315 void BookmarkNodeRemovedImpl(BookmarkModel
* model
,
316 const BookmarkNode
* parent
,
319 // If the node is a child of the root node, the button is updated
321 void BookmarkNodeChangedImpl(BookmarkModel
* model
, const BookmarkNode
* node
);
323 // Shows the menu used during drag and drop for the specified node.
324 void ShowDropFolderForNode(const BookmarkNode
* node
);
326 // Cancels the timer used to show a drop menu.
327 void StopShowFolderDropMenuTimer();
329 // Stars the timer used to show a drop menu for node.
330 void StartShowFolderDropMenuTimer(const BookmarkNode
* node
);
332 // Calculates the location for the drop in |location|.
333 void CalculateDropLocation(const ui::DropTargetEvent
& event
,
334 const BookmarkNodeData
& data
,
335 DropLocation
* location
);
337 // Writes a BookmarkNodeData for node to data.
338 void WriteBookmarkDragData(const BookmarkNode
* node
,
339 ui::OSExchangeData
* data
);
341 // This determines which view should throb and starts it
342 // throbbing (e.g when the bookmark bubble is showing).
343 // If |overflow_only| is true, start throbbing only if |node| is hidden in
344 // the overflow menu.
345 void StartThrobbing(const BookmarkNode
* node
, bool overflow_only
);
347 // Returns the view to throb when a node is removed. |parent| is the parent of
348 // the node that was removed, and |old_index| the index of the node that was
350 views::CustomButton
* DetermineViewToThrobFromRemove(
351 const BookmarkNode
* parent
,
354 // Updates the colors for all the child objects in the bookmarks bar.
357 // Updates the visibility of |other_bookmarked_button_|. Also shows or hide
358 // the separator if required.
359 void UpdateOtherBookmarksVisibility();
361 // Updates the visibility of |bookmarks_separator_view_|.
362 void UpdateBookmarksSeparatorVisibility();
364 // This method computes the bounds for the bookmark bar items. If
365 // |compute_bounds_only| = TRUE, the bounds for the items are just computed,
366 // but are not set. This mode is used by GetPreferredSize() to obtain the
367 // desired bounds. If |compute_bounds_only| = FALSE, the bounds are set.
368 gfx::Size
LayoutItems(bool compute_bounds_only
);
370 // Updates the visibility of the apps shortcut based on the pref value.
371 void OnAppsPageShortcutVisibilityPrefChanged();
373 // Needed to react to kShowAppsShortcutInBookmarkBar changes.
374 PrefChangeRegistrar profile_pref_registrar_
;
376 // Used for opening urls.
377 content::PageNavigator
* page_navigator_
;
379 // Model providing details as to the starred entries/folders that should be
380 // shown. This is owned by the Profile.
381 BookmarkModel
* model_
;
383 // Used to manage showing a Menu, either for the most recently bookmarked
384 // entries, or for the starred folder.
385 BookmarkMenuController
* bookmark_menu_
;
387 // Used when showing a menu for drag and drop. That is, if the user drags
388 // over a folder this becomes non-null and manages the menu showing the
389 // contents of the node.
390 BookmarkMenuController
* bookmark_drop_menu_
;
392 // If non-NULL we're showing a context menu for one of the items on the
394 scoped_ptr
<BookmarkContextMenu
> context_menu_
;
396 // Shows the other bookmark entries.
397 views::MenuButton
* other_bookmarked_button_
;
399 // Shows the Apps page shortcut.
400 views::TextButton
* apps_page_shortcut_
;
402 // Task used to delay showing of the drop menu.
403 base::WeakPtrFactory
<BookmarkBarView
> show_folder_method_factory_
;
405 // Used to track drops on the bookmark bar view.
406 scoped_ptr
<DropInfo
> drop_info_
;
408 // Visible if not all the bookmark buttons fit.
409 views::MenuButton
* overflow_button_
;
411 // Shows a text and a link to import bookmarks if there are no bookmarks in
412 // the Bookmarks Bar.
413 views::View
* instructions_
;
415 ButtonSeparatorView
* bookmarks_separator_view_
;
418 BrowserView
* browser_view_
;
420 // True if the owning browser is showing an infobar.
421 bool infobar_visible_
;
423 // Animation controlling showing and hiding of the bar.
424 scoped_ptr
<ui::SlideAnimation
> size_animation_
;
426 // If the bookmark bubble is showing, this is the visible ancestor of the URL.
427 // The visible ancestor is either the other_bookmarked_button_,
428 // overflow_button_ or a button on the bar.
429 views::CustomButton
* throbbing_view_
;
431 BookmarkBar::State bookmark_bar_state_
;
433 // Are we animating to or from the detached state?
434 bool animating_detached_
;
436 DISALLOW_COPY_AND_ASSIGN(BookmarkBarView
);
439 #endif // CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_BAR_VIEW_H_