OLD | NEW |
---|---|
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_main_axis_size = 0; |
38 int visible = 0; | 37 int visible = 0; |
sky
2014/05/13 16:24:59
visible -> num_visible or visible_count
calamity
2014/05/14 02:03:27
Done.
| |
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_main_axis_size += |
44 child->GetPreferredSize().width() + between_child_spacing_; | |
45 } else { | 45 } else { |
46 total += child->GetHeightForWidth(child_area.width()) + | 46 total_main_axis_size += child->GetHeightForWidth(child_area.width()) + |
47 between_child_spacing_; | 47 between_child_spacing_; |
48 } | 48 } |
49 ++visible; | 49 ++visible; |
50 } | 50 } |
51 | 51 |
52 if (visible) { | 52 if (visible) { |
53 total -= between_child_spacing_; | 53 total_main_axis_size -= between_child_spacing_; |
54 if (orientation_ == kHorizontal) | 54 int free_space = MainAxisSize(child_area) - total_main_axis_size; |
55 padding = (child_area.width() - total) / visible; | 55 if (spread_blank_space_) { |
56 else | 56 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 .
| |
57 padding = (child_area.height() - total) / visible; | 57 if (padding < 0) |
58 | 58 padding = 0; |
59 if (padding < 0) | 59 } else { |
60 padding = 0; | 60 int position = MainAxisPosition(child_area); |
61 int size = MainAxisSize(child_area); | |
62 if (box_pack_ == BOX_PACK_CENTER) { | |
63 position += free_space / 2; | |
64 size = total_main_axis_size; | |
65 } else if (box_pack_ == BOX_PACK_END) { | |
66 position += free_space; | |
67 size = total_main_axis_size; | |
68 } | |
69 gfx::Rect new_child_area(child_area); | |
70 SetMainAxisPosition(&new_child_area, position); | |
71 SetMainAxisSize(&new_child_area, size); | |
72 child_area.Intersect(new_child_area); | |
73 } | |
61 } | 74 } |
62 } | 75 } |
63 | 76 |
77 int x = child_area.x(); | |
78 int y = child_area.y(); | |
64 for (int i = 0; i < host->child_count(); ++i) { | 79 for (int i = 0; i < host->child_count(); ++i) { |
65 View* child = host->child_at(i); | 80 View* child = host->child_at(i); |
66 if (child->visible()) { | 81 if (child->visible()) { |
67 gfx::Rect bounds(x, y, child_area.width(), child_area.height()); | 82 gfx::Rect bounds(x, y, child_area.width(), child_area.height()); |
68 if (orientation_ == kHorizontal) { | 83 if (orientation_ == kHorizontal) { |
69 bounds.set_width(child->GetPreferredSize().width() + padding); | 84 bounds.set_width(child->GetPreferredSize().width() + padding); |
70 if (bounds.width() > 0) | 85 if (bounds.width() > 0) |
71 x += bounds.width() + between_child_spacing_; | 86 x += bounds.width() + between_child_spacing_; |
72 } else { | 87 } else { |
73 bounds.set_height(child->GetHeightForWidth(bounds.width()) + padding); | 88 bounds.set_height(child->GetHeightForWidth(bounds.width()) + padding); |
(...skipping 21 matching lines...) Expand all Loading... | |
95 } | 110 } |
96 | 111 |
97 return GetPreferredSizeForChildWidth(host, width); | 112 return GetPreferredSizeForChildWidth(host, width); |
98 } | 113 } |
99 | 114 |
100 int BoxLayout::GetPreferredHeightForWidth(View* host, int width) { | 115 int BoxLayout::GetPreferredHeightForWidth(View* host, int width) { |
101 int child_width = width - NonChildSize(host).width(); | 116 int child_width = width - NonChildSize(host).width(); |
102 return GetPreferredSizeForChildWidth(host, child_width).height(); | 117 return GetPreferredSizeForChildWidth(host, child_width).height(); |
103 } | 118 } |
104 | 119 |
120 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.
| |
121 return orientation_ == kHorizontal ? child_area.width() : child_area.height(); | |
122 } | |
123 | |
124 int BoxLayout::MainAxisPosition(const gfx::Rect& child_area) { | |
125 return orientation_ == kHorizontal ? child_area.x() : child_area.y(); | |
126 } | |
127 | |
128 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..
| |
129 if (orientation_ == kHorizontal) | |
130 child_area->set_width(size); | |
131 else | |
132 child_area->set_height(size); | |
133 } | |
134 | |
135 void BoxLayout::SetMainAxisPosition(gfx::Rect* child_area, int position) { | |
136 if (orientation_ == kHorizontal) | |
137 child_area->set_x(position); | |
138 else | |
139 child_area->set_y(position); | |
140 } | |
141 | |
105 gfx::Size BoxLayout::GetPreferredSizeForChildWidth(View* host, | 142 gfx::Size BoxLayout::GetPreferredSizeForChildWidth(View* host, |
106 int child_area_width) { | 143 int child_area_width) { |
107 gfx::Rect child_area_bounds; | 144 gfx::Rect child_area_bounds; |
108 | 145 |
109 if (orientation_ == kHorizontal) { | 146 if (orientation_ == kHorizontal) { |
110 // Horizontal layouts ignore |child_area_width|, meaning they mimic the | 147 // Horizontal layouts ignore |child_area_width|, meaning they mimic the |
111 // default behavior of GridLayout::GetPreferredHeightForWidth(). | 148 // default behavior of GridLayout::GetPreferredHeightForWidth(). |
112 // TODO(estade): fix this if it ever becomes a problem. | 149 // TODO(estade): fix this if it ever becomes a problem. |
113 int position = 0; | 150 int position = 0; |
114 for (int i = 0; i < host->child_count(); ++i) { | 151 for (int i = 0; i < host->child_count(); ++i) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
147 child_area_bounds.height() + non_child_size.height()); | 184 child_area_bounds.height() + non_child_size.height()); |
148 } | 185 } |
149 | 186 |
150 gfx::Size BoxLayout::NonChildSize(View* host) { | 187 gfx::Size BoxLayout::NonChildSize(View* host) { |
151 gfx::Insets insets(host->GetInsets()); | 188 gfx::Insets insets(host->GetInsets()); |
152 return gfx::Size(insets.width() + inside_border_insets_.width(), | 189 return gfx::Size(insets.width() + inside_border_insets_.width(), |
153 insets.height() + inside_border_insets_.height()); | 190 insets.height() + inside_border_insets_.height()); |
154 } | 191 } |
155 | 192 |
156 } // namespace views | 193 } // namespace views |
OLD | NEW |