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

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: More compile fix for 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 {
668 return LayoutItems(true); 668 gfx::Size prefsize;
669 if (IsDetached()) {
670 prefsize.set_height(
671 chrome::kBookmarkBarHeight +
672 static_cast<int>(
673 (chrome::kNTPBookmarkBarHeight - chrome::kBookmarkBarHeight) *
674 (1 - size_animation_->GetCurrentValue())));
675 } else {
676 prefsize.set_height(static_cast<int>(chrome::kBookmarkBarHeight *
677 size_animation_->GetCurrentValue()));
678 }
679 return prefsize;
669 } 680 }
670 681
671 bool BookmarkBarView::HitTestRect(const gfx::Rect& rect) const { 682 bool BookmarkBarView::HitTestRect(const gfx::Rect& rect) const {
672 // If bookmark bar is attached and omnibox popup is open (on top of the bar), 683 // 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 684 // 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 685 // omnibox popup from activating the top few pixels of items on the bookmark
675 // bar. 686 // bar.
676 if (!IsDetached() && browser_view_ && 687 if (!IsDetached() && browser_view_ &&
677 browser_view_->GetLocationBar()->GetOmniboxView()->model()-> 688 browser_view_->GetLocationBar()->GetOmniboxView()->model()->
678 popup_model()->IsOpen()) { 689 popup_model()->IsOpen()) {
679 return false; 690 return false;
680 } 691 }
681 return DetachableToolbarView::HitTestRect(rect); 692 return DetachableToolbarView::HitTestRect(rect);
682 } 693 }
683 694
684 gfx::Size BookmarkBarView::GetMinimumSize() { 695 gfx::Size BookmarkBarView::GetMinimumSize() const {
685 // The minimum width of the bookmark bar should at least contain the overflow 696 // 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 697 // button, by which one can access all the Bookmark Bar items, and the "Other
687 // Bookmarks" folder, along with appropriate margins and button padding. 698 // Bookmarks" folder, along with appropriate margins and button padding.
688 int width = kLeftMargin; 699 int width = kLeftMargin;
689 700
690 int height = chrome::kBookmarkBarHeight; 701 int height = chrome::kBookmarkBarHeight;
691 if (IsDetached()) { 702 if (IsDetached()) {
692 double current_state = 1 - size_animation_->GetCurrentValue(); 703 double current_state = 1 - size_animation_->GetCurrentValue();
693 width += 2 * static_cast<int>(kNewtabHorizontalPadding * current_state); 704 width += 2 * static_cast<int>(kNewtabHorizontalPadding * current_state);
694 height += static_cast<int>( 705 height += static_cast<int>(
(...skipping 16 matching lines...) Expand all
711 apps_page_shortcut_pref = apps_page_shortcut_->GetPreferredSize(); 722 apps_page_shortcut_pref = apps_page_shortcut_->GetPreferredSize();
712 width += other_bookmarked_pref.width() + kButtonPadding + 723 width += other_bookmarked_pref.width() + kButtonPadding +
713 apps_page_shortcut_pref.width() + kButtonPadding + 724 apps_page_shortcut_pref.width() + kButtonPadding +
714 overflow_pref.width() + kButtonPadding + 725 overflow_pref.width() + kButtonPadding +
715 bookmarks_separator_pref.width(); 726 bookmarks_separator_pref.width();
716 727
717 return gfx::Size(width, height); 728 return gfx::Size(width, height);
718 } 729 }
719 730
720 void BookmarkBarView::Layout() { 731 void BookmarkBarView::Layout() {
721 LayoutItems(false); 732 LayoutItems();
722 } 733 }
723 734
724 void BookmarkBarView::ViewHierarchyChanged( 735 void BookmarkBarView::ViewHierarchyChanged(
725 const ViewHierarchyChangedDetails& details) { 736 const ViewHierarchyChangedDetails& details) {
726 if (details.is_add && details.child == this) { 737 if (details.is_add && details.child == this) {
727 // We may get inserted into a hierarchy with a profile - this typically 738 // 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 739 // occurs when the bar's contents get populated fast enough that the
729 // buttons are created before the bar is attached to a frame. 740 // buttons are created before the bar is attached to a frame.
730 UpdateColors(); 741 UpdateColors();
731 742
(...skipping 560 matching lines...) Expand 10 before | Expand all | Expand 10 after
1292 model_ = BookmarkModelFactory::GetForProfile(browser_->profile()); 1303 model_ = BookmarkModelFactory::GetForProfile(browser_->profile());
1293 if (model_) { 1304 if (model_) {
1294 model_->AddObserver(this); 1305 model_->AddObserver(this);
1295 if (model_->loaded()) 1306 if (model_->loaded())
1296 BookmarkModelLoaded(model_, false); 1307 BookmarkModelLoaded(model_, false);
1297 // else case: we'll receive notification back from the BookmarkModel when 1308 // else case: we'll receive notification back from the BookmarkModel when
1298 // done loading, then we'll populate the bar. 1309 // done loading, then we'll populate the bar.
1299 } 1310 }
1300 } 1311 }
1301 1312
1302 int BookmarkBarView::GetBookmarkButtonCount() { 1313 int BookmarkBarView::GetBookmarkButtonCount() const {
1303 // We contain four non-bookmark button views: other bookmarks, bookmarks 1314 // We contain four non-bookmark button views: other bookmarks, bookmarks
1304 // separator, chevrons (for overflow), apps page, and the instruction label. 1315 // separator, chevrons (for overflow), apps page, and the instruction label.
1305 return child_count() - 5; 1316 return child_count() - 5;
1306 } 1317 }
1307 1318
1308 views::TextButton* BookmarkBarView::GetBookmarkButton(int index) { 1319 views::TextButton* BookmarkBarView::GetBookmarkButton(int index) {
1309 DCHECK(index >= 0 && index < GetBookmarkButtonCount()); 1320 DCHECK(index >= 0 && index < GetBookmarkButtonCount());
1310 return static_cast<views::TextButton*>(child_at(index)); 1321 return static_cast<views::TextButton*>(child_at(index));
1311 } 1322 }
1312 1323
(...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after
1707 } 1718 }
1708 1719
1709 void BookmarkBarView::UpdateBookmarksSeparatorVisibility() { 1720 void BookmarkBarView::UpdateBookmarksSeparatorVisibility() {
1710 // Ash does not paint the bookmarks separator line because it looks odd on 1721 // 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. 1722 // the flat background. We keep it present for layout, but don't draw it.
1712 bookmarks_separator_view_->SetVisible( 1723 bookmarks_separator_view_->SetVisible(
1713 browser_->host_desktop_type() != chrome::HOST_DESKTOP_TYPE_ASH && 1724 browser_->host_desktop_type() != chrome::HOST_DESKTOP_TYPE_ASH &&
1714 other_bookmarked_button_->visible()); 1725 other_bookmarked_button_->visible());
1715 } 1726 }
1716 1727
1717 gfx::Size BookmarkBarView::LayoutItems(bool compute_bounds_only) { 1728 void BookmarkBarView::LayoutItems() {
1718 gfx::Size prefsize; 1729 if (!parent())
1719 if (!parent() && !compute_bounds_only) 1730 return;
1720 return prefsize;
1721 1731
1722 int x = kLeftMargin; 1732 int x = kLeftMargin;
1723 int top_margin = IsDetached() ? kDetachedTopMargin : 0; 1733 int top_margin = IsDetached() ? kDetachedTopMargin : 0;
1724 int y = top_margin; 1734 int y = top_margin;
1725 int width = View::width() - kRightMargin - kLeftMargin; 1735 int width = View::width() - kRightMargin - kLeftMargin;
1726 int height = chrome::kBookmarkBarHeight - kBottomMargin; 1736 int height = chrome::kBookmarkBarHeight - kBottomMargin;
1727 int separator_margin = kSeparatorMargin; 1737 int separator_margin = kSeparatorMargin;
1728 1738
1729 if (IsDetached()) { 1739 if (IsDetached()) {
1730 double current_state = 1 - size_animation_->GetCurrentValue(); 1740 double current_state = 1 - size_animation_->GetCurrentValue();
(...skipping 19 matching lines...) Expand all
1750 int max_x = width - overflow_pref.width() - kButtonPadding - 1760 int max_x = width - overflow_pref.width() - kButtonPadding -
1751 bookmarks_separator_pref.width(); 1761 bookmarks_separator_pref.width();
1752 if (other_bookmarked_button_->visible()) 1762 if (other_bookmarked_button_->visible())
1753 max_x -= other_bookmarked_pref.width() + kButtonPadding; 1763 max_x -= other_bookmarked_pref.width() + kButtonPadding;
1754 1764
1755 // Next, layout out the buttons. Any buttons that are placed beyond the 1765 // Next, layout out the buttons. Any buttons that are placed beyond the
1756 // visible region and made invisible. 1766 // visible region and made invisible.
1757 1767
1758 // Start with the apps page shortcut button. 1768 // Start with the apps page shortcut button.
1759 if (apps_page_shortcut_->visible()) { 1769 if (apps_page_shortcut_->visible()) {
1760 if (!compute_bounds_only) { 1770 apps_page_shortcut_->SetBounds(x, y, apps_page_shortcut_pref.width(),
1761 apps_page_shortcut_->SetBounds(x, y, apps_page_shortcut_pref.width(), 1771 height);
1762 height);
1763 }
1764 x += apps_page_shortcut_pref.width() + kButtonPadding; 1772 x += apps_page_shortcut_pref.width() + kButtonPadding;
1765 } 1773 }
1766 1774
1767 // Then go through the bookmark buttons. 1775 // Then go through the bookmark buttons.
1768 if (GetBookmarkButtonCount() == 0 && model_ && model_->loaded()) { 1776 if (GetBookmarkButtonCount() == 0 && model_ && model_->loaded()) {
1769 gfx::Size pref = instructions_->GetPreferredSize(); 1777 gfx::Size pref = instructions_->GetPreferredSize();
1770 if (!compute_bounds_only) { 1778 instructions_->SetBounds(
1771 instructions_->SetBounds( 1779 x + kInstructionsPadding, y,
1772 x + kInstructionsPadding, y, 1780 std::min(static_cast<int>(pref.width()),
1773 std::min(static_cast<int>(pref.width()), 1781 max_x - x),
1774 max_x - x), 1782 height);
1775 height); 1783 instructions_->SetVisible(true);
1776 instructions_->SetVisible(true);
1777 }
1778 } else { 1784 } else {
1779 if (!compute_bounds_only) 1785 instructions_->SetVisible(false);
1780 instructions_->SetVisible(false);
1781 1786
1782 for (int i = 0; i < GetBookmarkButtonCount(); ++i) { 1787 for (int i = 0; i < GetBookmarkButtonCount(); ++i) {
1783 views::View* child = child_at(i); 1788 views::View* child = child_at(i);
1784 gfx::Size pref = child->GetPreferredSize(); 1789 gfx::Size pref = child->GetPreferredSize();
1785 int next_x = x + pref.width() + kButtonPadding; 1790 int next_x = x + pref.width() + kButtonPadding;
1786 if (!compute_bounds_only) { 1791 child->SetVisible(next_x < max_x);
1787 child->SetVisible(next_x < max_x); 1792 child->SetBounds(x, y, pref.width(), height);
1788 child->SetBounds(x, y, pref.width(), height);
1789 }
1790 x = next_x; 1793 x = next_x;
1791 } 1794 }
1792 } 1795 }
1793 1796
1794 // Layout the right side of the bar. 1797 // Layout the right side of the bar.
1795 const bool all_visible = (GetBookmarkButtonCount() == 0 || 1798 const bool all_visible = (GetBookmarkButtonCount() == 0 ||
1796 child_at(GetBookmarkButtonCount() - 1)->visible()); 1799 child_at(GetBookmarkButtonCount() - 1)->visible());
1797 1800
1798 // Layout the right side buttons. 1801 // Layout the right side buttons.
1799 if (!compute_bounds_only) 1802 x = max_x + kButtonPadding;
1800 x = max_x + kButtonPadding;
1801 else
1802 x += kButtonPadding;
1803 1803
1804 // The overflow button. 1804 // The overflow button.
1805 if (!compute_bounds_only) { 1805 overflow_button_->SetBounds(x, y, overflow_pref.width(), height);
1806 overflow_button_->SetBounds(x, y, overflow_pref.width(), height); 1806 overflow_button_->SetVisible(!all_visible);
1807 overflow_button_->SetVisible(!all_visible);
1808 }
1809 x += overflow_pref.width(); 1807 x += overflow_pref.width();
1810 1808
1811 // Separator. 1809 // Separator.
1812 if (bookmarks_separator_view_->visible()) { 1810 if (bookmarks_separator_view_->visible()) {
1813 if (!compute_bounds_only) { 1811 bookmarks_separator_view_->SetBounds(x,
1814 bookmarks_separator_view_->SetBounds(x, 1812 y - top_margin,
1815 y - top_margin, 1813 bookmarks_separator_pref.width(),
1816 bookmarks_separator_pref.width(), 1814 height + top_margin + kBottomMargin -
1817 height + top_margin + kBottomMargin - 1815 separator_margin);
1818 separator_margin);
1819 }
1820 1816
1821 x += bookmarks_separator_pref.width(); 1817 x += bookmarks_separator_pref.width();
1822 } 1818 }
1823 1819
1824 // The other bookmarks button. 1820 // The other bookmarks button.
1825 if (other_bookmarked_button_->visible()) { 1821 if (other_bookmarked_button_->visible()) {
1826 if (!compute_bounds_only) { 1822 other_bookmarked_button_->SetBounds(x, y, other_bookmarked_pref.width(),
1827 other_bookmarked_button_->SetBounds(x, y, other_bookmarked_pref.width(), 1823 height);
1828 height);
1829 }
1830 x += other_bookmarked_pref.width() + kButtonPadding; 1824 x += other_bookmarked_pref.width() + kButtonPadding;
1831 } 1825 }
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 } 1826 }
1852 1827
1853 void BookmarkBarView::OnAppsPageShortcutVisibilityPrefChanged() { 1828 void BookmarkBarView::OnAppsPageShortcutVisibilityPrefChanged() {
1854 DCHECK(apps_page_shortcut_); 1829 DCHECK(apps_page_shortcut_);
1855 // Only perform layout if required. 1830 // Only perform layout if required.
1856 bool visible = chrome::ShouldShowAppsShortcutInBookmarkBar( 1831 bool visible = chrome::ShouldShowAppsShortcutInBookmarkBar(
1857 browser_->profile(), browser_->host_desktop_type()); 1832 browser_->profile(), browser_->host_desktop_type());
1858 if (apps_page_shortcut_->visible() == visible) 1833 if (apps_page_shortcut_->visible() == visible)
1859 return; 1834 return;
1860 apps_page_shortcut_->SetVisible(visible); 1835 apps_page_shortcut_->SetVisible(visible);
1861 UpdateBookmarksSeparatorVisibility(); 1836 UpdateBookmarksSeparatorVisibility();
1862 Layout(); 1837 Layout();
1863 } 1838 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/bookmarks/bookmark_bar_view.h ('k') | chrome/browser/ui/views/bookmarks/bookmark_bar_view_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698