Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / views / bookmarks / bookmark_bar_view.h
blob6e166fa8bfd466706619e44f5ead5dd69e62fbab
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_model_observer.h"
17 #include "chrome/browser/bookmarks/bookmark_node_data.h"
18 #include "chrome/browser/bookmarks/bookmark_stats.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/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_item_view.h"
29 #include "ui/views/drag_controller.h"
31 class BookmarkContextMenu;
32 class Browser;
33 class BrowserView;
35 namespace content {
36 class PageNavigator;
39 namespace gfx {
40 class SlideAnimation;
43 namespace views {
44 class CustomButton;
45 class MenuButton;
46 class TextButton;
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 gfx::AnimationDelegate,
63 public BookmarkMenuControllerObserver,
64 public BookmarkBarInstructionsDelegate,
65 public BookmarkBubbleViewObserver {
66 public:
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
90 // the bookmark bar.
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.
107 bool is_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
125 // folder.
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
144 // throbs.
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 base::string16 CreateToolTipForURLAndTitle(const views::Widget* widget,
154 const gfx::Point& screen_loc,
155 const GURL& url,
156 const base::string16& title,
157 Profile* profile);
159 // DetachableToolbarView methods:
160 virtual bool IsDetached() const OVERRIDE;
161 virtual double GetAnimationValue() const OVERRIDE;
162 virtual int GetToolbarOverlap() const OVERRIDE;
164 // View methods:
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(
173 int* formats,
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 // gfx::AnimationDelegate:
188 virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE;
189 virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE;
191 // BookmarkMenuControllerObserver:
192 virtual void BookmarkMenuControllerDeleted(
193 BookmarkMenuController* controller) OVERRIDE;
195 // 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 BookmarkModelLoaded(BookmarkModel* model,
204 bool ids_reassigned) OVERRIDE;
205 virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE;
206 virtual void BookmarkNodeMoved(BookmarkModel* model,
207 const BookmarkNode* old_parent,
208 int old_index,
209 const BookmarkNode* new_parent,
210 int new_index) OVERRIDE;
211 virtual void BookmarkNodeAdded(BookmarkModel* model,
212 const BookmarkNode* parent,
213 int index) OVERRIDE;
214 virtual void BookmarkNodeRemoved(BookmarkModel* model,
215 const BookmarkNode* parent,
216 int old_index,
217 const BookmarkNode* node) OVERRIDE;
218 virtual void BookmarkAllNodesRemoved(BookmarkModel* model) OVERRIDE;
219 virtual void BookmarkNodeChanged(BookmarkModel* model,
220 const BookmarkNode* node) OVERRIDE;
221 virtual void BookmarkNodeChildrenReordered(BookmarkModel* model,
222 const BookmarkNode* node) OVERRIDE;
223 virtual void BookmarkNodeFaviconChanged(BookmarkModel* model,
224 const BookmarkNode* node) OVERRIDE;
226 // views::DragController:
227 virtual void WriteDragDataForView(views::View* sender,
228 const gfx::Point& press_pt,
229 ui::OSExchangeData* data) OVERRIDE;
230 virtual int GetDragOperationsForView(views::View* sender,
231 const gfx::Point& p) OVERRIDE;
232 virtual bool CanStartDragForView(views::View* sender,
233 const gfx::Point& press_pt,
234 const gfx::Point& p) OVERRIDE;
236 // views::MenuButtonListener:
237 virtual void OnMenuButtonClicked(views::View* view,
238 const gfx::Point& point) OVERRIDE;
240 // views::ButtonListener:
241 virtual void ButtonPressed(views::Button* sender,
242 const ui::Event& event) OVERRIDE;
244 // views::ContextMenuController:
245 virtual void ShowContextMenuForView(views::View* source,
246 const gfx::Point& point,
247 ui::MenuSourceType source_type) OVERRIDE;
249 private:
250 class ButtonSeparatorView;
251 struct DropInfo;
252 struct DropLocation;
254 friend class BookmarkBarViewEventTestBase;
255 FRIEND_TEST_ALL_PREFIXES(BookmarkBarViewTest, SwitchProfile);
256 FRIEND_TEST_ALL_PREFIXES(BookmarkBarViewTest,
257 NoAppsShortcutWithoutInstantExtended);
258 FRIEND_TEST_ALL_PREFIXES(BookmarkBarViewInstantExtendedTest,
259 AppsShortcutVisibility);
261 // Used to identify what the user is dropping onto.
262 enum DropButtonType {
263 DROP_BOOKMARK,
264 DROP_OTHER_FOLDER,
265 DROP_OVERFLOW
268 // Creates recent bookmark button and when visible button as well as
269 // calculating the preferred height.
270 void Init();
272 // NOTE: unless otherwise stated all methods that take an int for an index are
273 // in terms of the bookmark bar view. Typically the view index and model index
274 // are the same, but they may differ during animations or drag and drop.
276 // It's easy to get the mapping wrong. For this reason all these methods are
277 // private.
279 // Returns the number of buttons corresponding to starred urls/folders. This
280 // is equivalent to the number of children the bookmark bar node from the
281 // bookmark bar model has.
282 int GetBookmarkButtonCount();
284 // Returns the button at the specified index.
285 views::TextButton* GetBookmarkButton(int index);
287 // Returns BOOKMARK_LAUNCH_LOCATION_DETACHED_BAR or
288 // BOOKMARK_LAUNCH_LOCATION_ATTACHED_BAR based on detached state.
289 BookmarkLaunchLocation GetBookmarkLaunchLocation() const;
291 // Returns the index of the first hidden bookmark button. If all buttons are
292 // visible, this returns GetBookmarkButtonCount().
293 int GetFirstHiddenNodeIndex();
295 // Creates the button showing the other bookmarked items.
296 views::MenuButton* CreateOtherBookmarkedButton();
298 // Creates the button used when not all bookmark buttons fit.
299 views::MenuButton* CreateOverflowButton();
301 // Creates the button for rendering the specified bookmark node.
302 views::View* CreateBookmarkButton(const BookmarkNode* node);
304 // Creates the button for rendering the apps page shortcut.
305 views::TextButton* CreateAppsPageShortcutButton();
307 // Configures the button from the specified node. This sets the text,
308 // and icon.
309 void ConfigureButton(const BookmarkNode* node, views::TextButton* button);
311 // Implementation for BookmarkNodeAddedImpl.
312 void BookmarkNodeAddedImpl(BookmarkModel* model,
313 const BookmarkNode* parent,
314 int index);
316 // Implementation for BookmarkNodeRemoved.
317 void BookmarkNodeRemovedImpl(BookmarkModel* model,
318 const BookmarkNode* parent,
319 int index);
321 // If the node is a child of the root node, the button is updated
322 // appropriately.
323 void BookmarkNodeChangedImpl(BookmarkModel* model, const BookmarkNode* node);
325 // Shows the menu used during drag and drop for the specified node.
326 void ShowDropFolderForNode(const BookmarkNode* node);
328 // Cancels the timer used to show a drop menu.
329 void StopShowFolderDropMenuTimer();
331 // Stars the timer used to show a drop menu for node.
332 void StartShowFolderDropMenuTimer(const BookmarkNode* node);
334 // Calculates the location for the drop in |location|.
335 void CalculateDropLocation(const ui::DropTargetEvent& event,
336 const BookmarkNodeData& data,
337 DropLocation* location);
339 // Writes a BookmarkNodeData for node to data.
340 void WriteBookmarkDragData(const BookmarkNode* node,
341 ui::OSExchangeData* data);
343 // This determines which view should throb and starts it
344 // throbbing (e.g when the bookmark bubble is showing).
345 // If |overflow_only| is true, start throbbing only if |node| is hidden in
346 // the overflow menu.
347 void StartThrobbing(const BookmarkNode* node, bool overflow_only);
349 // Returns the view to throb when a node is removed. |parent| is the parent of
350 // the node that was removed, and |old_index| the index of the node that was
351 // removed.
352 views::CustomButton* DetermineViewToThrobFromRemove(
353 const BookmarkNode* parent,
354 int old_index);
356 // Updates the colors for all the child objects in the bookmarks bar.
357 void UpdateColors();
359 // Updates the visibility of |other_bookmarked_button_|. Also shows or hide
360 // the separator if required.
361 void UpdateOtherBookmarksVisibility();
363 // Updates the visibility of |bookmarks_separator_view_|.
364 void UpdateBookmarksSeparatorVisibility();
366 // This method computes the bounds for the bookmark bar items. If
367 // |compute_bounds_only| = TRUE, the bounds for the items are just computed,
368 // but are not set. This mode is used by GetPreferredSize() to obtain the
369 // desired bounds. If |compute_bounds_only| = FALSE, the bounds are set.
370 gfx::Size LayoutItems(bool compute_bounds_only);
372 // Updates the visibility of the apps shortcut based on the pref value.
373 void OnAppsPageShortcutVisibilityPrefChanged();
375 // Needed to react to kShowAppsShortcutInBookmarkBar changes.
376 PrefChangeRegistrar profile_pref_registrar_;
378 // Used for opening urls.
379 content::PageNavigator* page_navigator_;
381 // Model providing details as to the starred entries/folders that should be
382 // shown. This is owned by the Profile.
383 BookmarkModel* model_;
385 // Used to manage showing a Menu, either for the most recently bookmarked
386 // entries, or for the starred folder.
387 BookmarkMenuController* bookmark_menu_;
389 // Used when showing a menu for drag and drop. That is, if the user drags
390 // over a folder this becomes non-null and manages the menu showing the
391 // contents of the node.
392 BookmarkMenuController* bookmark_drop_menu_;
394 // If non-NULL we're showing a context menu for one of the items on the
395 // bookmark bar.
396 scoped_ptr<BookmarkContextMenu> context_menu_;
398 // Shows the other bookmark entries.
399 views::MenuButton* other_bookmarked_button_;
401 // Shows the Apps page shortcut.
402 views::TextButton* apps_page_shortcut_;
404 // Task used to delay showing of the drop menu.
405 base::WeakPtrFactory<BookmarkBarView> show_folder_method_factory_;
407 // Used to track drops on the bookmark bar view.
408 scoped_ptr<DropInfo> drop_info_;
410 // Visible if not all the bookmark buttons fit.
411 views::MenuButton* overflow_button_;
413 // Shows a text and a link to import bookmarks if there are no bookmarks in
414 // the Bookmarks Bar.
415 views::View* instructions_;
417 ButtonSeparatorView* bookmarks_separator_view_;
419 Browser* browser_;
420 BrowserView* browser_view_;
422 // True if the owning browser is showing an infobar.
423 bool infobar_visible_;
425 // Animation controlling showing and hiding of the bar.
426 scoped_ptr<gfx::SlideAnimation> size_animation_;
428 // If the bookmark bubble is showing, this is the visible ancestor of the URL.
429 // The visible ancestor is either the other_bookmarked_button_,
430 // overflow_button_ or a button on the bar.
431 views::CustomButton* throbbing_view_;
433 BookmarkBar::State bookmark_bar_state_;
435 // Are we animating to or from the detached state?
436 bool animating_detached_;
438 DISALLOW_COPY_AND_ASSIGN(BookmarkBarView);
441 #endif // CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_BAR_VIEW_H_