OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/views/location_bar/location_bar_layout.h" |
| 6 |
| 7 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
| 8 #include "ui/gfx/rect.h" |
| 9 #include "ui/views/view.h" |
| 10 |
| 11 // Description of a decoration to be added inside the location bar, either to |
| 12 // the left or to the right. |
| 13 struct LocationBarDecoration { |
| 14 LocationBarDecoration(int y, |
| 15 int height, |
| 16 bool auto_collapse, |
| 17 double max_fraction, |
| 18 int edge_item_padding, |
| 19 int item_padding, |
| 20 int builtin_padding, |
| 21 views::View* view); |
| 22 |
| 23 // The y position of the view inside its parent. |
| 24 int y; |
| 25 |
| 26 // If 0, will use the preferred height of the view. |
| 27 int height; |
| 28 |
| 29 // True means that, if there is not enough available space in the location |
| 30 // bar, the view will reduce its width either to its minimal width or to zero |
| 31 // (making it invisible), whichever fits. If true, |max_fraction| must be 0. |
| 32 bool auto_collapse; |
| 33 |
| 34 // Used for resizeable decorations, indicates the maximum fraction of the |
| 35 // location bar that can be taken by this decoration, 0 for non-resizable |
| 36 // decorations. If non-zero, |auto_collapse| must be false. |
| 37 double max_fraction; |
| 38 |
| 39 // Padding to use if the decoration is the first element next to the edge. |
| 40 int edge_item_padding; |
| 41 |
| 42 // Padding to use if the decoration follows another decoration. |
| 43 int item_padding; |
| 44 |
| 45 // Padding built into the decoration and that should be removed, on |
| 46 // both sides, during layout. |
| 47 int builtin_padding; |
| 48 |
| 49 views::View* view; |
| 50 |
| 51 // The width computed by the layout process. |
| 52 double computed_width; |
| 53 }; |
| 54 |
| 55 LocationBarDecoration::LocationBarDecoration(int y, |
| 56 int height, |
| 57 bool auto_collapse, |
| 58 double max_fraction, |
| 59 int edge_item_padding, |
| 60 int item_padding, |
| 61 int builtin_padding, |
| 62 views::View* view) |
| 63 : y(y), |
| 64 height(height), |
| 65 auto_collapse(auto_collapse), |
| 66 max_fraction(max_fraction), |
| 67 edge_item_padding(edge_item_padding), |
| 68 item_padding(item_padding), |
| 69 builtin_padding(builtin_padding), |
| 70 view(view), |
| 71 computed_width(0) { |
| 72 DCHECK(!auto_collapse || max_fraction == 0.0); |
| 73 DCHECK(max_fraction >= 0.0); |
| 74 } |
| 75 |
| 76 |
| 77 // LocationBarLayout --------------------------------------------------------- |
| 78 |
| 79 LocationBarLayout::LocationBarLayout(Position position, |
| 80 int item_edit_padding, |
| 81 int edge_edit_padding) |
| 82 : position_(position), |
| 83 item_edit_padding_(item_edit_padding), |
| 84 edge_edit_padding_(edge_edit_padding) {} |
| 85 |
| 86 |
| 87 LocationBarLayout::~LocationBarLayout() { |
| 88 } |
| 89 |
| 90 void LocationBarLayout::AddDecoration(int y, |
| 91 int height, |
| 92 bool auto_collapse, |
| 93 double max_fraction, |
| 94 int edge_item_padding, |
| 95 int item_padding, |
| 96 int builtin_padding, |
| 97 views::View* view) { |
| 98 decorations_.push_back(new LocationBarDecoration(y, height, auto_collapse, |
| 99 max_fraction, edge_item_padding, item_padding, builtin_padding, view)); |
| 100 } |
| 101 |
| 102 void LocationBarLayout::AddDecoration(int height, |
| 103 int builtin_padding, |
| 104 views::View* view) { |
| 105 decorations_.push_back(new LocationBarDecoration( |
| 106 LocationBarView::kVerticalEdgeThickness, height, false, 0, |
| 107 LocationBarView::GetEdgeItemPadding(), LocationBarView::GetItemPadding(), |
| 108 builtin_padding, view)); |
| 109 } |
| 110 |
| 111 void LocationBarLayout::LayoutPass1(int* entry_width) { |
| 112 |
| 113 bool first_item = true; |
| 114 bool at_least_one_visible = false; |
| 115 for (ScopedVector<LocationBarDecoration>::iterator it(decorations_.begin()); |
| 116 it != decorations_.end(); ++it) { |
| 117 // Autocollapsing decorations are ignored in this pass. |
| 118 if (!(*it)->auto_collapse) { |
| 119 at_least_one_visible = true; |
| 120 *entry_width -= -2 * (*it)->builtin_padding + |
| 121 (first_item ? (*it)->edge_item_padding : (*it)->item_padding); |
| 122 } |
| 123 first_item = false; |
| 124 // Resizing decorations are ignored in this pass. |
| 125 if (!(*it)->auto_collapse && (*it)->max_fraction == 0.0) { |
| 126 (*it)->computed_width = (*it)->view->GetPreferredSize().width(); |
| 127 *entry_width -= (*it)->computed_width; |
| 128 } |
| 129 } |
| 130 *entry_width -= at_least_one_visible ? item_edit_padding_ : |
| 131 edge_edit_padding_; |
| 132 } |
| 133 |
| 134 void LocationBarLayout::LayoutPass2(int *entry_width) { |
| 135 for (ScopedVector<LocationBarDecoration>::iterator it(decorations_.begin()); |
| 136 it != decorations_.end(); ++it) { |
| 137 if ((*it)->max_fraction > 0.0) { |
| 138 int max_width = static_cast<int>(*entry_width * (*it)->max_fraction); |
| 139 (*it)->computed_width = std::min((*it)->view->GetPreferredSize().width(), |
| 140 std::max((*it)->view->GetMinimumSize().width(), max_width)); |
| 141 *entry_width -= (*it)->computed_width; |
| 142 } |
| 143 } |
| 144 } |
| 145 |
| 146 void LocationBarLayout::LayoutPass3(gfx::Rect* bounds, int* available_width) { |
| 147 bool first_visible = true; |
| 148 for (ScopedVector<LocationBarDecoration>::iterator it(decorations_.begin()); |
| 149 it != decorations_.end(); ++it) { |
| 150 // Collapse decorations if needed. |
| 151 if ((*it)->auto_collapse) { |
| 152 int padding = -2 * (*it)->builtin_padding + |
| 153 (first_visible ? (*it)->edge_item_padding : (*it)->item_padding); |
| 154 // Try preferred size, if it fails try minimum size, if it fails collapse. |
| 155 (*it)->computed_width = (*it)->view->GetPreferredSize().width(); |
| 156 if ((*it)->computed_width + padding > *available_width) |
| 157 (*it)->computed_width = (*it)->view->GetMinimumSize().width(); |
| 158 if ((*it)->computed_width + padding > *available_width) { |
| 159 (*it)->computed_width = 0; |
| 160 (*it)->view->SetVisible(false); |
| 161 } else { |
| 162 (*it)->view->SetVisible(true); |
| 163 (*available_width) -= (*it)->computed_width + padding; |
| 164 } |
| 165 } else { |
| 166 (*it)->view->SetVisible(true); |
| 167 } |
| 168 // Layout visible decorations. |
| 169 if ((*it)->view->visible()) { |
| 170 int padding = -(*it)->builtin_padding + |
| 171 (first_visible ? (*it)->edge_item_padding : (*it)->item_padding); |
| 172 first_visible = false; |
| 173 int x; |
| 174 if (position_ == LEFT_EDGE) |
| 175 x = bounds->x() + padding; |
| 176 else |
| 177 x = bounds->x() + bounds->width() - padding - (*it)->computed_width; |
| 178 int height = (*it)->height == 0 ? |
| 179 (*it)->view->GetPreferredSize().height() : (*it)->height; |
| 180 (*it)->view->SetBounds(x, (*it)->y, (*it)->computed_width, height); |
| 181 bounds->set_width(bounds->width() - padding - (*it)->computed_width + |
| 182 (*it)->builtin_padding); |
| 183 if (position_ == LEFT_EDGE) { |
| 184 bounds->set_x(bounds->x() + padding + (*it)->computed_width - |
| 185 (*it)->builtin_padding); |
| 186 } |
| 187 } |
| 188 } |
| 189 int final_padding = first_visible ? edge_edit_padding_ : item_edit_padding_; |
| 190 bounds->set_width(bounds->width() - final_padding); |
| 191 if (position_ == LEFT_EDGE) |
| 192 bounds->set_x(bounds->x() + final_padding); |
| 193 } |
OLD | NEW |