FTXUI  6.0.2
C++ functional terminal UI.
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
gridbox.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 max, min
5#include <cstddef> // for size_t
6#include <memory> // for __shared_ptr_access, shared_ptr, make_shared, allocator_traits<>::value_type
7#include <utility> // for move
8#include <vector> // for vector, __alloc_traits<>::value_type
9
10#include "ftxui/dom/box_helper.hpp" // for Element, Compute
11#include "ftxui/dom/elements.hpp" // for Elements, filler, Element, gridbox
12#include "ftxui/dom/node.hpp" // for Node
13#include "ftxui/dom/requirement.hpp" // for Requirement
14#include "ftxui/screen/box.hpp" // for Box
15
16namespace ftxui {
17class Screen;
18
19namespace {
20
21// Accumulate the values of a list U[n] into v[n]. So that:
22// V[0] = 0;
23// V[n+1] = v[n] + U[n]
24// return the sum of U[n].
25int Integrate(std::vector<int>& elements) {
26 int accu = 0;
27 for (auto& i : elements) {
28 const int old_accu = accu;
29 accu += i;
30 i = old_accu;
31 }
32 return accu;
33}
34
35class GridBox : public Node {
36 public:
37 explicit GridBox(std::vector<Elements> lines) : lines_(std::move(lines)) {
38 y_size = static_cast<int>(lines_.size());
39 for (const auto& line : lines_) {
40 x_size = std::max(x_size, int(line.size()));
41 }
42
43 // Fill in empty cells, in case the user did not used the API correctly:
44 for (auto& line : lines_) {
45 while (line.size() < size_t(x_size)) {
46 line.push_back(filler());
47 }
48 }
49 }
50
51 void ComputeRequirement() override {
52 requirement_ = Requirement{};
53 for (auto& line : lines_) {
54 for (auto& cell : line) {
55 cell->ComputeRequirement();
56 }
57 }
58
59 // Compute the size of each columns/row.
60 std::vector<int> size_x(x_size, 0);
61 std::vector<int> size_y(y_size, 0);
62 for (int x = 0; x < x_size; ++x) {
63 for (int y = 0; y < y_size; ++y) {
64 size_x[x] = std::max(size_x[x], lines_[y][x]->requirement().min_x);
65 size_y[y] = std::max(size_y[y], lines_[y][x]->requirement().min_y);
66 }
67 }
68
69 requirement_.min_x = Integrate(size_x);
70 requirement_.min_y = Integrate(size_y);
71
72 // Forward the focused/focused child state:
73 for (int x = 0; x < x_size; ++x) {
74 for (int y = 0; y < y_size; ++y) {
75 if (requirement_.focused.enabled ||
76 !lines_[y][x]->requirement().focused.enabled) {
77 continue;
78 }
79 requirement_.focused = lines_[y][x]->requirement().focused;
80 requirement_.focused.box.Shift(size_x[x], size_y[y]);
81 }
82 }
83 }
84
85 void SetBox(Box box) override {
86 Node::SetBox(box);
87
88 box_helper::Element init;
89 init.min_size = 0;
90 init.flex_grow = 1024; // NOLINT
91 init.flex_shrink = 1024; // NOLINT
92 std::vector<box_helper::Element> elements_x(x_size, init);
93 std::vector<box_helper::Element> elements_y(y_size, init);
94
95 for (int y = 0; y < y_size; ++y) {
96 for (int x = 0; x < x_size; ++x) {
97 const auto& cell = lines_[y][x];
98 const auto& requirement = cell->requirement();
99 auto& e_x = elements_x[x];
100 auto& e_y = elements_y[y];
101 e_x.min_size = std::max(e_x.min_size, requirement.min_x);
102 e_y.min_size = std::max(e_y.min_size, requirement.min_y);
103 e_x.flex_grow = std::min(e_x.flex_grow, requirement.flex_grow_x);
104 e_y.flex_grow = std::min(e_y.flex_grow, requirement.flex_grow_y);
105 e_x.flex_shrink = std::min(e_x.flex_shrink, requirement.flex_shrink_x);
106 e_y.flex_shrink = std::min(e_y.flex_shrink, requirement.flex_shrink_y);
107 }
108 }
109
110 const int target_size_x = box.x_max - box.x_min + 1;
111 const int target_size_y = box.y_max - box.y_min + 1;
112 box_helper::Compute(&elements_x, target_size_x);
113 box_helper::Compute(&elements_y, target_size_y);
114
115 Box box_y = box;
116 int y = box_y.y_min;
117 for (int iy = 0; iy < y_size; ++iy) {
118 box_y.y_min = y;
119 y += elements_y[iy].size;
120 box_y.y_max = y - 1;
121
122 Box box_x = box_y;
123 int x = box_x.x_min;
124 for (int ix = 0; ix < x_size; ++ix) {
125 box_x.x_min = x;
126 x += elements_x[ix].size;
127 box_x.x_max = x - 1;
128 lines_[iy][ix]->SetBox(box_x);
129 }
130 }
131 }
132
133 void Render(Screen& screen) override {
134 for (auto& line : lines_) {
135 for (auto& cell : line) {
136 cell->Render(screen);
137 }
138 }
139 }
140
141 int x_size = 0;
142 int y_size = 0;
143 std::vector<Elements> lines_;
144};
145} // namespace
146 //
147/// @brief A container displaying a grid of elements.
148/// @param lines A list of lines, each line being a list of elements.
149/// @return The container.
150///
151/// #### Example
152///
153/// ```cpp
154/// auto cell = [](const char* t) { return text(t) | border; };
155/// auto document = gridbox({
156/// {cell("north-west") , cell("north") , cell("north-east")} ,
157/// {cell("west") , cell("center") , cell("east")} ,
158/// {cell("south-west") , cell("south") , cell("south-east")} ,
159/// });
160/// ```
161/// Output:
162/// ```
163/// ╭──────────╮╭──────╮╭──────────╮
164/// │north-west││north ││north-east│
165/// ╰──────────╯╰──────╯╰──────────╯
166/// ╭──────────╮╭──────╮╭──────────╮
167/// │west ││center││east │
168/// ╰──────────╯╰──────╯╰──────────╯
169/// ╭──────────╮╭──────╮╭──────────╮
170/// │south-west││south ││south-east│
171/// ╰──────────╯╰──────╯╰──────────╯
172/// ```
173Element gridbox(std::vector<Elements> lines) {
174 return std::make_shared<GridBox>(std::move(lines));
175}
176
177} // namespace ftxui
std::shared_ptr< Node > Element
Definition elements.hpp:22
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
Element gridbox(std::vector< Elements > lines)
A container displaying a grid of elements.
Definition gridbox.cpp:173
Element filler()
An element that will take expand proportionally to the space left in a container.
Definition flex.cpp:98
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition node.cpp:88