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 main_axis_alignment_(MAIN_AXIS_ALIGNMENT_START), | 22 main_axis_alignment_(MAIN_AXIS_ALIGNMENT_START), |
23 cross_axis_alignment_(CROSS_AXIS_ALIGNMENT_STRETCH) { | 23 cross_axis_alignment_(CROSS_AXIS_ALIGNMENT_STRETCH), |
24 default_flex_(0) { | |
24 } | 25 } |
25 | 26 |
26 BoxLayout::~BoxLayout() { | 27 BoxLayout::~BoxLayout() { |
27 } | 28 } |
28 | 29 |
30 void BoxLayout::SetFlexForViewAt(int index, int flex_weight) { | |
31 DCHECK(index >= 0); | |
32 DCHECK(flex_weight >= 0); | |
33 flex_map_[index] = flex_weight; | |
34 } | |
35 | |
36 void BoxLayout::ClearFlexForViewAt(int index) { | |
37 DCHECK(index >= 0); | |
38 flex_map_.erase(index); | |
39 } | |
40 | |
41 void BoxLayout::SetDefaultFlex(int default_flex) { | |
42 DCHECK(default_flex >= 0); | |
43 default_flex_ = default_flex; | |
44 } | |
45 | |
29 void BoxLayout::Layout(View* host) { | 46 void BoxLayout::Layout(View* host) { |
30 gfx::Rect child_area(host->GetLocalBounds()); | 47 gfx::Rect child_area(host->GetLocalBounds()); |
31 child_area.Inset(host->GetInsets()); | 48 child_area.Inset(host->GetInsets()); |
32 child_area.Inset(inside_border_insets_); | 49 child_area.Inset(inside_border_insets_); |
33 | 50 |
34 int padding = 0; | 51 int total_main_axis_size = 0; |
35 if (main_axis_alignment_ != MAIN_AXIS_ALIGNMENT_START) { | 52 int num_visible = 0; |
36 int total_main_axis_size = 0; | 53 int flex_sum = 0; |
37 int num_visible = 0; | 54 for (int i = 0; i < host->child_count(); ++i) { |
38 for (int i = 0; i < host->child_count(); ++i) { | 55 View* child = host->child_at(i); |
39 View* child = host->child_at(i); | 56 if (!child->visible()) |
40 if (!child->visible()) | 57 continue; |
41 continue; | 58 total_main_axis_size += |
42 total_main_axis_size += MainAxisSizeForView(child, child_area.width()) + | 59 MainAxisSizeForView(child, child_area.width()) + between_child_spacing_; |
43 between_child_spacing_; | 60 ++num_visible; |
44 ++num_visible; | 61 flex_sum += GetFlexForViewAt(i); |
45 } | 62 } |
63 total_main_axis_size -= between_child_spacing_; | |
benwells
2014/07/04 07:27:56
Nit: this line can move after the if (!num_visible
calamity
2014/07/07 05:11:17
Done.
| |
46 | 64 |
47 if (num_visible) { | 65 if (!num_visible) |
48 total_main_axis_size -= between_child_spacing_; | 66 return; |
49 int free_space = MainAxisSize(child_area) - total_main_axis_size; | 67 |
50 int position = MainAxisPosition(child_area); | 68 // Free space can be negative indicating that the views want to overflow. |
51 int size = MainAxisSize(child_area); | 69 int main_free_space = MainAxisSize(child_area) - total_main_axis_size; |
70 { | |
71 int position = MainAxisPosition(child_area); | |
72 int size = MainAxisSize(child_area); | |
73 if (!flex_sum) { | |
52 switch (main_axis_alignment_) { | 74 switch (main_axis_alignment_) { |
53 case MAIN_AXIS_ALIGNMENT_FILL: | 75 case MAIN_AXIS_ALIGNMENT_START: |
54 padding = std::max(free_space / num_visible, 0); | |
55 break; | 76 break; |
56 case MAIN_AXIS_ALIGNMENT_CENTER: | 77 case MAIN_AXIS_ALIGNMENT_CENTER: |
57 position += free_space / 2; | 78 position += main_free_space / 2; |
58 size = total_main_axis_size; | 79 size = total_main_axis_size; |
59 break; | 80 break; |
60 case MAIN_AXIS_ALIGNMENT_END: | 81 case MAIN_AXIS_ALIGNMENT_END: |
61 position += free_space; | 82 position += main_free_space; |
62 size = total_main_axis_size; | 83 size = total_main_axis_size; |
63 break; | 84 break; |
64 default: | 85 default: |
65 NOTREACHED(); | 86 NOTREACHED(); |
66 break; | 87 break; |
67 } | 88 } |
68 gfx::Rect new_child_area(child_area); | |
69 SetMainAxisPosition(position, &new_child_area); | |
70 SetMainAxisSize(size, &new_child_area); | |
71 child_area.Intersect(new_child_area); | |
72 } | 89 } |
90 gfx::Rect new_child_area(child_area); | |
91 SetMainAxisPosition(position, &new_child_area); | |
92 SetMainAxisSize(size, &new_child_area); | |
93 child_area.Intersect(new_child_area); | |
73 } | 94 } |
74 | 95 |
75 int main_position = MainAxisPosition(child_area); | 96 int main_position = MainAxisPosition(child_area); |
97 int current_remainder = 0; | |
76 for (int i = 0; i < host->child_count(); ++i) { | 98 for (int i = 0; i < host->child_count(); ++i) { |
77 View* child = host->child_at(i); | 99 View* child = host->child_at(i); |
78 if (child->visible()) { | 100 if (!child->visible()) |
79 gfx::Rect bounds(child_area); | 101 continue; |
80 SetMainAxisPosition(main_position, &bounds); | 102 |
81 if (cross_axis_alignment_ != CROSS_AXIS_ALIGNMENT_STRETCH) { | 103 gfx::Rect bounds(child_area); |
82 int free_space = CrossAxisSize(bounds) - CrossAxisSizeForView(child); | 104 SetMainAxisPosition(main_position, &bounds); |
83 int position = CrossAxisPosition(bounds); | 105 if (cross_axis_alignment_ != CROSS_AXIS_ALIGNMENT_STRETCH) { |
84 if (cross_axis_alignment_ == CROSS_AXIS_ALIGNMENT_CENTER) { | 106 int free_space = CrossAxisSize(bounds) - CrossAxisSizeForView(child); |
85 position += free_space / 2; | 107 int position = CrossAxisPosition(bounds); |
86 } else if (cross_axis_alignment_ == CROSS_AXIS_ALIGNMENT_END) { | 108 if (cross_axis_alignment_ == CROSS_AXIS_ALIGNMENT_CENTER) { |
87 position += free_space; | 109 position += free_space / 2; |
88 } | 110 } else if (cross_axis_alignment_ == CROSS_AXIS_ALIGNMENT_END) { |
89 SetCrossAxisPosition(position, &bounds); | 111 position += free_space; |
90 SetCrossAxisSize(CrossAxisSizeForView(child), &bounds); | |
91 } | 112 } |
92 int child_main_axis_size = MainAxisSizeForView(child, child_area.width()); | 113 SetCrossAxisPosition(position, &bounds); |
93 SetMainAxisSize(child_main_axis_size + padding, &bounds); | 114 SetCrossAxisSize(CrossAxisSizeForView(child), &bounds); |
94 if (MainAxisSize(bounds) > 0) | 115 } |
95 main_position += MainAxisSize(bounds) + between_child_spacing_; | |
96 | 116 |
97 // Clamp child view bounds to |child_area|. | 117 int current_padding = 0; |
98 bounds.Intersect(child_area); | 118 if (GetFlexForViewAt(i) > 0) { |
99 child->SetBoundsRect(bounds); | 119 current_padding = main_free_space * GetFlexForViewAt(i) / flex_sum; |
120 current_remainder += main_free_space * GetFlexForViewAt(i); | |
121 current_remainder %= flex_sum; | |
benwells
2014/07/04 07:27:56
As discussed today I'm worried this will leak pixe
calamity
2014/07/07 05:11:17
Indeed it does. Reworked algorithm.
| |
122 // Use the current remainder to round to the nearest pixel. | |
123 if (std::abs(current_remainder) * 2 >= flex_sum) | |
124 current_padding += main_free_space > 0 ? 1 : -1; | |
100 } | 125 } |
126 | |
127 int child_main_axis_size = MainAxisSizeForView(child, child_area.width()); | |
128 SetMainAxisSize(child_main_axis_size + current_padding, &bounds); | |
129 if (MainAxisSize(bounds) > 0 || GetFlexForViewAt(i) > 0) | |
130 main_position += MainAxisSize(bounds) + between_child_spacing_; | |
131 | |
132 // Clamp child view bounds to |child_area|. | |
133 bounds.Intersect(child_area); | |
134 child->SetBoundsRect(bounds); | |
101 } | 135 } |
136 DCHECK_EQ(0, current_remainder); | |
102 } | 137 } |
103 | 138 |
104 gfx::Size BoxLayout::GetPreferredSize(const View* host) const { | 139 gfx::Size BoxLayout::GetPreferredSize(const View* host) const { |
105 // Calculate the child views' preferred width. | 140 // Calculate the child views' preferred width. |
106 int width = 0; | 141 int width = 0; |
107 if (orientation_ == kVertical) { | 142 if (orientation_ == kVertical) { |
108 for (int i = 0; i < host->child_count(); ++i) { | 143 for (int i = 0; i < host->child_count(); ++i) { |
109 const View* child = host->child_at(i); | 144 const View* child = host->child_at(i); |
110 if (!child->visible()) | 145 if (!child->visible()) |
111 continue; | 146 continue; |
112 | 147 |
113 width = std::max(width, child->GetPreferredSize().width()); | 148 width = std::max(width, child->GetPreferredSize().width()); |
114 } | 149 } |
115 } | 150 } |
116 | 151 |
117 return GetPreferredSizeForChildWidth(host, width); | 152 return GetPreferredSizeForChildWidth(host, width); |
118 } | 153 } |
119 | 154 |
120 int BoxLayout::GetPreferredHeightForWidth(const View* host, int width) const { | 155 int BoxLayout::GetPreferredHeightForWidth(const View* host, int width) const { |
121 int child_width = width - NonChildSize(host).width(); | 156 int child_width = width - NonChildSize(host).width(); |
122 return GetPreferredSizeForChildWidth(host, child_width).height(); | 157 return GetPreferredSizeForChildWidth(host, child_width).height(); |
123 } | 158 } |
124 | 159 |
160 int BoxLayout::GetFlexForViewAt(int index) { | |
161 std::map<int, int>::const_iterator it = flex_map_.find(index); | |
162 if (it == flex_map_.end()) | |
163 return default_flex_; | |
164 | |
165 return it->second; | |
166 } | |
167 | |
125 int BoxLayout::MainAxisSize(const gfx::Rect& rect) const { | 168 int BoxLayout::MainAxisSize(const gfx::Rect& rect) const { |
126 return orientation_ == kHorizontal ? rect.width() : rect.height(); | 169 return orientation_ == kHorizontal ? rect.width() : rect.height(); |
127 } | 170 } |
128 | 171 |
129 int BoxLayout::MainAxisPosition(const gfx::Rect& rect) const { | 172 int BoxLayout::MainAxisPosition(const gfx::Rect& rect) const { |
130 return orientation_ == kHorizontal ? rect.x() : rect.y(); | 173 return orientation_ == kHorizontal ? rect.x() : rect.y(); |
131 } | 174 } |
132 | 175 |
133 void BoxLayout::SetMainAxisSize(int size, gfx::Rect* rect) const { | 176 void BoxLayout::SetMainAxisSize(int size, gfx::Rect* rect) const { |
134 if (orientation_ == kHorizontal) | 177 if (orientation_ == kHorizontal) |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
229 child_area_bounds.height() + non_child_size.height()); | 272 child_area_bounds.height() + non_child_size.height()); |
230 } | 273 } |
231 | 274 |
232 gfx::Size BoxLayout::NonChildSize(const View* host) const { | 275 gfx::Size BoxLayout::NonChildSize(const View* host) const { |
233 gfx::Insets insets(host->GetInsets()); | 276 gfx::Insets insets(host->GetInsets()); |
234 return gfx::Size(insets.width() + inside_border_insets_.width(), | 277 return gfx::Size(insets.width() + inside_border_insets_.width(), |
235 insets.height() + inside_border_insets_.height()); | 278 insets.height() + inside_border_insets_.height()); |
236 } | 279 } |
237 | 280 |
238 } // namespace views | 281 } // namespace views |
OLD | NEW |