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

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

Issue 273223002: views: Make view::Views::GetPreferredSize() const. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase to ToT Created 6 years, 7 months 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 | Annotate | Revision Log
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 <set> 9 #include <set>
10 #include <vector> 10 #include <vector>
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after
388 virtual ~ButtonSeparatorView() {} 388 virtual ~ButtonSeparatorView() {}
389 389
390 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { 390 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
391 DetachableToolbarView::PaintVerticalDivider( 391 DetachableToolbarView::PaintVerticalDivider(
392 canvas, kSeparatorStartX, height(), 1, 392 canvas, kSeparatorStartX, height(), 1,
393 DetachableToolbarView::kEdgeDividerColor, 393 DetachableToolbarView::kEdgeDividerColor,
394 DetachableToolbarView::kMiddleDividerColor, 394 DetachableToolbarView::kMiddleDividerColor,
395 GetThemeProvider()->GetColor(ThemeProperties::COLOR_TOOLBAR)); 395 GetThemeProvider()->GetColor(ThemeProperties::COLOR_TOOLBAR));
396 } 396 }
397 397
398 virtual gfx::Size GetPreferredSize() OVERRIDE { 398 virtual gfx::Size GetPreferredSize() const OVERRIDE {
399 // We get the full height of the bookmark bar, so that the height returned 399 // We get the full height of the bookmark bar, so that the height returned
400 // here doesn't matter. 400 // here doesn't matter.
401 return gfx::Size(kSeparatorWidth, 1); 401 return gfx::Size(kSeparatorWidth, 1);
402 } 402 }
403 403
404 virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE { 404 virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE {
405 state->name = l10n_util::GetStringUTF16(IDS_ACCNAME_SEPARATOR); 405 state->name = l10n_util::GetStringUTF16(IDS_ACCNAME_SEPARATOR);
406 state->role = ui::AX_ROLE_SPLITTER; 406 state->role = ui::AX_ROLE_SPLITTER;
407 } 407 }
408 408
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
657 if (infobar_visible_) 657 if (infobar_visible_)
658 return detached_overlap; 658 return detached_overlap;
659 659
660 // When detached with no infobar, animate the overlap between the attached and 660 // When detached with no infobar, animate the overlap between the attached and
661 // detached states. 661 // detached states.
662 return detached_overlap + static_cast<int>( 662 return detached_overlap + static_cast<int>(
663 (attached_overlap - detached_overlap) * 663 (attached_overlap - detached_overlap) *
664 size_animation_->GetCurrentValue()); 664 size_animation_->GetCurrentValue());
665 } 665 }
666 666
667 gfx::Size BookmarkBarView::GetPreferredSize() { 667 gfx::Size BookmarkBarView::GetPreferredSize() const {
sky 2014/05/13 16:13:57 The width of the bookmark bar is forced to that of
Elliot Glaysher 2014/05/14 00:48:58 Done.
668 return LayoutItems(true); 668 int x = kLeftMargin;
669 if (IsDetached()) {
670 double current_state = 1 - size_animation_->GetCurrentValue();
671 x += static_cast<int>(kNewtabHorizontalPadding * current_state);
672 }
673
674 if (apps_page_shortcut_->visible())
675 x += apps_page_shortcut_->GetPreferredSize().width() + kButtonPadding;
676
677 for (int i = 0; i < GetBookmarkButtonCount(); ++i) {
678 const views::View* child = child_at(i);
679 gfx::Size pref = child->GetPreferredSize();
680 x += pref.width() + kButtonPadding;
681 }
682
683 // Layout the right side buttons.
684 x += kButtonPadding;
685 x += overflow_button_->GetPreferredSize().width();
686 if (bookmarks_separator_view_->visible())
687 x += bookmarks_separator_view_->GetPreferredSize().width();
688 if (other_bookmarked_button_->visible())
689 x += other_bookmarked_button_->GetPreferredSize().width() + kButtonPadding;
690
691 x += kRightMargin;
692
693 gfx::Size prefsize;
694 prefsize.set_width(x);
695 if (IsDetached()) {
696 prefsize.set_height(
697 chrome::kBookmarkBarHeight +
698 static_cast<int>(
699 (chrome::kNTPBookmarkBarHeight - chrome::kBookmarkBarHeight) *
700 (1 - size_animation_->GetCurrentValue())));
701 } else {
702 prefsize.set_height(static_cast<int>(chrome::kBookmarkBarHeight *
703 size_animation_->GetCurrentValue()));
704 }
705 return prefsize;
669 } 706 }
670 707
671 bool BookmarkBarView::HitTestRect(const gfx::Rect& rect) const { 708 bool BookmarkBarView::HitTestRect(const gfx::Rect& rect) const {
672 // If bookmark bar is attached and omnibox popup is open (on top of the bar), 709 // If bookmark bar is attached and omnibox popup is open (on top of the bar),
673 // force hit-testing to fail. This prevents hovers/clicks just above the 710 // force hit-testing to fail. This prevents hovers/clicks just above the
674 // omnibox popup from activating the top few pixels of items on the bookmark 711 // omnibox popup from activating the top few pixels of items on the bookmark
675 // bar. 712 // bar.
676 if (!IsDetached() && browser_view_ && 713 if (!IsDetached() && browser_view_ &&
677 browser_view_->GetLocationBar()->GetOmniboxView()->model()-> 714 browser_view_->GetLocationBar()->GetOmniboxView()->model()->
678 popup_model()->IsOpen()) { 715 popup_model()->IsOpen()) {
679 return false; 716 return false;
680 } 717 }
681 return DetachableToolbarView::HitTestRect(rect); 718 return DetachableToolbarView::HitTestRect(rect);
682 } 719 }
683 720
684 gfx::Size BookmarkBarView::GetMinimumSize() { 721 gfx::Size BookmarkBarView::GetMinimumSize() const {
685 // The minimum width of the bookmark bar should at least contain the overflow 722 // The minimum width of the bookmark bar should at least contain the overflow
686 // button, by which one can access all the Bookmark Bar items, and the "Other 723 // button, by which one can access all the Bookmark Bar items, and the "Other
687 // Bookmarks" folder, along with appropriate margins and button padding. 724 // Bookmarks" folder, along with appropriate margins and button padding.
688 int width = kLeftMargin; 725 int width = kLeftMargin;
689 726
690 int height = chrome::kBookmarkBarHeight; 727 int height = chrome::kBookmarkBarHeight;
691 if (IsDetached()) { 728 if (IsDetached()) {
692 double current_state = 1 - size_animation_->GetCurrentValue(); 729 double current_state = 1 - size_animation_->GetCurrentValue();
693 width += 2 * static_cast<int>(kNewtabHorizontalPadding * current_state); 730 width += 2 * static_cast<int>(kNewtabHorizontalPadding * current_state);
694 height += static_cast<int>( 731 height += static_cast<int>(
(...skipping 16 matching lines...) Expand all
711 apps_page_shortcut_pref = apps_page_shortcut_->GetPreferredSize(); 748 apps_page_shortcut_pref = apps_page_shortcut_->GetPreferredSize();
712 width += other_bookmarked_pref.width() + kButtonPadding + 749 width += other_bookmarked_pref.width() + kButtonPadding +
713 apps_page_shortcut_pref.width() + kButtonPadding + 750 apps_page_shortcut_pref.width() + kButtonPadding +
714 overflow_pref.width() + kButtonPadding + 751 overflow_pref.width() + kButtonPadding +
715 bookmarks_separator_pref.width(); 752 bookmarks_separator_pref.width();
716 753
717 return gfx::Size(width, height); 754 return gfx::Size(width, height);
718 } 755 }
719 756
720 void BookmarkBarView::Layout() { 757 void BookmarkBarView::Layout() {
721 LayoutItems(false); 758 LayoutItems();
722 } 759 }
723 760
724 void BookmarkBarView::ViewHierarchyChanged( 761 void BookmarkBarView::ViewHierarchyChanged(
725 const ViewHierarchyChangedDetails& details) { 762 const ViewHierarchyChangedDetails& details) {
726 if (details.is_add && details.child == this) { 763 if (details.is_add && details.child == this) {
727 // We may get inserted into a hierarchy with a profile - this typically 764 // We may get inserted into a hierarchy with a profile - this typically
728 // occurs when the bar's contents get populated fast enough that the 765 // occurs when the bar's contents get populated fast enough that the
729 // buttons are created before the bar is attached to a frame. 766 // buttons are created before the bar is attached to a frame.
730 UpdateColors(); 767 UpdateColors();
731 768
(...skipping 560 matching lines...) Expand 10 before | Expand all | Expand 10 after
1292 model_ = BookmarkModelFactory::GetForProfile(browser_->profile()); 1329 model_ = BookmarkModelFactory::GetForProfile(browser_->profile());
1293 if (model_) { 1330 if (model_) {
1294 model_->AddObserver(this); 1331 model_->AddObserver(this);
1295 if (model_->loaded()) 1332 if (model_->loaded())
1296 BookmarkModelLoaded(model_, false); 1333 BookmarkModelLoaded(model_, false);
1297 // else case: we'll receive notification back from the BookmarkModel when 1334 // else case: we'll receive notification back from the BookmarkModel when
1298 // done loading, then we'll populate the bar. 1335 // done loading, then we'll populate the bar.
1299 } 1336 }
1300 } 1337 }
1301 1338
1302 int BookmarkBarView::GetBookmarkButtonCount() { 1339 int BookmarkBarView::GetBookmarkButtonCount() const {
1303 // We contain four non-bookmark button views: other bookmarks, bookmarks 1340 // We contain four non-bookmark button views: other bookmarks, bookmarks
1304 // separator, chevrons (for overflow), apps page, and the instruction label. 1341 // separator, chevrons (for overflow), apps page, and the instruction label.
1305 return child_count() - 5; 1342 return child_count() - 5;
1306 } 1343 }
1307 1344
1308 views::TextButton* BookmarkBarView::GetBookmarkButton(int index) { 1345 views::TextButton* BookmarkBarView::GetBookmarkButton(int index) {
1309 DCHECK(index >= 0 && index < GetBookmarkButtonCount()); 1346 DCHECK(index >= 0 && index < GetBookmarkButtonCount());
1310 return static_cast<views::TextButton*>(child_at(index)); 1347 return static_cast<views::TextButton*>(child_at(index));
1311 } 1348 }
1312 1349
(...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after
1707 } 1744 }
1708 1745
1709 void BookmarkBarView::UpdateBookmarksSeparatorVisibility() { 1746 void BookmarkBarView::UpdateBookmarksSeparatorVisibility() {
1710 // Ash does not paint the bookmarks separator line because it looks odd on 1747 // Ash does not paint the bookmarks separator line because it looks odd on
1711 // the flat background. We keep it present for layout, but don't draw it. 1748 // the flat background. We keep it present for layout, but don't draw it.
1712 bookmarks_separator_view_->SetVisible( 1749 bookmarks_separator_view_->SetVisible(
1713 browser_->host_desktop_type() != chrome::HOST_DESKTOP_TYPE_ASH && 1750 browser_->host_desktop_type() != chrome::HOST_DESKTOP_TYPE_ASH &&
1714 other_bookmarked_button_->visible()); 1751 other_bookmarked_button_->visible());
1715 } 1752 }
1716 1753
1717 gfx::Size BookmarkBarView::LayoutItems(bool compute_bounds_only) { 1754 void BookmarkBarView::LayoutItems() {
1718 gfx::Size prefsize; 1755 if (!parent())
1719 if (!parent() && !compute_bounds_only) 1756 return;
1720 return prefsize;
1721 1757
1722 int x = kLeftMargin; 1758 int x = kLeftMargin;
1723 int top_margin = IsDetached() ? kDetachedTopMargin : 0; 1759 int top_margin = IsDetached() ? kDetachedTopMargin : 0;
1724 int y = top_margin; 1760 int y = top_margin;
1725 int width = View::width() - kRightMargin - kLeftMargin; 1761 int width = View::width() - kRightMargin - kLeftMargin;
1726 int height = chrome::kBookmarkBarHeight - kBottomMargin; 1762 int height = chrome::kBookmarkBarHeight - kBottomMargin;
1727 int separator_margin = kSeparatorMargin; 1763 int separator_margin = kSeparatorMargin;
1728 1764
1729 if (IsDetached()) { 1765 if (IsDetached()) {
1730 double current_state = 1 - size_animation_->GetCurrentValue(); 1766 double current_state = 1 - size_animation_->GetCurrentValue();
(...skipping 19 matching lines...) Expand all
1750 int max_x = width - overflow_pref.width() - kButtonPadding - 1786 int max_x = width - overflow_pref.width() - kButtonPadding -
1751 bookmarks_separator_pref.width(); 1787 bookmarks_separator_pref.width();
1752 if (other_bookmarked_button_->visible()) 1788 if (other_bookmarked_button_->visible())
1753 max_x -= other_bookmarked_pref.width() + kButtonPadding; 1789 max_x -= other_bookmarked_pref.width() + kButtonPadding;
1754 1790
1755 // Next, layout out the buttons. Any buttons that are placed beyond the 1791 // Next, layout out the buttons. Any buttons that are placed beyond the
1756 // visible region and made invisible. 1792 // visible region and made invisible.
1757 1793
1758 // Start with the apps page shortcut button. 1794 // Start with the apps page shortcut button.
1759 if (apps_page_shortcut_->visible()) { 1795 if (apps_page_shortcut_->visible()) {
1760 if (!compute_bounds_only) { 1796 apps_page_shortcut_->SetBounds(x, y, apps_page_shortcut_pref.width(),
1761 apps_page_shortcut_->SetBounds(x, y, apps_page_shortcut_pref.width(), 1797 height);
1762 height);
1763 }
1764 x += apps_page_shortcut_pref.width() + kButtonPadding; 1798 x += apps_page_shortcut_pref.width() + kButtonPadding;
1765 } 1799 }
1766 1800
1767 // Then go through the bookmark buttons. 1801 // Then go through the bookmark buttons.
1768 if (GetBookmarkButtonCount() == 0 && model_ && model_->loaded()) { 1802 if (GetBookmarkButtonCount() == 0 && model_ && model_->loaded()) {
1769 gfx::Size pref = instructions_->GetPreferredSize(); 1803 gfx::Size pref = instructions_->GetPreferredSize();
1770 if (!compute_bounds_only) { 1804 instructions_->SetBounds(
1771 instructions_->SetBounds( 1805 x + kInstructionsPadding, y,
1772 x + kInstructionsPadding, y, 1806 std::min(static_cast<int>(pref.width()),
1773 std::min(static_cast<int>(pref.width()), 1807 max_x - x),
1774 max_x - x), 1808 height);
1775 height); 1809 instructions_->SetVisible(true);
1776 instructions_->SetVisible(true);
1777 }
1778 } else { 1810 } else {
1779 if (!compute_bounds_only) 1811 instructions_->SetVisible(false);
1780 instructions_->SetVisible(false);
1781 1812
1782 for (int i = 0; i < GetBookmarkButtonCount(); ++i) { 1813 for (int i = 0; i < GetBookmarkButtonCount(); ++i) {
1783 views::View* child = child_at(i); 1814 views::View* child = child_at(i);
1784 gfx::Size pref = child->GetPreferredSize(); 1815 gfx::Size pref = child->GetPreferredSize();
1785 int next_x = x + pref.width() + kButtonPadding; 1816 int next_x = x + pref.width() + kButtonPadding;
1786 if (!compute_bounds_only) { 1817 child->SetVisible(next_x < max_x);
1787 child->SetVisible(next_x < max_x); 1818 child->SetBounds(x, y, pref.width(), height);
1788 child->SetBounds(x, y, pref.width(), height);
1789 }
1790 x = next_x; 1819 x = next_x;
1791 } 1820 }
1792 } 1821 }
1793 1822
1794 // Layout the right side of the bar. 1823 // Layout the right side of the bar.
1795 const bool all_visible = (GetBookmarkButtonCount() == 0 || 1824 const bool all_visible = (GetBookmarkButtonCount() == 0 ||
1796 child_at(GetBookmarkButtonCount() - 1)->visible()); 1825 child_at(GetBookmarkButtonCount() - 1)->visible());
1797 1826
1798 // Layout the right side buttons. 1827 // Layout the right side buttons.
1799 if (!compute_bounds_only) 1828 x = max_x + kButtonPadding;
1800 x = max_x + kButtonPadding;
1801 else
1802 x += kButtonPadding;
1803 1829
1804 // The overflow button. 1830 // The overflow button.
1805 if (!compute_bounds_only) { 1831 overflow_button_->SetBounds(x, y, overflow_pref.width(), height);
1806 overflow_button_->SetBounds(x, y, overflow_pref.width(), height); 1832 overflow_button_->SetVisible(!all_visible);
1807 overflow_button_->SetVisible(!all_visible);
1808 }
1809 x += overflow_pref.width(); 1833 x += overflow_pref.width();
1810 1834
1811 // Separator. 1835 // Separator.
1812 if (bookmarks_separator_view_->visible()) { 1836 if (bookmarks_separator_view_->visible()) {
1813 if (!compute_bounds_only) { 1837 bookmarks_separator_view_->SetBounds(x,
1814 bookmarks_separator_view_->SetBounds(x, 1838 y - top_margin,
1815 y - top_margin, 1839 bookmarks_separator_pref.width(),
1816 bookmarks_separator_pref.width(), 1840 height + top_margin + kBottomMargin -
1817 height + top_margin + kBottomMargin - 1841 separator_margin);
1818 separator_margin);
1819 }
1820 1842
1821 x += bookmarks_separator_pref.width(); 1843 x += bookmarks_separator_pref.width();
1822 } 1844 }
1823 1845
1824 // The other bookmarks button. 1846 // The other bookmarks button.
1825 if (other_bookmarked_button_->visible()) { 1847 if (other_bookmarked_button_->visible()) {
1826 if (!compute_bounds_only) { 1848 other_bookmarked_button_->SetBounds(x, y, other_bookmarked_pref.width(),
1827 other_bookmarked_button_->SetBounds(x, y, other_bookmarked_pref.width(), 1849 height);
1828 height);
1829 }
1830 x += other_bookmarked_pref.width() + kButtonPadding; 1850 x += other_bookmarked_pref.width() + kButtonPadding;
1831 } 1851 }
1832
1833 // Set the preferred size computed so far.
1834 if (compute_bounds_only) {
1835 x += kRightMargin;
1836 prefsize.set_width(x);
1837 if (IsDetached()) {
1838 x += static_cast<int>(kNewtabHorizontalPadding *
1839 (1 - size_animation_->GetCurrentValue()));
1840 prefsize.set_height(
1841 chrome::kBookmarkBarHeight +
1842 static_cast<int>(
1843 (chrome::kNTPBookmarkBarHeight - chrome::kBookmarkBarHeight) *
1844 (1 - size_animation_->GetCurrentValue())));
1845 } else {
1846 prefsize.set_height(static_cast<int>(chrome::kBookmarkBarHeight *
1847 size_animation_->GetCurrentValue()));
1848 }
1849 }
1850 return prefsize;
1851 } 1852 }
1852 1853
1853 void BookmarkBarView::OnAppsPageShortcutVisibilityPrefChanged() { 1854 void BookmarkBarView::OnAppsPageShortcutVisibilityPrefChanged() {
1854 DCHECK(apps_page_shortcut_); 1855 DCHECK(apps_page_shortcut_);
1855 // Only perform layout if required. 1856 // Only perform layout if required.
1856 bool visible = chrome::ShouldShowAppsShortcutInBookmarkBar( 1857 bool visible = chrome::ShouldShowAppsShortcutInBookmarkBar(
1857 browser_->profile(), browser_->host_desktop_type()); 1858 browser_->profile(), browser_->host_desktop_type());
1858 if (apps_page_shortcut_->visible() == visible) 1859 if (apps_page_shortcut_->visible() == visible)
1859 return; 1860 return;
1860 apps_page_shortcut_->SetVisible(visible); 1861 apps_page_shortcut_->SetVisible(visible);
1861 UpdateBookmarksSeparatorVisibility(); 1862 UpdateBookmarksSeparatorVisibility();
1862 Layout(); 1863 Layout();
1863 } 1864 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698