Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: chrome/browser/ui/views/bookmarks/bookmark_bar_view.cc

Issue 677533003: Changes BookmarkBarView to only create buttons as needed (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: done Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h" 5 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <limits> 8 #include <limits>
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
(...skipping 765 matching lines...) Expand 10 before | Expand all | Expand 10 after
776 } 776 }
777 if (apps_page_shortcut_->visible()) { 777 if (apps_page_shortcut_->visible()) {
778 gfx::Size size = apps_page_shortcut_->GetPreferredSize(); 778 gfx::Size size = apps_page_shortcut_->GetPreferredSize();
779 width += size.width() + kButtonPadding; 779 width += size.width() + kButtonPadding;
780 } 780 }
781 781
782 return gfx::Size(width, height); 782 return gfx::Size(width, height);
783 } 783 }
784 784
785 void BookmarkBarView::Layout() { 785 void BookmarkBarView::Layout() {
786 // If we have no model we're being destroyed and there is no point in doing a
msw 2014/10/27 20:40:24 nit: "Skip layout during destruction, when no mode
sky 2014/10/27 21:48:18 Done.
787 // layout.
788 if (!model_)
789 return;
790
786 int x = kLeftMargin; 791 int x = kLeftMargin;
787 int top_margin = IsDetached() ? kDetachedTopMargin : 0; 792 int top_margin = IsDetached() ? kDetachedTopMargin : 0;
788 int y = top_margin; 793 int y = top_margin;
789 int width = View::width() - kRightMargin - kLeftMargin; 794 int width = View::width() - kRightMargin - kLeftMargin;
790 int height = chrome::kBookmarkBarHeight - kBottomMargin; 795 int height = chrome::kBookmarkBarHeight - kBottomMargin;
791 int separator_margin = kSeparatorMargin; 796 int separator_margin = kSeparatorMargin;
792 797
793 if (IsDetached()) { 798 if (IsDetached()) {
794 double current_state = 1 - size_animation_->GetCurrentValue(); 799 double current_state = 1 - size_animation_->GetCurrentValue();
795 x += static_cast<int>(kNewtabHorizontalPadding * current_state); 800 x += static_cast<int>(kNewtabHorizontalPadding * current_state);
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
837 model_->bookmark_bar_node()->child_count() == 0; 842 model_->bookmark_bar_node()->child_count() == 0;
838 instructions_->SetVisible(show_instructions); 843 instructions_->SetVisible(show_instructions);
839 if (show_instructions) { 844 if (show_instructions) {
840 gfx::Size pref = instructions_->GetPreferredSize(); 845 gfx::Size pref = instructions_->GetPreferredSize();
841 instructions_->SetBounds( 846 instructions_->SetBounds(
842 x + kInstructionsPadding, y, 847 x + kInstructionsPadding, y,
843 std::min(static_cast<int>(pref.width()), 848 std::min(static_cast<int>(pref.width()),
844 max_x - x), 849 max_x - x),
845 height); 850 height);
846 } else { 851 } else {
847 for (int i = 0; i < GetBookmarkButtonCount(); ++i) { 852 bool last_visible = x < max_x;
853 int button_count = GetBookmarkButtonCount();
854 for (int i = 0; i <= button_count; ++i) {
855 if (i == button_count) {
856 // Add another button if there is room for it (and there is another
857 // button to load).
858 if (!last_visible || !model_->loaded() ||
859 model_->bookmark_bar_node()->child_count() <= button_count)
860 break;
861 LoadNextBookmarkButton();
862 DCHECK_NE(button_count, GetBookmarkButtonCount());
msw 2014/10/27 20:40:24 nit: DCHECK_LT, if at all?
sky 2014/10/27 21:48:18 I removed the DCHECK as LoadNextBookmarkButton has
863 button_count = GetBookmarkButtonCount();
864 }
848 views::View* child = child_at(i); 865 views::View* child = child_at(i);
849 gfx::Size pref = child->GetPreferredSize(); 866 gfx::Size pref = child->GetPreferredSize();
850 int next_x = x + pref.width() + kButtonPadding; 867 int next_x = x + pref.width() + kButtonPadding;
851 child->SetVisible(next_x < max_x); 868 last_visible = next_x < max_x;
852 child->SetBounds(x, y, pref.width(), height); 869 child->SetVisible(last_visible);
870 // Only need to set bounds if the view is actually visible.
871 if (last_visible)
872 child->SetBounds(x, y, pref.width(), height);
853 x = next_x; 873 x = next_x;
854 } 874 }
855 } 875 }
856 876
857 // Layout the right side of the bar. 877 // Layout the right side of the bar.
msw 2014/10/27 20:40:24 nit: Remove this comment.
sky 2014/10/27 21:48:18 Done.
858 const bool all_visible = (GetBookmarkButtonCount() == 0 ||
859 child_at(GetBookmarkButtonCount() - 1)->visible());
860 878
861 // Layout the right side buttons. 879 // Layout the right side buttons.
862 x = max_x + kButtonPadding; 880 x = max_x + kButtonPadding;
863 881
864 // The overflow button. 882 // The overflow button.
865 overflow_button_->SetBounds(x, y, overflow_pref.width(), height); 883 overflow_button_->SetBounds(x, y, overflow_pref.width(), height);
866 overflow_button_->SetVisible(!all_visible); 884 const bool show_overflow =
885 model_->loaded() &&
886 (model_->bookmark_bar_node()->child_count() != GetBookmarkButtonCount() ||
msw 2014/10/27 20:40:24 nit: explicitly check for '>' instead of '!='.
sky 2014/10/27 21:48:18 Done.
887 (GetBookmarkButtonCount() > 0 &&
888 !GetBookmarkButton(GetBookmarkButtonCount() - 1)->visible()));
889 overflow_button_->SetVisible(show_overflow);
867 x += overflow_pref.width(); 890 x += overflow_pref.width();
868 891
869 // Separator. 892 // Separator.
870 if (bookmarks_separator_view_->visible()) { 893 if (bookmarks_separator_view_->visible()) {
871 bookmarks_separator_view_->SetBounds(x, 894 bookmarks_separator_view_->SetBounds(x,
872 y - top_margin, 895 y - top_margin,
873 bookmarks_separator_pref.width(), 896 bookmarks_separator_pref.width(),
874 height + top_margin + kBottomMargin - 897 height + top_margin + kBottomMargin -
875 separator_margin); 898 separator_margin);
876 899
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
1145 1168
1146 void BookmarkBarView::OnBookmarkBubbleHidden() { 1169 void BookmarkBarView::OnBookmarkBubbleHidden() {
1147 StopThrobbing(false); 1170 StopThrobbing(false);
1148 } 1171 }
1149 1172
1150 void BookmarkBarView::BookmarkModelLoaded(BookmarkModel* model, 1173 void BookmarkBarView::BookmarkModelLoaded(BookmarkModel* model,
1151 bool ids_reassigned) { 1174 bool ids_reassigned) {
1152 // There should be no buttons. If non-zero it means Load was invoked more than 1175 // There should be no buttons. If non-zero it means Load was invoked more than
1153 // once, or we didn't properly clear things. Either of which shouldn't happen. 1176 // once, or we didn't properly clear things. Either of which shouldn't happen.
1154 DCHECK_EQ(0, GetBookmarkButtonCount()); 1177 DCHECK_EQ(0, GetBookmarkButtonCount());
1155 const BookmarkNode* node = model->bookmark_bar_node(); 1178 // The actual bookmark buttons are added from Layout().
msw 2014/10/27 20:40:24 nit: combine with the comment above.
sky 2014/10/27 21:48:18 Done.
1156 DCHECK(node);
1157 // Create a button for each of the children on the bookmark bar.
1158 for (int i = 0, child_count = node->child_count(); i < child_count; ++i)
1159 AddChildViewAt(CreateBookmarkButton(node->GetChild(i)), i);
1160 DCHECK(model->other_node()); 1179 DCHECK(model->other_node());
1161 other_bookmarked_button_->SetAccessibleName(model->other_node()->GetTitle()); 1180 other_bookmarked_button_->SetAccessibleName(model->other_node()->GetTitle());
1162 other_bookmarked_button_->SetText(model->other_node()->GetTitle()); 1181 other_bookmarked_button_->SetText(model->other_node()->GetTitle());
1163 managed_bookmarks_button_->SetAccessibleName( 1182 managed_bookmarks_button_->SetAccessibleName(
1164 client_->managed_node()->GetTitle()); 1183 client_->managed_node()->GetTitle());
1165 managed_bookmarks_button_->SetText(client_->managed_node()->GetTitle()); 1184 managed_bookmarks_button_->SetText(client_->managed_node()->GetTitle());
1166 UpdateColors(); 1185 UpdateColors();
1167 UpdateOtherAndManagedButtonsVisibility(); 1186 UpdateOtherAndManagedButtonsVisibility();
1168 other_bookmarked_button_->SetEnabled(true); 1187 other_bookmarked_button_->SetEnabled(true);
1169 managed_bookmarks_button_->SetEnabled(true); 1188 managed_bookmarks_button_->SetEnabled(true);
(...skipping 13 matching lines...) Expand all
1183 const BookmarkNode* new_parent, 1202 const BookmarkNode* new_parent,
1184 int new_index) { 1203 int new_index) {
1185 bool was_throbbing = throbbing_view_ && 1204 bool was_throbbing = throbbing_view_ &&
1186 throbbing_view_ == DetermineViewToThrobFromRemove(old_parent, old_index); 1205 throbbing_view_ == DetermineViewToThrobFromRemove(old_parent, old_index);
1187 if (was_throbbing) 1206 if (was_throbbing)
1188 throbbing_view_->StopThrobbing(); 1207 throbbing_view_->StopThrobbing();
1189 bool needs_layout_and_paint = 1208 bool needs_layout_and_paint =
1190 BookmarkNodeRemovedImpl(model, old_parent, old_index); 1209 BookmarkNodeRemovedImpl(model, old_parent, old_index);
1191 if (BookmarkNodeAddedImpl(model, new_parent, new_index)) 1210 if (BookmarkNodeAddedImpl(model, new_parent, new_index))
1192 needs_layout_and_paint = true; 1211 needs_layout_and_paint = true;
1193 if (was_throbbing) 1212 if (was_throbbing && new_index < GetBookmarkButtonCount())
1194 StartThrobbing(new_parent->GetChild(new_index), false); 1213 StartThrobbing(new_parent->GetChild(new_index), false);
1195 if (needs_layout_and_paint) 1214 if (needs_layout_and_paint)
1196 LayoutAndPaint(); 1215 LayoutAndPaint();
1197 } 1216 }
1198 1217
1199 void BookmarkBarView::BookmarkNodeAdded(BookmarkModel* model, 1218 void BookmarkBarView::BookmarkNodeAdded(BookmarkModel* model,
1200 const BookmarkNode* parent, 1219 const BookmarkNode* parent,
1201 int index) { 1220 int index) {
1202 if (BookmarkNodeAddedImpl(model, parent, index)) 1221 if (BookmarkNodeAddedImpl(model, parent, index))
1203 LayoutAndPaint(); 1222 LayoutAndPaint();
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
1500 } 1519 }
1501 1520
1502 int BookmarkBarView::GetBookmarkButtonCount() const { 1521 int BookmarkBarView::GetBookmarkButtonCount() const {
1503 // We contain six non-bookmark button views: managed bookmarks, 1522 // We contain six non-bookmark button views: managed bookmarks,
1504 // other bookmarks, bookmarks separator, chevrons (for overflow), apps page, 1523 // other bookmarks, bookmarks separator, chevrons (for overflow), apps page,
1505 // and the instruction label. 1524 // and the instruction label.
1506 return child_count() - 6; 1525 return child_count() - 6;
1507 } 1526 }
1508 1527
1509 views::LabelButton* BookmarkBarView::GetBookmarkButton(int index) { 1528 views::LabelButton* BookmarkBarView::GetBookmarkButton(int index) {
1510 DCHECK(index >= 0 && index < GetBookmarkButtonCount()); 1529 // CHECK as otherwise we may do the wrong cast.
1530 CHECK(index >= 0 && index < GetBookmarkButtonCount());
1511 return static_cast<views::LabelButton*>(child_at(index)); 1531 return static_cast<views::LabelButton*>(child_at(index));
1512 } 1532 }
1513 1533
1514 BookmarkLaunchLocation BookmarkBarView::GetBookmarkLaunchLocation() const { 1534 BookmarkLaunchLocation BookmarkBarView::GetBookmarkLaunchLocation() const {
1515 return IsDetached() ? BOOKMARK_LAUNCH_LOCATION_DETACHED_BAR : 1535 return IsDetached() ? BOOKMARK_LAUNCH_LOCATION_DETACHED_BAR :
1516 BOOKMARK_LAUNCH_LOCATION_ATTACHED_BAR; 1536 BOOKMARK_LAUNCH_LOCATION_ATTACHED_BAR;
1517 } 1537 }
1518 1538
1519 int BookmarkBarView::GetFirstHiddenNodeIndex() { 1539 int BookmarkBarView::GetFirstHiddenNodeIndex() {
1520 const int bb_count = GetBookmarkButtonCount(); 1540 const int bb_count = GetBookmarkButtonCount();
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
1619 if (node->is_url()) { 1639 if (node->is_url()) {
1620 const gfx::Image& favicon = model_->GetFavicon(node); 1640 const gfx::Image& favicon = model_->GetFavicon(node);
1621 if (!favicon.IsEmpty()) 1641 if (!favicon.IsEmpty())
1622 button->SetImage(views::Button::STATE_NORMAL, *favicon.ToImageSkia()); 1642 button->SetImage(views::Button::STATE_NORMAL, *favicon.ToImageSkia());
1623 else 1643 else
1624 button->SetImage(views::Button::STATE_NORMAL, GetDefaultFavicon()); 1644 button->SetImage(views::Button::STATE_NORMAL, GetDefaultFavicon());
1625 } 1645 }
1626 button->SetMaxSize(gfx::Size(kMaxButtonWidth, 0)); 1646 button->SetMaxSize(gfx::Size(kMaxButtonWidth, 0));
1627 } 1647 }
1628 1648
1649 void BookmarkBarView::LoadNextBookmarkButton() {
msw 2014/10/27 20:40:24 This is only called in Layout, consider in-lining
sky 2014/10/27 21:48:18 Done.
1650 DCHECK(model_->loaded());
1651 const int model_index = GetBookmarkButtonCount();
1652 DCHECK_LT(model_index, model_->bookmark_bar_node()->child_count());
1653 AddChildViewAt(
1654 CreateBookmarkButton(model_->bookmark_bar_node()->GetChild(model_index)),
1655 model_index);
1656 // NOTE: no layout/schedulepaint here. We assume this is called during layout.
1657 }
1658
1629 bool BookmarkBarView::BookmarkNodeAddedImpl(BookmarkModel* model, 1659 bool BookmarkBarView::BookmarkNodeAddedImpl(BookmarkModel* model,
1630 const BookmarkNode* parent, 1660 const BookmarkNode* parent,
1631 int index) { 1661 int index) {
1632 const bool needs_layout_and_paint = UpdateOtherAndManagedButtonsVisibility(); 1662 const bool needs_layout_and_paint = UpdateOtherAndManagedButtonsVisibility();
1633 if (parent != model->bookmark_bar_node()) { 1663 if (parent != model->bookmark_bar_node())
1634 // Only children of the bookmark_bar_node get buttons.
1635 return needs_layout_and_paint; 1664 return needs_layout_and_paint;
1665 if (index < GetBookmarkButtonCount()) {
1666 const BookmarkNode* node = parent->GetChild(index);
1667 AddChildViewAt(CreateBookmarkButton(node), index);
1668 return true;
1636 } 1669 }
1637 DCHECK(index >= 0 && index <= GetBookmarkButtonCount()); 1670 // If the new node was added after the last button we've created we may be
1638 const BookmarkNode* node = parent->GetChild(index); 1671 // able to fit it. Assume we can by returning true, which forces a Layout()
1639 ProfileSyncService* sync_service(ProfileSyncServiceFactory:: 1672 // and creation of the button (if it fits).
1640 GetInstance()->GetForProfile(browser_->profile())); 1673 return index == GetBookmarkButtonCount();
1641 if (!throbbing_view_ && sync_service && sync_service->FirstSetupInProgress())
1642 StartThrobbing(node, true);
1643 AddChildViewAt(CreateBookmarkButton(node), index);
1644 return true;
1645 } 1674 }
1646 1675
1647 bool BookmarkBarView::BookmarkNodeRemovedImpl(BookmarkModel* model, 1676 bool BookmarkBarView::BookmarkNodeRemovedImpl(BookmarkModel* model,
1648 const BookmarkNode* parent, 1677 const BookmarkNode* parent,
1649 int index) { 1678 int index) {
1650 const bool needs_layout = UpdateOtherAndManagedButtonsVisibility(); 1679 const bool needs_layout = UpdateOtherAndManagedButtonsVisibility();
1651 1680
1652 StopThrobbing(true); 1681 StopThrobbing(true);
1653 // No need to start throbbing again as the bookmark bubble can't be up at 1682 // No need to start throbbing again as the bookmark bubble can't be up at
1654 // the same time as the user reorders. 1683 // the same time as the user reorders.
1655 1684
1656 if (parent != model->bookmark_bar_node()) { 1685 if (parent != model->bookmark_bar_node()) {
1657 // Only children of the bookmark_bar_node get buttons. 1686 // Only children of the bookmark_bar_node get buttons.
1658 return needs_layout; 1687 return needs_layout;
1659 } 1688 }
1689 if (index >= GetBookmarkButtonCount())
1690 return needs_layout;
1691
1660 delete child_at(index); 1692 delete child_at(index);
1661 return true; 1693 return true;
1662 } 1694 }
1663 1695
1664 void BookmarkBarView::BookmarkNodeChangedImpl(BookmarkModel* model, 1696 void BookmarkBarView::BookmarkNodeChangedImpl(BookmarkModel* model,
1665 const BookmarkNode* node) { 1697 const BookmarkNode* node) {
1666 if (node == client_->managed_node()) { 1698 if (node == client_->managed_node()) {
1667 // The managed node may have its title updated. 1699 // The managed node may have its title updated.
1668 managed_bookmarks_button_->SetAccessibleName( 1700 managed_bookmarks_button_->SetAccessibleName(
1669 client_->managed_node()->GetTitle()); 1701 client_->managed_node()->GetTitle());
1670 managed_bookmarks_button_->SetText(client_->managed_node()->GetTitle()); 1702 managed_bookmarks_button_->SetText(client_->managed_node()->GetTitle());
1671 return; 1703 return;
1672 } 1704 }
1673 1705
1674 if (node->parent() != model->bookmark_bar_node()) { 1706 if (node->parent() != model->bookmark_bar_node()) {
1675 // We only care about nodes on the bookmark bar. 1707 // We only care about nodes on the bookmark bar.
1676 return; 1708 return;
1677 } 1709 }
1678 int index = model->bookmark_bar_node()->GetIndexOf(node); 1710 int index = model->bookmark_bar_node()->GetIndexOf(node);
1679 DCHECK_NE(-1, index); 1711 DCHECK_NE(-1, index);
1712 if (index >= GetBookmarkButtonCount())
1713 return; // Buttons are created as needed.
1680 views::LabelButton* button = GetBookmarkButton(index); 1714 views::LabelButton* button = GetBookmarkButton(index);
1681 gfx::Size old_pref = button->GetPreferredSize(); 1715 const int old_pref_width = button->GetPreferredSize().width();
1682 ConfigureButton(node, button); 1716 ConfigureButton(node, button);
1683 gfx::Size new_pref = button->GetPreferredSize(); 1717 if (old_pref_width != button->GetPreferredSize().width())
1684 if (old_pref.width() != new_pref.width()) {
1685 LayoutAndPaint(); 1718 LayoutAndPaint();
1686 } else if (button->visible()) {
1687 button->SchedulePaint();
1688 }
1689 } 1719 }
1690 1720
1691 void BookmarkBarView::ShowDropFolderForNode(const BookmarkNode* node) { 1721 void BookmarkBarView::ShowDropFolderForNode(const BookmarkNode* node) {
1692 if (bookmark_drop_menu_) { 1722 if (bookmark_drop_menu_) {
1693 if (bookmark_drop_menu_->node() == node) { 1723 if (bookmark_drop_menu_->node() == node) {
1694 // Already showing for the specified node. 1724 // Already showing for the specified node.
1695 return; 1725 return;
1696 } 1726 }
1697 bookmark_drop_menu_->Cancel(); 1727 bookmark_drop_menu_->Cancel();
1698 } 1728 }
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
1955 return; 1985 return;
1956 apps_page_shortcut_->SetVisible(visible); 1986 apps_page_shortcut_->SetVisible(visible);
1957 UpdateBookmarksSeparatorVisibility(); 1987 UpdateBookmarksSeparatorVisibility();
1958 LayoutAndPaint(); 1988 LayoutAndPaint();
1959 } 1989 }
1960 1990
1961 void BookmarkBarView::OnShowManagedBookmarksPrefChanged() { 1991 void BookmarkBarView::OnShowManagedBookmarksPrefChanged() {
1962 if (UpdateOtherAndManagedButtonsVisibility()) 1992 if (UpdateOtherAndManagedButtonsVisibility())
1963 LayoutAndPaint(); 1993 LayoutAndPaint();
1964 } 1994 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698