FTXUI 6.1.9 C++ functional terminal UI.

Loading...
Searching...
No Matches
flexbox.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 <algorithm> // for min, max
5#include <cstddef> // for size_t
6#include <memory> // for __shared_ptr_access, shared_ptr, allocator_traits<>::value_type, make_shared
7#include <tuple> // for ignore
8#include <utility> // for move, swap
9#include <vector> // for vector
10
11#include "ftxui/dom/elements.hpp" // for Element, Elements, flexbox, hflow, vflow
12#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
13#include "ftxui/dom/flexbox_helper.hpp" // for Block, Global, Compute
14#include "ftxui/dom/node.hpp" // for Node, Elements, Node::Status
15#include "ftxui/dom/requirement.hpp" // for Requirement
16#include "ftxui/dom/selection.hpp" // for Selection
17#include "ftxui/screen/box.hpp" // for Box
18
19 namespace ftxui {
20
21 namespace {
22 void Normalize(FlexboxConfig::Direction& direction) {
23 switch (direction) {
24 case FlexboxConfig::Direction::Row:
25 case FlexboxConfig::Direction::RowInversed: {
26 direction = FlexboxConfig::Direction::Row;
27 } break;
28 case FlexboxConfig::Direction::Column:
29 case FlexboxConfig::Direction::ColumnInversed: {
30 direction = FlexboxConfig::Direction::Column;
31 } break;
32 }
33}
34
35 void Normalize(FlexboxConfig::AlignContent& align_content) {
36 align_content = FlexboxConfig::AlignContent::FlexStart;
37}
38
39 void Normalize(FlexboxConfig::JustifyContent& justify_content) {
40 justify_content = FlexboxConfig::JustifyContent::FlexStart;
41}
42
43 void Normalize(FlexboxConfig::Wrap& wrap) {
44 wrap = FlexboxConfig::Wrap::Wrap;
45}
46
47FlexboxConfig Normalize(FlexboxConfig config) {
48 Normalize(config.direction);
49 Normalize(config.wrap);
50 Normalize(config.justify_content);
51 Normalize(config.align_content);
52 return config;
53}
54
55 class Flexbox : public Node {
56 public:
57 Flexbox(Elements children, FlexboxConfig config)
58 : Node(std::move(children)),
59 config_(config),
60 config_normalized_(Normalize(config)) {
61 requirement_.flex_grow_x = 1;
62 requirement_.flex_grow_y = 0;
63
64 if (IsColumnOriented()) {
65 std::swap(requirement_.flex_grow_x, requirement_.flex_grow_y);
66 }
67 }
68
69 bool IsColumnOriented() const {
70 return config_.direction == FlexboxConfig::Direction::Column ||
71 config_.direction == FlexboxConfig::Direction::ColumnInversed;
72 }
73
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;
86 }
87 global.blocks.push_back(block);
88 }
89
90 flexbox_helper::Compute(global);
91 }
92
93 void ComputeRequirement() override {
94 requirement_ = Requirement{};
95 for (auto& child : children_) {
96 child->ComputeRequirement();
97 }
98 global_ = flexbox_helper::Global();
99 global_.config = config_normalized_;
100 if (IsColumnOriented()) {
101 global_.size_x = 100000; // NOLINT
102 global_.size_y = asked_;
103 } else {
104 global_.size_x = asked_;
105 global_.size_y = 100000; // NOLINT
106 }
107 Layout(global_, true);
108
109 if (global_.blocks.empty()) {
110 return;
111 }
112
113 // Compute the union of all the blocks:
114 Box box;
115 box.x_min = global_.blocks[0].x;
116 box.y_min = global_.blocks[0].y;
117 box.x_max = global_.blocks[0].x + global_.blocks[0].dim_x;
118 box.y_max = global_.blocks[0].y + global_.blocks[0].dim_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);
124 }
125 requirement_.min_x = box.x_max - box.x_min;
126 requirement_.min_y = box.y_max - box.y_min;
127
128 // Find the selection:
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;
132 // Shift |focused.box| according to its position inside this component:
133 auto& b = global_.blocks[i];
134 requirement_.focused.box.Shift(b.x, b.y);
135 requirement_.focused.box =
136 Box::Intersection(requirement_.focused.box, box);
137 }
138 }
139 }
140
141 void SetBox(Box box) override {
142 Node::SetBox(box);
143
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);
147 need_iteration_ = (asked_ != asked_previous);
148
149 flexbox_helper::Global global;
150 global.config = config_;
151 global.size_x = box.x_max - box.x_min + 1;
152 global.size_y = box.y_max - box.y_min + 1;
153 Layout(global);
154
155 for (size_t i = 0; i < children_.size(); ++i) {
156 auto& child = children_[i];
157 auto& b = global.blocks[i];
158
159 Box children_box;
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;
164
165 const Box intersection = Box::Intersection(children_box, box);
166 child->SetBox(intersection);
167
168 need_iteration_ |= (intersection != children_box);
169 }
170 }
171
172 void Select(Selection& selection) override {
173 // If this Node box_ doesn't intersect with the selection, then no
174 // selection.
175 if (Box::Intersection(selection.GetBox(), box_).IsEmpty()) {
176 return;
177 }
178
179 Selection selection_lines = IsColumnOriented()
180 ? selection.SaturateVertical(box_)
181 : selection.SaturateHorizontal(box_);
182
183 size_t i = 0;
184 for (auto& line : global_.lines) {
185 Box box;
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;
190
191 // If the line box doesn't intersect with the selection, then no
192 // selection.
193 if (Box::Intersection(selection.GetBox(), box).IsEmpty()) {
194 continue;
195 }
196
197 Selection selection_line = IsColumnOriented()
198 ? selection_lines.SaturateHorizontal(box)
199 : selection_lines.SaturateVertical(box);
200
201 for (auto& block : line.blocks) {
202 std::ignore = block;
203 children_[i]->Select(selection_line);
204 i++;
205 }
206 }
207 }
208
209 void Check(Status* status) override {
210 for (auto& child : children_) {
211 child->Check(status);
212 }
213
214 if (status->iteration == 0) {
215 asked_ = 6000; // NOLINT
216 need_iteration_ = true;
217 }
218
219 status->need_iteration |= need_iteration_;
220 }
221
222 int asked_ = 6000; // NOLINT
223 bool need_iteration_ = true;
224 const FlexboxConfig config_;
225 const FlexboxConfig config_normalized_;
226 flexbox_helper::Global global_;
227};
228
229} // namespace
230
231/// @brief A container displaying elements on row/columns and capable of
232/// wrapping on the next column/row when full.
233/// @param children The elements in the container
234/// @param config The option
235/// @return The container.
236///
237/// #### Example
238///
239/// ```cpp
240/// flexbox({
241/// text("element 1"),
242/// text("element 2"),
243/// text("element 3"),
244/// }, FlexboxConfig()
245/// .Set(FlexboxConfig::Direction::Column)
246/// .Set(FlexboxConfig::Wrap::WrapInversed)
247/// .SetGapMainAxis(1)
248/// .SetGapCrossAxis(1)
249/// )
250/// ```
251 Element flexbox(Elements children, FlexboxConfig config) {
252 return std::make_shared<Flexbox>(std::move(children), config);
253}
254
255/// @brief A container displaying elements in rows from left to right. When
256/// filled, it starts on a new row below.
257/// @param children The elements in the container
258/// @return The container.
259///
260/// #### Example
261///
262/// ```cpp
263/// hflow({
264/// text("element 1"),
265/// text("element 2"),
266/// text("element 3"),
267/// });
268/// ```
269 Element hflow(Elements children) {
270 return flexbox(std::move(children), FlexboxConfig());
271}
272
273/// @brief A container displaying elements in rows from top to bottom. When
274/// filled, it starts on a new columns on the right.
275/// filled, it starts on a new row.
276/// is full, it starts a new row.
277/// @param children The elements in the container
278/// @return The container.
279///
280/// #### Example
281///
282/// ```cpp
283/// vflow({
284/// text("element 1"),
285/// text("element 2"),
286/// text("element 3"),
287/// });
288/// ```
289 Element vflow(Elements children) {
290 return flexbox(std::move(children),
291 FlexboxConfig().Set(FlexboxConfig::Direction::Column));
292}
293
294} // namespace ftxui
box.hpp
elements.hpp
asked_
int asked_
Definition flexbox.cpp:222
need_iteration_
bool need_iteration_
Definition flexbox.cpp:223
config_
const FlexboxConfig config_
Definition flexbox.cpp:224
config_normalized_
const FlexboxConfig config_normalized_
Definition flexbox.cpp:225
global_
flexbox_helper::Global global_
Definition flexbox.cpp:226
flexbox_config.hpp
flexbox_helper.hpp
ftxui::FlexboxConfig::AlignContent
AlignContent
Definition flexbox_config.hpp:94
ftxui::FlexboxConfig::AlignContent::FlexStart
@ FlexStart
items are placed at the start of the cross axis.
ftxui::FlexboxConfig::Direction
Direction
Definition flexbox_config.hpp:32
ftxui::FlexboxConfig::Direction::Column
@ Column
Flex items are laid out in a column.
ftxui::FlexboxConfig::Direction::ColumnInversed
@ ColumnInversed
ftxui::FlexboxConfig::Direction::Row
@ Row
Flex items are laid out in a row.
ftxui::FlexboxConfig::Direction::RowInversed
@ RowInversed
Flex items are laid out in a row, but in reverse order.
ftxui::Node::SetBox
virtual void SetBox(Box box)
Assign a position and a dimension to an element for drawing.
Definition node.cpp:41
ftxui::FlexboxConfig::Wrap
Wrap
Definition flexbox_config.hpp:43
ftxui::FlexboxConfig::Wrap::Wrap
@ Wrap
Flex items will wrap onto multiple lines.
ftxui::FlexboxConfig::JustifyContent
JustifyContent
Definition flexbox_config.hpp:56
ftxui::FlexboxConfig::JustifyContent::FlexStart
@ FlexStart
Items are aligned to the start of flexbox's direction.
ftxui::FlexboxConfig
FlexboxConfig is a configuration structure that defines the layout properties for a flexbox container...
Definition flexbox_config.hpp:27
ftxui::Box::Intersection
static auto Intersection(Box a, Box b) -> Box
Definition box.cpp:11
ftxui::flexbox_helper::Compute
void Compute(Global &global)
Definition flexbox_helper.cpp:369
ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
ftxui::flexbox
Element flexbox(Elements, FlexboxConfig config=FlexboxConfig())
A container displaying elements on row/columns and capable of wrapping on the next column/row when fu...
Definition flexbox.cpp:251
ftxui::Element
std::shared_ptr< Node > Element
Definition elements.hpp:22
ftxui::hflow
Element hflow(Elements)
A container displaying elements in rows from left to right. When filled, it starts on a new row below...
Definition flexbox.cpp:269
ftxui::Elements
std::vector< Element > Elements
Definition elements.hpp:23
ftxui::vflow
Element vflow(Elements)
A container displaying elements in rows from top to bottom. When filled, it starts on a new columns o...
Definition flexbox.cpp:289
node.hpp
requirement.hpp
selection.hpp