FTXUI 7.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
flex.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
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
16class Flex : public Node {
17 public:
18 Flex(int grow_x, int grow_y, int shrink_x, int shrink_y)
19 : grow_x_(grow_x),
20 grow_y_(grow_y),
21 shrink_x_(shrink_x),
22 shrink_y_(shrink_y) {}
23 Flex(Element child, int grow_x, int grow_y, int shrink_x, int shrink_y)
24 : Node(unpack(std::move(child))),
25 grow_x_(grow_x),
26 grow_y_(grow_y),
27 shrink_x_(shrink_x),
28 shrink_y_(shrink_y) {}
29
30 void ComputeRequirement() override {
31 requirement_.min_x = 0;
32 requirement_.min_y = 0;
33 if (!children_.empty()) {
34 children_[0]->ComputeRequirement();
35 requirement_ = children_[0]->requirement();
36 }
37 if (grow_x_ != -1) {
38 requirement_.flex_grow_x = grow_x_;
39 }
40 if (grow_y_ != -1) {
41 requirement_.flex_grow_y = grow_y_;
42 }
43 if (shrink_x_ != -1) {
44 requirement_.flex_shrink_x = shrink_x_;
45 }
46 if (shrink_y_ != -1) {
47 requirement_.flex_shrink_y = shrink_y_;
48 }
49 }
50
51 void SetBox(Box box) override {
52 Node::SetBox(box);
53 if (children_.empty()) {
54 return;
55 }
56 children_[0]->SetBox(box);
57 }
58
63};
64
65} // namespace
66
67/// @brief An element that will expand proportionally to the space left in
68/// a container.
69/// @ingroup dom
71 return std::make_shared<Flex>(1, 1, 1, 1);
72}
73
74/// @brief Make a child element to expand proportionally to the space left in a
75/// container.
76/// @ingroup dom
77///
78/// #### Examples:
79///
80/// ~~~cpp
81/// hbox({
82/// text("left") | border ,
83/// text("middle") | border | flex,
84/// text("right") | border,
85/// });
86/// ~~~
87///
88/// #### Output:
89///
90/// ~~~bash
91/// ┌────┐┌─────────────────────────────────────────────────────────┐┌─────┐
92/// │left││middle ││right│
93/// └────┘└─────────────────────────────────────────────────────────┘└─────┘
94/// ~~~
96 return std::make_shared<Flex>(std::move(child), 1, 1, 1, 1);
97}
98
99/// @brief Expand/Minimize if possible/needed on the X axis.
100/// @ingroup dom
102 return std::make_shared<Flex>(std::move(child), 1, -1, 1, -1);
103}
104
105/// @brief Expand/Minimize if possible/needed on the Y axis.
106/// @ingroup dom
108 return std::make_shared<Flex>(std::move(child), -1, 1, -1, 1);
109}
110
111/// @brief Expand if possible.
112/// @ingroup dom
114 return std::make_shared<Flex>(std::move(child), 1, 1, -1, -1);
115}
116
117/// @brief Expand if possible on the X axis.
118/// @ingroup dom
120 return std::make_shared<Flex>(std::move(child), 1, -1, -1, -1);
121}
122
123/// @brief Expand if possible on the Y axis.
124/// @ingroup dom
126 return std::make_shared<Flex>(std::move(child), -1, 1, -1, -1);
127}
128
129/// @brief Minimize if needed.
130/// @ingroup dom
132 return std::make_shared<Flex>(std::move(child), -1, -1, 1, 1);
133}
134
135/// @brief Minimize if needed on the X axis.
136/// @ingroup dom
138 return std::make_shared<Flex>(std::move(child), -1, -1, 1, -1);
139}
140
141/// @brief Minimize if needed on the Y axis.
142/// @ingroup dom
144 return std::make_shared<Flex>(std::move(child), -1, -1, -1, 1);
145}
146
147/// @brief Make the element not flexible.
148/// @ingroup dom
150 return std::make_shared<Flex>(std::move(child), 0, 0, 0, 0);
151}
152
154 return [grow, shrink](Element child) {
155 return std::make_shared<Flex>(std::move(child), grow, grow, shrink, shrink);
156 };
157}
158
160 return [grow](Element child) {
161 return std::make_shared<Flex>(std::move(child), grow, grow, -1, -1);
162 };
163}
164
166 return [shrink](Element child) {
167 return std::make_shared<Flex>(std::move(child), -1, -1, shrink, shrink);
168 };
169}
170
172 return [grow, shrink](Element child) {
173 return std::make_shared<Flex>(std::move(child), grow, -1, shrink, -1);
174 };
175}
176
178 return [grow](Element child) {
179 return std::make_shared<Flex>(std::move(child), grow, -1, -1, -1);
180 };
181}
182
184 return [shrink](Element child) {
185 return std::make_shared<Flex>(std::move(child), -1, -1, shrink, -1);
186 };
187}
188
190 return [grow, shrink](Element child) {
191 return std::make_shared<Flex>(std::move(child), -1, grow, -1, shrink);
192 };
193}
194
196 return [grow](Element child) {
197 return std::make_shared<Flex>(std::move(child), -1, grow, -1, -1);
198 };
199}
200
202 return [shrink](Element child) {
203 return std::make_shared<Flex>(std::move(child), -1, -1, -1, shrink);
204 };
205}
206
207} // namespace ftxui
int grow_y_
Definition flex.cpp:60
int shrink_y_
Definition flex.cpp:62
int shrink_x_
Definition flex.cpp:61
int grow_x_
Definition flex.cpp:59
Element xflex(Element)
Expand/Minimize if possible/needed on the X axis.
Definition flex.cpp:101
Element xflex_grow(Element)
Expand if possible on the X axis.
Definition flex.cpp:119
Element flex(Element child)
Make a child element to expand proportionally to the space left in a container.
Definition flex.cpp:95
Element yflex(Element)
Expand/Minimize if possible/needed on the Y axis.
Definition flex.cpp:107
Element flex_shrink(Element child)
Minimize if needed.
Definition flex.cpp:131
Element yflex_grow(Element)
Expand if possible on the Y axis.
Definition flex.cpp:125
Element flex_grow(Element child)
Expand if possible.
Definition flex.cpp:113
Element notflex(Element child)
Make the element not flexible.
Definition flex.cpp:149
Element xflex_shrink(Element)
Minimize if needed on the X axis.
Definition flex.cpp:137
Element filler()
An element that will expand proportionally to the space left in a container.
Definition flex.cpp:70
Element yflex_shrink(Element)
Minimize if needed on the Y axis.
Definition flex.cpp:143
The FTXUI ftxui:: namespace.
Definition animation.hpp:11
Decorator yflex_shrink_factor(int shrink)
Definition flex.cpp:201
std::shared_ptr< Node > Element
Definition elements.hpp:25
Decorator xflex_grow_factor(int grow)
Definition flex.cpp:177
Decorator yflex_grow_factor(int grow)
Definition flex.cpp:195
Decorator yflex_factor(int grow, int shrink)
Definition flex.cpp:189
std::function< Element(Element)> Decorator
Definition elements.hpp:27
Decorator flex_shrink_factor(int shrink)
Definition flex.cpp:165
Decorator flex_grow_factor(int grow)
Definition flex.cpp:159
int shrink
Definition elements.hpp:158
Decorator xflex_shrink_factor(int shrink)
Definition flex.cpp:183
Decorator xflex_factor(int grow, int shrink)
Definition flex.cpp:171
Decorator flex_factor(int grow, int shrink)
Definition flex.cpp:153