FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
flex.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. Todos los derechos reservados.
2// El uso de este código fuente se rige por la licencia MIT que se puede encontrar en
3// el archivo LICENSE.
4#include <memory> // for make_shared, __shared_ptr_access
5#include <utility> // for move
6
7#include "ftxui/dom/elements.hpp" // for Element, unpack, filler, flex, flex_grow, flex_shrink, notflex, xflex, xflex_grow, xflex_shrink, yflex, yflex_grow, yflex_shrink
8#include "ftxui/dom/node.hpp" // for Elements, Node
9#include "ftxui/dom/requirement.hpp" // for Requirement
10#include "ftxui/screen/box.hpp" // for Box
11
12namespace ftxui {
13
14namespace {
15
16using FlexFunction = void (*)(Requirement&);
17
18void function_flex_grow(Requirement& r) {
19 r.flex_grow_x = 1;
20 r.flex_grow_y = 1;
21}
22
23void function_xflex_grow(Requirement& r) {
24 r.flex_grow_x = 1;
25}
26
27void function_yflex_grow(Requirement& r) {
28 r.flex_grow_y = 1;
29}
30
31void function_flex_shrink(Requirement& r) {
32 r.flex_shrink_x = 1;
33 r.flex_shrink_y = 1;
34}
35
36void function_xflex_shrink(Requirement& r) {
37 r.flex_shrink_x = 1;
38}
39
40void function_yflex_shrink(Requirement& r) {
41 r.flex_shrink_y = 1;
42}
43
44void function_flex(Requirement& r) {
45 r.flex_grow_x = 1;
46 r.flex_grow_y = 1;
47 r.flex_shrink_x = 1;
48 r.flex_shrink_y = 1;
49}
50
51void function_xflex(Requirement& r) {
52 r.flex_grow_x = 1;
53 r.flex_shrink_x = 1;
54}
55
56void function_yflex(Requirement& r) {
57 r.flex_grow_y = 1;
58 r.flex_shrink_y = 1;
59}
60
61void function_not_flex(Requirement& r) {
62 r.flex_grow_x = 0;
63 r.flex_grow_y = 0;
64 r.flex_shrink_x = 0;
65 r.flex_shrink_y = 0;
66}
67
68class Flex : public Node {
69 public:
70 explicit Flex(FlexFunction f) : f_(f) {}
71 Flex(FlexFunction f, Element child) : Node(unpack(std::move(child))), f_(f) {}
72 void ComputeRequirement() override {
73 requirement_.min_x = 0;
74 requirement_.min_y = 0;
75 if (!children_.empty()) {
76 children_[0]->ComputeRequirement();
77 requirement_ = children_[0]->requirement();
78 }
79 f_(requirement_);
80 }
81
82 void SetBox(Box box) override {
83 Node::SetBox(box);
84 if (children_.empty()) {
85 return;
86 }
87 children_[0]->SetBox(box);
88 }
89
90 FlexFunction f_;
91};
92
93} // namespace
94
95/// @brief Un elemento que se expandirá proporcionalmente al espacio restante en
96/// un contenedor.
97/// @ingroup dom
99 return std::make_shared<Flex>(function_flex);
100}
101
102/// @brief Hace que un elemento hijo se expanda proporcionalmente al espacio restante en un
103/// contenedor.
104/// @ingroup dom
105///
106/// #### Ejemplos:
107///
108/// ~~~cpp
109/// hbox({
110/// text("left") | border ,
111/// text("middle") | border | flex,
112/// text("right") | border,
113/// });
114/// ~~~
115///
116/// #### Salida:
117///
118/// ~~~bash
119/// ┌────┐┌─────────────────────────────────────────────────────────┐┌─────┐
120/// │left││middle ││right│
121/// └────┘└─────────────────────────────────────────────────────────┘└─────┘
122/// ~~~
124 return std::make_shared<Flex>(function_flex, std::move(child));
125}
126
127/// @brief Expandir/Minimizar si es posible/necesario en el eje X.
128/// @ingroup dom
130 return std::make_shared<Flex>(function_xflex, std::move(child));
131}
132
133/// @brief Expandir/Minimizar si es posible/necesario en el eje Y.
134/// @ingroup dom
136 return std::make_shared<Flex>(function_yflex, std::move(child));
137}
138
139/// @brief Expandir si es posible.
140/// @ingroup dom
142 return std::make_shared<Flex>(function_flex_grow, std::move(child));
143}
144
145/// @brief Expandir si es posible en el eje X.
146/// @ingroup dom
148 return std::make_shared<Flex>(function_xflex_grow, std::move(child));
149}
150
151/// @brief Expandir si es posible en el eje Y.
152/// @ingroup dom
154 return std::make_shared<Flex>(function_yflex_grow, std::move(child));
155}
156
157/// @brief Minimizar si es necesario.
158/// @ingroup dom
160 return std::make_shared<Flex>(function_flex_shrink, std::move(child));
161}
162
163/// @brief Minimizar si es necesario en el eje X.
164/// @ingroup dom
166 return std::make_shared<Flex>(function_xflex_shrink, std::move(child));
167}
168
169/// @brief Minimizar si es necesario en el eje Y.
170/// @ingroup dom
172 return std::make_shared<Flex>(function_yflex_shrink, std::move(child));
173}
174
175/// @brief Hace que el elemento no sea flexible.
176/// @ingroup dom
178 return std::make_shared<Flex>(function_not_flex, std::move(child));
179}
180
181} // namespace ftxui
FlexFunction f_
Definition flex.cpp:90
virtual void SetBox(Box box)
Asigna una posición y una dimensión a un elemento para dibujarlo.
Definition node.cpp:41
Element xflex(Element)
Expandir/Minimizar si es posible/necesario en el eje X.
Definition flex.cpp:129
Element xflex_grow(Element)
Expandir si es posible en el eje X.
Definition flex.cpp:147
Element flex(Element)
Hace que un elemento hijo se expanda proporcionalmente al espacio restante en un contenedor.
Definition flex.cpp:123
Element yflex(Element)
Expandir/Minimizar si es posible/necesario en el eje Y.
Definition flex.cpp:135
Element flex_shrink(Element)
Minimizar si es necesario.
Definition flex.cpp:159
Element yflex_grow(Element)
Expandir si es posible en el eje Y.
Definition flex.cpp:153
Element flex_grow(Element)
Expandir si es posible.
Definition flex.cpp:141
Element notflex(Element)
Hace que el elemento no sea flexible.
Definition flex.cpp:177
Element xflex_shrink(Element)
Minimizar si es necesario en el eje X.
Definition flex.cpp:165
Element filler()
Un elemento que se expandirá proporcionalmente al espacio restante en un contenedor.
Definition flex.cpp:98
Element yflex_shrink(Element)
Minimizar si es necesario en el eje Y.
Definition flex.cpp:171
El espacio de nombres ftxui:: de FTXUI.
Definition animation.hpp:10
std::shared_ptr< Node > Element
Definition elements.hpp:22