FTXUI  4.1.1
C++ functional terminal UI.
Loading...
Searching...
No Matches
flexbox.cpp
Go to the documentation of this file.
1#include <algorithm> // for min, max
2#include <cstddef> // for size_t
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
64 bool IsColumnOriented() const {
65 return config_.direction == FlexboxConfig::Direction::Column ||
67 }
68
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;
80 }
81 global.blocks.push_back(block);
82 }
83
85 }
86
87 void ComputeRequirement() override {
88 for (auto& child : children_) {
89 child->ComputeRequirement();
90 }
91 flexbox_helper::Global global;
92 global.config = config_normalized_;
93 if (IsColumnOriented()) {
94 global.size_x = 100000; // NOLINT
95 global.size_y = asked_;
96 } else {
97 global.size_x = asked_;
98 global.size_y = 100000; // NOLINT
99 }
100 Layout(global, true);
101
102 // Reset:
103 requirement_.selection = Requirement::Selection::NORMAL;
104 requirement_.selected_box = Box();
105 requirement_.min_x = 0;
106 requirement_.min_y = 0;
107
108 if (global.blocks.empty()) {
109 return;
110 }
111
112 // Compute the union of all the blocks:
113 Box box;
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);
123 }
124 requirement_.min_x = box.x_max - box.x_min;
125 requirement_.min_y = box.y_max - box.y_min;
126
127 // Find the selection:
128 for (size_t i = 0; i < children_.size(); ++i) {
129 if (requirement_.selection >= children_[i]->requirement().selection) {
130 continue;
131 }
132 requirement_.selection = children_[i]->requirement().selection;
133 Box selected_box = children_[i]->requirement().selected_box;
134
135 // Shift |selected_box| according to its position inside this component:
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;
141 requirement_.selected_box = Box::Intersection(selected_box, box);
142 }
143 }
144
145 void SetBox(Box box) override {
146 Node::SetBox(box);
147
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);
152
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;
157 Layout(global);
158
159 for (size_t i = 0; i < children_.size(); ++i) {
160 auto& child = children_[i];
161 auto& b = global.blocks[i];
162
163 Box children_box;
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;
168
169 const Box intersection = Box::Intersection(children_box, box);
170 child->SetBox(intersection);
171
172 need_iteration_ |= (intersection != children_box);
173 }
174 }
175
176 void Check(Status* status) override {
177 for (auto& child : children_) {
178 child->Check(status);
179 }
180
181 if (status->iteration == 0) {
182 asked_ = 6000; // NOLINT
183 need_iteration_ = true;
184 }
185
186 status->need_iteration |= need_iteration_;
187 }
188
189 int asked_ = 6000; // NOLINT
190 bool need_iteration_ = true;
191 const FlexboxConfig config_;
192 const FlexboxConfig config_normalized_;
193};
194
195} // namespace
196
197/// @brief A container displaying elements on row/columns and capable of
198/// wrapping on the next column/row when full.
199/// @param children The elements in the container
200/// @param config The option
201/// @return The container.
202///
203/// #### Example
204///
205/// ```cpp
206/// flexbox({
207/// text("element 1"),
208/// text("element 2"),
209/// text("element 3"),
210/// }, FlexboxConfig()
211// .Set(FlexboxConfig::Direction::Column)
212// .Set(FlexboxConfig::Wrap::WrapInversed)
213// .SetGapMainAxis(1)
214// .SetGapCrossAxis(1)
215// )
216/// ```
217Element flexbox(Elements children, FlexboxConfig config) {
218 return std::make_shared<Flexbox>(std::move(children), config);
219}
220
221/// @brief A container displaying elements in rows from left to right. When
222/// filled, it starts on a new row below.
223/// @param children The elements in the container
224/// @return The container.
225///
226/// #### Example
227///
228/// ```cpp
229/// hflow({
230/// text("element 1"),
231/// text("element 2"),
232/// text("element 3"),
233/// });
234/// ```
235Element hflow(Elements children) {
236 return flexbox(std::move(children), FlexboxConfig());
237}
238
239/// @brief A container displaying elements in rows from top to bottom. When
240/// filled, it starts on a new columns on the right.
241/// filled, it starts on a new row.
242/// is full, it starts a new row.
243/// @param children The elements in the container
244/// @return The container.
245///
246/// #### Example
247///
248/// ```cpp
249/// vflow({
250/// text("element 1"),
251/// text("element 2"),
252/// text("element 3"),
253/// });
254/// ```
255Element vflow(Elements children) {
256 return flexbox(std::move(children),
257 FlexboxConfig().Set(FlexboxConfig::Direction::Column));
258}
259
260} // namespace ftxui
261
262// Copyright 2020 Arthur Sonzogni. All rights reserved.
263// Use of this source code is governed by the MIT license that can be found in
264// the LICENSE file.
virtual void SetBox(Box box)
Assign a position and a dimension to an element for drawing.
Definition node.cpp:23
void Compute(Global &global)
Element flexbox(Elements, FlexboxConfig config=FlexboxConfig())
std::shared_ptr< Node > Element
Definition elements.hpp:19
Element hflow(Elements)
std::vector< Element > Elements
Definition elements.hpp:20
Element vflow(Elements)
static auto Intersection(Box a, Box b) -> Box
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.