FTXUI/src/ftxui/dom/hflow.cpp

60 lines
1.5 KiB
C++
Raw Normal View History

2019-01-23 06:42:57 +08:00
#include "ftxui/dom/node.hpp"
#include "ftxui/dom/elements.hpp"
namespace ftxui {
class HFlow : public Node {
public:
HFlow(Elements children) : Node(std::move(children)) {}
~HFlow() {}
void ComputeRequirement() override {
requirement_.min.x = 0;
requirement_.min.y = 0;
requirement_.flex.x = 1;
2019-01-27 04:52:55 +08:00
requirement_.flex.y = 1;
2019-01-23 06:42:57 +08:00
for(auto& child : children)
child->ComputeRequirement();
}
void SetBox(Box box) override {
Node::SetBox(box);
// The position of the first component.
int x = box.x_min;
int y = box.y_min;
int y_next = y; // The position of next row of elements.
for (auto& child : children) {
Requirement requirement = child->requirement();
// Does it fit the end of the row?
if (x + requirement.min.x > box.x_max) {
// No? Use the next row.
x = box.x_min;
y = y_next;
}
// Does the current row big enough to contain the element?
2019-01-23 07:26:36 +08:00
if (y + requirement.min.y > box.y_max + 1)
2019-01-23 06:42:57 +08:00
break; // No? Ignore the element.
Box children_box;
children_box.x_min = x;
2019-01-23 07:26:36 +08:00
children_box.x_max = x + requirement.min.x - 1;
2019-01-23 06:42:57 +08:00
children_box.y_min = y;
2019-01-23 07:26:36 +08:00
children_box.y_max = y + requirement.min.y - 1;
2019-01-23 06:42:57 +08:00
child->SetBox(children_box);
2019-01-23 07:26:36 +08:00
x = x + requirement.min.x;
y_next = std::max(y_next, y + requirement.min.y);
2019-01-23 06:42:57 +08:00
}
}
};
std::unique_ptr<Node> hflow(Elements children) {
return std::make_unique<HFlow>(std::move(children));
}
2020-02-12 04:44:55 +08:00
} // namespace ftxui