[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / views / bookmarks / bookmark_bar_view.h
blobf58a790d6694d091ed6809ebb4433dc849998d31
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_
8 #include <set>
9 #include <string>
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/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 BookmarkContextMenu;
32 class Browser;
33 class BrowserView;
34 class Profile;
36 namespace content {
37 class PageNavigator;
40 namespace gfx {
41 class SlideAnimation;
44 namespace views {
45 class CustomButton;
46 class MenuButton;
47 class MenuItemView;
48 class TextButton;
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 {
68 public:
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
92 // the bookmark bar.
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.
109 bool is_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
127 // folder.
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
146 // throbs.
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,
157 const GURL& url,
158 const base::string16& title,
159 Profile* profile);
161 // DetachableToolbarView methods:
162 virtual bool IsDetached() const OVERRIDE;
163 virtual double GetAnimationValue() const OVERRIDE;
164 virtual int GetToolbarOverlap() const OVERRIDE;
166 // View methods:
167 virtual gfx::Size GetPreferredSize() const OVERRIDE;
168 virtual gfx::Size GetMinimumSize() const 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,
174 const views::CullSet& cull_set) OVERRIDE;
175 virtual bool GetDropFormats(
176 int* formats,
177 std::set<ui::OSExchangeData::CustomFormat>* custom_formats) OVERRIDE;
178 virtual bool AreDropTypesRequired() OVERRIDE;
179 virtual bool CanDrop(const ui::OSExchangeData& data) OVERRIDE;
180 virtual void OnDragEntered(const ui::DropTargetEvent& event) OVERRIDE;
181 virtual int OnDragUpdated(const ui::DropTargetEvent& event) OVERRIDE;
182 virtual void OnDragExited() OVERRIDE;
183 virtual int OnPerformDrop(const ui::DropTargetEvent& event) OVERRIDE;
184 virtual void OnThemeChanged() OVERRIDE;
185 virtual const char* GetClassName() const OVERRIDE;
187 // AccessiblePaneView:
188 virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE;
190 // gfx::AnimationDelegate:
191 virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE;
192 virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE;
194 // BookmarkMenuControllerObserver:
195 virtual void BookmarkMenuControllerDeleted(
196 BookmarkMenuController* controller) OVERRIDE;
198 // BookmarkBarInstructionsDelegate:
199 virtual void ShowImportDialog() OVERRIDE;
201 // BookmarkBubbleViewObserver:
202 virtual void OnBookmarkBubbleShown(const GURL& url) OVERRIDE;
203 virtual void OnBookmarkBubbleHidden() OVERRIDE;
205 // BookmarkModelObserver:
206 virtual void BookmarkModelLoaded(BookmarkModel* model,
207 bool ids_reassigned) OVERRIDE;
208 virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE;
209 virtual void BookmarkNodeMoved(BookmarkModel* model,
210 const BookmarkNode* old_parent,
211 int old_index,
212 const BookmarkNode* new_parent,
213 int new_index) OVERRIDE;
214 virtual void BookmarkNodeAdded(BookmarkModel* model,
215 const BookmarkNode* parent,
216 int index) OVERRIDE;
217 virtual void BookmarkNodeRemoved(BookmarkModel* model,
218 const BookmarkNode* parent,
219 int old_index,
220 const BookmarkNode* node,
221 const std::set<GURL>& removed_urls) OVERRIDE;
222 virtual void BookmarkAllNodesRemoved(
223 BookmarkModel* model,
224 const std::set<GURL>& removed_urls) OVERRIDE;
225 virtual void BookmarkNodeChanged(BookmarkModel* model,
226 const BookmarkNode* node) OVERRIDE;
227 virtual void BookmarkNodeChildrenReordered(BookmarkModel* model,
228 const BookmarkNode* node) OVERRIDE;
229 virtual void BookmarkNodeFaviconChanged(BookmarkModel* model,
230 const BookmarkNode* node) OVERRIDE;
232 // views::DragController:
233 virtual void WriteDragDataForView(views::View* sender,
234 const gfx::Point& press_pt,
235 ui::OSExchangeData* data) OVERRIDE;
236 virtual int GetDragOperationsForView(views::View* sender,
237 const gfx::Point& p) OVERRIDE;
238 virtual bool CanStartDragForView(views::View* sender,
239 const gfx::Point& press_pt,
240 const gfx::Point& p) OVERRIDE;
242 // views::MenuButtonListener:
243 virtual void OnMenuButtonClicked(views::View* view,
244 const gfx::Point& point) OVERRIDE;
246 // views::ButtonListener:
247 virtual void ButtonPressed(views::Button* sender,
248 const ui::Event& event) OVERRIDE;
250 // views::ContextMenuController:
251 virtual void ShowContextMenuForView(views::View* source,
252 const gfx::Point& point,
253 ui::MenuSourceType source_type) OVERRIDE;
255 private:
256 class ButtonSeparatorView;
257 struct DropInfo;
258 struct DropLocation;
260 friend class BookmarkBarViewEventTestBase;
261 FRIEND_TEST_ALL_PREFIXES(BookmarkBarViewTest, SwitchProfile);
262 FRIEND_TEST_ALL_PREFIXES(BookmarkBarViewTest,
263 NoAppsShortcutWithoutInstantExtended);
264 FRIEND_TEST_ALL_PREFIXES(BookmarkBarViewInstantExtendedTest,
265 AppsShortcutVisibility);
267 // Used to identify what the user is dropping onto.
268 enum DropButtonType {
269 DROP_BOOKMARK,
270 DROP_OTHER_FOLDER,
271 DROP_OVERFLOW
274 // Creates recent bookmark button and when visible button as well as
275 // calculating the preferred height.
276 void Init();
278 // NOTE: unless otherwise stated all methods that take an int for an index are
279 // in terms of the bookmark bar view. Typically the view index and model index
280 // are the same, but they may differ during animations or drag and drop.
282 // It's easy to get the mapping wrong. For this reason all these methods are
283 // private.
285 // Returns the number of buttons corresponding to starred urls/folders. This
286 // is equivalent to the number of children the bookmark bar node from the
287 // bookmark bar model has.
288 int GetBookmarkButtonCount() const;
290 // Returns the button at the specified index.
291 views::TextButton* GetBookmarkButton(int index);
293 // Returns BOOKMARK_LAUNCH_LOCATION_DETACHED_BAR or
294 // BOOKMARK_LAUNCH_LOCATION_ATTACHED_BAR based on detached state.
295 BookmarkLaunchLocation GetBookmarkLaunchLocation() const;
297 // Returns the index of the first hidden bookmark button. If all buttons are
298 // visible, this returns GetBookmarkButtonCount().
299 int GetFirstHiddenNodeIndex();
301 // Creates the button showing the other bookmarked items.
302 views::MenuButton* CreateOtherBookmarkedButton();
304 // Creates the button used when not all bookmark buttons fit.
305 views::MenuButton* CreateOverflowButton();
307 // Creates the button for rendering the specified bookmark node.
308 views::View* CreateBookmarkButton(const BookmarkNode* node);
310 // Creates the button for rendering the apps page shortcut.
311 views::TextButton* CreateAppsPageShortcutButton();
313 // Configures the button from the specified node. This sets the text,
314 // and icon.
315 void ConfigureButton(const BookmarkNode* node, views::TextButton* button);
317 // Implementation for BookmarkNodeAddedImpl.
318 void BookmarkNodeAddedImpl(BookmarkModel* model,
319 const BookmarkNode* parent,
320 int index);
322 // Implementation for BookmarkNodeRemoved.
323 void BookmarkNodeRemovedImpl(BookmarkModel* model,
324 const BookmarkNode* parent,
325 int index);
327 // If the node is a child of the root node, the button is updated
328 // appropriately.
329 void BookmarkNodeChangedImpl(BookmarkModel* model, const BookmarkNode* node);
331 // Shows the menu used during drag and drop for the specified node.
332 void ShowDropFolderForNode(const BookmarkNode* node);
334 // Cancels the timer used to show a drop menu.
335 void StopShowFolderDropMenuTimer();
337 // Stars the timer used to show a drop menu for node.
338 void StartShowFolderDropMenuTimer(const BookmarkNode* node);
340 // Calculates the location for the drop in |location|.
341 void CalculateDropLocation(const ui::DropTargetEvent& event,
342 const BookmarkNodeData& data,
343 DropLocation* location);
345 // Writes a BookmarkNodeData for node to data.
346 void WriteBookmarkDragData(const BookmarkNode* node,
347 ui::OSExchangeData* data);
349 // This determines which view should throb and starts it
350 // throbbing (e.g when the bookmark bubble is showing).
351 // If |overflow_only| is true, start throbbing only if |node| is hidden in
352 // the overflow menu.
353 void StartThrobbing(const BookmarkNode* node, bool overflow_only);
355 // Returns the view to throb when a node is removed. |parent| is the parent of
356 // the node that was removed, and |old_index| the index of the node that was
357 // removed.
358 views::CustomButton* DetermineViewToThrobFromRemove(
359 const BookmarkNode* parent,
360 int old_index);
362 // Updates the colors for all the child objects in the bookmarks bar.
363 void UpdateColors();
365 // Updates the visibility of |other_bookmarked_button_|. Also shows or hide
366 // the separator if required.
367 void UpdateOtherBookmarksVisibility();
369 // Updates the visibility of |bookmarks_separator_view_|.
370 void UpdateBookmarksSeparatorVisibility();
372 // This method computes the bounds for the bookmark bar items.
373 void LayoutItems();
375 // Updates the visibility of the apps shortcut based on the pref value.
376 void OnAppsPageShortcutVisibilityPrefChanged();
378 // Needed to react to kShowAppsShortcutInBookmarkBar changes.
379 PrefChangeRegistrar profile_pref_registrar_;
381 // Used for opening urls.
382 content::PageNavigator* page_navigator_;
384 // Model providing details as to the starred entries/folders that should be
385 // shown. This is owned by the Profile.
386 BookmarkModel* model_;
388 // Used to manage showing a Menu, either for the most recently bookmarked
389 // entries, or for the starred folder.
390 BookmarkMenuController* bookmark_menu_;
392 // Used when showing a menu for drag and drop. That is, if the user drags
393 // over a folder this becomes non-null and manages the menu showing the
394 // contents of the node.
395 BookmarkMenuController* bookmark_drop_menu_;
397 // If non-NULL we're showing a context menu for one of the items on the
398 // bookmark bar.
399 scoped_ptr<BookmarkContextMenu> context_menu_;
401 // Shows the other bookmark entries.
402 views::MenuButton* other_bookmarked_button_;
404 // Shows the Apps page shortcut.
405 views::TextButton* apps_page_shortcut_;
407 // Task used to delay showing of the drop menu.
408 base::WeakPtrFactory<BookmarkBarView> show_folder_method_factory_;
410 // Used to track drops on the bookmark bar view.
411 scoped_ptr<DropInfo> drop_info_;
413 // Visible if not all the bookmark buttons fit.
414 views::MenuButton* overflow_button_;
416 // Shows a text and a link to import bookmarks if there are no bookmarks in
417 // the Bookmarks Bar.
418 views::View* instructions_;
420 ButtonSeparatorView* bookmarks_separator_view_;
422 Browser* browser_;
423 BrowserView* browser_view_;
425 // True if the owning browser is showing an infobar.
426 bool infobar_visible_;
428 // Animation controlling showing and hiding of the bar.
429 scoped_ptr<gfx::SlideAnimation> size_animation_;
431 // If the bookmark bubble is showing, this is the visible ancestor of the URL.
432 // The visible ancestor is either the other_bookmarked_button_,
433 // overflow_button_ or a button on the bar.
434 views::CustomButton* throbbing_view_;
436 BookmarkBar::State bookmark_bar_state_;
438 // Are we animating to or from the detached state?
439 bool animating_detached_;
441 DISALLOW_COPY_AND_ASSIGN(BookmarkBarView);
444 #endif // CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_BAR_VIEW_H_