FTXUI  0.11.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
gridbox.cpp
Go to the documentation of this file.
1#include <stddef.h> // for size_t
2#include <algorithm> // for max, min
3#include <memory> // for __shared_ptr_access, shared_ptr, make_shared, allocator_traits<>::value_type
4#include <utility> // for move
5#include <vector> // for vector, __alloc_traits<>::value_type
6
7#include "ftxui/dom/box_helper.hpp" // for Element, Compute
8#include "ftxui/dom/elements.hpp" // for Elements, filler, Element, gridbox
9#include "ftxui/dom/node.hpp" // for Node
10#include "ftxui/dom/requirement.hpp" // for Requirement
11#include "ftxui/screen/box.hpp" // for Box
12
13namespace ftxui {
14class Screen;
15
16class GridBox : public Node {
17 public:
18 GridBox(std::vector<Elements> lines) : Node(), lines_(std::move(lines)) {
19 y_size = lines_.size();
20 for (const auto& line : lines_)
21 x_size = std::max(x_size, (int)line.size());
22 for (auto& line : lines_) {
23 while (line.size() < (size_t)y_size) {
24 line.push_back(filler());
25 }
26 }
27 }
28
29 void ComputeRequirement() override {
36
37 for (auto& line : lines_) {
38 for (auto& cell : line) {
39 cell->ComputeRequirement();
40
41 // Determine focus based on the focused child.
42 if (requirement_.selection >= cell->requirement().selection)
43 continue;
44 requirement_.selection = cell->requirement().selection;
45 requirement_.selected_box = cell->requirement().selected_box;
48 }
49 }
50
51 // Work on the x-axis.
52 for (int x = 0; x < x_size; ++x) {
53 int min_x = 0;
54 for (int y = 0; y < y_size; ++y)
55 min_x = std::max(min_x, lines_[y][x]->requirement().min_x);
56 requirement_.min_x += min_x;
57 }
58
59 // Work on the y-axis.
60 for (int y = 0; y < y_size; ++y) {
61 int min_y = 0;
62 for (int x = 0; x < x_size; ++x)
63 min_y = std::max(min_y, lines_[y][x]->requirement().min_y);
64 requirement_.min_y += min_y;
65 }
66 }
67
68 void SetBox(Box box) override {
69 Node::SetBox(box);
70
71 box_helper::Element init;
72 init.min_size = 0;
73 init.flex_grow = 1024;
74 init.flex_shrink = 1024;
75 std::vector<box_helper::Element> elements_x(x_size, init);
76 std::vector<box_helper::Element> elements_y(y_size, init);
77
78 for (int y = 0; y < y_size; ++y) {
79 for (int x = 0; x < x_size; ++x) {
80 const auto& cell = lines_[y][x];
81 const auto& requirement = cell->requirement();
82 auto& e_x = elements_x[x];
83 auto& e_y = elements_y[y];
84 e_x.min_size = std::max(e_x.min_size, requirement.min_x);
85 e_y.min_size = std::max(e_y.min_size, requirement.min_y);
86 e_x.flex_grow = std::min(e_x.flex_grow, requirement.flex_grow_x);
87 e_y.flex_grow = std::min(e_y.flex_grow, requirement.flex_grow_y);
88 e_x.flex_shrink = std::min(e_x.flex_shrink, requirement.flex_shrink_x);
89 e_y.flex_shrink = std::min(e_y.flex_shrink, requirement.flex_shrink_y);
90 }
91 }
92
93 int target_size_x = box.x_max - box.x_min + 1;
94 int target_size_y = box.y_max - box.y_min + 1;
95 box_helper::Compute(&elements_x, target_size_x);
96 box_helper::Compute(&elements_y, target_size_y);
97
98 Box box_y = box;
99 int y = box_y.y_min;
100 for (int iy = 0; iy < y_size; ++iy) {
101 box_y.y_min = y;
102 y += elements_y[iy].size;
103 box_y.y_max = y - 1;
104
105 Box box_x = box_y;
106 int x = box_x.x_min;
107 for (int ix = 0; ix < x_size; ++ix) {
108 box_x.x_min = x;
109 x += elements_x[ix].size;
110 box_x.x_max = x - 1;
111 lines_[iy][ix]->SetBox(box_x);
112 }
113 }
114 }
115
116 void Render(Screen& screen) override {
117 for (auto& line : lines_) {
118 for (auto& cell : line)
119 cell->Render(screen);
120 }
121 }
122
123 int x_size = 0;
124 int y_size = 0;
125 std::vector<Elements> lines_;
126};
127
128/// @brief A container displaying a grid of elements.
129/// @param lines A list of lines, each line being a list of elements.
130/// @return The container.
131///
132/// #### Example
133///
134/// ```cpp
135/// auto cell = [](const char* t) { return text(t) | border; };
136/// auto document = gridbox({
137/// {cell("north-west") , cell("north") , cell("north-east")} ,
138/// {cell("west") , cell("center") , cell("east")} ,
139/// {cell("south-west") , cell("south") , cell("south-east")} ,
140/// });
141/// ```
142/// Output:
143/// ```
144///╭──────────╮╭──────╮╭──────────╮
145///│north-west││north ││north-east│
146///╰──────────╯╰──────╯╰──────────╯
147///╭──────────╮╭──────╮╭──────────╮
148///│west ││center││east │
149///╰──────────╯╰──────╯╰──────────╯
150///╭──────────╮╭──────╮╭──────────╮
151///│south-west││south ││south-east│
152///╰──────────╯╰──────╯╰──────────╯
153/// ```
154Element gridbox(std::vector<Elements> lines) {
155 return std::make_shared<GridBox>(std::move(lines));
156}
157
158} // namespace ftxui
159
160// Copyright 2020 Arthur Sonzogni. All rights reserved.
161// Use of this source code is governed by the MIT license that can be found in
162// the LICENSE file.
virtual void SetBox(Box box)
Assign a position and a dimension to an element for drawing.
Definition node.cpp:21
Requirement requirement_
Definition node.hpp:40
Requirement requirement()
Definition node.hpp:29
void Compute(std::vector< Element > *elements, int target_size)
std::shared_ptr< Node > Element
Definition elements.hpp:15
Element gridbox(std::vector< Elements > lines)
A container displaying a grid of elements.
Definition gridbox.cpp:154
Element filler()
An element that will take expand proportionnally to the space left in a container.
Definition flex.cpp:94
int x_max
Definition box.hpp:8
int x_min
Definition box.hpp:7