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/geometry/rect.h" | 7 #include "ui/gfx/geometry/rect.h" |
8 #include "ui/views/view.h" | 8 #include "ui/views/view.h" |
9 | 9 |
10 namespace views { | 10 namespace views { |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 } | 96 } |
97 gfx::Rect new_child_area(child_area); | 97 gfx::Rect new_child_area(child_area); |
98 SetMainAxisPosition(position, &new_child_area); | 98 SetMainAxisPosition(position, &new_child_area); |
99 SetMainAxisSize(size, &new_child_area); | 99 SetMainAxisSize(size, &new_child_area); |
100 child_area.Intersect(new_child_area); | 100 child_area.Intersect(new_child_area); |
101 } | 101 } |
102 | 102 |
103 int main_position = MainAxisPosition(child_area); | 103 int main_position = MainAxisPosition(child_area); |
104 int total_padding = 0; | 104 int total_padding = 0; |
105 int current_flex = 0; | 105 int current_flex = 0; |
106 for (int i = 0; i < host->child_count(); ++i) { | 106 LayoutChildren(host, child_area, main_free_space, flex_sum, main_position, |
107 View* child = host->child_at(i); | 107 current_flex, &total_padding); |
108 if (!child->visible()) | |
109 continue; | |
110 | |
111 // Calculate cross axis size. | |
112 gfx::Rect bounds(child_area); | |
113 SetMainAxisPosition(main_position, &bounds); | |
114 if (cross_axis_alignment_ != CROSS_AXIS_ALIGNMENT_STRETCH) { | |
115 int free_space = CrossAxisSize(bounds) - CrossAxisSizeForView(child); | |
116 int position = CrossAxisPosition(bounds); | |
117 if (cross_axis_alignment_ == CROSS_AXIS_ALIGNMENT_CENTER) { | |
118 position += free_space / 2; | |
119 } else if (cross_axis_alignment_ == CROSS_AXIS_ALIGNMENT_END) { | |
120 position += free_space; | |
121 } | |
122 SetCrossAxisPosition(position, &bounds); | |
123 SetCrossAxisSize(CrossAxisSizeForView(child), &bounds); | |
124 } | |
125 | |
126 // Calculate flex padding. | |
127 int current_padding = 0; | |
128 if (GetFlexForView(child) > 0) { | |
129 current_flex += GetFlexForView(child); | |
130 int quot = (main_free_space * current_flex) / flex_sum; | |
131 int rem = (main_free_space * current_flex) % flex_sum; | |
132 current_padding = quot - total_padding; | |
133 // Use the current remainder to round to the nearest pixel. | |
134 if (std::abs(rem) * 2 >= flex_sum) | |
135 current_padding += main_free_space > 0 ? 1 : -1; | |
136 total_padding += current_padding; | |
137 } | |
138 | |
139 // Set main axis size. | |
140 int child_main_axis_size = MainAxisSizeForView(child, child_area.width()); | |
141 SetMainAxisSize(child_main_axis_size + current_padding, &bounds); | |
142 if (MainAxisSize(bounds) > 0 || GetFlexForView(child) > 0) | |
143 main_position += MainAxisSize(bounds) + between_child_spacing_; | |
144 | |
145 // Clamp child view bounds to |child_area|. | |
146 bounds.Intersect(child_area); | |
147 child->SetBoundsRect(bounds); | |
148 } | |
149 | 108 |
150 // Flex views should have grown/shrunk to consume all free space. | 109 // Flex views should have grown/shrunk to consume all free space. |
151 if (flex_sum) | 110 if (flex_sum) |
152 DCHECK_EQ(total_padding, main_free_space); | 111 DCHECK_EQ(total_padding, main_free_space); |
153 } | 112 } |
154 | 113 |
155 gfx::Size BoxLayout::GetPreferredSize(const View* host) const { | 114 gfx::Size BoxLayout::GetPreferredSize(const View* host) const { |
156 DCHECK_EQ(host_, host); | 115 DCHECK_EQ(host_, host); |
157 // Calculate the child views' preferred width. | 116 // Calculate the child views' preferred width. |
158 int width = 0; | 117 int width = 0; |
(...skipping 10 matching lines...) Expand all Loading... |
169 | 128 |
170 return GetPreferredSizeForChildWidth(host, width); | 129 return GetPreferredSizeForChildWidth(host, width); |
171 } | 130 } |
172 | 131 |
173 int BoxLayout::GetPreferredHeightForWidth(const View* host, int width) const { | 132 int BoxLayout::GetPreferredHeightForWidth(const View* host, int width) const { |
174 DCHECK_EQ(host_, host); | 133 DCHECK_EQ(host_, host); |
175 int child_width = width - NonChildSize(host).width(); | 134 int child_width = width - NonChildSize(host).width(); |
176 return GetPreferredSizeForChildWidth(host, child_width).height(); | 135 return GetPreferredSizeForChildWidth(host, child_width).height(); |
177 } | 136 } |
178 | 137 |
| 138 void BoxLayout::LayoutChild(View* child, |
| 139 const gfx::Rect& child_area, |
| 140 int main_free_space, |
| 141 int flex_sum, |
| 142 int* main_position, |
| 143 int* current_flex, |
| 144 int* total_padding) { |
| 145 // Calculate cross axis size. |
| 146 gfx::Rect bounds(child_area); |
| 147 SetMainAxisPosition(*main_position, &bounds); |
| 148 if (cross_axis_alignment_ != CROSS_AXIS_ALIGNMENT_STRETCH) { |
| 149 int free_space = CrossAxisSize(bounds) - CrossAxisSizeForView(child); |
| 150 int position = CrossAxisPosition(bounds); |
| 151 if (cross_axis_alignment_ == CROSS_AXIS_ALIGNMENT_CENTER) { |
| 152 position += free_space / 2; |
| 153 } else if (cross_axis_alignment_ == CROSS_AXIS_ALIGNMENT_END) { |
| 154 position += free_space; |
| 155 } |
| 156 SetCrossAxisPosition(position, &bounds); |
| 157 SetCrossAxisSize(CrossAxisSizeForView(child), &bounds); |
| 158 } |
| 159 |
| 160 // Calculate flex padding. |
| 161 int current_padding = 0; |
| 162 if (GetFlexForView(child) > 0) { |
| 163 *current_flex += GetFlexForView(child); |
| 164 int quot = (main_free_space * *current_flex) / flex_sum; |
| 165 int rem = (main_free_space * *current_flex) % flex_sum; |
| 166 current_padding = quot - *total_padding; |
| 167 // Use the current remainder to round to the nearest pixel. |
| 168 if (std::abs(rem) * 2 >= flex_sum) |
| 169 current_padding += main_free_space > 0 ? 1 : -1; |
| 170 *total_padding += current_padding; |
| 171 } |
| 172 |
| 173 // Set main axis size. |
| 174 int child_main_axis_size = MainAxisSizeForView(child, child_area.width()); |
| 175 SetMainAxisSize(child_main_axis_size + current_padding, &bounds); |
| 176 if (MainAxisSize(bounds) > 0 || GetFlexForView(child) > 0) |
| 177 *main_position += MainAxisSize(bounds) + between_child_spacing_; |
| 178 |
| 179 // Clamp child view bounds to |child_area|. |
| 180 bounds.Intersect(child_area); |
| 181 child->SetBoundsRect(bounds); |
| 182 } |
| 183 |
| 184 void BoxLayout::LayoutChildren(View* host, |
| 185 const gfx::Rect& child_area, |
| 186 int main_free_space, |
| 187 int flex_sum, |
| 188 int main_position, |
| 189 int current_flex, |
| 190 int* total_padding) { |
| 191 for (int i = 0; i < host->child_count(); ++i) { |
| 192 View* child = host->child_at(i); |
| 193 if (!child->visible()) |
| 194 continue; |
| 195 LayoutChild(child, child_area, main_free_space, flex_sum, &main_position, |
| 196 ¤t_flex, total_padding); |
| 197 } |
| 198 } |
| 199 |
179 void BoxLayout::Installed(View* host) { | 200 void BoxLayout::Installed(View* host) { |
180 DCHECK(!host_); | 201 DCHECK(!host_); |
181 host_ = host; | 202 host_ = host; |
182 } | 203 } |
183 | 204 |
184 void BoxLayout::ViewRemoved(View* host, View* view) { | 205 void BoxLayout::ViewRemoved(View* host, View* view) { |
185 ClearFlexForView(view); | 206 ClearFlexForView(view); |
186 } | 207 } |
187 | 208 |
188 int BoxLayout::GetFlexForView(const View* view) const { | 209 int BoxLayout::GetFlexForView(const View* view) const { |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 child_area_bounds.height() + non_child_size.height()); | 323 child_area_bounds.height() + non_child_size.height()); |
303 } | 324 } |
304 | 325 |
305 gfx::Size BoxLayout::NonChildSize(const View* host) const { | 326 gfx::Size BoxLayout::NonChildSize(const View* host) const { |
306 gfx::Insets insets(host->GetInsets()); | 327 gfx::Insets insets(host->GetInsets()); |
307 return gfx::Size(insets.width() + inside_border_insets_.width(), | 328 return gfx::Size(insets.width() + inside_border_insets_.width(), |
308 insets.height() + inside_border_insets_.height()); | 329 insets.height() + inside_border_insets_.height()); |
309 } | 330 } |
310 | 331 |
311 } // namespace views | 332 } // namespace views |
OLD | NEW |