45FlexboxConfig Normalize(FlexboxConfig config) {
46 Normalize(config.direction);
47 Normalize(config.wrap);
48 Normalize(config.justify_content);
49 Normalize(config.align_content);
53class Flexbox :
public Node {
55 Flexbox(
Elements children, FlexboxConfig config)
56 : Node(std::move(children)),
58 config_normalized_(Normalize(config)) {
59 requirement_.flex_grow_x = 1;
60 requirement_.flex_grow_y = 0;
62 if (IsColumnOriented()) {
63 std::swap(requirement_.flex_grow_x, requirement_.flex_grow_y);
67 bool IsColumnOriented()
const {
72 void Layout(flexbox_helper::Global& global,
73 bool compute_requirement =
false) {
74 global.blocks.reserve(children_.size());
75 for (
auto& child : children_) {
76 flexbox_helper::Block block;
77 block.min_size_x = child->requirement().min_x;
78 block.min_size_y = child->requirement().min_y;
79 if (!compute_requirement) {
80 block.flex_grow_x = child->requirement().flex_grow_x;
81 block.flex_grow_y = child->requirement().flex_grow_y;
82 block.flex_shrink_x = child->requirement().flex_shrink_x;
83 block.flex_shrink_y = child->requirement().flex_shrink_y;
85 global.blocks.push_back(block);
91 void ComputeRequirement()
override {
92 for (
auto& child : children_) {
93 child->ComputeRequirement();
95 flexbox_helper::Global global;
96 global.config = config_normalized_;
97 if (IsColumnOriented()) {
98 global.size_x = 100000;
99 global.size_y = asked_;
101 global.size_x = asked_;
102 global.size_y = 100000;
104 Layout(global,
true);
107 requirement_.selection = Requirement::Selection::NORMAL;
108 requirement_.selected_box = Box();
109 requirement_.min_x = 0;
110 requirement_.min_y = 0;
112 if (global.blocks.empty()) {
118 box.x_min = global.blocks[0].x;
119 box.y_min = global.blocks[0].y;
120 box.x_max = global.blocks[0].x + global.blocks[0].dim_x;
121 box.y_max = global.blocks[0].y + global.blocks[0].dim_y;
122 for (
auto& b : global.blocks) {
123 box.x_min = std::min(box.x_min, b.x);
124 box.y_min = std::min(box.y_min, b.y);
125 box.x_max = std::max(box.x_max, b.x + b.dim_x);
126 box.y_max = std::max(box.y_max, b.y + b.dim_y);
128 requirement_.min_x = box.x_max - box.x_min;
129 requirement_.min_y = box.y_max - box.y_min;
132 for (
size_t i = 0; i < children_.size(); ++i) {
133 if (requirement_.selection >= children_[i]->requirement().selection) {
136 requirement_.selection = children_[i]->requirement().selection;
137 Box selected_box = children_[i]->requirement().selected_box;
140 auto& b = global.blocks[i];
141 selected_box.x_min += b.x;
142 selected_box.y_min += b.y;
143 selected_box.x_max += b.x;
144 selected_box.y_max += b.y;
149 void SetBox(Box box)
override {
152 const int asked_previous = asked_;
153 asked_ = std::min(asked_, IsColumnOriented() ? box.y_max - box.y_min + 1
154 : box.x_max - box.x_min + 1);
155 need_iteration_ = (asked_ != asked_previous);
157 flexbox_helper::Global global;
158 global.config = config_;
159 global.size_x = box.x_max - box.x_min + 1;
160 global.size_y = box.y_max - box.y_min + 1;
163 for (
size_t i = 0; i < children_.size(); ++i) {
164 auto& child = children_[i];
165 auto& b = global.blocks[i];
168 children_box.x_min = box.x_min + b.x;
169 children_box.y_min = box.y_min + b.y;
170 children_box.x_max = box.x_min + b.x + b.dim_x - 1;
171 children_box.y_max = box.y_min + b.y + b.dim_y - 1;
174 child->SetBox(intersection);
176 need_iteration_ |= (intersection != children_box);
180 void Check(Status* status)
override {
181 for (
auto& child : children_) {
182 child->Check(status);
185 if (status->iteration == 0) {
187 need_iteration_ =
true;
190 status->need_iteration |= need_iteration_;
194 bool need_iteration_ =
true;
195 const FlexboxConfig config_;
196 const FlexboxConfig config_normalized_;
222 return std::make_shared<Flexbox>(std::move(children), config);
240 return flexbox(std::move(children), FlexboxConfig());
260 return flexbox(std::move(children),
virtual void SetBox(Box box)
Assign a position and a dimension to an element for drawing.
void Compute(Global &global)
Element flexbox(Elements, FlexboxConfig config=FlexboxConfig())
std::shared_ptr< Node > Element
std::vector< Element > Elements
static auto Intersection(Box a, Box b) -> Box
@ FlexStart
items are placed at the start of the cross axis.
@ Column
Flex items are laid out in a column.
@ Row
Flex items are laid out in a row.
@ RowInversed
Flex items are laid out in a row, but in reverse order.
@ Wrap
Flex items will wrap onto multiple lines.
@ FlexStart
Items are aligned to the start of flexbox's direction.