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 } |
46 | 63 |
47 if (num_visible) { | 64 if (!num_visible) |
48 total_main_axis_size -= between_child_spacing_; | 65 return; |
49 int free_space = MainAxisSize(child_area) - total_main_axis_size; | 66 |
50 int position = MainAxisPosition(child_area); | 67 total_main_axis_size -= between_child_spacing_; |
51 int size = MainAxisSize(child_area); | 68 // Free space can be negative indicating that the views want to overflow. |
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 total_padding = 0; | |
98 int current_flex = 0; | |
76 for (int i = 0; i < host->child_count(); ++i) { | 99 for (int i = 0; i < host->child_count(); ++i) { |
77 View* child = host->child_at(i); | 100 View* child = host->child_at(i); |
78 if (child->visible()) { | 101 if (!child->visible()) |
79 gfx::Rect bounds(child_area); | 102 continue; |
80 SetMainAxisPosition(main_position, &bounds); | 103 |
81 if (cross_axis_alignment_ != CROSS_AXIS_ALIGNMENT_STRETCH) { | 104 gfx::Rect bounds(child_area); |
82 int free_space = CrossAxisSize(bounds) - CrossAxisSizeForView(child); | 105 SetMainAxisPosition(main_position, &bounds); |
83 int position = CrossAxisPosition(bounds); | 106 if (cross_axis_alignment_ != CROSS_AXIS_ALIGNMENT_STRETCH) { |
84 if (cross_axis_alignment_ == CROSS_AXIS_ALIGNMENT_CENTER) { | 107 int free_space = CrossAxisSize(bounds) - CrossAxisSizeForView(child); |
85 position += free_space / 2; | 108 int position = CrossAxisPosition(bounds); |
86 } else if (cross_axis_alignment_ == CROSS_AXIS_ALIGNMENT_END) { | 109 if (cross_axis_alignment_ == CROSS_AXIS_ALIGNMENT_CENTER) { |
87 position += free_space; | 110 position += free_space / 2; |
88 } | 111 } else if (cross_axis_alignment_ == CROSS_AXIS_ALIGNMENT_END) { |
89 SetCrossAxisPosition(position, &bounds); | 112 position += free_space; |
90 SetCrossAxisSize(CrossAxisSizeForView(child), &bounds); | |
91 } | 113 } |
92 int child_main_axis_size = MainAxisSizeForView(child, child_area.width()); | 114 SetCrossAxisPosition(position, &bounds); |
93 SetMainAxisSize(child_main_axis_size + padding, &bounds); | 115 SetCrossAxisSize(CrossAxisSizeForView(child), &bounds); |
94 if (MainAxisSize(bounds) > 0) | 116 } |
95 main_position += MainAxisSize(bounds) + between_child_spacing_; | |
96 | 117 |
97 // Clamp child view bounds to |child_area|. | 118 int current_padding = 0; |
98 bounds.Intersect(child_area); | 119 if (GetFlexForViewAt(i) > 0) { |
99 child->SetBoundsRect(bounds); | 120 current_flex += GetFlexForViewAt(i); |
121 int quot = (main_free_space * current_flex) / flex_sum; | |
122 int rem = (main_free_space * current_flex) % flex_sum; | |
123 current_padding = quot - total_padding; | |
124 // Use the current remainder to round to the nearest pixel. | |
125 if (std::abs(rem) * 2 >= flex_sum) | |
126 current_padding += main_free_space > 0 ? 1 : -1; | |
127 total_padding += current_padding; | |
100 } | 128 } |
129 | |
130 int child_main_axis_size = MainAxisSizeForView(child, child_area.width()); | |
131 SetMainAxisSize(child_main_axis_size + current_padding, &bounds); | |
132 if (MainAxisSize(bounds) > 0 || GetFlexForViewAt(i) > 0) | |
133 main_position += MainAxisSize(bounds) + between_child_spacing_; | |
134 | |
135 // Clamp child view bounds to |child_area|. | |
136 bounds.Intersect(child_area); | |
137 child->SetBoundsRect(bounds); | |
138 } | |
139 | |
140 if (flex_sum) { | |
141 DCHECK_LE(MainAxisPosition(child_area) + MainAxisSize(child_area), | |
benwells
2014/07/18 02:52:25
I think DCHECK_EQ(total_padding, main_free_space)
calamity
2014/07/18 03:20:03
Done.
| |
142 main_position - between_child_spacing_); | |
101 } | 143 } |
102 } | 144 } |
103 | 145 |
104 gfx::Size BoxLayout::GetPreferredSize(const View* host) const { | 146 gfx::Size BoxLayout::GetPreferredSize(const View* host) const { |
105 // Calculate the child views' preferred width. | 147 // Calculate the child views' preferred width. |
106 int width = 0; | 148 int width = 0; |
107 if (orientation_ == kVertical) { | 149 if (orientation_ == kVertical) { |
108 for (int i = 0; i < host->child_count(); ++i) { | 150 for (int i = 0; i < host->child_count(); ++i) { |
109 const View* child = host->child_at(i); | 151 const View* child = host->child_at(i); |
110 if (!child->visible()) | 152 if (!child->visible()) |
111 continue; | 153 continue; |
112 | 154 |
113 width = std::max(width, child->GetPreferredSize().width()); | 155 width = std::max(width, child->GetPreferredSize().width()); |
114 } | 156 } |
115 } | 157 } |
116 | 158 |
117 return GetPreferredSizeForChildWidth(host, width); | 159 return GetPreferredSizeForChildWidth(host, width); |
118 } | 160 } |
119 | 161 |
120 int BoxLayout::GetPreferredHeightForWidth(const View* host, int width) const { | 162 int BoxLayout::GetPreferredHeightForWidth(const View* host, int width) const { |
121 int child_width = width - NonChildSize(host).width(); | 163 int child_width = width - NonChildSize(host).width(); |
122 return GetPreferredSizeForChildWidth(host, child_width).height(); | 164 return GetPreferredSizeForChildWidth(host, child_width).height(); |
123 } | 165 } |
124 | 166 |
167 int BoxLayout::GetFlexForViewAt(int index) { | |
168 std::map<int, int>::const_iterator it = flex_map_.find(index); | |
169 if (it == flex_map_.end()) | |
170 return default_flex_; | |
171 | |
172 return it->second; | |
173 } | |
174 | |
125 int BoxLayout::MainAxisSize(const gfx::Rect& rect) const { | 175 int BoxLayout::MainAxisSize(const gfx::Rect& rect) const { |
126 return orientation_ == kHorizontal ? rect.width() : rect.height(); | 176 return orientation_ == kHorizontal ? rect.width() : rect.height(); |
127 } | 177 } |
128 | 178 |
129 int BoxLayout::MainAxisPosition(const gfx::Rect& rect) const { | 179 int BoxLayout::MainAxisPosition(const gfx::Rect& rect) const { |
130 return orientation_ == kHorizontal ? rect.x() : rect.y(); | 180 return orientation_ == kHorizontal ? rect.x() : rect.y(); |
131 } | 181 } |
132 | 182 |
133 void BoxLayout::SetMainAxisSize(int size, gfx::Rect* rect) const { | 183 void BoxLayout::SetMainAxisSize(int size, gfx::Rect* rect) const { |
134 if (orientation_ == kHorizontal) | 184 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()); | 279 child_area_bounds.height() + non_child_size.height()); |
230 } | 280 } |
231 | 281 |
232 gfx::Size BoxLayout::NonChildSize(const View* host) const { | 282 gfx::Size BoxLayout::NonChildSize(const View* host) const { |
233 gfx::Insets insets(host->GetInsets()); | 283 gfx::Insets insets(host->GetInsets()); |
234 return gfx::Size(insets.width() + inside_border_insets_.width(), | 284 return gfx::Size(insets.width() + inside_border_insets_.width(), |
235 insets.height() + inside_border_insets_.height()); | 285 insets.height() + inside_border_insets_.height()); |
236 } | 286 } |
237 | 287 |
238 } // namespace views | 288 } // namespace views |
OLD | NEW |