Chromium Code Reviews| 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 "ui/views/layout/box_layout.h" | 5 #include "ui/views/layout/box_layout.h" |
| 6 | 6 |
| 7 #include "ui/gfx/rect.h" | 7 #include "ui/gfx/rect.h" |
| 8 #include "ui/views/view.h" | 8 #include "ui/views/view.h" |
| 9 | 9 |
| 10 namespace views { | 10 namespace views { |
| 11 | 11 |
| 12 BoxLayout::BoxLayout(BoxLayout::Orientation orientation, | 12 BoxLayout::BoxLayout(BoxLayout::Orientation orientation, |
| 13 int inside_border_horizontal_spacing, | 13 int inside_border_horizontal_spacing, |
| 14 int inside_border_vertical_spacing, | 14 int inside_border_vertical_spacing, |
| 15 int between_child_spacing) | 15 int between_child_spacing) |
| 16 : orientation_(orientation), | 16 : orientation_(orientation), |
| 17 inside_border_insets_(inside_border_vertical_spacing, | 17 inside_border_insets_(inside_border_vertical_spacing, |
| 18 inside_border_horizontal_spacing, | 18 inside_border_horizontal_spacing, |
| 19 inside_border_vertical_spacing, | 19 inside_border_vertical_spacing, |
| 20 inside_border_horizontal_spacing), | 20 inside_border_horizontal_spacing), |
| 21 between_child_spacing_(between_child_spacing), | 21 between_child_spacing_(between_child_spacing), |
| 22 main_axis_alignment_(MAIN_AXIS_ALIGNMENT_START), | 22 main_axis_alignment_(MAIN_AXIS_ALIGNMENT_START), |
| 23 cross_axis_alignment_(CROSS_AXIS_ALIGNMENT_STRETCH) { | 23 cross_axis_alignment_(CROSS_AXIS_ALIGNMENT_STRETCH), |
| 24 default_flex_(0) { | |
| 24 } | 25 } |
| 25 | 26 |
| 26 BoxLayout::~BoxLayout() { | 27 BoxLayout::~BoxLayout() { |
| 27 } | 28 } |
| 28 | 29 |
| 30 void BoxLayout::SetFlexForViewAt(int index, int flex_weight) { | |
| 31 DCHECK(index >= 0); | |
|
sky
2014/07/18 15:54:00
DCHECK_GE for all these.
calamity
2014/07/22 05:35:14
Done.
| |
| 32 DCHECK(flex_weight >= 0); | |
| 33 flex_map_[index] = flex_weight; | |
| 34 } | |
| 35 | |
| 36 void BoxLayout::ClearFlexForViewAt(int index) { | |
| 37 DCHECK(index >= 0); | |
| 38 flex_map_.erase(index); | |
| 39 } | |
| 40 | |
| 41 void BoxLayout::SetDefaultFlex(int default_flex) { | |
| 42 DCHECK(default_flex >= 0); | |
| 43 default_flex_ = default_flex; | |
| 44 } | |
| 45 | |
| 29 void BoxLayout::Layout(View* host) { | 46 void BoxLayout::Layout(View* host) { |
| 30 gfx::Rect child_area(host->GetLocalBounds()); | 47 gfx::Rect child_area(host->GetLocalBounds()); |
| 31 child_area.Inset(host->GetInsets()); | 48 child_area.Inset(host->GetInsets()); |
| 32 child_area.Inset(inside_border_insets_); | 49 child_area.Inset(inside_border_insets_); |
| 33 | 50 |
| 34 int padding = 0; | 51 int total_main_axis_size = 0; |
|
sky
2014/07/18 15:54:00
This code needs some comments as to what each of t
calamity
2014/07/22 05:35:14
Done.
| |
| 35 if (main_axis_alignment_ != MAIN_AXIS_ALIGNMENT_START) { | 52 int num_visible = 0; |
| 36 int total_main_axis_size = 0; | 53 int flex_sum = 0; |
| 37 int num_visible = 0; | 54 for (int i = 0; i < host->child_count(); ++i) { |
| 38 for (int i = 0; i < host->child_count(); ++i) { | 55 View* child = host->child_at(i); |
| 39 View* child = host->child_at(i); | 56 if (!child->visible()) |
| 40 if (!child->visible()) | 57 continue; |
| 41 continue; | 58 total_main_axis_size += |
| 42 total_main_axis_size += MainAxisSizeForView(child, child_area.width()) + | 59 MainAxisSizeForView(child, child_area.width()) + between_child_spacing_; |
| 43 between_child_spacing_; | 60 ++num_visible; |
| 44 ++num_visible; | 61 flex_sum += GetFlexForViewAt(i); |
| 45 } | 62 } |
| 46 | 63 |
| 47 if (num_visible) { | 64 if (!num_visible) |
| 48 total_main_axis_size -= between_child_spacing_; | 65 return; |
| 49 int free_space = MainAxisSize(child_area) - total_main_axis_size; | 66 |
| 50 int position = MainAxisPosition(child_area); | 67 total_main_axis_size -= between_child_spacing_; |
| 51 int size = MainAxisSize(child_area); | 68 // Free space can be negative indicating that the views want to overflow. |
| 69 int main_free_space = MainAxisSize(child_area) - total_main_axis_size; | |
| 70 { | |
| 71 int position = MainAxisPosition(child_area); | |
| 72 int size = MainAxisSize(child_area); | |
| 73 if (!flex_sum) { | |
| 52 switch (main_axis_alignment_) { | 74 switch (main_axis_alignment_) { |
| 53 case MAIN_AXIS_ALIGNMENT_FILL: | 75 case MAIN_AXIS_ALIGNMENT_START: |
| 54 padding = std::max(free_space / num_visible, 0); | |
| 55 break; | 76 break; |
| 56 case MAIN_AXIS_ALIGNMENT_CENTER: | 77 case MAIN_AXIS_ALIGNMENT_CENTER: |
| 57 position += free_space / 2; | 78 position += main_free_space / 2; |
| 58 size = total_main_axis_size; | 79 size = total_main_axis_size; |
| 59 break; | 80 break; |
| 60 case MAIN_AXIS_ALIGNMENT_END: | 81 case MAIN_AXIS_ALIGNMENT_END: |
| 61 position += free_space; | 82 position += main_free_space; |
| 62 size = total_main_axis_size; | 83 size = total_main_axis_size; |
| 63 break; | 84 break; |
| 64 default: | 85 default: |
| 65 NOTREACHED(); | 86 NOTREACHED(); |
| 66 break; | 87 break; |
| 67 } | 88 } |
| 68 gfx::Rect new_child_area(child_area); | |
| 69 SetMainAxisPosition(position, &new_child_area); | |
| 70 SetMainAxisSize(size, &new_child_area); | |
| 71 child_area.Intersect(new_child_area); | |
| 72 } | 89 } |
| 90 gfx::Rect new_child_area(child_area); | |
| 91 SetMainAxisPosition(position, &new_child_area); | |
| 92 SetMainAxisSize(size, &new_child_area); | |
| 93 child_area.Intersect(new_child_area); | |
| 73 } | 94 } |
| 74 | 95 |
| 75 int main_position = MainAxisPosition(child_area); | 96 int main_position = MainAxisPosition(child_area); |
| 97 int total_padding = 0; | |
| 98 int current_flex = 0; | |
| 76 for (int i = 0; i < host->child_count(); ++i) { | 99 for (int i = 0; i < host->child_count(); ++i) { |
| 77 View* child = host->child_at(i); | 100 View* child = host->child_at(i); |
| 78 if (child->visible()) { | 101 if (!child->visible()) |
| 79 gfx::Rect bounds(child_area); | 102 continue; |
| 80 SetMainAxisPosition(main_position, &bounds); | 103 |
| 81 if (cross_axis_alignment_ != CROSS_AXIS_ALIGNMENT_STRETCH) { | 104 gfx::Rect bounds(child_area); |
| 82 int free_space = CrossAxisSize(bounds) - CrossAxisSizeForView(child); | 105 SetMainAxisPosition(main_position, &bounds); |
| 83 int position = CrossAxisPosition(bounds); | 106 if (cross_axis_alignment_ != CROSS_AXIS_ALIGNMENT_STRETCH) { |
| 84 if (cross_axis_alignment_ == CROSS_AXIS_ALIGNMENT_CENTER) { | 107 int free_space = CrossAxisSize(bounds) - CrossAxisSizeForView(child); |
| 85 position += free_space / 2; | 108 int position = CrossAxisPosition(bounds); |
| 86 } else if (cross_axis_alignment_ == CROSS_AXIS_ALIGNMENT_END) { | 109 if (cross_axis_alignment_ == CROSS_AXIS_ALIGNMENT_CENTER) { |
| 87 position += free_space; | 110 position += free_space / 2; |
| 88 } | 111 } else if (cross_axis_alignment_ == CROSS_AXIS_ALIGNMENT_END) { |
| 89 SetCrossAxisPosition(position, &bounds); | 112 position += free_space; |
| 90 SetCrossAxisSize(CrossAxisSizeForView(child), &bounds); | |
| 91 } | 113 } |
| 92 int child_main_axis_size = MainAxisSizeForView(child, child_area.width()); | 114 SetCrossAxisPosition(position, &bounds); |
| 93 SetMainAxisSize(child_main_axis_size + padding, &bounds); | 115 SetCrossAxisSize(CrossAxisSizeForView(child), &bounds); |
| 94 if (MainAxisSize(bounds) > 0) | 116 } |
| 95 main_position += MainAxisSize(bounds) + between_child_spacing_; | |
| 96 | 117 |
| 97 // Clamp child view bounds to |child_area|. | 118 int current_padding = 0; |
| 98 bounds.Intersect(child_area); | 119 if (GetFlexForViewAt(i) > 0) { |
| 99 child->SetBoundsRect(bounds); | 120 current_flex += GetFlexForViewAt(i); |
| 121 int quot = (main_free_space * current_flex) / flex_sum; | |
| 122 int rem = (main_free_space * current_flex) % flex_sum; | |
| 123 current_padding = quot - total_padding; | |
| 124 // Use the current remainder to round to the nearest pixel. | |
| 125 if (std::abs(rem) * 2 >= flex_sum) | |
| 126 current_padding += main_free_space > 0 ? 1 : -1; | |
| 127 total_padding += current_padding; | |
| 100 } | 128 } |
| 129 | |
| 130 int child_main_axis_size = MainAxisSizeForView(child, child_area.width()); | |
| 131 SetMainAxisSize(child_main_axis_size + current_padding, &bounds); | |
| 132 if (MainAxisSize(bounds) > 0 || GetFlexForViewAt(i) > 0) | |
| 133 main_position += MainAxisSize(bounds) + between_child_spacing_; | |
| 134 | |
| 135 // Clamp child view bounds to |child_area|. | |
| 136 bounds.Intersect(child_area); | |
| 137 child->SetBoundsRect(bounds); | |
| 101 } | 138 } |
| 139 | |
| 140 // Flex views should have grown/shrunk to consume all free space. | |
| 141 if (flex_sum) | |
| 142 DCHECK_EQ(total_padding, main_free_space); | |
| 102 } | 143 } |
| 103 | 144 |
| 104 gfx::Size BoxLayout::GetPreferredSize(const View* host) const { | 145 gfx::Size BoxLayout::GetPreferredSize(const View* host) const { |
| 105 // Calculate the child views' preferred width. | 146 // Calculate the child views' preferred width. |
| 106 int width = 0; | 147 int width = 0; |
| 107 if (orientation_ == kVertical) { | 148 if (orientation_ == kVertical) { |
| 108 for (int i = 0; i < host->child_count(); ++i) { | 149 for (int i = 0; i < host->child_count(); ++i) { |
| 109 const View* child = host->child_at(i); | 150 const View* child = host->child_at(i); |
| 110 if (!child->visible()) | 151 if (!child->visible()) |
| 111 continue; | 152 continue; |
| 112 | 153 |
| 113 width = std::max(width, child->GetPreferredSize().width()); | 154 width = std::max(width, child->GetPreferredSize().width()); |
| 114 } | 155 } |
| 115 } | 156 } |
| 116 | 157 |
| 117 return GetPreferredSizeForChildWidth(host, width); | 158 return GetPreferredSizeForChildWidth(host, width); |
| 118 } | 159 } |
| 119 | 160 |
| 120 int BoxLayout::GetPreferredHeightForWidth(const View* host, int width) const { | 161 int BoxLayout::GetPreferredHeightForWidth(const View* host, int width) const { |
| 121 int child_width = width - NonChildSize(host).width(); | 162 int child_width = width - NonChildSize(host).width(); |
| 122 return GetPreferredSizeForChildWidth(host, child_width).height(); | 163 return GetPreferredSizeForChildWidth(host, child_width).height(); |
| 123 } | 164 } |
| 124 | 165 |
| 166 int BoxLayout::GetFlexForViewAt(int index) { | |
| 167 std::map<int, int>::const_iterator it = flex_map_.find(index); | |
| 168 if (it == flex_map_.end()) | |
| 169 return default_flex_; | |
| 170 | |
| 171 return it->second; | |
| 172 } | |
| 173 | |
| 125 int BoxLayout::MainAxisSize(const gfx::Rect& rect) const { | 174 int BoxLayout::MainAxisSize(const gfx::Rect& rect) const { |
| 126 return orientation_ == kHorizontal ? rect.width() : rect.height(); | 175 return orientation_ == kHorizontal ? rect.width() : rect.height(); |
| 127 } | 176 } |
| 128 | 177 |
| 129 int BoxLayout::MainAxisPosition(const gfx::Rect& rect) const { | 178 int BoxLayout::MainAxisPosition(const gfx::Rect& rect) const { |
| 130 return orientation_ == kHorizontal ? rect.x() : rect.y(); | 179 return orientation_ == kHorizontal ? rect.x() : rect.y(); |
| 131 } | 180 } |
| 132 | 181 |
| 133 void BoxLayout::SetMainAxisSize(int size, gfx::Rect* rect) const { | 182 void BoxLayout::SetMainAxisSize(int size, gfx::Rect* rect) const { |
| 134 if (orientation_ == kHorizontal) | 183 if (orientation_ == kHorizontal) |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 229 child_area_bounds.height() + non_child_size.height()); | 278 child_area_bounds.height() + non_child_size.height()); |
| 230 } | 279 } |
| 231 | 280 |
| 232 gfx::Size BoxLayout::NonChildSize(const View* host) const { | 281 gfx::Size BoxLayout::NonChildSize(const View* host) const { |
| 233 gfx::Insets insets(host->GetInsets()); | 282 gfx::Insets insets(host->GetInsets()); |
| 234 return gfx::Size(insets.width() + inside_border_insets_.width(), | 283 return gfx::Size(insets.width() + inside_border_insets_.width(), |
| 235 insets.height() + inside_border_insets_.height()); | 284 insets.height() + inside_border_insets_.height()); |
| 236 } | 285 } |
| 237 | 286 |
| 238 } // namespace views | 287 } // namespace views |
| OLD | NEW |