FTXUI  2.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
flexbox.cpp
Go to the documentation of this file.
1#include <stddef.h> // for size_t
2#include <algorithm> // for min, max
3#include <memory> // for __shared_ptr_access, shared_ptr, allocator_traits<>::value_type, make_shared
4#include <utility> // for move, swap
5#include <vector> // for vector
6
7#include "ftxui/dom/elements.hpp" // for Element, Elements, flexbox, hflow, vflow
8#include "ftxui/dom/flexbox_config.hpp" // for FlexboxConfig, FlexboxConfig::Direction, FlexboxConfig::Direction::Column, FlexboxConfig::AlignContent, FlexboxConfig::Direction::ColumnInversed, FlexboxConfig::Direction::Row, FlexboxConfig::JustifyContent, FlexboxConfig::Wrap, FlexboxConfig::AlignContent::FlexStart, FlexboxConfig::Direction::RowInversed, FlexboxConfig::JustifyContent::FlexStart, FlexboxConfig::Wrap::Wrap
9#include "ftxui/dom/flexbox_helper.hpp" // for Block, Global, Compute
10#include "ftxui/dom/node.hpp" // for Node, Elements, Node::Status
11#include "ftxui/dom/requirement.hpp" // for Requirement
12#include "ftxui/screen/box.hpp" // for Box
13
14namespace ftxui {
15
16namespace {
17void Normalize(FlexboxConfig::Direction& direction) {
18 switch (direction) {
22 } break;
26 } break;
27 }
28}
29
30void Normalize(FlexboxConfig::AlignContent& align_content) {
32}
33
34void Normalize(FlexboxConfig::JustifyContent& justify_content) {
36}
37
38void Normalize(FlexboxConfig::Wrap& wrap) {
40}
41
42FlexboxConfig Normalize(FlexboxConfig config) {
43 Normalize(config.direction);
44 Normalize(config.wrap);
45 Normalize(config.justify_content);
46 Normalize(config.align_content);
47 return config;
48}
49
50class Flexbox : public Node {
51 public:
52 Flexbox(Elements children, FlexboxConfig config)
53 : Node(std::move(children)),
54 config_(config),
55 config_normalized_(Normalize(config)) {
56 requirement_.flex_grow_x = 1;
57 requirement_.flex_grow_y = 0;
58
59 if (IsColumnOriented())
60 std::swap(requirement_.flex_grow_x, requirement_.flex_grow_y);
61 }
62
63 bool IsColumnOriented() {
64 return config_.direction == FlexboxConfig::Direction::Column ||
66 }
67
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;
79 }
80 global.blocks.push_back(block);
81 }
82
84 }
85
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_;
94 } else {
95 global.size_x = asked_;
96 global.size_y = 100000;
97 }
98 Layout(global, true);
99
100 if (global.blocks.size() == 0) {
101 requirement_.min_x = 0;
102 requirement_.min_y = 0;
103 return;
104 }
105
106 Box box;
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;
111
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);
117 }
118
119 requirement_.min_x = box.x_max - box.x_min;
120 requirement_.min_y = box.y_max - box.y_min;
121 }
122
123 void SetBox(Box box) override {
124 Node::SetBox(box);
125
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;
132 Layout(global);
133
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];
138
139 Box children_box;
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;
144
145 Box intersection = Box::Intersection(children_box, box);
146 child->SetBox(intersection);
147
148 need_iteration_ |= (intersection != children_box);
149 }
150 }
151
152 void Check(Status* status) override {
153 for (auto& child : children_)
154 child->Check(status);
155
156 if (status->iteration == 0) {
157 asked_ = 6000;
158 need_iteration_ = true;
159 }
160
161 status->need_iteration |= need_iteration_;
162 }
163
164 int asked_ = 6000;
165 bool need_iteration_ = true;
166 const FlexboxConfig config_;
167 const FlexboxConfig config_normalized_;
168};
169
170} // namespace
171
172/// @brief A container displaying elements on row/columns and capable of
173/// wrapping on the next column/row when full.
174/// @param children The elements in the container
175/// @param config The option
176/// @return The container.
177///
178/// #### Example
179///
180/// ```cpp
181/// flexbox({
182/// text("element 1"),
183/// text("element 2"),
184/// text("element 3"),
185/// }, FlexboxConfig()
186// .Set(FlexboxConfig::Direction::Column)
187// .Set(FlexboxConfig::Wrap::WrapInversed)
188// .SetGapMainAxis(1)
189// .SetGapCrossAxis(1)
190// )
191/// ```
192Element flexbox(Elements children, FlexboxConfig config) {
193 return std::make_shared<Flexbox>(std::move(children), std::move(config));
194}
195
196/// @brief A container displaying elements in rows from left to right. When
197/// filled, it starts on a new row below.
198/// @param children The elements in the container
199/// @return The container.
200///
201/// #### Example
202///
203/// ```cpp
204/// hflow({
205/// text("element 1"),
206/// text("element 2"),
207/// text("element 3"),
208/// });
209/// ```
210Element hflow(Elements children) {
211 return flexbox(std::move(children), FlexboxConfig());
212}
213
214/// @brief A container displaying elements in rows from top to bottom. When
215/// filled, it starts on a new columns on the right.
216/// filled, it starts on a new row.
217/// is full, it starts a new row.
218/// @param children The elements in the container
219/// @return The container.
220///
221/// #### Example
222///
223/// ```cpp
224/// vflow({
225/// text("element 1"),
226/// text("element 2"),
227/// text("element 3"),
228/// });
229/// ```
230Element vflow(Elements children) {
231 return flexbox(std::move(children),
232 FlexboxConfig().Set(FlexboxConfig::Direction::Column));
233}
234
235} // namespace ftxui
236
237// Copyright 2020 Arthur Sonzogni. All rights reserved.
238// Use of this source code is governed by the MIT license that can be found in
239// the LICENSE file.
virtual void SetBox(Box box)
Assign a position and a dimension to an element for drawing.
Definition node.cpp:21
void Compute(Global &global)
Element flexbox(Elements, FlexboxConfig config=FlexboxConfig())
std::shared_ptr< Node > Element
Definition elements.hpp:18
Element hflow(Elements)
std::vector< Element > Elements
Definition elements.hpp:19
Element vflow(Elements)
static Box Intersection(Box a, Box b)
Definition box.cpp:9
@ 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.