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

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

Issue 656033007: Minor cleanup in BookmarkBarView (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix test Created 6 years, 2 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
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 LayoutItems(); 786 int x = kLeftMargin;
787 int top_margin = IsDetached() ? kDetachedTopMargin : 0;
788 int y = top_margin;
789 int width = View::width() - kRightMargin - kLeftMargin;
790 int height = chrome::kBookmarkBarHeight - kBottomMargin;
791 int separator_margin = kSeparatorMargin;
792
793 if (IsDetached()) {
794 double current_state = 1 - size_animation_->GetCurrentValue();
795 x += static_cast<int>(kNewtabHorizontalPadding * current_state);
796 y += (View::height() - chrome::kBookmarkBarHeight) / 2;
797 width -= static_cast<int>(kNewtabHorizontalPadding * current_state);
798 separator_margin -= static_cast<int>(kSeparatorMargin * current_state);
799 } else {
800 // For the attached appearance, pin the content to the bottom of the bar
801 // when animating in/out, as shrinking its height instead looks weird. This
802 // also matches how we layout infobars.
803 y += View::height() - chrome::kBookmarkBarHeight;
804 }
805
806 gfx::Size other_bookmarked_pref = other_bookmarked_button_->visible() ?
807 other_bookmarked_button_->GetPreferredSize() : gfx::Size();
808 gfx::Size overflow_pref = overflow_button_->GetPreferredSize();
809 gfx::Size bookmarks_separator_pref =
810 bookmarks_separator_view_->GetPreferredSize();
811 gfx::Size apps_page_shortcut_pref = apps_page_shortcut_->visible() ?
812 apps_page_shortcut_->GetPreferredSize() : gfx::Size();
813
814 int max_x = width - overflow_pref.width() - kButtonPadding -
815 bookmarks_separator_pref.width();
816 if (other_bookmarked_button_->visible())
817 max_x -= other_bookmarked_pref.width() + kButtonPadding;
818
819 // Next, layout out the buttons. Any buttons that are placed beyond the
820 // visible region are made invisible.
821
822 // Start with the apps page shortcut button.
823 if (apps_page_shortcut_->visible()) {
824 apps_page_shortcut_->SetBounds(x, y, apps_page_shortcut_pref.width(),
825 height);
826 x += apps_page_shortcut_pref.width() + kButtonPadding;
827 }
828
829 // Then comes the managed bookmarks folder, if visible.
830 if (managed_bookmarks_button_->visible()) {
831 gfx::Size managed_bookmarks_pref = managed_bookmarks_button_->visible() ?
832 managed_bookmarks_button_->GetPreferredSize() : gfx::Size();
833 managed_bookmarks_button_->SetBounds(x, y, managed_bookmarks_pref.width(),
834 height);
835 x += managed_bookmarks_pref.width() + kButtonPadding;
836 }
837
838 // Then go through the bookmark buttons.
839 if (GetBookmarkButtonCount() == 0 && model_ && model_->loaded()) {
840 gfx::Size pref = instructions_->GetPreferredSize();
841 instructions_->SetBounds(
842 x + kInstructionsPadding, y,
843 std::min(static_cast<int>(pref.width()),
844 max_x - x),
845 height);
846 instructions_->SetVisible(true);
847 } else {
848 instructions_->SetVisible(false);
849
850 for (int i = 0; i < GetBookmarkButtonCount(); ++i) {
851 views::View* child = child_at(i);
852 gfx::Size pref = child->GetPreferredSize();
853 int next_x = x + pref.width() + kButtonPadding;
854 child->SetVisible(next_x < max_x);
855 child->SetBounds(x, y, pref.width(), height);
856 x = next_x;
857 }
858 }
859
860 // Layout the right side of the bar.
861 const bool all_visible = (GetBookmarkButtonCount() == 0 ||
862 child_at(GetBookmarkButtonCount() - 1)->visible());
863
864 // Layout the right side buttons.
865 x = max_x + kButtonPadding;
866
867 // The overflow button.
868 overflow_button_->SetBounds(x, y, overflow_pref.width(), height);
869 overflow_button_->SetVisible(!all_visible);
870 x += overflow_pref.width();
871
872 // Separator.
873 if (bookmarks_separator_view_->visible()) {
874 bookmarks_separator_view_->SetBounds(x,
875 y - top_margin,
876 bookmarks_separator_pref.width(),
877 height + top_margin + kBottomMargin -
878 separator_margin);
879
880 x += bookmarks_separator_pref.width();
881 }
882
883 // The other bookmarks button.
884 if (other_bookmarked_button_->visible()) {
885 other_bookmarked_button_->SetBounds(x, y, other_bookmarked_pref.width(),
886 height);
887 x += other_bookmarked_pref.width() + kButtonPadding;
888 }
787 } 889 }
788 890
789 void BookmarkBarView::ViewHierarchyChanged( 891 void BookmarkBarView::ViewHierarchyChanged(
790 const ViewHierarchyChangedDetails& details) { 892 const ViewHierarchyChangedDetails& details) {
791 if (details.is_add && details.child == this) { 893 if (details.is_add && details.child == this) {
792 // We may get inserted into a hierarchy with a profile - this typically 894 // We may get inserted into a hierarchy with a profile - this typically
793 // occurs when the bar's contents get populated fast enough that the 895 // occurs when the bar's contents get populated fast enough that the
794 // buttons are created before the bar is attached to a frame. 896 // buttons are created before the bar is attached to a frame.
795 UpdateColors(); 897 UpdateColors();
796 898
(...skipping 1057 matching lines...) Expand 10 before | Expand all | Expand 10 after
1854 } 1956 }
1855 1957
1856 void BookmarkBarView::UpdateBookmarksSeparatorVisibility() { 1958 void BookmarkBarView::UpdateBookmarksSeparatorVisibility() {
1857 // Ash does not paint the bookmarks separator line because it looks odd on 1959 // Ash does not paint the bookmarks separator line because it looks odd on
1858 // the flat background. We keep it present for layout, but don't draw it. 1960 // the flat background. We keep it present for layout, but don't draw it.
1859 bookmarks_separator_view_->SetVisible( 1961 bookmarks_separator_view_->SetVisible(
1860 browser_->host_desktop_type() != chrome::HOST_DESKTOP_TYPE_ASH && 1962 browser_->host_desktop_type() != chrome::HOST_DESKTOP_TYPE_ASH &&
1861 other_bookmarked_button_->visible()); 1963 other_bookmarked_button_->visible());
1862 } 1964 }
1863 1965
1864 void BookmarkBarView::LayoutItems() {
1865 if (!parent())
1866 return;
1867
1868 int x = kLeftMargin;
1869 int top_margin = IsDetached() ? kDetachedTopMargin : 0;
1870 int y = top_margin;
1871 int width = View::width() - kRightMargin - kLeftMargin;
1872 int height = chrome::kBookmarkBarHeight - kBottomMargin;
1873 int separator_margin = kSeparatorMargin;
1874
1875 if (IsDetached()) {
1876 double current_state = 1 - size_animation_->GetCurrentValue();
1877 x += static_cast<int>(kNewtabHorizontalPadding * current_state);
1878 y += (View::height() - chrome::kBookmarkBarHeight) / 2;
1879 width -= static_cast<int>(kNewtabHorizontalPadding * current_state);
1880 separator_margin -= static_cast<int>(kSeparatorMargin * current_state);
1881 } else {
1882 // For the attached appearance, pin the content to the bottom of the bar
1883 // when animating in/out, as shrinking its height instead looks weird. This
1884 // also matches how we layout infobars.
1885 y += View::height() - chrome::kBookmarkBarHeight;
1886 }
1887
1888 gfx::Size other_bookmarked_pref = other_bookmarked_button_->visible() ?
1889 other_bookmarked_button_->GetPreferredSize() : gfx::Size();
1890 gfx::Size overflow_pref = overflow_button_->GetPreferredSize();
1891 gfx::Size bookmarks_separator_pref =
1892 bookmarks_separator_view_->GetPreferredSize();
1893 gfx::Size apps_page_shortcut_pref = apps_page_shortcut_->visible() ?
1894 apps_page_shortcut_->GetPreferredSize() : gfx::Size();
1895
1896 int max_x = width - overflow_pref.width() - kButtonPadding -
1897 bookmarks_separator_pref.width();
1898 if (other_bookmarked_button_->visible())
1899 max_x -= other_bookmarked_pref.width() + kButtonPadding;
1900
1901 // Next, layout out the buttons. Any buttons that are placed beyond the
1902 // visible region are made invisible.
1903
1904 // Start with the apps page shortcut button.
1905 if (apps_page_shortcut_->visible()) {
1906 apps_page_shortcut_->SetBounds(x, y, apps_page_shortcut_pref.width(),
1907 height);
1908 x += apps_page_shortcut_pref.width() + kButtonPadding;
1909 }
1910
1911 // Then comes the managed bookmarks folder, if visible.
1912 if (managed_bookmarks_button_->visible()) {
1913 gfx::Size managed_bookmarks_pref = managed_bookmarks_button_->visible() ?
1914 managed_bookmarks_button_->GetPreferredSize() : gfx::Size();
1915 managed_bookmarks_button_->SetBounds(x, y, managed_bookmarks_pref.width(),
1916 height);
1917 x += managed_bookmarks_pref.width() + kButtonPadding;
1918 }
1919
1920 // Then go through the bookmark buttons.
1921 if (GetBookmarkButtonCount() == 0 && model_ && model_->loaded()) {
1922 gfx::Size pref = instructions_->GetPreferredSize();
1923 instructions_->SetBounds(
1924 x + kInstructionsPadding, y,
1925 std::min(static_cast<int>(pref.width()),
1926 max_x - x),
1927 height);
1928 instructions_->SetVisible(true);
1929 } else {
1930 instructions_->SetVisible(false);
1931
1932 for (int i = 0; i < GetBookmarkButtonCount(); ++i) {
1933 views::View* child = child_at(i);
1934 gfx::Size pref = child->GetPreferredSize();
1935 int next_x = x + pref.width() + kButtonPadding;
1936 child->SetVisible(next_x < max_x);
1937 child->SetBounds(x, y, pref.width(), height);
1938 x = next_x;
1939 }
1940 }
1941
1942 // Layout the right side of the bar.
1943 const bool all_visible = (GetBookmarkButtonCount() == 0 ||
1944 child_at(GetBookmarkButtonCount() - 1)->visible());
1945
1946 // Layout the right side buttons.
1947 x = max_x + kButtonPadding;
1948
1949 // The overflow button.
1950 overflow_button_->SetBounds(x, y, overflow_pref.width(), height);
1951 overflow_button_->SetVisible(!all_visible);
1952 x += overflow_pref.width();
1953
1954 // Separator.
1955 if (bookmarks_separator_view_->visible()) {
1956 bookmarks_separator_view_->SetBounds(x,
1957 y - top_margin,
1958 bookmarks_separator_pref.width(),
1959 height + top_margin + kBottomMargin -
1960 separator_margin);
1961
1962 x += bookmarks_separator_pref.width();
1963 }
1964
1965 // The other bookmarks button.
1966 if (other_bookmarked_button_->visible()) {
1967 other_bookmarked_button_->SetBounds(x, y, other_bookmarked_pref.width(),
1968 height);
1969 x += other_bookmarked_pref.width() + kButtonPadding;
1970 }
1971 }
1972
1973 void BookmarkBarView::OnAppsPageShortcutVisibilityPrefChanged() { 1966 void BookmarkBarView::OnAppsPageShortcutVisibilityPrefChanged() {
1974 DCHECK(apps_page_shortcut_); 1967 DCHECK(apps_page_shortcut_);
1975 // Only perform layout if required. 1968 // Only perform layout if required.
1976 bool visible = chrome::ShouldShowAppsShortcutInBookmarkBar( 1969 bool visible = chrome::ShouldShowAppsShortcutInBookmarkBar(
1977 browser_->profile(), browser_->host_desktop_type()); 1970 browser_->profile(), browser_->host_desktop_type());
1978 if (apps_page_shortcut_->visible() == visible) 1971 if (apps_page_shortcut_->visible() == visible)
1979 return; 1972 return;
1980 apps_page_shortcut_->SetVisible(visible); 1973 apps_page_shortcut_->SetVisible(visible);
1981 UpdateBookmarksSeparatorVisibility(); 1974 UpdateBookmarksSeparatorVisibility();
1982 Layout(); 1975 Layout();
1983 } 1976 }
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