| OLD | NEW |
| 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/location_bar/location_bar_layout.h" | 5 #include "chrome/browser/ui/views/location_bar/location_bar_layout.h" |
| 6 | 6 |
| 7 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" | 7 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
| 8 #include "ui/gfx/rect.h" | 8 #include "ui/gfx/rect.h" |
| 9 #include "ui/views/view.h" | 9 #include "ui/views/view.h" |
| 10 | 10 |
| 11 namespace { |
| 12 |
| 13 enum DecorationType { |
| 14 // Decoration is always visible. |
| 15 NORMAL = 0, |
| 16 // If there is not enough available space in the location bar, the decoration |
| 17 // will reduce its width either to its minimal width or to zero (making it |
| 18 // invisible), whichever fits. |LocationBarDecoration::max_fraction| must be |
| 19 // 0. |
| 20 AUTO_COLLAPSE, |
| 21 // Decoration is a separator, only visible if it's not leading, or not |
| 22 // trailing, or not next to another separator. |
| 23 SEPARATOR, |
| 24 }; |
| 25 |
| 26 } // namespace |
| 27 |
| 28 |
| 11 // Description of a decoration to be added inside the location bar, either to | 29 // Description of a decoration to be added inside the location bar, either to |
| 12 // the left or to the right. | 30 // the left or to the right. |
| 13 struct LocationBarDecoration { | 31 struct LocationBarDecoration { |
| 14 LocationBarDecoration(int y, | 32 LocationBarDecoration(DecorationType type, |
| 33 int y, |
| 15 int height, | 34 int height, |
| 16 bool auto_collapse, | |
| 17 double max_fraction, | 35 double max_fraction, |
| 18 int edge_item_padding, | 36 int edge_item_padding, |
| 19 int item_padding, | 37 int item_padding, |
| 20 int builtin_padding, | 38 int builtin_padding, |
| 21 views::View* view); | 39 views::View* view); |
| 22 | 40 |
| 41 // The type of decoration. |
| 42 DecorationType type; |
| 43 |
| 23 // The y position of the view inside its parent. | 44 // The y position of the view inside its parent. |
| 24 int y; | 45 int y; |
| 25 | 46 |
| 26 // If 0, will use the preferred height of the view. | 47 // If 0, will use the preferred height of the view. |
| 27 int height; | 48 int height; |
| 28 | 49 |
| 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 | 50 // Used for resizeable decorations, indicates the maximum fraction of the |
| 35 // location bar that can be taken by this decoration, 0 for non-resizable | 51 // location bar that can be taken by this decoration, 0 for non-resizable |
| 36 // decorations. If non-zero, |auto_collapse| must be false. | 52 // decorations. If non-zero, |type| must not be AUTO_COLLAPSE. |
| 37 double max_fraction; | 53 double max_fraction; |
| 38 | 54 |
| 39 // Padding to use if the decoration is the first element next to the edge. | 55 // Padding to use if the decoration is the first element next to the edge. |
| 40 int edge_item_padding; | 56 int edge_item_padding; |
| 41 | 57 |
| 42 // Padding to use if the decoration follows another decoration. | 58 // Padding to use if the decoration follows another decoration. |
| 43 int item_padding; | 59 int item_padding; |
| 44 | 60 |
| 45 // Padding built into the decoration and that should be removed, on | 61 // Padding built into the decoration and that should be removed, on |
| 46 // both sides, during layout. | 62 // both sides, during layout. |
| 47 int builtin_padding; | 63 int builtin_padding; |
| 48 | 64 |
| 49 views::View* view; | 65 views::View* view; |
| 50 | 66 |
| 51 // The width computed by the layout process. | 67 // The width computed by the layout process. |
| 52 double computed_width; | 68 double computed_width; |
| 53 }; | 69 }; |
| 54 | 70 |
| 55 LocationBarDecoration::LocationBarDecoration(int y, | 71 LocationBarDecoration::LocationBarDecoration(DecorationType type, |
| 72 int y, |
| 56 int height, | 73 int height, |
| 57 bool auto_collapse, | |
| 58 double max_fraction, | 74 double max_fraction, |
| 59 int edge_item_padding, | 75 int edge_item_padding, |
| 60 int item_padding, | 76 int item_padding, |
| 61 int builtin_padding, | 77 int builtin_padding, |
| 62 views::View* view) | 78 views::View* view) |
| 63 : y(y), | 79 : type(type), |
| 80 y(y), |
| 64 height(height), | 81 height(height), |
| 65 auto_collapse(auto_collapse), | |
| 66 max_fraction(max_fraction), | 82 max_fraction(max_fraction), |
| 67 edge_item_padding(edge_item_padding), | 83 edge_item_padding(edge_item_padding), |
| 68 item_padding(item_padding), | 84 item_padding(item_padding), |
| 69 builtin_padding(builtin_padding), | 85 builtin_padding(builtin_padding), |
| 70 view(view), | 86 view(view), |
| 71 computed_width(0) { | 87 computed_width(0) { |
| 72 DCHECK(!auto_collapse || max_fraction == 0.0); | 88 if (type == NORMAL) { |
| 73 DCHECK(max_fraction >= 0.0); | 89 DCHECK_GE(max_fraction, 0.0); |
| 90 } else { |
| 91 DCHECK_EQ(0.0, max_fraction); |
| 92 } |
| 74 } | 93 } |
| 75 | 94 |
| 76 | 95 |
| 77 // LocationBarLayout --------------------------------------------------------- | 96 // LocationBarLayout --------------------------------------------------------- |
| 78 | 97 |
| 79 LocationBarLayout::LocationBarLayout(Position position, | 98 LocationBarLayout::LocationBarLayout(Position position, |
| 80 int item_edit_padding, | 99 int item_edit_padding, |
| 81 int edge_edit_padding) | 100 int edge_edit_padding) |
| 82 : position_(position), | 101 : position_(position), |
| 83 item_edit_padding_(item_edit_padding), | 102 item_edit_padding_(item_edit_padding), |
| 84 edge_edit_padding_(edge_edit_padding) {} | 103 edge_edit_padding_(edge_edit_padding) {} |
| 85 | 104 |
| 86 | 105 |
| 87 LocationBarLayout::~LocationBarLayout() { | 106 LocationBarLayout::~LocationBarLayout() { |
| 88 } | 107 } |
| 89 | 108 |
| 90 void LocationBarLayout::AddDecoration(int y, | 109 void LocationBarLayout::AddDecoration(int y, |
| 91 int height, | 110 int height, |
| 92 bool auto_collapse, | 111 bool auto_collapse, |
| 93 double max_fraction, | 112 double max_fraction, |
| 94 int edge_item_padding, | 113 int edge_item_padding, |
| 95 int item_padding, | 114 int item_padding, |
| 96 int builtin_padding, | 115 int builtin_padding, |
| 97 views::View* view) { | 116 views::View* view) { |
| 98 decorations_.push_back(new LocationBarDecoration(y, height, auto_collapse, | 117 decorations_.push_back(new LocationBarDecoration( |
| 99 max_fraction, edge_item_padding, item_padding, builtin_padding, view)); | 118 auto_collapse ? AUTO_COLLAPSE : NORMAL, y, height, max_fraction, |
| 119 edge_item_padding, item_padding, builtin_padding, view)); |
| 100 } | 120 } |
| 101 | 121 |
| 102 void LocationBarLayout::AddDecoration(int height, | 122 void LocationBarLayout::AddDecoration(int height, |
| 103 int builtin_padding, | 123 int builtin_padding, |
| 104 views::View* view) { | 124 views::View* view) { |
| 105 decorations_.push_back(new LocationBarDecoration( | 125 decorations_.push_back(new LocationBarDecoration( |
| 106 LocationBarView::kVerticalEdgeThickness, height, false, 0, | 126 NORMAL, LocationBarView::kVerticalEdgeThickness, height, 0, |
| 107 LocationBarView::GetEdgeItemPadding(), LocationBarView::GetItemPadding(), | 127 LocationBarView::GetEdgeItemPadding(), LocationBarView::GetItemPadding(), |
| 108 builtin_padding, view)); | 128 builtin_padding, view)); |
| 109 } | 129 } |
| 110 | 130 |
| 131 void LocationBarLayout::AddSeparator(int y, |
| 132 int height, |
| 133 int padding_from_previous_item, |
| 134 views::View* separator) { |
| 135 // Edge item padding won't apply since a separator won't be by the edge, so |
| 136 // use 0 for more accurate evaluation of |entry_width| in LayoutPass1(). |
| 137 decorations_.push_back(new LocationBarDecoration( |
| 138 SEPARATOR, y, height, 0, 0, padding_from_previous_item, 0, separator)); |
| 139 } |
| 140 |
| 111 void LocationBarLayout::LayoutPass1(int* entry_width) { | 141 void LocationBarLayout::LayoutPass1(int* entry_width) { |
| 112 | |
| 113 bool first_item = true; | 142 bool first_item = true; |
| 114 bool at_least_one_visible = false; | 143 bool at_least_one_visible = false; |
| 115 for (ScopedVector<LocationBarDecoration>::iterator it(decorations_.begin()); | 144 for (Decorations::iterator it(decorations_.begin()); it != decorations_.end(); |
| 116 it != decorations_.end(); ++it) { | 145 ++it) { |
| 117 // Autocollapsing decorations are ignored in this pass. | 146 // Autocollapsing decorations are ignored in this pass. |
| 118 if (!(*it)->auto_collapse) { | 147 if ((*it)->type != AUTO_COLLAPSE) { |
| 119 at_least_one_visible = true; | 148 at_least_one_visible = true; |
| 120 *entry_width -= -2 * (*it)->builtin_padding + | 149 *entry_width -= -2 * (*it)->builtin_padding + |
| 121 (first_item ? (*it)->edge_item_padding : (*it)->item_padding); | 150 (first_item ? (*it)->edge_item_padding : (*it)->item_padding); |
| 122 } | 151 } |
| 123 first_item = false; | 152 first_item = false; |
| 124 // Resizing decorations are ignored in this pass. | 153 // Resizing decorations are ignored in this pass. |
| 125 if (!(*it)->auto_collapse && (*it)->max_fraction == 0.0) { | 154 if (((*it)->type != AUTO_COLLAPSE) && ((*it)->max_fraction == 0.0)) { |
| 126 (*it)->computed_width = (*it)->view->GetPreferredSize().width(); | 155 (*it)->computed_width = (*it)->view->GetPreferredSize().width(); |
| 127 *entry_width -= (*it)->computed_width; | 156 *entry_width -= (*it)->computed_width; |
| 128 } | 157 } |
| 129 } | 158 } |
| 130 *entry_width -= at_least_one_visible ? item_edit_padding_ : | 159 *entry_width -= at_least_one_visible ? item_edit_padding_ : |
| 131 edge_edit_padding_; | 160 edge_edit_padding_; |
| 132 } | 161 } |
| 133 | 162 |
| 134 void LocationBarLayout::LayoutPass2(int *entry_width) { | 163 void LocationBarLayout::LayoutPass2(int *entry_width) { |
| 135 for (ScopedVector<LocationBarDecoration>::iterator it(decorations_.begin()); | 164 for (Decorations::iterator it(decorations_.begin()); it != decorations_.end(); |
| 136 it != decorations_.end(); ++it) { | 165 ++it) { |
| 137 if ((*it)->max_fraction > 0.0) { | 166 if ((*it)->max_fraction > 0.0) { |
| 138 int max_width = static_cast<int>(*entry_width * (*it)->max_fraction); | 167 int max_width = static_cast<int>(*entry_width * (*it)->max_fraction); |
| 139 (*it)->computed_width = std::min((*it)->view->GetPreferredSize().width(), | 168 (*it)->computed_width = std::min((*it)->view->GetPreferredSize().width(), |
| 140 std::max((*it)->view->GetMinimumSize().width(), max_width)); | 169 std::max((*it)->view->GetMinimumSize().width(), max_width)); |
| 141 *entry_width -= (*it)->computed_width; | 170 *entry_width -= (*it)->computed_width; |
| 142 } | 171 } |
| 143 } | 172 } |
| 144 } | 173 } |
| 145 | 174 |
| 146 void LocationBarLayout::LayoutPass3(gfx::Rect* bounds, int* available_width) { | 175 void LocationBarLayout::LayoutPass3(gfx::Rect* bounds, int* available_width) { |
| 176 SetVisibilityForDecorations(available_width); |
| 177 HideUnneededSeparators(available_width); |
| 178 SetBoundsForDecorations(bounds); |
| 179 } |
| 180 |
| 181 void LocationBarLayout::SetVisibilityForDecorations(int* available_width) { |
| 147 bool first_visible = true; | 182 bool first_visible = true; |
| 148 for (ScopedVector<LocationBarDecoration>::iterator it(decorations_.begin()); | 183 for (Decorations::iterator it(decorations_.begin()); it != decorations_.end(); |
| 149 it != decorations_.end(); ++it) { | 184 ++it) { |
| 150 // Collapse decorations if needed. | 185 // Collapse decorations if needed. |
| 151 if ((*it)->auto_collapse) { | 186 if ((*it)->type == AUTO_COLLAPSE) { |
| 152 int padding = -2 * (*it)->builtin_padding + | 187 int padding = -2 * (*it)->builtin_padding + |
| 153 (first_visible ? (*it)->edge_item_padding : (*it)->item_padding); | 188 (first_visible ? (*it)->edge_item_padding : (*it)->item_padding); |
| 154 // Try preferred size, if it fails try minimum size, if it fails collapse. | 189 // Try preferred size, if it fails try minimum size, if it fails |
| 190 // collapse. |
| 155 (*it)->computed_width = (*it)->view->GetPreferredSize().width(); | 191 (*it)->computed_width = (*it)->view->GetPreferredSize().width(); |
| 156 if ((*it)->computed_width + padding > *available_width) | 192 if ((*it)->computed_width + padding > *available_width) |
| 157 (*it)->computed_width = (*it)->view->GetMinimumSize().width(); | 193 (*it)->computed_width = (*it)->view->GetMinimumSize().width(); |
| 158 if ((*it)->computed_width + padding > *available_width) { | 194 if ((*it)->computed_width + padding > *available_width) { |
| 159 (*it)->computed_width = 0; | 195 (*it)->computed_width = 0; |
| 160 (*it)->view->SetVisible(false); | 196 (*it)->view->SetVisible(false); |
| 161 } else { | 197 } else { |
| 162 (*it)->view->SetVisible(true); | 198 (*it)->view->SetVisible(true); |
| 163 (*available_width) -= (*it)->computed_width + padding; | 199 (*available_width) -= (*it)->computed_width + padding; |
| 164 } | 200 } |
| 165 } else { | 201 } else { |
| 166 (*it)->view->SetVisible(true); | 202 (*it)->view->SetVisible(true); |
| 167 } | 203 } |
| 168 // Layout visible decorations. | 204 |
| 169 if ((*it)->view->visible()) { | 205 if ((*it)->view->visible()) |
| 170 int padding = -(*it)->builtin_padding + | |
| 171 (first_visible ? (*it)->edge_item_padding : (*it)->item_padding); | |
| 172 first_visible = false; | 206 first_visible = false; |
| 173 int x; | 207 } |
| 174 if (position_ == LEFT_EDGE) | 208 } |
| 175 x = bounds->x() + padding; | 209 |
| 176 else | 210 void LocationBarLayout::HideUnneededSeparators(int* available_width) { |
| 177 x = bounds->x() + bounds->width() - padding - (*it)->computed_width; | 211 // Initialize |trailing_separator| to first decoration so that any leading |
| 178 int height = (*it)->height == 0 ? | 212 // separator will be hidden. |
| 179 (*it)->view->GetPreferredSize().height() : (*it)->height; | 213 Decorations::iterator trailing_separator = decorations_.begin(); |
| 180 (*it)->view->SetBounds(x, (*it)->y, (*it)->computed_width, height); | 214 for (Decorations::iterator it(decorations_.begin()); it != decorations_.end(); |
| 181 bounds->set_width(bounds->width() - padding - (*it)->computed_width + | 215 ++it) { |
| 182 (*it)->builtin_padding); | 216 if ((*it)->type == SEPARATOR) { |
| 183 if (position_ == LEFT_EDGE) { | 217 if (trailing_separator != decorations_.end()) { |
| 184 bounds->set_x(bounds->x() + padding + (*it)->computed_width - | 218 (*it)->view->SetVisible(false); |
| 185 (*it)->builtin_padding); | 219 // Add back what was subtracted when setting this separator visible in |
| 220 // LayoutPass1(). |
| 221 (*available_width) += (*it)->item_padding + (*it)->computed_width; |
| 222 } else { |
| 223 trailing_separator = it; |
| 186 } | 224 } |
| 225 } else if ((*it)->view->visible()) { |
| 226 trailing_separator = decorations_.end(); |
| 227 } |
| 228 } |
| 229 // If there's a trailing separator, hide it. |
| 230 if (trailing_separator != decorations_.end()) { |
| 231 (*trailing_separator)->view->SetVisible(false); |
| 232 // Add back what was subtracted when setting this separator visible in |
| 233 // LayoutPass1(). |
| 234 (*available_width) += (*trailing_separator)->item_padding + |
| 235 (*trailing_separator)->computed_width; |
| 236 } |
| 237 } |
| 238 |
| 239 void LocationBarLayout::SetBoundsForDecorations(gfx::Rect* bounds) { |
| 240 bool first_visible = true; |
| 241 for (Decorations::iterator it(decorations_.begin()); it != decorations_.end(); |
| 242 ++it) { |
| 243 LocationBarDecoration* curr = *it; |
| 244 if (!curr->view->visible()) |
| 245 continue; |
| 246 int padding = -curr->builtin_padding + |
| 247 (first_visible ? curr->edge_item_padding : curr->item_padding); |
| 248 first_visible = false; |
| 249 int x = (position_ == LEFT_EDGE) ? (bounds->x() + padding) : |
| 250 (bounds->right() - padding - curr->computed_width); |
| 251 int height = curr->height == 0 ? |
| 252 curr->view->GetPreferredSize().height() : curr->height; |
| 253 curr->view->SetBounds(x, curr->y, curr->computed_width, height); |
| 254 bounds->set_width(bounds->width() - padding - curr->computed_width + |
| 255 curr->builtin_padding); |
| 256 if (position_ == LEFT_EDGE) { |
| 257 bounds->set_x( |
| 258 bounds->x() + padding + curr->computed_width - curr->builtin_padding); |
| 187 } | 259 } |
| 188 } | 260 } |
| 189 int final_padding = first_visible ? edge_edit_padding_ : item_edit_padding_; | 261 int final_padding = first_visible ? edge_edit_padding_ : item_edit_padding_; |
| 190 bounds->set_width(bounds->width() - final_padding); | 262 bounds->set_width(bounds->width() - final_padding); |
| 191 if (position_ == LEFT_EDGE) | 263 if (position_ == LEFT_EDGE) |
| 192 bounds->set_x(bounds->x() + final_padding); | 264 bounds->set_x(bounds->x() + final_padding); |
| 193 } | 265 } |
| OLD | NEW |