25int Integrate(std::vector<int>& elements) {
27 for (
auto& i : elements) {
28 const int old_accu = accu;
35class GridBox :
public Node {
37 explicit GridBox(std::vector<Elements> lines) :
lines_(std::move(lines)) {
39 for (
const auto& line :
lines_) {
44 for (
auto& line :
lines_) {
45 while (line.size() <
size_t(
x_size)) {
46 line.push_back(filler());
51 for (
auto& line :
lines_) {
52 for (
auto& cell : line) {
53 children_.push_back(cell);
58 void ComputeRequirement()
override {
59 requirement_ = Requirement{};
60 for (
auto& line :
lines_) {
61 for (
auto& cell : line) {
62 cell->ComputeRequirement();
67 std::vector<int> size_x(
x_size, 0);
68 std::vector<int> size_y(
y_size, 0);
69 for (
int x = 0; x <
x_size; ++x) {
70 for (
int y = 0; y <
y_size; ++y) {
71 size_x[x] = std::max(size_x[x],
lines_[y][x]->requirement().min_x);
72 size_y[y] = std::max(size_y[y],
lines_[y][x]->requirement().min_y);
76 requirement_.min_x = Integrate(size_x);
77 requirement_.min_y = Integrate(size_y);
80 for (
int x = 0; x <
x_size; ++x) {
81 for (
int y = 0; y <
y_size; ++y) {
82 if (requirement_.focused.Prefer(
lines_[y][x]->requirement().focused)) {
83 requirement_.focused =
lines_[y][x]->requirement().focused;
84 requirement_.focused.box.Shift(size_x[x], size_y[y]);
90 void SetBox(Box box)
override {
93 box_helper::Element init;
95 init.flex_grow = 1024;
96 init.flex_shrink = 1024;
97 std::vector<box_helper::Element> elements_x(
x_size, init);
98 std::vector<box_helper::Element> elements_y(
y_size, init);
100 for (
int y = 0; y <
y_size; ++y) {
101 for (
int x = 0; x <
x_size; ++x) {
102 const auto& cell =
lines_[y][x];
103 const auto& requirement = cell->requirement();
104 auto& e_x = elements_x[x];
105 auto& e_y = elements_y[y];
106 e_x.min_size = std::max(e_x.min_size, requirement.min_x);
107 e_y.min_size = std::max(e_y.min_size, requirement.min_y);
108 e_x.flex_grow = std::min(e_x.flex_grow, requirement.flex_grow_x);
109 e_y.flex_grow = std::min(e_y.flex_grow, requirement.flex_grow_y);
110 e_x.flex_shrink = std::min(e_x.flex_shrink, requirement.flex_shrink_x);
111 e_y.flex_shrink = std::min(e_y.flex_shrink, requirement.flex_shrink_y);
115 const int target_size_x = box.x_max - box.x_min + 1;
116 const int target_size_y = box.y_max - box.y_min + 1;
117 box_helper::Compute(&elements_x, target_size_x);
118 box_helper::Compute(&elements_y, target_size_y);
122 for (
int iy = 0; iy <
y_size; ++iy) {
124 y += elements_y[iy].size;
129 for (
int ix = 0; ix <
x_size; ++ix) {
131 x += elements_x[ix].size;
133 lines_[iy][ix]->SetBox(box_x);
138 void Render(Screen& screen)
override {
139 for (
auto& line :
lines_) {
140 for (
auto& cell : line) {
141 cell->Render(screen);
179 return std::make_shared<GridBox>(std::move(lines));
The FTXUI ftxui:: namespace.
std::shared_ptr< Node > Element
Element gridbox(std::vector< Elements > lines)
A container displaying a grid of elements.
std::vector< Elements > lines_