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

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: remove set_spread_blank_space, rename to MainAxisAlignment 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
« no previous file with comments | « ui/views/layout/box_layout.h ('k') | ui/views/layout/box_layout_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..ae44a0812a6d46e6f78b8bc3c7af5c3cf9ec9ca5 100644
--- a/ui/views/layout/box_layout.cc
+++ b/ui/views/layout/box_layout.cc
@@ -19,7 +19,7 @@ BoxLayout::BoxLayout(BoxLayout::Orientation orientation,
inside_border_vertical_spacing,
inside_border_horizontal_spacing),
between_child_spacing_(between_child_spacing),
- spread_blank_space_(false) {
+ main_axis_alignment_(MAIN_AXIS_ALIGNMENT_START) {
}
BoxLayout::~BoxLayout() {
@@ -29,38 +29,55 @@ 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;
- int visible = 0;
+ if (main_axis_alignment_ != MAIN_AXIS_ALIGNMENT_START) {
+ int total_main_axis_size = 0;
+ int num_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;
+ ++num_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;
+ if (num_visible) {
+ total_main_axis_size -= between_child_spacing_;
+ int free_space = MainAxisSize(child_area) - total_main_axis_size;
+ int position = MainAxisPosition(child_area);
+ int size = MainAxisSize(child_area);
+ switch (main_axis_alignment_) {
+ case MAIN_AXIS_ALIGNMENT_FILL:
+ padding = std::max(free_space / num_visible, 0);
+ break;
+ case MAIN_AXIS_ALIGNMENT_CENTER:
+ position += free_space / 2;
+ size = total_main_axis_size;
+ break;
+ case MAIN_AXIS_ALIGNMENT_END:
+ position += free_space;
+ size = total_main_axis_size;
+ break;
+ default:
+ NOTREACHED();
+ break;
+ }
+ gfx::Rect new_child_area(child_area);
+ SetMainAxisPosition(position, &new_child_area);
+ SetMainAxisSize(size, &new_child_area);
+ 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 +119,28 @@ int BoxLayout::GetPreferredHeightForWidth(View* host, int width) {
return GetPreferredSizeForChildWidth(host, child_width).height();
}
+int BoxLayout::MainAxisSize(const gfx::Rect& child_area) const {
+ return orientation_ == kHorizontal ? child_area.width() : child_area.height();
+}
+
+int BoxLayout::MainAxisPosition(const gfx::Rect& child_area) const {
+ return orientation_ == kHorizontal ? child_area.x() : child_area.y();
+}
+
+void BoxLayout::SetMainAxisSize(int size, gfx::Rect* child_area) const {
+ if (orientation_ == kHorizontal)
+ child_area->set_width(size);
+ else
+ child_area->set_height(size);
+}
+
+void BoxLayout::SetMainAxisPosition(int position, gfx::Rect* child_area) const {
+ 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;
« no previous file with comments | « ui/views/layout/box_layout.h ('k') | ui/views/layout/box_layout_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698