FTXUI/src/ftxui/dom/vbox.cpp

75 lines
2.0 KiB
C++
Raw Normal View History

2020-03-23 05:32:44 +08:00
#include <algorithm>
#include "ftxui/dom/elements.hpp"
#include "ftxui/dom/node.hpp"
2018-09-18 14:48:40 +08:00
namespace ftxui {
2018-09-18 14:48:40 +08:00
class VBox : public Node {
public:
2019-01-13 01:24:46 +08:00
VBox(Elements children) : Node(std::move(children)) {}
2018-09-18 14:48:40 +08:00
~VBox() {}
void ComputeRequirement() {
requirement_.min.x = 0;
requirement_.min.y = 0;
requirement_.flex.x = 0;
requirement_.flex.y = 1;
for (auto& child : children) {
child->ComputeRequirement();
2019-01-20 05:06:05 +08:00
if (requirement_.selection < child->requirement().selection) {
requirement_.selection = child->requirement().selection;
requirement_.selected_box = child->requirement().selected_box;
requirement_.selected_box.y_min += requirement_.min.y;
requirement_.selected_box.y_max += requirement_.min.y;
}
2018-09-18 14:48:40 +08:00
requirement_.min.y += child->requirement().min.y;
requirement_.min.x =
std::max(requirement_.min.x, child->requirement().min.x);
}
}
void SetBox(Box box) {
Node::SetBox(box);
int flex_sum = 0;
for (auto& child : children)
flex_sum += child->requirement().flex.y;
2019-01-20 05:06:05 +08:00
int space = box.y_max - box.y_min + 1;
2018-09-18 14:48:40 +08:00
int extra_space = space - requirement_.min.y;
int remaining_flex = flex_sum;
int remaining_extra_space = extra_space;
2019-01-20 05:06:05 +08:00
int y = box.y_min;
2018-09-18 14:48:40 +08:00
for (auto& child : children) {
2019-01-20 05:06:05 +08:00
if (y > box.y_max)
2018-09-18 14:48:40 +08:00
break;
Box child_box = box;
2019-01-20 05:06:05 +08:00
child_box.y_min = y;
2018-09-18 14:48:40 +08:00
2019-01-20 05:06:05 +08:00
child_box.y_max = y + child->requirement().min.y - 1;
2018-09-18 14:48:40 +08:00
if (child->requirement().flex.y && remaining_extra_space > 0) {
int added_space = remaining_extra_space * child->requirement().flex.y /
remaining_flex;
remaining_extra_space -= added_space;
remaining_flex -= child->requirement().flex.y;
2019-01-20 05:06:05 +08:00
child_box.y_max += added_space;
2018-09-18 14:48:40 +08:00
}
2019-01-20 05:06:05 +08:00
child_box.y_max = std::min(child_box.y_max, box.y_max);
2018-09-18 14:48:40 +08:00
child->SetBox(child_box);
2019-01-20 05:06:05 +08:00
y = child_box.y_max + 1;
2018-09-18 14:48:40 +08:00
}
}
};
2019-01-13 01:24:46 +08:00
std::unique_ptr<Node> vbox(Elements children) {
2018-09-18 14:48:40 +08:00
return std::make_unique<VBox>(std::move(children));
}
2020-02-12 04:44:55 +08:00
} // namespace ftxui