35class GridBox :
public Node {
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()));
44 for (
auto& line : lines_) {
45 while (line.size() <
size_t(x_size)) {
51 void ComputeRequirement()
override {
52 requirement_ = Requirement{};
53 for (
auto& line : lines_) {
54 for (
auto& cell : line) {
55 cell->ComputeRequirement();
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);
69 requirement_.min_x = Integrate(size_x);
70 requirement_.min_y = Integrate(size_y);
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) {
79 requirement_.focused = lines_[y][x]->requirement().focused;
80 requirement_.focused.box.Shift(size_x[x], size_y[y]);
85 void SetBox(Box box)
override {
88 box_helper::Element init;
90 init.flex_grow = 1024;
91 init.flex_shrink = 1024;
92 std::vector<box_helper::Element> elements_x(x_size, init);
93 std::vector<box_helper::Element> elements_y(y_size, init);
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);
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);
117 for (
int iy = 0; iy < y_size; ++iy) {
119 y += elements_y[iy].size;
124 for (
int ix = 0; ix < x_size; ++ix) {
126 x += elements_x[ix].size;
128 lines_[iy][ix]->SetBox(box_x);
133 void Render(Screen& screen)
override {
134 for (
auto& line : lines_) {
135 for (
auto& cell : line) {
136 cell->Render(screen);
143 std::vector<Elements> lines_;
174 return std::make_shared<GridBox>(std::move(lines));
std::shared_ptr< Node > Element
std::shared_ptr< T > Make(Args &&... args)
Element gridbox(std::vector< Elements > lines)
A container displaying a grid of elements.
Element filler()
An element that will take expand proportionally to the space left in a container.
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.