Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(427)

Unified Diff: ui/views/layout/box_layout.cc

Issue 284753002: Add main axis alignment for BoxLayout. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix nit, factor out a few more variables Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..8df124cf67906d335b8db57d53f5513f34eaeaac 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,52 @@ 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;
sky 2014/05/13 16:24:59 visible -> num_visible or visible_count
calamity 2014/05/14 02:03:27 Done.
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_;
+ int free_space = MainAxisSize(child_area) - total_main_axis_size;
+ if (spread_blank_space_) {
+ padding = free_space / visible;
sky 2014/05/13 16:24:59 This calculation isn't quite right.It has rounding
calamity 2014/05/14 02:03:27 I don't quite understand what the problem here is?
sky 2014/05/14 18:13:40 Yes and sure.
calamity 2014/05/15 02:51:07 Logged a bug at crbug.com/373571 .
+ if (padding < 0)
+ padding = 0;
+ } else {
+ int position = MainAxisPosition(child_area);
+ int size = MainAxisSize(child_area);
+ if (box_pack_ == BOX_PACK_CENTER) {
+ position += free_space / 2;
+ size = total_main_axis_size;
+ } else if (box_pack_ == BOX_PACK_END) {
+ position += free_space;
+ size = total_main_axis_size;
+ }
+ gfx::Rect new_child_area(child_area);
+ SetMainAxisPosition(&new_child_area, position);
+ SetMainAxisSize(&new_child_area, 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 +117,28 @@ int BoxLayout::GetPreferredHeightForWidth(View* host, int width) {
return GetPreferredSizeForChildWidth(host, child_width).height();
}
+int BoxLayout::MainAxisSize(const gfx::Rect& child_area) {
sky 2014/05/13 16:24:59 const on this and the next.
calamity 2014/05/14 02:03:27 Done.
+ 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) {
sky 2014/05/13 16:24:59 nit: style guide says out params should be last.
calamity 2014/05/14 02:03:27 Done. This can be const too..
+ 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;

Powered by Google App Engine
This is Rietveld 408576698