Index: ui/views/layout/box_layout.cc |
diff --git a/ui/views/layout/box_layout.cc b/ui/views/layout/box_layout.cc |
index 7e5abff4880af33a0b34583c3d0c7f127b7480c5..f699ed1ba2fb646d44146e72687766b8e56a6628 100644 |
--- a/ui/views/layout/box_layout.cc |
+++ b/ui/views/layout/box_layout.cc |
@@ -11,6 +11,36 @@ |
namespace views { |
+BoxLayout::ViewWrapper::ViewWrapper() : view_(nullptr) {} |
+ |
+BoxLayout::ViewWrapper::ViewWrapper(View* view) : view_(view) { |
+ gfx::Insets* margins = view_->GetProperty(kMarginsKey); |
+ if (margins) |
+ margins_ = *margins; |
+} |
+ |
+BoxLayout::ViewWrapper::~ViewWrapper() {} |
+ |
+int BoxLayout::ViewWrapper::GetHeightForWidth(int width) const { |
+ return view_->GetHeightForWidth(width - margins_.width()) + margins_.height(); |
+} |
+ |
+gfx::Size BoxLayout::ViewWrapper::GetPreferredSize() const { |
+ gfx::Size preferred_size = view_->GetPreferredSize(); |
+ preferred_size.Enlarge(margins_.width(), margins_.height()); |
+ return preferred_size; |
+} |
+ |
+void BoxLayout::ViewWrapper::SetBoundsRect(const gfx::Rect& bounds) { |
+ view_->SetBounds(bounds.x() + margins_.left(), bounds.y() + margins_.top(), |
+ bounds.width() - margins_.width(), |
+ bounds.height() - margins_.height()); |
+} |
+ |
+bool BoxLayout::ViewWrapper::visible() const { |
+ return view_->visible(); |
+} |
+ |
BoxLayout::BoxLayout(BoxLayout::Orientation orientation, |
int inside_border_horizontal_spacing, |
int inside_border_vertical_spacing, |
@@ -49,8 +79,7 @@ void BoxLayout::SetDefaultFlex(int default_flex) { |
void BoxLayout::Layout(View* host) { |
DCHECK_EQ(host_, host); |
- gfx::Rect child_area(host->GetLocalBounds()); |
- child_area.Inset(host->GetInsets()); |
+ gfx::Rect child_area(host->GetContentsBounds()); |
child_area.Inset(inside_border_insets_); |
int total_main_axis_size = 0; |
@@ -58,10 +87,10 @@ void BoxLayout::Layout(View* host) { |
int flex_sum = 0; |
// Calculate the total size of children in the main axis. |
for (int i = 0; i < host->child_count(); ++i) { |
- View* child = host->child_at(i); |
- if (!child->visible()) |
+ ViewWrapper child(host->child_at(i)); |
+ if (!child.visible()) |
continue; |
- int flex = GetFlexForView(child); |
+ int flex = GetFlexForView(child.view()); |
int child_main_axis_size = MainAxisSizeForView(child, child_area.width()); |
if (child_main_axis_size == 0 && flex == 0) |
continue; |
@@ -106,8 +135,8 @@ void BoxLayout::Layout(View* host) { |
int total_padding = 0; |
int current_flex = 0; |
for (int i = 0; i < host->child_count(); ++i) { |
- View* child = host->child_at(i); |
- if (!child->visible()) |
+ ViewWrapper child(host->child_at(i)); |
+ if (!child.visible()) |
continue; |
// TODO(bruthig): Fix this. The main axis should be calculated before |
@@ -131,8 +160,8 @@ void BoxLayout::Layout(View* host) { |
// Calculate flex padding. |
int current_padding = 0; |
- if (GetFlexForView(child) > 0) { |
- current_flex += GetFlexForView(child); |
+ if (GetFlexForView(child.view()) > 0) { |
+ current_flex += GetFlexForView(child.view()); |
int quot = (main_free_space * current_flex) / flex_sum; |
int rem = (main_free_space * current_flex) % flex_sum; |
current_padding = quot - total_padding; |
@@ -147,12 +176,12 @@ void BoxLayout::Layout(View* host) { |
// See https://crbug.com/682266. |
int child_main_axis_size = MainAxisSizeForView(child, child_area.width()); |
SetMainAxisSize(child_main_axis_size + current_padding, &bounds); |
- if (MainAxisSize(bounds) > 0 || GetFlexForView(child) > 0) |
+ if (MainAxisSize(bounds) > 0 || GetFlexForView(child.view()) > 0) |
main_position += MainAxisSize(bounds) + between_child_spacing_; |
// Clamp child view bounds to |child_area|. |
bounds.Intersect(child_area); |
- child->SetBoundsRect(bounds); |
+ child.SetBoundsRect(bounds); |
} |
// Flex views should have grown/shrunk to consume all free space. |
@@ -165,12 +194,12 @@ gfx::Size BoxLayout::GetPreferredSize(const View* host) const { |
// Calculate the child views' preferred width. |
int width = 0; |
if (orientation_ == kVertical) { |
- for (int i = 0; i < host->child_count(); ++i) { |
- const View* child = host->child_at(i); |
- if (!child->visible()) |
+ for (int i = 0; i < host_->child_count(); ++i) { |
+ const ViewWrapper child(host_->child_at(i)); |
+ if (!child.visible()) |
continue; |
- width = std::max(width, child->GetPreferredSize().width()); |
+ width = std::max(width, child.GetPreferredSize().width()); |
} |
width = std::max(width, minimum_cross_axis_size_); |
} |
@@ -194,7 +223,7 @@ void BoxLayout::ViewRemoved(View* host, View* view) { |
} |
int BoxLayout::GetFlexForView(const View* view) const { |
- std::map<const View*, int>::const_iterator it = flex_map_.find(view); |
+ FlexMap::const_iterator it = flex_map_.find(view); |
if (it == flex_map_.end()) |
return default_flex_; |
@@ -245,26 +274,27 @@ void BoxLayout::SetCrossAxisPosition(int position, gfx::Rect* rect) const { |
rect->set_y(position); |
} |
-int BoxLayout::MainAxisSizeForView(const View* view, |
+int BoxLayout::MainAxisSizeForView(const ViewWrapper& view, |
int child_area_width) const { |
return orientation_ == kHorizontal |
- ? view->GetPreferredSize().width() |
- : view->GetHeightForWidth(cross_axis_alignment_ == |
- CROSS_AXIS_ALIGNMENT_STRETCH |
- ? child_area_width |
- : view->GetPreferredSize().width()); |
+ ? view.GetPreferredSize().width() |
+ : view.GetHeightForWidth(cross_axis_alignment_ == |
+ CROSS_AXIS_ALIGNMENT_STRETCH |
+ ? child_area_width |
+ : view.GetPreferredSize().width()); |
} |
-int BoxLayout::CrossAxisSizeForView(const View* view) const { |
+int BoxLayout::CrossAxisSizeForView(const ViewWrapper& view) const { |
// TODO(bruthig): For horizontal case use the available width and not the |
// preferred width. See https://crbug.com/682266. |
return orientation_ == kVertical |
- ? view->GetPreferredSize().width() |
- : view->GetHeightForWidth(view->GetPreferredSize().width()); |
+ ? view.GetPreferredSize().width() |
+ : view.GetHeightForWidth(view.GetPreferredSize().width()); |
} |
gfx::Size BoxLayout::GetPreferredSizeForChildWidth(const View* host, |
int child_area_width) const { |
+ DCHECK_EQ(host, host_); |
gfx::Rect child_area_bounds; |
if (orientation_ == kHorizontal) { |
@@ -272,12 +302,12 @@ gfx::Size BoxLayout::GetPreferredSizeForChildWidth(const View* host, |
// default behavior of GridLayout::GetPreferredHeightForWidth(). |
// TODO(estade|bruthig): Fix this See // https://crbug.com/682266. |
int position = 0; |
- for (int i = 0; i < host->child_count(); ++i) { |
- const View* child = host->child_at(i); |
- if (!child->visible()) |
+ for (int i = 0; i < host_->child_count(); ++i) { |
+ const ViewWrapper child(host_->child_at(i)); |
+ if (!child.visible()) |
continue; |
- gfx::Size size(child->GetPreferredSize()); |
+ gfx::Size size(child.GetPreferredSize()); |
if (size.IsEmpty()) |
continue; |
@@ -289,9 +319,9 @@ gfx::Size BoxLayout::GetPreferredSizeForChildWidth(const View* host, |
std::max(child_area_bounds.height(), minimum_cross_axis_size_)); |
} else { |
int height = 0; |
- for (int i = 0; i < host->child_count(); ++i) { |
- const View* child = host->child_at(i); |
- if (!child->visible()) |
+ for (int i = 0; i < host_->child_count(); ++i) { |
+ const ViewWrapper child(host_->child_at(i)); |
+ if (!child.visible()) |
continue; |
// Use the child area width for getting the height if the child is |
@@ -307,7 +337,7 @@ gfx::Size BoxLayout::GetPreferredSizeForChildWidth(const View* host, |
child_area_bounds.set_height(height); |
} |
- gfx::Size non_child_size = NonChildSize(host); |
+ gfx::Size non_child_size = NonChildSize(host_); |
return gfx::Size(child_area_bounds.width() + non_child_size.width(), |
child_area_bounds.height() + non_child_size.height()); |
} |