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

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: really dont join 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 // Skip layout during destruction, when no model exists.
787 if (!model_)
788 return;
789
786 int x = kLeftMargin; 790 int x = kLeftMargin;
787 int top_margin = IsDetached() ? kDetachedTopMargin : 0; 791 int top_margin = IsDetached() ? kDetachedTopMargin : 0;
788 int y = top_margin; 792 int y = top_margin;
789 int width = View::width() - kRightMargin - kLeftMargin; 793 int width = View::width() - kRightMargin - kLeftMargin;
790 int height = chrome::kBookmarkBarHeight - kBottomMargin; 794 int height = chrome::kBookmarkBarHeight - kBottomMargin;
791 int separator_margin = kSeparatorMargin; 795 int separator_margin = kSeparatorMargin;
792 796
793 if (IsDetached()) { 797 if (IsDetached()) {
794 double current_state = 1 - size_animation_->GetCurrentValue(); 798 double current_state = 1 - size_animation_->GetCurrentValue();
795 x += static_cast<int>(kNewtabHorizontalPadding * current_state); 799 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; 841 model_->bookmark_bar_node()->child_count() == 0;
838 instructions_->SetVisible(show_instructions); 842 instructions_->SetVisible(show_instructions);
839 if (show_instructions) { 843 if (show_instructions) {
840 gfx::Size pref = instructions_->GetPreferredSize(); 844 gfx::Size pref = instructions_->GetPreferredSize();
841 instructions_->SetBounds( 845 instructions_->SetBounds(
842 x + kInstructionsPadding, y, 846 x + kInstructionsPadding, y,
843 std::min(static_cast<int>(pref.width()), 847 std::min(static_cast<int>(pref.width()),
844 max_x - x), 848 max_x - x),
845 height); 849 height);
846 } else { 850 } else {
847 for (int i = 0; i < GetBookmarkButtonCount(); ++i) { 851 bool last_visible = x < max_x;
852 int button_count = GetBookmarkButtonCount();
853 for (int i = 0; i <= button_count; ++i) {
854 if (i == button_count) {
855 // Add another button if there is room for it (and there is another
856 // button to load).
857 if (!last_visible || !model_->loaded() ||
858 model_->bookmark_bar_node()->child_count() <= button_count)
859 break;
860 AddChildViewAt(
861 CreateBookmarkButton(model_->bookmark_bar_node()->GetChild(i)), i);
862 button_count = GetBookmarkButtonCount();
863 }
848 views::View* child = child_at(i); 864 views::View* child = child_at(i);
849 gfx::Size pref = child->GetPreferredSize(); 865 gfx::Size pref = child->GetPreferredSize();
850 int next_x = x + pref.width() + kButtonPadding; 866 int next_x = x + pref.width() + kButtonPadding;
851 child->SetVisible(next_x < max_x); 867 last_visible = next_x < max_x;
852 child->SetBounds(x, y, pref.width(), height); 868 child->SetVisible(last_visible);
869 // Only need to set bounds if the view is actually visible.
870 if (last_visible)
871 child->SetBounds(x, y, pref.width(), height);
853 x = next_x; 872 x = next_x;
854 } 873 }
855 } 874 }
856 875
857 // Layout the right side of the bar.
858 const bool all_visible = (GetBookmarkButtonCount() == 0 ||
859 child_at(GetBookmarkButtonCount() - 1)->visible());
860
861 // Layout the right side buttons. 876 // Layout the right side buttons.
862 x = max_x + kButtonPadding; 877 x = max_x + kButtonPadding;
863 878
864 // The overflow button. 879 // The overflow button.
865 overflow_button_->SetBounds(x, y, overflow_pref.width(), height); 880 overflow_button_->SetBounds(x, y, overflow_pref.width(), height);
866 overflow_button_->SetVisible(!all_visible); 881 const bool show_overflow =
882 model_->loaded() &&
883 (model_->bookmark_bar_node()->child_count() > GetBookmarkButtonCount() ||
884 (GetBookmarkButtonCount() > 0 &&
885 !GetBookmarkButton(GetBookmarkButtonCount() - 1)->visible()));
886 overflow_button_->SetVisible(show_overflow);
867 x += overflow_pref.width(); 887 x += overflow_pref.width();
868 888
869 // Separator. 889 // Separator.
870 if (bookmarks_separator_view_->visible()) { 890 if (bookmarks_separator_view_->visible()) {
871 bookmarks_separator_view_->SetBounds(x, 891 bookmarks_separator_view_->SetBounds(x,
872 y - top_margin, 892 y - top_margin,
873 bookmarks_separator_pref.width(), 893 bookmarks_separator_pref.width(),
874 height + top_margin + kBottomMargin - 894 height + top_margin + kBottomMargin -
875 separator_margin); 895 separator_margin);
876 896
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
1144 } 1164 }
1145 1165
1146 void BookmarkBarView::OnBookmarkBubbleHidden() { 1166 void BookmarkBarView::OnBookmarkBubbleHidden() {
1147 StopThrobbing(false); 1167 StopThrobbing(false);
1148 } 1168 }
1149 1169
1150 void BookmarkBarView::BookmarkModelLoaded(BookmarkModel* model, 1170 void BookmarkBarView::BookmarkModelLoaded(BookmarkModel* model,
1151 bool ids_reassigned) { 1171 bool ids_reassigned) {
1152 // There should be no buttons. If non-zero it means Load was invoked more than 1172 // 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. 1173 // once, or we didn't properly clear things. Either of which shouldn't happen.
1174 // The actual bookmark buttons are added from Layout().
1154 DCHECK_EQ(0, GetBookmarkButtonCount()); 1175 DCHECK_EQ(0, GetBookmarkButtonCount());
1155 const BookmarkNode* node = model->bookmark_bar_node();
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()); 1176 DCHECK(model->other_node());
1161 other_bookmarked_button_->SetAccessibleName(model->other_node()->GetTitle()); 1177 other_bookmarked_button_->SetAccessibleName(model->other_node()->GetTitle());
1162 other_bookmarked_button_->SetText(model->other_node()->GetTitle()); 1178 other_bookmarked_button_->SetText(model->other_node()->GetTitle());
1163 managed_bookmarks_button_->SetAccessibleName( 1179 managed_bookmarks_button_->SetAccessibleName(
1164 client_->managed_node()->GetTitle()); 1180 client_->managed_node()->GetTitle());
1165 managed_bookmarks_button_->SetText(client_->managed_node()->GetTitle()); 1181 managed_bookmarks_button_->SetText(client_->managed_node()->GetTitle());
1166 UpdateColors(); 1182 UpdateColors();
1167 UpdateOtherAndManagedButtonsVisibility(); 1183 UpdateOtherAndManagedButtonsVisibility();
1168 other_bookmarked_button_->SetEnabled(true); 1184 other_bookmarked_button_->SetEnabled(true);
1169 managed_bookmarks_button_->SetEnabled(true); 1185 managed_bookmarks_button_->SetEnabled(true);
(...skipping 13 matching lines...) Expand all
1183 const BookmarkNode* new_parent, 1199 const BookmarkNode* new_parent,
1184 int new_index) { 1200 int new_index) {
1185 bool was_throbbing = throbbing_view_ && 1201 bool was_throbbing = throbbing_view_ &&
1186 throbbing_view_ == DetermineViewToThrobFromRemove(old_parent, old_index); 1202 throbbing_view_ == DetermineViewToThrobFromRemove(old_parent, old_index);
1187 if (was_throbbing) 1203 if (was_throbbing)
1188 throbbing_view_->StopThrobbing(); 1204 throbbing_view_->StopThrobbing();
1189 bool needs_layout_and_paint = 1205 bool needs_layout_and_paint =
1190 BookmarkNodeRemovedImpl(model, old_parent, old_index); 1206 BookmarkNodeRemovedImpl(model, old_parent, old_index);
1191 if (BookmarkNodeAddedImpl(model, new_parent, new_index)) 1207 if (BookmarkNodeAddedImpl(model, new_parent, new_index))
1192 needs_layout_and_paint = true; 1208 needs_layout_and_paint = true;
1193 if (was_throbbing) 1209 if (was_throbbing && new_index < GetBookmarkButtonCount())
1194 StartThrobbing(new_parent->GetChild(new_index), false); 1210 StartThrobbing(new_parent->GetChild(new_index), false);
1195 if (needs_layout_and_paint) 1211 if (needs_layout_and_paint)
1196 LayoutAndPaint(); 1212 LayoutAndPaint();
1197 } 1213 }
1198 1214
1199 void BookmarkBarView::BookmarkNodeAdded(BookmarkModel* model, 1215 void BookmarkBarView::BookmarkNodeAdded(BookmarkModel* model,
1200 const BookmarkNode* parent, 1216 const BookmarkNode* parent,
1201 int index) { 1217 int index) {
1202 if (BookmarkNodeAddedImpl(model, parent, index)) 1218 if (BookmarkNodeAddedImpl(model, parent, index))
1203 LayoutAndPaint(); 1219 LayoutAndPaint();
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
1500 } 1516 }
1501 1517
1502 int BookmarkBarView::GetBookmarkButtonCount() const { 1518 int BookmarkBarView::GetBookmarkButtonCount() const {
1503 // We contain six non-bookmark button views: managed bookmarks, 1519 // We contain six non-bookmark button views: managed bookmarks,
1504 // other bookmarks, bookmarks separator, chevrons (for overflow), apps page, 1520 // other bookmarks, bookmarks separator, chevrons (for overflow), apps page,
1505 // and the instruction label. 1521 // and the instruction label.
1506 return child_count() - 6; 1522 return child_count() - 6;
1507 } 1523 }
1508 1524
1509 views::LabelButton* BookmarkBarView::GetBookmarkButton(int index) { 1525 views::LabelButton* BookmarkBarView::GetBookmarkButton(int index) {
1510 DCHECK(index >= 0 && index < GetBookmarkButtonCount()); 1526 // CHECK as otherwise we may do the wrong cast.
1527 CHECK(index >= 0 && index < GetBookmarkButtonCount());
1511 return static_cast<views::LabelButton*>(child_at(index)); 1528 return static_cast<views::LabelButton*>(child_at(index));
1512 } 1529 }
1513 1530
1514 BookmarkLaunchLocation BookmarkBarView::GetBookmarkLaunchLocation() const { 1531 BookmarkLaunchLocation BookmarkBarView::GetBookmarkLaunchLocation() const {
1515 return IsDetached() ? BOOKMARK_LAUNCH_LOCATION_DETACHED_BAR : 1532 return IsDetached() ? BOOKMARK_LAUNCH_LOCATION_DETACHED_BAR :
1516 BOOKMARK_LAUNCH_LOCATION_ATTACHED_BAR; 1533 BOOKMARK_LAUNCH_LOCATION_ATTACHED_BAR;
1517 } 1534 }
1518 1535
1519 int BookmarkBarView::GetFirstHiddenNodeIndex() { 1536 int BookmarkBarView::GetFirstHiddenNodeIndex() {
1520 const int bb_count = GetBookmarkButtonCount(); 1537 const int bb_count = GetBookmarkButtonCount();
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
1623 else 1640 else
1624 button->SetImage(views::Button::STATE_NORMAL, GetDefaultFavicon()); 1641 button->SetImage(views::Button::STATE_NORMAL, GetDefaultFavicon());
1625 } 1642 }
1626 button->SetMaxSize(gfx::Size(kMaxButtonWidth, 0)); 1643 button->SetMaxSize(gfx::Size(kMaxButtonWidth, 0));
1627 } 1644 }
1628 1645
1629 bool BookmarkBarView::BookmarkNodeAddedImpl(BookmarkModel* model, 1646 bool BookmarkBarView::BookmarkNodeAddedImpl(BookmarkModel* model,
1630 const BookmarkNode* parent, 1647 const BookmarkNode* parent,
1631 int index) { 1648 int index) {
1632 const bool needs_layout_and_paint = UpdateOtherAndManagedButtonsVisibility(); 1649 const bool needs_layout_and_paint = UpdateOtherAndManagedButtonsVisibility();
1633 if (parent != model->bookmark_bar_node()) { 1650 if (parent != model->bookmark_bar_node())
1634 // Only children of the bookmark_bar_node get buttons.
1635 return needs_layout_and_paint; 1651 return needs_layout_and_paint;
1652 if (index < GetBookmarkButtonCount()) {
1653 const BookmarkNode* node = parent->GetChild(index);
1654 AddChildViewAt(CreateBookmarkButton(node), index);
1655 return true;
1636 } 1656 }
1637 DCHECK(index >= 0 && index <= GetBookmarkButtonCount()); 1657 // If the new node was added after the last button we've created we may be
1638 const BookmarkNode* node = parent->GetChild(index); 1658 // able to fit it. Assume we can by returning true, which forces a Layout()
1639 ProfileSyncService* sync_service(ProfileSyncServiceFactory:: 1659 // and creation of the button (if it fits).
1640 GetInstance()->GetForProfile(browser_->profile())); 1660 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 } 1661 }
1646 1662
1647 bool BookmarkBarView::BookmarkNodeRemovedImpl(BookmarkModel* model, 1663 bool BookmarkBarView::BookmarkNodeRemovedImpl(BookmarkModel* model,
1648 const BookmarkNode* parent, 1664 const BookmarkNode* parent,
1649 int index) { 1665 int index) {
1650 const bool needs_layout = UpdateOtherAndManagedButtonsVisibility(); 1666 const bool needs_layout = UpdateOtherAndManagedButtonsVisibility();
1651 1667
1652 StopThrobbing(true); 1668 StopThrobbing(true);
1653 // No need to start throbbing again as the bookmark bubble can't be up at 1669 // No need to start throbbing again as the bookmark bubble can't be up at
1654 // the same time as the user reorders. 1670 // the same time as the user reorders.
1655 1671
1656 if (parent != model->bookmark_bar_node()) { 1672 if (parent != model->bookmark_bar_node()) {
1657 // Only children of the bookmark_bar_node get buttons. 1673 // Only children of the bookmark_bar_node get buttons.
1658 return needs_layout; 1674 return needs_layout;
1659 } 1675 }
1676 if (index >= GetBookmarkButtonCount())
1677 return needs_layout;
1678
1660 delete child_at(index); 1679 delete child_at(index);
1661 return true; 1680 return true;
1662 } 1681 }
1663 1682
1664 void BookmarkBarView::BookmarkNodeChangedImpl(BookmarkModel* model, 1683 void BookmarkBarView::BookmarkNodeChangedImpl(BookmarkModel* model,
1665 const BookmarkNode* node) { 1684 const BookmarkNode* node) {
1666 if (node == client_->managed_node()) { 1685 if (node == client_->managed_node()) {
1667 // The managed node may have its title updated. 1686 // The managed node may have its title updated.
1668 managed_bookmarks_button_->SetAccessibleName( 1687 managed_bookmarks_button_->SetAccessibleName(
1669 client_->managed_node()->GetTitle()); 1688 client_->managed_node()->GetTitle());
1670 managed_bookmarks_button_->SetText(client_->managed_node()->GetTitle()); 1689 managed_bookmarks_button_->SetText(client_->managed_node()->GetTitle());
1671 return; 1690 return;
1672 } 1691 }
1673 1692
1674 if (node->parent() != model->bookmark_bar_node()) { 1693 if (node->parent() != model->bookmark_bar_node()) {
1675 // We only care about nodes on the bookmark bar. 1694 // We only care about nodes on the bookmark bar.
1676 return; 1695 return;
1677 } 1696 }
1678 int index = model->bookmark_bar_node()->GetIndexOf(node); 1697 int index = model->bookmark_bar_node()->GetIndexOf(node);
1679 DCHECK_NE(-1, index); 1698 DCHECK_NE(-1, index);
1699 if (index >= GetBookmarkButtonCount())
1700 return; // Buttons are created as needed.
1680 views::LabelButton* button = GetBookmarkButton(index); 1701 views::LabelButton* button = GetBookmarkButton(index);
1681 gfx::Size old_pref = button->GetPreferredSize(); 1702 const int old_pref_width = button->GetPreferredSize().width();
1682 ConfigureButton(node, button); 1703 ConfigureButton(node, button);
1683 gfx::Size new_pref = button->GetPreferredSize(); 1704 if (old_pref_width != button->GetPreferredSize().width())
1684 if (old_pref.width() != new_pref.width()) {
1685 LayoutAndPaint(); 1705 LayoutAndPaint();
1686 } else if (button->visible()) {
1687 button->SchedulePaint();
1688 }
1689 } 1706 }
1690 1707
1691 void BookmarkBarView::ShowDropFolderForNode(const BookmarkNode* node) { 1708 void BookmarkBarView::ShowDropFolderForNode(const BookmarkNode* node) {
1692 if (bookmark_drop_menu_) { 1709 if (bookmark_drop_menu_) {
1693 if (bookmark_drop_menu_->node() == node) { 1710 if (bookmark_drop_menu_->node() == node) {
1694 // Already showing for the specified node. 1711 // Already showing for the specified node.
1695 return; 1712 return;
1696 } 1713 }
1697 bookmark_drop_menu_->Cancel(); 1714 bookmark_drop_menu_->Cancel();
1698 } 1715 }
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
1955 return; 1972 return;
1956 apps_page_shortcut_->SetVisible(visible); 1973 apps_page_shortcut_->SetVisible(visible);
1957 UpdateBookmarksSeparatorVisibility(); 1974 UpdateBookmarksSeparatorVisibility();
1958 LayoutAndPaint(); 1975 LayoutAndPaint();
1959 } 1976 }
1960 1977
1961 void BookmarkBarView::OnShowManagedBookmarksPrefChanged() { 1978 void BookmarkBarView::OnShowManagedBookmarksPrefChanged() {
1962 if (UpdateOtherAndManagedButtonsVisibility()) 1979 if (UpdateOtherAndManagedButtonsVisibility())
1963 LayoutAndPaint(); 1980 LayoutAndPaint();
1964 } 1981 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698