Chromium Code Reviews| Index: ui/views/layout/box_layout.cc |
| diff --git a/ui/views/layout/box_layout.cc b/ui/views/layout/box_layout.cc |
| index c11479678a998fe492103648b145b3e4911d81d2..60d0903b8ce9d7829d74adb9da9c71d2afce9bb1 100644 |
| --- a/ui/views/layout/box_layout.cc |
| +++ b/ui/views/layout/box_layout.cc |
| @@ -19,7 +19,8 @@ BoxLayout::BoxLayout(BoxLayout::Orientation orientation, |
| inside_border_vertical_spacing, |
| inside_border_horizontal_spacing), |
| between_child_spacing_(between_child_spacing), |
| - spread_blank_space_(false) { |
| + spread_blank_space_(false), |
| + box_pack_(BOX_PACK_START) { |
| } |
| BoxLayout::~BoxLayout() { |
| @@ -29,38 +30,53 @@ void BoxLayout::Layout(View* host) { |
| gfx::Rect child_area(host->GetLocalBounds()); |
| child_area.Inset(host->GetInsets()); |
| child_area.Inset(inside_border_insets_); |
| - int x = child_area.x(); |
| - int y = child_area.y(); |
| int padding = 0; |
| - if (spread_blank_space_) { |
| - int total = 0; |
| + if (box_pack_ != BOX_PACK_START || spread_blank_space_) { |
| + int total_main_axis_size = 0; |
| int visible = 0; |
| for (int i = 0; i < host->child_count(); ++i) { |
| View* child = host->child_at(i); |
| if (!child->visible()) |
| continue; |
| if (orientation_ == kHorizontal) { |
| - total += child->GetPreferredSize().width() + between_child_spacing_; |
| + total_main_axis_size += |
| + child->GetPreferredSize().width() + between_child_spacing_; |
| } else { |
| - total += child->GetHeightForWidth(child_area.width()) + |
| - between_child_spacing_; |
| + total_main_axis_size += child->GetHeightForWidth(child_area.width()) + |
| + between_child_spacing_; |
| } |
| ++visible; |
| } |
| if (visible) { |
| - total -= between_child_spacing_; |
| - if (orientation_ == kHorizontal) |
| - padding = (child_area.width() - total) / visible; |
| - else |
| - padding = (child_area.height() - total) / visible; |
| - |
| - if (padding < 0) |
| - padding = 0; |
| + total_main_axis_size -= between_child_spacing_; |
| + if (spread_blank_space_) { |
| + padding = (MainAxisSize(child_area) - total_main_axis_size) / visible; |
| + if (padding < 0) |
| + padding = 0; |
| + } else { |
| + gfx::Rect new_child_area(child_area); |
| + if (box_pack_ == BOX_PACK_CENTER) { |
| + SetMainAxisPosition( |
| + &new_child_area, |
| + MainAxisPosition(new_child_area) + |
| + (MainAxisSize(new_child_area) - total_main_axis_size) / 2); |
|
benwells
2014/05/13 04:44:21
Nit: a variable:
free_space = MainAxisSize(new_ch
calamity
2014/05/13 05:04:06
Done.
|
| + SetMainAxisSize(&new_child_area, total_main_axis_size); |
| + } else if (box_pack_ == BOX_PACK_END) { |
| + SetMainAxisPosition(&new_child_area, |
| + MainAxisPosition(new_child_area) + |
| + MainAxisSize(new_child_area) - |
| + total_main_axis_size); |
| + SetMainAxisSize(&new_child_area, total_main_axis_size); |
| + } |
| + child_area.Intersect(new_child_area); |
| + } |
| } |
| } |
| + int x = child_area.x(); |
| + int y = child_area.y(); |
| for (int i = 0; i < host->child_count(); ++i) { |
| View* child = host->child_at(i); |
| if (child->visible()) { |
| @@ -102,6 +118,28 @@ int BoxLayout::GetPreferredHeightForWidth(View* host, int width) { |
| return GetPreferredSizeForChildWidth(host, child_width).height(); |
| } |
| +int BoxLayout::MainAxisSize(const gfx::Rect& child_area) { |
| + return orientation_ == kHorizontal ? child_area.width() : child_area.height(); |
| +} |
| + |
| +int BoxLayout::MainAxisPosition(const gfx::Rect& child_area) { |
| + return orientation_ == kHorizontal ? child_area.x() : child_area.y(); |
| +} |
| + |
| +void BoxLayout::SetMainAxisSize(gfx::Rect* child_area, int size) { |
| + if (orientation_ == kHorizontal) |
| + child_area->set_width(size); |
| + else |
| + child_area->set_height(size); |
| +} |
| + |
| +void BoxLayout::SetMainAxisPosition(gfx::Rect* child_area, int position) { |
| + if (orientation_ == kHorizontal) |
| + child_area->set_x(position); |
| + else |
| + child_area->set_y(position); |
| +} |
| + |
| gfx::Size BoxLayout::GetPreferredSizeForChildWidth(View* host, |
| int child_area_width) { |
| gfx::Rect child_area_bounds; |