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