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

Side by Side 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: 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 spread_blank_space_(false) { 22 spread_blank_space_(false),
23 box_pack_(BOX_PACK_START) {
23 } 24 }
24 25
25 BoxLayout::~BoxLayout() { 26 BoxLayout::~BoxLayout() {
26 } 27 }
27 28
28 void BoxLayout::Layout(View* host) { 29 void BoxLayout::Layout(View* host) {
29 gfx::Rect child_area(host->GetLocalBounds()); 30 gfx::Rect child_area(host->GetLocalBounds());
30 child_area.Inset(host->GetInsets()); 31 child_area.Inset(host->GetInsets());
31 child_area.Inset(inside_border_insets_); 32 child_area.Inset(inside_border_insets_);
32 int x = child_area.x();
33 int y = child_area.y();
34 33
35 int padding = 0; 34 int padding = 0;
36 if (spread_blank_space_) { 35 if (box_pack_ != BOX_PACK_START || spread_blank_space_) {
37 int total = 0; 36 int total = 0;
38 int visible = 0; 37 int visible = 0;
39 for (int i = 0; i < host->child_count(); ++i) { 38 for (int i = 0; i < host->child_count(); ++i) {
40 View* child = host->child_at(i); 39 View* child = host->child_at(i);
41 if (!child->visible()) 40 if (!child->visible())
42 continue; 41 continue;
43 if (orientation_ == kHorizontal) { 42 if (orientation_ == kHorizontal) {
44 total += child->GetPreferredSize().width() + between_child_spacing_; 43 total += child->GetPreferredSize().width() + between_child_spacing_;
45 } else { 44 } else {
46 total += child->GetHeightForWidth(child_area.width()) + 45 total += child->GetHeightForWidth(child_area.width()) +
47 between_child_spacing_; 46 between_child_spacing_;
48 } 47 }
49 ++visible; 48 ++visible;
50 } 49 }
51 50
52 if (visible) { 51 if (visible) {
53 total -= between_child_spacing_; 52 total -= between_child_spacing_;
54 if (orientation_ == kHorizontal) 53 if (spread_blank_space_) {
55 padding = (child_area.width() - total) / visible; 54 if (orientation_ == kHorizontal)
56 else 55 padding = (child_area.width() - total) / visible;
benwells 2014/05/13 02:48:05 I think this code would be easier to follow at a g
calamity 2014/05/13 04:29:29 Done.
57 padding = (child_area.height() - total) / visible; 56 else
57 padding = (child_area.height() - total) / visible;
58 58
59 if (padding < 0) 59 if (padding < 0)
60 padding = 0; 60 padding = 0;
61 } else {
62 if (box_pack_ == BOX_PACK_CENTER) {
63 if (orientation_ == kHorizontal) {
64 child_area.set_x(child_area.x() + (child_area.width() - total) / 2);
65 child_area.set_width(total);
66 } else {
67 child_area.set_y(child_area.y() +
68 (child_area.height() - total) / 2);
69 child_area.set_height(total);
70 }
71 } else if (box_pack_ == BOX_PACK_END) {
72 if (orientation_ == kHorizontal) {
73 child_area.set_x(child_area.x() + child_area.width() - total);
74 child_area.set_width(total);
75 } else {
76 child_area.set_y(child_area.y() + child_area.height() - total);
77 child_area.set_height(total);
78 }
79 }
80 }
61 } 81 }
62 } 82 }
63 83
84 int x = child_area.x();
85 int y = child_area.y();
64 for (int i = 0; i < host->child_count(); ++i) { 86 for (int i = 0; i < host->child_count(); ++i) {
65 View* child = host->child_at(i); 87 View* child = host->child_at(i);
66 if (child->visible()) { 88 if (child->visible()) {
67 gfx::Rect bounds(x, y, child_area.width(), child_area.height()); 89 gfx::Rect bounds(x, y, child_area.width(), child_area.height());
68 if (orientation_ == kHorizontal) { 90 if (orientation_ == kHorizontal) {
69 bounds.set_width(child->GetPreferredSize().width() + padding); 91 bounds.set_width(child->GetPreferredSize().width() + padding);
70 if (bounds.width() > 0) 92 if (bounds.width() > 0)
71 x += bounds.width() + between_child_spacing_; 93 x += bounds.width() + between_child_spacing_;
72 } else { 94 } else {
73 bounds.set_height(child->GetHeightForWidth(bounds.width()) + padding); 95 bounds.set_height(child->GetHeightForWidth(bounds.width()) + padding);
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 child_area_bounds.height() + non_child_size.height()); 169 child_area_bounds.height() + non_child_size.height());
148 } 170 }
149 171
150 gfx::Size BoxLayout::NonChildSize(View* host) { 172 gfx::Size BoxLayout::NonChildSize(View* host) {
151 gfx::Insets insets(host->GetInsets()); 173 gfx::Insets insets(host->GetInsets());
152 return gfx::Size(insets.width() + inside_border_insets_.width(), 174 return gfx::Size(insets.width() + inside_border_insets_.width(),
153 insets.height() + inside_border_insets_.height()); 175 insets.height() + inside_border_insets_.height());
154 } 176 }
155 177
156 } // namespace views 178 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698