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_
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/observer_list.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"
31 class BookmarkBarViewObserver
;
32 class BookmarkContextMenu
;
36 class ChromeBookmarkClient
;
54 // BookmarkBarView renders the BookmarkModel. Each starred entry on the
55 // BookmarkBar is rendered as a MenuButton. An additional MenuButton aligned to
56 // the right allows the user to quickly see recently starred entries.
58 // BookmarkBarView shows the bookmarks from a specific Profile. BookmarkBarView
59 // waits until the HistoryService for the profile has been loaded before
60 // creating the BookmarkModel.
61 class BookmarkBarView
: public DetachableToolbarView
,
62 public BookmarkModelObserver
,
63 public views::MenuButtonListener
,
64 public views::ButtonListener
,
65 public views::ContextMenuController
,
66 public views::DragController
,
67 public gfx::AnimationDelegate
,
68 public BookmarkMenuControllerObserver
,
69 public BookmarkBarInstructionsDelegate
,
70 public BookmarkBubbleViewObserver
{
72 // The internal view class name.
73 static const char kViewClassName
[];
75 // Constant used in Browser View, as well as here.
76 // How inset the bookmarks bar is when displayed on the new tab page.
77 static const int kNewtabHorizontalPadding
;
79 // Maximum size of buttons on the bookmark bar.
80 static const int kMaxButtonWidth
;
82 // Number of pixels the attached bookmark bar overlaps with the toolbar.
83 static const int kToolbarAttachedBookmarkBarOverlap
;
85 // |browser_view| can be NULL during tests.
86 BookmarkBarView(Browser
* browser
, BrowserView
* browser_view
);
87 virtual ~BookmarkBarView();
89 static void DisableAnimationsForTesting(bool disabled
);
91 // Returns the current browser.
92 Browser
* browser() const { return browser_
; }
94 void AddObserver(BookmarkBarViewObserver
* observer
);
95 void RemoveObserver(BookmarkBarViewObserver
* observer
);
97 // Sets the PageNavigator that is used when the user selects an entry on
99 void SetPageNavigator(content::PageNavigator
* navigator
);
101 // Sets whether the containing browser is showing an infobar. This affects
102 // layout during animation.
103 void set_infobar_visible(bool infobar_visible
) {
104 infobar_visible_
= infobar_visible
;
107 // Changes the state of the bookmark bar.
108 void SetBookmarkBarState(BookmarkBar::State state
,
109 BookmarkBar::AnimateChangeType animate_type
);
111 // Returns the toolbar overlap when fully detached.
112 int GetFullyDetachedToolbarOverlap() const;
114 // Whether or not we are animating.
117 // If |loc| is over a bookmark button the node is returned corresponding to
118 // the button and |model_start_index| is set to 0. If a overflow button is
119 // showing and |loc| is over the overflow button, the bookmark bar node is
120 // returned and |model_start_index| is set to the index of the first node
121 // contained in the overflow menu.
122 const BookmarkNode
* GetNodeForButtonAtModelIndex(const gfx::Point
& loc
,
123 int* model_start_index
);
125 // Returns the MenuButton for node.
126 views::MenuButton
* GetMenuButtonForNode(const BookmarkNode
* node
);
128 // Returns the position to anchor the menu for |button| at.
129 void GetAnchorPositionForButton(views::MenuButton
* button
,
130 views::MenuAnchorPosition
* anchor
);
132 // Returns the button responsible for showing bookmarks in the other bookmark
134 views::MenuButton
* other_bookmarked_button() const {
135 return other_bookmarked_button_
;
138 // Returns the button used when not all the items on the bookmark bar fit.
139 views::MenuButton
* overflow_button() const { return overflow_button_
; }
141 // Returns the active MenuItemView, or NULL if a menu isn't showing.
142 views::MenuItemView
* GetMenu();
144 // Returns the context menu, or null if one isn't showing.
145 views::MenuItemView
* GetContextMenu();
147 // Returns the drop MenuItemView, or NULL if a menu isn't showing.
148 views::MenuItemView
* GetDropMenu();
150 // If a button is currently throbbing, it is stopped. If immediate is true
151 // the throb stops immediately, otherwise it stops after a couple more
153 void StopThrobbing(bool immediate
);
155 // Returns the tooltip text for the specified url and title. The returned
156 // text is clipped to fit within the bounds of the monitor. |context| is
157 // used to determine which gfx::Screen is used to retrieve bounds.
159 // Note that we adjust the direction of both the URL and the title based on
160 // the locale so that pure LTR strings are displayed properly in RTL locales.
161 static base::string16
CreateToolTipForURLAndTitle(const views::Widget
* widget
,
162 const gfx::Point
& screen_loc
,
164 const base::string16
& title
,
167 // DetachableToolbarView methods:
168 virtual bool IsDetached() const override
;
169 virtual double GetAnimationValue() const override
;
170 virtual int GetToolbarOverlap() const override
;
173 virtual gfx::Size
GetPreferredSize() const override
;
174 virtual gfx::Size
GetMinimumSize() const override
;
175 virtual bool CanProcessEventsWithinSubtree() const override
;
176 virtual void Layout() override
;
177 virtual void ViewHierarchyChanged(
178 const ViewHierarchyChangedDetails
& details
) override
;
179 virtual void PaintChildren(gfx::Canvas
* canvas
,
180 const views::CullSet
& cull_set
) override
;
181 virtual bool GetDropFormats(
183 std::set
<ui::OSExchangeData::CustomFormat
>* custom_formats
) override
;
184 virtual bool AreDropTypesRequired() override
;
185 virtual bool CanDrop(const ui::OSExchangeData
& data
) override
;
186 virtual void OnDragEntered(const ui::DropTargetEvent
& event
) override
;
187 virtual int OnDragUpdated(const ui::DropTargetEvent
& event
) override
;
188 virtual void OnDragExited() override
;
189 virtual int OnPerformDrop(const ui::DropTargetEvent
& event
) override
;
190 virtual void OnThemeChanged() override
;
191 virtual const char* GetClassName() const override
;
192 virtual void SetVisible(bool visible
) override
;
194 // AccessiblePaneView:
195 virtual void GetAccessibleState(ui::AXViewState
* state
) override
;
197 // gfx::AnimationDelegate:
198 virtual void AnimationProgressed(const gfx::Animation
* animation
) override
;
199 virtual void AnimationEnded(const gfx::Animation
* animation
) override
;
201 // BookmarkMenuControllerObserver:
202 virtual void BookmarkMenuControllerDeleted(
203 BookmarkMenuController
* controller
) override
;
205 // BookmarkBarInstructionsDelegate:
206 virtual void ShowImportDialog() override
;
208 // BookmarkBubbleViewObserver:
209 virtual void OnBookmarkBubbleShown(const GURL
& url
) override
;
210 virtual void OnBookmarkBubbleHidden() override
;
212 // BookmarkModelObserver:
213 virtual void BookmarkModelLoaded(BookmarkModel
* model
,
214 bool ids_reassigned
) override
;
215 virtual void BookmarkModelBeingDeleted(BookmarkModel
* model
) override
;
216 virtual void BookmarkNodeMoved(BookmarkModel
* model
,
217 const BookmarkNode
* old_parent
,
219 const BookmarkNode
* new_parent
,
220 int new_index
) override
;
221 virtual void BookmarkNodeAdded(BookmarkModel
* model
,
222 const BookmarkNode
* parent
,
224 virtual void BookmarkNodeRemoved(BookmarkModel
* model
,
225 const BookmarkNode
* parent
,
227 const BookmarkNode
* node
,
228 const std::set
<GURL
>& removed_urls
) override
;
229 virtual void BookmarkAllUserNodesRemoved(
230 BookmarkModel
* model
,
231 const std::set
<GURL
>& removed_urls
) override
;
232 virtual void BookmarkNodeChanged(BookmarkModel
* model
,
233 const BookmarkNode
* node
) override
;
234 virtual void BookmarkNodeChildrenReordered(BookmarkModel
* model
,
235 const BookmarkNode
* node
) override
;
236 virtual void BookmarkNodeFaviconChanged(BookmarkModel
* model
,
237 const BookmarkNode
* node
) override
;
239 // views::DragController:
240 virtual void WriteDragDataForView(views::View
* sender
,
241 const gfx::Point
& press_pt
,
242 ui::OSExchangeData
* data
) override
;
243 virtual int GetDragOperationsForView(views::View
* sender
,
244 const gfx::Point
& p
) override
;
245 virtual bool CanStartDragForView(views::View
* sender
,
246 const gfx::Point
& press_pt
,
247 const gfx::Point
& p
) override
;
249 // views::MenuButtonListener:
250 virtual void OnMenuButtonClicked(views::View
* view
,
251 const gfx::Point
& point
) override
;
253 // views::ButtonListener:
254 virtual void ButtonPressed(views::Button
* sender
,
255 const ui::Event
& event
) override
;
257 // views::ContextMenuController:
258 virtual void ShowContextMenuForView(views::View
* source
,
259 const gfx::Point
& point
,
260 ui::MenuSourceType source_type
) override
;
263 class ButtonSeparatorView
;
267 friend class BookmarkBarViewEventTestBase
;
268 FRIEND_TEST_ALL_PREFIXES(BookmarkBarViewTest
, SwitchProfile
);
269 FRIEND_TEST_ALL_PREFIXES(BookmarkBarViewTest
,
270 ManagedShowAppsShortcutInBookmarksBar
);
271 FRIEND_TEST_ALL_PREFIXES(BookmarkBarViewInstantExtendedTest
,
272 AppsShortcutVisibility
);
274 // Used to identify what the user is dropping onto.
275 enum DropButtonType
{
281 // Creates recent bookmark button and when visible button as well as
282 // calculating the preferred height.
285 // NOTE: unless otherwise stated all methods that take an int for an index are
286 // in terms of the bookmark bar view. Typically the view index and model index
287 // are the same, but they may differ during animations or drag and drop.
289 // It's easy to get the mapping wrong. For this reason all these methods are
292 // Returns the number of buttons corresponding to starred urls/folders. This
293 // is equivalent to the number of children the bookmark bar node from the
294 // bookmark bar model has.
295 int GetBookmarkButtonCount() const;
297 // Returns the button at the specified index.
298 views::LabelButton
* GetBookmarkButton(int index
);
300 // Returns BOOKMARK_LAUNCH_LOCATION_DETACHED_BAR or
301 // BOOKMARK_LAUNCH_LOCATION_ATTACHED_BAR based on detached state.
302 BookmarkLaunchLocation
GetBookmarkLaunchLocation() const;
304 // Returns the index of the first hidden bookmark button. If all buttons are
305 // visible, this returns GetBookmarkButtonCount().
306 int GetFirstHiddenNodeIndex();
308 // Creates the button showing the other bookmarked items.
309 views::MenuButton
* CreateOtherBookmarkedButton();
311 // Creates the button showing the managed bookmarks items.
312 views::MenuButton
* CreateManagedBookmarksButton();
314 // Creates the button used when not all bookmark buttons fit.
315 views::MenuButton
* CreateOverflowButton();
317 // Creates the button for rendering the specified bookmark node.
318 views::View
* CreateBookmarkButton(const BookmarkNode
* node
);
320 // Creates the button for rendering the apps page shortcut.
321 views::LabelButton
* CreateAppsPageShortcutButton();
323 // Configures the button from the specified node. This sets the text,
325 void ConfigureButton(const BookmarkNode
* node
, views::LabelButton
* button
);
327 // Implementation for BookmarkNodeAddedImpl.
328 void BookmarkNodeAddedImpl(BookmarkModel
* model
,
329 const BookmarkNode
* parent
,
332 // Implementation for BookmarkNodeRemoved.
333 void BookmarkNodeRemovedImpl(BookmarkModel
* model
,
334 const BookmarkNode
* parent
,
337 // If the node is a child of the root node, the button is updated
339 void BookmarkNodeChangedImpl(BookmarkModel
* model
, const BookmarkNode
* node
);
341 // Shows the menu used during drag and drop for the specified node.
342 void ShowDropFolderForNode(const BookmarkNode
* node
);
344 // Cancels the timer used to show a drop menu.
345 void StopShowFolderDropMenuTimer();
347 // Stars the timer used to show a drop menu for node.
348 void StartShowFolderDropMenuTimer(const BookmarkNode
* node
);
350 // Calculates the location for the drop in |location|.
351 void CalculateDropLocation(const ui::DropTargetEvent
& event
,
352 const bookmarks::BookmarkNodeData
& data
,
353 DropLocation
* location
);
355 // Writes a BookmarkNodeData for node to data.
356 void WriteBookmarkDragData(const BookmarkNode
* node
,
357 ui::OSExchangeData
* data
);
359 // This determines which view should throb and starts it
360 // throbbing (e.g when the bookmark bubble is showing).
361 // If |overflow_only| is true, start throbbing only if |node| is hidden in
362 // the overflow menu.
363 void StartThrobbing(const BookmarkNode
* node
, bool overflow_only
);
365 // Returns the view to throb when a node is removed. |parent| is the parent of
366 // the node that was removed, and |old_index| the index of the node that was
368 views::CustomButton
* DetermineViewToThrobFromRemove(
369 const BookmarkNode
* parent
,
372 // Updates the colors for all the child objects in the bookmarks bar.
375 // Updates the visibility of |other_bookmarked_button_| and
376 // |managed_bookmarks_button_|. Also shows or hides the separator if required.
377 void UpdateButtonsVisibility();
379 // Updates the visibility of |bookmarks_separator_view_|.
380 void UpdateBookmarksSeparatorVisibility();
382 // Updates the visibility of the apps shortcut based on the pref value.
383 void OnAppsPageShortcutVisibilityPrefChanged();
385 // Needed to react to kShowAppsShortcutInBookmarkBar changes.
386 PrefChangeRegistrar profile_pref_registrar_
;
388 // Used for opening urls.
389 content::PageNavigator
* page_navigator_
;
391 // BookmarkModel that owns the entries and folders that are shown in this
392 // view. This is owned by the Profile.
393 BookmarkModel
* model_
;
395 // ChromeBookmarkClient. This is owned by the Profile.
396 ChromeBookmarkClient
* client_
;
398 // Used to manage showing a Menu, either for the most recently bookmarked
399 // entries, or for the starred folder.
400 BookmarkMenuController
* bookmark_menu_
;
402 // Used when showing a menu for drag and drop. That is, if the user drags
403 // over a folder this becomes non-null and manages the menu showing the
404 // contents of the node.
405 BookmarkMenuController
* bookmark_drop_menu_
;
407 // If non-NULL we're showing a context menu for one of the items on the
409 scoped_ptr
<BookmarkContextMenu
> context_menu_
;
411 // Shows the other bookmark entries.
412 views::MenuButton
* other_bookmarked_button_
;
414 // Shows the managed bookmarks entries.
415 views::MenuButton
* managed_bookmarks_button_
;
417 // Shows the Apps page shortcut.
418 views::LabelButton
* apps_page_shortcut_
;
420 // Used to track drops on the bookmark bar view.
421 scoped_ptr
<DropInfo
> drop_info_
;
423 // Visible if not all the bookmark buttons fit.
424 views::MenuButton
* overflow_button_
;
426 // Shows a text and a link to import bookmarks if there are no bookmarks in
427 // the Bookmarks Bar.
428 views::View
* instructions_
;
430 ButtonSeparatorView
* bookmarks_separator_view_
;
433 BrowserView
* browser_view_
;
435 // True if the owning browser is showing an infobar.
436 bool infobar_visible_
;
438 // Animation controlling showing and hiding of the bar.
439 scoped_ptr
<gfx::SlideAnimation
> size_animation_
;
441 // If the bookmark bubble is showing, this is the visible ancestor of the URL.
442 // The visible ancestor is either the other_bookmarked_button_,
443 // overflow_button_ or a button on the bar.
444 views::CustomButton
* throbbing_view_
;
446 BookmarkBar::State bookmark_bar_state_
;
448 // Are we animating to or from the detached state?
449 bool animating_detached_
;
451 ObserverList
<BookmarkBarViewObserver
> observers_
;
453 // Factory used to delay showing of the drop menu.
454 base::WeakPtrFactory
<BookmarkBarView
> show_folder_method_factory_
;
456 DISALLOW_COPY_AND_ASSIGN(BookmarkBarView
);
459 #endif // CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_BAR_VIEW_H_