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);
64 bool IsColumnOriented()
const {
69 void Layout(flexbox_helper::Global& global,
70 bool compute_requirement =
false) {
71 for (
auto& child : children_) {
72 flexbox_helper::Block block;
73 block.min_size_x = child->requirement().min_x;
74 block.min_size_y = child->requirement().min_y;
75 if (!compute_requirement) {
76 block.flex_grow_x = child->requirement().flex_grow_x;
77 block.flex_grow_y = child->requirement().flex_grow_y;
78 block.flex_shrink_x = child->requirement().flex_shrink_x;
79 block.flex_shrink_y = child->requirement().flex_shrink_y;
81 global.blocks.push_back(block);
87 void ComputeRequirement()
override {
88 for (
auto& child : children_) {
89 child->ComputeRequirement();
91 flexbox_helper::Global global;
92 global.config = config_normalized_;
93 if (IsColumnOriented()) {
94 global.size_x = 100000;
95 global.size_y = asked_;
97 global.size_x = asked_;
98 global.size_y = 100000;
100 Layout(global,
true);
104 requirement_.selected_box = Box();
105 requirement_.min_x = 0;
106 requirement_.min_y = 0;
108 if (global.blocks.empty()) {
114 box.x_min = global.blocks[0].x;
115 box.y_min = global.blocks[0].y;
116 box.x_max = global.blocks[0].x + global.blocks[0].dim_x;
117 box.y_max = global.blocks[0].y + global.blocks[0].dim_y;
118 for (
auto& b : global.blocks) {
119 box.x_min = std::min(box.x_min, b.x);
120 box.y_min = std::min(box.y_min, b.y);
121 box.x_max = std::max(box.x_max, b.x + b.dim_x);
122 box.y_max = std::max(box.y_max, b.y + b.dim_y);
124 requirement_.min_x = box.x_max - box.x_min;
125 requirement_.min_y = box.y_max - box.y_min;
128 for (
size_t i = 0; i < children_.size(); ++i) {
129 if (requirement_.selection >= children_[i]->requirement().selection) {
132 requirement_.selection = children_[i]->requirement().selection;
133 Box selected_box = children_[i]->requirement().selected_box;
136 auto& b = global.blocks[i];
137 selected_box.x_min += b.x;
138 selected_box.y_min += b.y;
139 selected_box.x_max += b.x;
140 selected_box.y_max += b.y;
145 void SetBox(Box box)
override {
148 const int asked_previous = asked_;
149 asked_ = std::min(asked_, IsColumnOriented() ? box.y_max - box.y_min + 1
150 : box.x_max - box.x_min + 1);
151 need_iteration_ = (asked_ != asked_previous);
153 flexbox_helper::Global global;
154 global.config = config_;
155 global.size_x = box.x_max - box.x_min + 1;
156 global.size_y = box.y_max - box.y_min + 1;
159 for (
size_t i = 0; i < children_.size(); ++i) {
160 auto& child = children_[i];
161 auto& b = global.blocks[i];
164 children_box.x_min = box.x_min + b.x;
165 children_box.y_min = box.y_min + b.y;
166 children_box.x_max = box.x_min + b.x + b.dim_x - 1;
167 children_box.y_max = box.y_min + b.y + b.dim_y - 1;
170 child->SetBox(intersection);
172 need_iteration_ |= (intersection != children_box);
176 void Check(Status* status)
override {
177 for (
auto& child : children_) {
178 child->Check(status);
181 if (status->iteration == 0) {
183 need_iteration_ =
true;
186 status->need_iteration |= need_iteration_;
190 bool need_iteration_ =
true;
191 const FlexboxConfig config_;
192 const FlexboxConfig config_normalized_;
218 return std::make_shared<Flexbox>(std::move(children), config);
236 return flexbox(std::move(children), FlexboxConfig());
256 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.