42FlexboxConfig Normalize(FlexboxConfig config) {
43 Normalize(config.direction);
44 Normalize(config.wrap);
45 Normalize(config.justify_content);
46 Normalize(config.align_content);
50class Flexbox :
public Node {
52 Flexbox(
Elements children, FlexboxConfig config)
53 : Node(std::move(children)),
55 config_normalized_(Normalize(config)) {
56 requirement_.flex_grow_x = 1;
57 requirement_.flex_grow_y = 0;
59 if (IsColumnOriented())
60 std::swap(requirement_.flex_grow_x, requirement_.flex_grow_y);
63 bool IsColumnOriented() {
68 void Layout(flexbox_helper::Global& global,
69 bool compute_requirement =
false) {
70 for (
auto& child : children_) {
71 flexbox_helper::Block block;
72 block.min_size_x = child->requirement().min_x;
73 block.min_size_y = child->requirement().min_y;
74 if (!compute_requirement) {
75 block.flex_grow_x = child->requirement().flex_grow_x;
76 block.flex_grow_y = child->requirement().flex_grow_y;
77 block.flex_shrink_x = child->requirement().flex_shrink_x;
78 block.flex_shrink_y = child->requirement().flex_shrink_y;
80 global.blocks.push_back(block);
86 void ComputeRequirement()
override {
87 for (
auto& child : children_)
88 child->ComputeRequirement();
89 flexbox_helper::Global global;
90 global.config = config_normalized_;
91 if (IsColumnOriented()) {
92 global.size_x = 100000;
93 global.size_y = asked_;
95 global.size_x = asked_;
96 global.size_y = 100000;
100 if (global.blocks.size() == 0) {
101 requirement_.min_x = 0;
102 requirement_.min_y = 0;
107 box.x_min = global.blocks[0].x;
108 box.y_min = global.blocks[0].y;
109 box.x_max = global.blocks[0].x + global.blocks[0].dim_x;
110 box.y_max = global.blocks[0].y + global.blocks[0].dim_y;
112 for (
auto& b : global.blocks) {
113 box.x_min = std::min(box.x_min, b.x);
114 box.y_min = std::min(box.y_min, b.y);
115 box.x_max = std::max(box.x_max, b.x + b.dim_x);
116 box.y_max = std::max(box.y_max, b.y + b.dim_y);
119 requirement_.min_x = box.x_max - box.x_min;
120 requirement_.min_y = box.y_max - box.y_min;
123 void SetBox(Box box)
override {
126 asked_ = std::min(asked_, IsColumnOriented() ? box.y_max - box.y_min + 1
127 : box.x_max - box.x_min + 1);
128 flexbox_helper::Global global;
129 global.config = config_;
130 global.size_x = box.x_max - box.x_min + 1;
131 global.size_y = box.y_max - box.y_min + 1;
134 need_iteration_ =
false;
135 for (
size_t i = 0; i < children_.size(); ++i) {
136 auto& child = children_[i];
137 auto& b = global.blocks[i];
140 children_box.x_min = box.x_min + b.x;
141 children_box.y_min = box.y_min + b.y;
142 children_box.x_max = box.x_min + b.x + b.dim_x - 1;
143 children_box.y_max = box.y_min + b.y + b.dim_y - 1;
146 child->SetBox(intersection);
148 need_iteration_ |= (intersection != children_box);
152 void Check(Status* status)
override {
153 for (
auto& child : children_)
154 child->Check(status);
156 if (status->iteration == 0) {
158 need_iteration_ =
true;
161 status->need_iteration |= need_iteration_;
165 bool need_iteration_ =
true;
166 const FlexboxConfig config_;
167 const FlexboxConfig config_normalized_;
193 return std::make_shared<Flexbox>(std::move(children), std::move(config));
211 return flexbox(std::move(children), FlexboxConfig());
231 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 Box Intersection(Box a, Box b)
@ 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.