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/bookmarks/bookmark_bubble_observer.h"
20 #include "chrome/browser/ui/views/bookmarks/bookmark_menu_controller_observer.h"
21 #include "components/bookmarks/browser/bookmark_model_observer.h"
22 #include "components/bookmarks/browser/bookmark_node_data.h"
23 #include "ui/gfx/animation/animation_delegate.h"
24 #include "ui/views/accessible_pane_view.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 BookmarkBarViewTestHelper
;
33 class BookmarkContextMenu
;
40 class ManagedBookmarkService
;
58 // BookmarkBarView renders the BookmarkModel. Each starred entry on the
59 // BookmarkBar is rendered as a MenuButton. An additional MenuButton aligned to
60 // the right allows the user to quickly see recently starred entries.
62 // BookmarkBarView shows the bookmarks from a specific Profile. BookmarkBarView
63 // waits until the HistoryService for the profile has been loaded before
64 // creating the BookmarkModel.
65 class BookmarkBarView
: public views::AccessiblePaneView
,
66 public bookmarks::BookmarkModelObserver
,
67 public views::MenuButtonListener
,
68 public views::ButtonListener
,
69 public views::ContextMenuController
,
70 public views::DragController
,
71 public gfx::AnimationDelegate
,
72 public BookmarkMenuControllerObserver
,
73 public BookmarkBarInstructionsDelegate
,
74 public bookmarks::BookmarkBubbleObserver
{
76 // The internal view class name.
77 static const char kViewClassName
[];
79 // |browser_view| can be NULL during tests.
80 BookmarkBarView(Browser
* browser
, BrowserView
* browser_view
);
81 ~BookmarkBarView() override
;
83 static void DisableAnimationsForTesting(bool disabled
);
85 // Returns the current browser.
86 Browser
* browser() const { return browser_
; }
88 void AddObserver(BookmarkBarViewObserver
* observer
);
89 void RemoveObserver(BookmarkBarViewObserver
* observer
);
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 bookmarks::BookmarkNode
* GetNodeForButtonAtModelIndex(
117 const gfx::Point
& loc
,
118 int* model_start_index
);
120 // Returns the MenuButton for node.
121 views::MenuButton
* GetMenuButtonForNode(const bookmarks::BookmarkNode
* node
);
123 // Returns the position to anchor the menu for |button| at.
124 void GetAnchorPositionForButton(views::MenuButton
* button
,
125 views::MenuAnchorPosition
* anchor
);
127 // Returns the button responsible for showing bookmarks in the
128 // "Other Bookmarks" folder.
129 views::MenuButton
* other_bookmarks_button() const {
130 return other_bookmarks_button_
;
133 // Returns the button used when not all the items on the bookmark bar fit.
134 views::MenuButton
* overflow_button() const { return overflow_button_
; }
136 // Returns the active MenuItemView, or NULL if a menu isn't showing.
137 views::MenuItemView
* GetMenu();
139 // Returns the context menu, or null if one isn't showing.
140 views::MenuItemView
* GetContextMenu();
142 // Returns the drop MenuItemView, or NULL if a menu isn't showing.
143 views::MenuItemView
* GetDropMenu();
145 // If a button is currently throbbing, it is stopped. If immediate is true
146 // the throb stops immediately, otherwise it stops after a couple more
148 void StopThrobbing(bool immediate
);
150 // Returns the tooltip text for the specified url and title. The returned
151 // text is clipped to fit within the bounds of the monitor. |context| is
152 // used to determine which gfx::Screen is used to retrieve bounds.
154 // Note that we adjust the direction of both the URL and the title based on
155 // the locale so that pure LTR strings are displayed properly in RTL locales.
156 static base::string16
CreateToolTipForURLAndTitle(const views::Widget
* widget
,
157 const gfx::Point
& screen_loc
,
159 const base::string16
& title
,
162 // Returns true if Bookmarks Bar is currently detached from the Toolbar.
163 bool IsDetached() const;
165 // Returns the current state of the resize animation (show/hide).
166 double GetAnimationValue() const;
168 // Returns the current amount of overlap atop the browser toolbar.
169 int GetToolbarOverlap() const;
172 gfx::Size
GetPreferredSize() const override
;
173 gfx::Size
GetMinimumSize() const override
;
174 bool CanProcessEventsWithinSubtree() const override
;
175 void Layout() override
;
176 void ViewHierarchyChanged(
177 const ViewHierarchyChangedDetails
& details
) override
;
178 void PaintChildren(const ui::PaintContext
& context
) override
;
181 std::set
<ui::OSExchangeData::CustomFormat
>* custom_formats
) override
;
182 bool AreDropTypesRequired() override
;
183 bool CanDrop(const ui::OSExchangeData
& data
) override
;
184 void OnDragEntered(const ui::DropTargetEvent
& event
) override
;
185 int OnDragUpdated(const ui::DropTargetEvent
& event
) override
;
186 void OnDragExited() override
;
187 int OnPerformDrop(const ui::DropTargetEvent
& event
) override
;
188 void OnThemeChanged() override
;
189 const char* GetClassName() const override
;
190 void SetVisible(bool visible
) override
;
192 // AccessiblePaneView:
193 void GetAccessibleState(ui::AXViewState
* state
) override
;
195 // gfx::AnimationDelegate:
196 void AnimationProgressed(const gfx::Animation
* animation
) override
;
197 void AnimationEnded(const gfx::Animation
* animation
) override
;
199 // BookmarkMenuControllerObserver:
200 void BookmarkMenuControllerDeleted(
201 BookmarkMenuController
* controller
) override
;
203 // BookmarkBarInstructionsDelegate:
204 void OnImportBookmarks() override
;
206 // bookmarks::BookmarkBubbleObserver:
207 void OnBookmarkBubbleShown(const bookmarks::BookmarkNode
* node
) override
;
208 void OnBookmarkBubbleHidden() override
;
210 // bookmarks::BookmarkModelObserver:
211 void BookmarkModelLoaded(bookmarks::BookmarkModel
* model
,
212 bool ids_reassigned
) override
;
213 void BookmarkModelBeingDeleted(bookmarks::BookmarkModel
* model
) override
;
214 void BookmarkNodeMoved(bookmarks::BookmarkModel
* model
,
215 const bookmarks::BookmarkNode
* old_parent
,
217 const bookmarks::BookmarkNode
* new_parent
,
218 int new_index
) override
;
219 void BookmarkNodeAdded(bookmarks::BookmarkModel
* model
,
220 const bookmarks::BookmarkNode
* parent
,
222 void BookmarkNodeRemoved(bookmarks::BookmarkModel
* model
,
223 const bookmarks::BookmarkNode
* parent
,
225 const bookmarks::BookmarkNode
* node
,
226 const std::set
<GURL
>& removed_urls
) override
;
227 void BookmarkAllUserNodesRemoved(bookmarks::BookmarkModel
* model
,
228 const std::set
<GURL
>& removed_urls
) override
;
229 void BookmarkNodeChanged(bookmarks::BookmarkModel
* model
,
230 const bookmarks::BookmarkNode
* node
) override
;
231 void BookmarkNodeChildrenReordered(
232 bookmarks::BookmarkModel
* model
,
233 const bookmarks::BookmarkNode
* node
) override
;
234 void BookmarkNodeFaviconChanged(bookmarks::BookmarkModel
* model
,
235 const bookmarks::BookmarkNode
* node
) override
;
237 // views::DragController:
238 void WriteDragDataForView(views::View
* sender
,
239 const gfx::Point
& press_pt
,
240 ui::OSExchangeData
* data
) override
;
241 int GetDragOperationsForView(views::View
* sender
,
242 const gfx::Point
& p
) override
;
243 bool CanStartDragForView(views::View
* sender
,
244 const gfx::Point
& press_pt
,
245 const gfx::Point
& p
) override
;
247 // views::MenuButtonListener:
248 void OnMenuButtonClicked(views::View
* view
, const gfx::Point
& point
) override
;
250 // views::ButtonListener:
251 void ButtonPressed(views::Button
* sender
, const ui::Event
& event
) override
;
253 // views::ContextMenuController:
254 void ShowContextMenuForView(views::View
* source
,
255 const gfx::Point
& point
,
256 ui::MenuSourceType source_type
) override
;
259 class ButtonSeparatorView
;
263 friend class BookmarkBarViewTestHelper
;
264 friend class BookmarkBarViewEventTestBase
;
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 bookmark bar url/folder buttons that have been
285 // created. This does not necessarily represent the number of bookmark bar
286 // nodes, nor the number of visible bookmark bar buttons. Buttons are created
287 // lazily to fill available space, and may be hidden for ordering or sizing
289 int GetBookmarkButtonCount() const;
291 // Returns the button at the specified index.
292 views::LabelButton
* GetBookmarkButton(int index
);
294 // Returns BOOKMARK_LAUNCH_LOCATION_DETACHED_BAR or
295 // BOOKMARK_LAUNCH_LOCATION_ATTACHED_BAR based on detached state.
296 BookmarkLaunchLocation
GetBookmarkLaunchLocation() const;
298 // Returns the index of the first hidden bookmark button. If all buttons are
299 // visible, this returns GetBookmarkButtonCount().
300 int GetFirstHiddenNodeIndex();
302 // Creates the button showing the "Other Bookmarks" folder.
303 views::MenuButton
* CreateOtherBookmarksButton();
305 // Creates the button showing the "Managed Bookmarks" folder.
306 views::MenuButton
* CreateManagedBookmarksButton();
308 // Creates the button showing the supervised bookmarks items.
309 views::MenuButton
* CreateSupervisedBookmarksButton();
311 // Creates the button used when not all bookmark buttons fit.
312 views::MenuButton
* CreateOverflowButton();
314 // Creates the button for rendering the specified bookmark node.
315 views::View
* CreateBookmarkButton(const bookmarks::BookmarkNode
* node
);
317 // Creates the button for rendering the apps page shortcut.
318 views::LabelButton
* CreateAppsPageShortcutButton();
320 // Configures the button from the specified node. This sets the text,
322 void ConfigureButton(const bookmarks::BookmarkNode
* node
,
323 views::LabelButton
* button
);
325 // Implementation for BookmarkNodeAddedImpl. Returns true if LayoutAndPaint()
327 bool BookmarkNodeAddedImpl(bookmarks::BookmarkModel
* model
,
328 const bookmarks::BookmarkNode
* parent
,
331 // Implementation for BookmarkNodeRemoved. Returns true if LayoutAndPaint() is
333 bool BookmarkNodeRemovedImpl(bookmarks::BookmarkModel
* model
,
334 const bookmarks::BookmarkNode
* parent
,
337 // If the node is a child of the root node, the button is updated
339 void BookmarkNodeChangedImpl(bookmarks::BookmarkModel
* model
,
340 const bookmarks::BookmarkNode
* node
);
342 // Shows the menu used during drag and drop for the specified node.
343 void ShowDropFolderForNode(const bookmarks::BookmarkNode
* node
);
345 // Cancels the timer used to show a drop menu.
346 void StopShowFolderDropMenuTimer();
348 // Stars the timer used to show a drop menu for node.
349 void StartShowFolderDropMenuTimer(const bookmarks::BookmarkNode
* node
);
351 // Calculates the location for the drop in |location|.
352 void CalculateDropLocation(const ui::DropTargetEvent
& event
,
353 const bookmarks::BookmarkNodeData
& data
,
354 DropLocation
* location
);
356 // Writes a BookmarkNodeData for node to data.
357 void WriteBookmarkDragData(const bookmarks::BookmarkNode
* node
,
358 ui::OSExchangeData
* data
);
360 // This determines which view should throb and starts it
361 // throbbing (e.g when the bookmark bubble is showing).
362 // If |overflow_only| is true, start throbbing only if |node| is hidden in
363 // the overflow menu.
364 void StartThrobbing(const bookmarks::BookmarkNode
* node
, bool overflow_only
);
366 // Returns the view to throb when a node is removed. |parent| is the parent of
367 // the node that was removed, and |old_index| the index of the node that was
369 views::CustomButton
* DetermineViewToThrobFromRemove(
370 const bookmarks::BookmarkNode
* parent
,
373 // Updates the colors for all the child objects in the bookmarks bar.
376 // Updates the visibility of |other_bookmarks_button_|,
377 // |managed_bookmarks_button_|, and |supervised_bookmarks_button_|. Also
378 // shows or hides the separator if required.
379 // Returns true if something changed and a LayoutAndPaint() is needed.
380 bool UpdateOtherAndManagedButtonsVisibility();
382 // Updates the visibility of |bookmarks_separator_view_|.
383 void UpdateBookmarksSeparatorVisibility();
385 // Updates the visibility of the apps shortcut based on the pref value.
386 void OnAppsPageShortcutVisibilityPrefChanged();
388 void OnShowManagedBookmarksPrefChanged();
390 void LayoutAndPaint() {
395 // Needed to react to kShowAppsShortcutInBookmarkBar changes.
396 PrefChangeRegistrar profile_pref_registrar_
;
398 // Used for opening urls.
399 content::PageNavigator
* page_navigator_
;
401 // BookmarkModel that owns the entries and folders that are shown in this
402 // view. This is owned by the Profile.
403 bookmarks::BookmarkModel
* model_
;
405 // ManagedBookmarkService. This is owned by the Profile.
406 bookmarks::ManagedBookmarkService
* managed_
;
408 // Used to manage showing a Menu, either for the most recently bookmarked
409 // entries, or for the starred folder.
410 BookmarkMenuController
* bookmark_menu_
;
412 // Used when showing a menu for drag and drop. That is, if the user drags
413 // over a folder this becomes non-null and manages the menu showing the
414 // contents of the node.
415 BookmarkMenuController
* bookmark_drop_menu_
;
417 // If non-NULL we're showing a context menu for one of the items on the
419 scoped_ptr
<BookmarkContextMenu
> context_menu_
;
421 // Shows the "Other Bookmarks" folder button.
422 views::MenuButton
* other_bookmarks_button_
;
424 // Shows the managed bookmarks entries.
425 views::MenuButton
* managed_bookmarks_button_
;
427 // Shows the supervised bookmarks entries.
428 views::MenuButton
* supervised_bookmarks_button_
;
430 // Shows the Apps page shortcut.
431 views::LabelButton
* apps_page_shortcut_
;
433 // Used to track drops on the bookmark bar view.
434 scoped_ptr
<DropInfo
> drop_info_
;
436 // Visible if not all the bookmark buttons fit.
437 views::MenuButton
* overflow_button_
;
439 // Shows a text and a link to import bookmarks if there are no bookmarks in
440 // the Bookmarks Bar.
441 views::View
* instructions_
;
443 ButtonSeparatorView
* bookmarks_separator_view_
;
446 BrowserView
* browser_view_
;
448 // True if the owning browser is showing an infobar.
449 bool infobar_visible_
;
451 // Animation controlling showing and hiding of the bar.
452 scoped_ptr
<gfx::SlideAnimation
> size_animation_
;
454 // If the bookmark bubble is showing, this is the visible ancestor of the URL.
455 // The visible ancestor is either the |other_bookmarks_button_|,
456 // |overflow_button_| or a button on the bar.
457 views::CustomButton
* throbbing_view_
;
459 BookmarkBar::State bookmark_bar_state_
;
461 // Are we animating to or from the detached state?
462 bool animating_detached_
;
464 base::ObserverList
<BookmarkBarViewObserver
> observers_
;
466 // Factory used to delay showing of the drop menu.
467 base::WeakPtrFactory
<BookmarkBarView
> show_folder_method_factory_
;
469 DISALLOW_COPY_AND_ASSIGN(BookmarkBarView
);
472 #endif // CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_BAR_VIEW_H_