47FlexboxConfig Normalize(FlexboxConfig config) {
48 Normalize(config.direction);
49 Normalize(config.wrap);
50 Normalize(config.justify_content);
51 Normalize(config.align_content);
55class Flexbox :
public Node {
57 Flexbox(
Elements children, FlexboxConfig config)
58 : Node(std::move(children)),
61 requirement_.flex_grow_x = 1;
62 requirement_.flex_grow_y = 0;
64 if (IsColumnOriented()) {
65 std::swap(requirement_.flex_grow_x, requirement_.flex_grow_y);
69 bool IsColumnOriented()
const {
74 void Layout(flexbox_helper::Global& global,
75 bool compute_requirement =
false) {
76 global.blocks.reserve(children_.size());
77 for (
auto& child : children_) {
78 flexbox_helper::Block block;
79 block.min_size_x = child->requirement().min_x;
80 block.min_size_y = child->requirement().min_y;
81 if (!compute_requirement) {
82 block.flex_grow_x = child->requirement().flex_grow_x;
83 block.flex_grow_y = child->requirement().flex_grow_y;
84 block.flex_shrink_x = child->requirement().flex_shrink_x;
85 block.flex_shrink_y = child->requirement().flex_shrink_y;
87 global.blocks.push_back(block);
93 void ComputeRequirement()
override {
94 requirement_ = Requirement{};
95 for (
auto& child : children_) {
96 child->ComputeRequirement();
98 global_ = flexbox_helper::Global();
100 if (IsColumnOriented()) {
107 Layout(global_,
true);
115 box.x_min =
global_.blocks[0].x;
116 box.y_min =
global_.blocks[0].y;
119 for (
auto& b :
global_.blocks) {
120 box.x_min = std::min(box.x_min, b.x);
121 box.y_min = std::min(box.y_min, b.y);
122 box.x_max = std::max(box.x_max, b.x + b.dim_x);
123 box.y_max = std::max(box.y_max, b.y + b.dim_y);
125 requirement_.min_x = box.x_max - box.x_min;
126 requirement_.min_y = box.y_max - box.y_min;
129 for (
size_t i = 0; i < children_.size(); ++i) {
130 if (requirement_.focused.Prefer(children_[i]->requirement().focused)) {
131 requirement_.focused = children_[i]->requirement().focused;
134 requirement_.focused.box.Shift(b.x, b.y);
135 requirement_.focused.box =
141 void SetBox(Box box)
override {
144 const int asked_previous =
asked_;
145 asked_ = std::min(asked_, IsColumnOriented() ? box.y_max - box.y_min + 1
146 : box.x_max - box.x_min + 1);
149 flexbox_helper::Global global;
151 global.size_x = box.x_max - box.x_min + 1;
152 global.size_y = box.y_max - box.y_min + 1;
155 for (
size_t i = 0; i < children_.size(); ++i) {
156 auto& child = children_[i];
157 auto& b = global.blocks[i];
160 children_box.x_min = box.x_min + b.x;
161 children_box.y_min = box.y_min + b.y;
162 children_box.x_max = box.x_min + b.x + b.dim_x - 1;
163 children_box.y_max = box.y_min + b.y + b.dim_y - 1;
166 child->SetBox(intersection);
172 void Select(Selection& selection)
override {
179 Selection selection_lines = IsColumnOriented()
180 ? selection.SaturateVertical(box_)
181 : selection.SaturateHorizontal(box_);
184 for (
auto& line :
global_.lines) {
186 box.x_min = box_.x_min + line.x;
187 box.x_max = box_.x_min + line.x + line.dim_x - 1;
188 box.y_min = box_.y_min + line.y;
189 box.y_max = box_.y_min + line.y + line.dim_y - 1;
197 Selection selection_line = IsColumnOriented()
198 ? selection_lines.SaturateHorizontal(box)
199 : selection_lines.SaturateVertical(box);
201 for (
auto& block : line.blocks) {
203 children_[i]->Select(selection_line);
209 void Check(Status* status)
override {
210 for (
auto& child : children_) {
211 child->Check(status);
214 if (status->iteration == 0) {
252 return std::make_shared<Flexbox>(std::move(children), config);
288 return flexbox(std::move(children),
const FlexboxConfig config_
const FlexboxConfig config_normalized_
flexbox_helper::Global global_
@ FlexStart
Los elementos se colocan al inicio del eje transversal.
@ Column
Los elementos flex se distribuyen en una columna.
@ Row
Los elementos flex se distribuyen en una fila.
@ RowInversed
Los elementos flex se distribuyen en una fila, pero en orden inverso.
virtual void SetBox(Box box)
Asigna una posición y una dimensión a un elemento para dibujarlo.
@ Wrap
Los elementos flex se ajustarán en varias líneas.
@ FlexStart
Los elementos se alinean al inicio de la dirección del flexbox.
FlexboxConfig es una estructura de configuración que define las propiedades de diseño para un contene...
static auto Intersection(Box a, Box b) -> Box
void Compute(Global &global)
El espacio de nombres ftxui:: de FTXUI.
Element flexbox(Elements, FlexboxConfig config=FlexboxConfig())
Un contenedor que muestra elementos en filas/columnas y es capaz de ajustarse a la siguiente columna/...
std::shared_ptr< Node > Element
Element hflow(Elements)
Un contenedor que muestra elementos en filas de izquierda a derecha. Cuando está lleno,...
std::vector< Element > Elements
Element vflow(Elements)
Un contenedor que muestra elementos en filas de arriba a abajo. Cuando está lleno,...