views: Eliminate DetachableToolbarView.
[chromium-blink-merge.git] / chrome / browser / ui / views / bookmarks / bookmark_bar_view.h
blob3ad4c5bb742a885ecf6e73a7e7a177b673d875ec
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>
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 "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;
34 class BookmarkModel;
35 class Browser;
36 class BrowserView;
37 class ChromeBookmarkClient;
38 class Profile;
40 namespace content {
41 class PageNavigator;
44 namespace gfx {
45 class SlideAnimation;
48 namespace views {
49 class CustomButton;
50 class MenuButton;
51 class MenuItemView;
52 class LabelButton;
55 // BookmarkBarView renders the BookmarkModel. Each starred entry on the
56 // BookmarkBar is rendered as a MenuButton. An additional MenuButton aligned to
57 // the right allows the user to quickly see recently starred entries.
59 // BookmarkBarView shows the bookmarks from a specific Profile. BookmarkBarView
60 // waits until the HistoryService for the profile has been loaded before
61 // creating the BookmarkModel.
62 class BookmarkBarView : public views::AccessiblePaneView,
63 public bookmarks::BookmarkModelObserver,
64 public views::MenuButtonListener,
65 public views::ButtonListener,
66 public views::ContextMenuController,
67 public views::DragController,
68 public gfx::AnimationDelegate,
69 public BookmarkMenuControllerObserver,
70 public BookmarkBarInstructionsDelegate,
71 public BookmarkBubbleViewObserver {
72 public:
73 // The internal view class name.
74 static const char kViewClassName[];
76 // |browser_view| can be NULL during tests.
77 BookmarkBarView(Browser* browser, BrowserView* browser_view);
78 ~BookmarkBarView() override;
80 static void DisableAnimationsForTesting(bool disabled);
82 // Returns the current browser.
83 Browser* browser() const { return browser_; }
85 void AddObserver(BookmarkBarViewObserver* observer);
86 void RemoveObserver(BookmarkBarViewObserver* observer);
88 // Sets the PageNavigator that is used when the user selects an entry on
89 // the bookmark bar.
90 void SetPageNavigator(content::PageNavigator* navigator);
92 // Sets whether the containing browser is showing an infobar. This affects
93 // layout during animation.
94 void set_infobar_visible(bool infobar_visible) {
95 infobar_visible_ = infobar_visible;
98 // Changes the state of the bookmark bar.
99 void SetBookmarkBarState(BookmarkBar::State state,
100 BookmarkBar::AnimateChangeType animate_type);
102 // Returns the toolbar overlap when fully detached.
103 int GetFullyDetachedToolbarOverlap() const;
105 // Whether or not we are animating.
106 bool is_animating();
108 // If |loc| is over a bookmark button the node is returned corresponding to
109 // the button and |model_start_index| is set to 0. If a overflow button is
110 // showing and |loc| is over the overflow button, the bookmark bar node is
111 // returned and |model_start_index| is set to the index of the first node
112 // contained in the overflow menu.
113 const BookmarkNode* GetNodeForButtonAtModelIndex(const gfx::Point& loc,
114 int* model_start_index);
116 // Returns the MenuButton for node.
117 views::MenuButton* GetMenuButtonForNode(const BookmarkNode* node);
119 // Returns the position to anchor the menu for |button| at.
120 void GetAnchorPositionForButton(views::MenuButton* button,
121 views::MenuAnchorPosition* anchor);
123 // Returns the button responsible for showing bookmarks in the
124 // "Other Bookmarks" folder.
125 views::MenuButton* other_bookmarks_button() const {
126 return other_bookmarks_button_;
129 // Returns the button used when not all the items on the bookmark bar fit.
130 views::MenuButton* overflow_button() const { return overflow_button_; }
132 // Returns the active MenuItemView, or NULL if a menu isn't showing.
133 views::MenuItemView* GetMenu();
135 // Returns the context menu, or null if one isn't showing.
136 views::MenuItemView* GetContextMenu();
138 // Returns the drop MenuItemView, or NULL if a menu isn't showing.
139 views::MenuItemView* GetDropMenu();
141 // If a button is currently throbbing, it is stopped. If immediate is true
142 // the throb stops immediately, otherwise it stops after a couple more
143 // throbs.
144 void StopThrobbing(bool immediate);
146 // Returns the tooltip text for the specified url and title. The returned
147 // text is clipped to fit within the bounds of the monitor. |context| is
148 // used to determine which gfx::Screen is used to retrieve bounds.
150 // Note that we adjust the direction of both the URL and the title based on
151 // the locale so that pure LTR strings are displayed properly in RTL locales.
152 static base::string16 CreateToolTipForURLAndTitle(const views::Widget* widget,
153 const gfx::Point& screen_loc,
154 const GURL& url,
155 const base::string16& title,
156 Profile* profile);
158 // Returns true if Bookmarks Bar is currently detached from the Toolbar.
159 bool IsDetached() const;
161 // Returns the current state of the resize animation (show/hide).
162 double GetAnimationValue() const;
164 // Returns the current amount of overlap atop the browser toolbar.
165 int GetToolbarOverlap() const;
167 // views::View:
168 gfx::Size GetPreferredSize() const override;
169 gfx::Size GetMinimumSize() const override;
170 bool CanProcessEventsWithinSubtree() const override;
171 void Layout() override;
172 void ViewHierarchyChanged(
173 const ViewHierarchyChangedDetails& details) override;
174 void PaintChildren(gfx::Canvas* canvas,
175 const views::CullSet& cull_set) override;
176 bool GetDropFormats(
177 int* formats,
178 std::set<ui::OSExchangeData::CustomFormat>* custom_formats) override;
179 bool AreDropTypesRequired() override;
180 bool CanDrop(const ui::OSExchangeData& data) override;
181 void OnDragEntered(const ui::DropTargetEvent& event) override;
182 int OnDragUpdated(const ui::DropTargetEvent& event) override;
183 void OnDragExited() override;
184 int OnPerformDrop(const ui::DropTargetEvent& event) override;
185 void OnThemeChanged() override;
186 const char* GetClassName() const override;
187 void SetVisible(bool visible) override;
189 // AccessiblePaneView:
190 void GetAccessibleState(ui::AXViewState* state) override;
192 // gfx::AnimationDelegate:
193 void AnimationProgressed(const gfx::Animation* animation) override;
194 void AnimationEnded(const gfx::Animation* animation) override;
196 // BookmarkMenuControllerObserver:
197 void BookmarkMenuControllerDeleted(
198 BookmarkMenuController* controller) override;
200 // BookmarkBarInstructionsDelegate:
201 void OnImportBookmarks() override;
203 // BookmarkBubbleViewObserver:
204 void OnBookmarkBubbleShown(const GURL& url) override;
205 void OnBookmarkBubbleHidden() override;
207 // bookmarks::BookmarkModelObserver:
208 void BookmarkModelLoaded(BookmarkModel* model, bool ids_reassigned) override;
209 void BookmarkModelBeingDeleted(BookmarkModel* model) override;
210 void BookmarkNodeMoved(BookmarkModel* model,
211 const BookmarkNode* old_parent,
212 int old_index,
213 const BookmarkNode* new_parent,
214 int new_index) override;
215 void BookmarkNodeAdded(BookmarkModel* model,
216 const BookmarkNode* parent,
217 int index) override;
218 void BookmarkNodeRemoved(BookmarkModel* model,
219 const BookmarkNode* parent,
220 int old_index,
221 const BookmarkNode* node,
222 const std::set<GURL>& removed_urls) override;
223 void BookmarkAllUserNodesRemoved(BookmarkModel* model,
224 const std::set<GURL>& removed_urls) override;
225 void BookmarkNodeChanged(BookmarkModel* model,
226 const BookmarkNode* node) override;
227 void BookmarkNodeChildrenReordered(BookmarkModel* model,
228 const BookmarkNode* node) override;
229 void BookmarkNodeFaviconChanged(BookmarkModel* model,
230 const BookmarkNode* node) override;
232 // views::DragController:
233 void WriteDragDataForView(views::View* sender,
234 const gfx::Point& press_pt,
235 ui::OSExchangeData* data) override;
236 int GetDragOperationsForView(views::View* sender,
237 const gfx::Point& p) override;
238 bool CanStartDragForView(views::View* sender,
239 const gfx::Point& press_pt,
240 const gfx::Point& p) override;
242 // views::MenuButtonListener:
243 void OnMenuButtonClicked(views::View* view, const gfx::Point& point) override;
245 // views::ButtonListener:
246 void ButtonPressed(views::Button* sender, const ui::Event& event) override;
248 // views::ContextMenuController:
249 void ShowContextMenuForView(views::View* source,
250 const gfx::Point& point,
251 ui::MenuSourceType source_type) override;
253 private:
254 class ButtonSeparatorView;
255 struct DropInfo;
256 struct DropLocation;
258 friend class BookmarkBarViewTestHelper;
259 friend class BookmarkBarViewEventTestBase;
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 bookmark bar url/folder buttons that have been
280 // created. This does not necessarily represent the number of bookmark bar
281 // nodes, nor the number of visible bookmark bar buttons. Buttons are created
282 // lazily to fill available space, and may be hidden for ordering or sizing
283 // changes.
284 int GetBookmarkButtonCount() const;
286 // Returns the button at the specified index.
287 views::LabelButton* GetBookmarkButton(int index);
289 // Returns BOOKMARK_LAUNCH_LOCATION_DETACHED_BAR or
290 // BOOKMARK_LAUNCH_LOCATION_ATTACHED_BAR based on detached state.
291 BookmarkLaunchLocation GetBookmarkLaunchLocation() const;
293 // Returns the index of the first hidden bookmark button. If all buttons are
294 // visible, this returns GetBookmarkButtonCount().
295 int GetFirstHiddenNodeIndex();
297 // Creates the button showing the "Other Bookmarks" folder.
298 views::MenuButton* CreateOtherBookmarksButton();
300 // Creates the button showing the "Managed Bookmarks" folder.
301 views::MenuButton* CreateManagedBookmarksButton();
303 // Creates the button used when not all bookmark buttons fit.
304 views::MenuButton* CreateOverflowButton();
306 // Creates the button for rendering the specified bookmark node.
307 views::View* CreateBookmarkButton(const BookmarkNode* node);
309 // Creates the button for rendering the apps page shortcut.
310 views::LabelButton* CreateAppsPageShortcutButton();
312 // Configures the button from the specified node. This sets the text,
313 // and icon.
314 void ConfigureButton(const BookmarkNode* node, views::LabelButton* button);
316 // Implementation for BookmarkNodeAddedImpl. Returns true if LayoutAndPaint()
317 // is required.
318 bool BookmarkNodeAddedImpl(BookmarkModel* model,
319 const BookmarkNode* parent,
320 int index);
322 // Implementation for BookmarkNodeRemoved. Returns true if LayoutAndPaint() is
323 // required.
324 bool BookmarkNodeRemovedImpl(BookmarkModel* model,
325 const BookmarkNode* parent,
326 int index);
328 // If the node is a child of the root node, the button is updated
329 // appropriately.
330 void BookmarkNodeChangedImpl(BookmarkModel* model, const BookmarkNode* node);
332 // Shows the menu used during drag and drop for the specified node.
333 void ShowDropFolderForNode(const BookmarkNode* node);
335 // Cancels the timer used to show a drop menu.
336 void StopShowFolderDropMenuTimer();
338 // Stars the timer used to show a drop menu for node.
339 void StartShowFolderDropMenuTimer(const BookmarkNode* node);
341 // Calculates the location for the drop in |location|.
342 void CalculateDropLocation(const ui::DropTargetEvent& event,
343 const bookmarks::BookmarkNodeData& data,
344 DropLocation* location);
346 // Writes a BookmarkNodeData for node to data.
347 void WriteBookmarkDragData(const BookmarkNode* node,
348 ui::OSExchangeData* data);
350 // This determines which view should throb and starts it
351 // throbbing (e.g when the bookmark bubble is showing).
352 // If |overflow_only| is true, start throbbing only if |node| is hidden in
353 // the overflow menu.
354 void StartThrobbing(const BookmarkNode* node, bool overflow_only);
356 // Returns the view to throb when a node is removed. |parent| is the parent of
357 // the node that was removed, and |old_index| the index of the node that was
358 // removed.
359 views::CustomButton* DetermineViewToThrobFromRemove(
360 const BookmarkNode* parent,
361 int old_index);
363 // Updates the colors for all the child objects in the bookmarks bar.
364 void UpdateColors();
366 // Updates the visibility of |other_bookmarks_button_| and
367 // |managed_bookmarks_button_|. Also shows or hides the separator if required.
368 // Returns true if something changed and a LayoutAndPaint() is needed.
369 bool UpdateOtherAndManagedButtonsVisibility();
371 // Updates the visibility of |bookmarks_separator_view_|.
372 void UpdateBookmarksSeparatorVisibility();
374 // Updates the visibility of the apps shortcut based on the pref value.
375 void OnAppsPageShortcutVisibilityPrefChanged();
377 void OnShowManagedBookmarksPrefChanged();
379 void LayoutAndPaint() {
380 Layout();
381 SchedulePaint();
384 // Needed to react to kShowAppsShortcutInBookmarkBar changes.
385 PrefChangeRegistrar profile_pref_registrar_;
387 // Used for opening urls.
388 content::PageNavigator* page_navigator_;
390 // BookmarkModel that owns the entries and folders that are shown in this
391 // view. This is owned by the Profile.
392 BookmarkModel* model_;
394 // ChromeBookmarkClient. This is owned by the Profile.
395 ChromeBookmarkClient* client_;
397 // Used to manage showing a Menu, either for the most recently bookmarked
398 // entries, or for the starred folder.
399 BookmarkMenuController* bookmark_menu_;
401 // Used when showing a menu for drag and drop. That is, if the user drags
402 // over a folder this becomes non-null and manages the menu showing the
403 // contents of the node.
404 BookmarkMenuController* bookmark_drop_menu_;
406 // If non-NULL we're showing a context menu for one of the items on the
407 // bookmark bar.
408 scoped_ptr<BookmarkContextMenu> context_menu_;
410 // Shows the "Other Bookmarks" folder button.
411 views::MenuButton* other_bookmarks_button_;
413 // Shows the managed bookmarks entries.
414 views::MenuButton* managed_bookmarks_button_;
416 // Shows the Apps page shortcut.
417 views::LabelButton* apps_page_shortcut_;
419 // Used to track drops on the bookmark bar view.
420 scoped_ptr<DropInfo> drop_info_;
422 // Visible if not all the bookmark buttons fit.
423 views::MenuButton* overflow_button_;
425 // Shows a text and a link to import bookmarks if there are no bookmarks in
426 // the Bookmarks Bar.
427 views::View* instructions_;
429 ButtonSeparatorView* bookmarks_separator_view_;
431 Browser* browser_;
432 BrowserView* browser_view_;
434 // True if the owning browser is showing an infobar.
435 bool infobar_visible_;
437 // Animation controlling showing and hiding of the bar.
438 scoped_ptr<gfx::SlideAnimation> size_animation_;
440 // If the bookmark bubble is showing, this is the visible ancestor of the URL.
441 // The visible ancestor is either the |other_bookmarks_button_|,
442 // |overflow_button_| or a button on the bar.
443 views::CustomButton* throbbing_view_;
445 BookmarkBar::State bookmark_bar_state_;
447 // Are we animating to or from the detached state?
448 bool animating_detached_;
450 ObserverList<BookmarkBarViewObserver> observers_;
452 // Factory used to delay showing of the drop menu.
453 base::WeakPtrFactory<BookmarkBarView> show_folder_method_factory_;
455 DISALLOW_COPY_AND_ASSIGN(BookmarkBarView);
458 #endif // CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_BAR_VIEW_H_