Add hflow.

This commit is contained in:
Arthur Sonzogni
2019-01-22 23:42:57 +01:00
parent 456ede70fd
commit 610b86183b
10 changed files with 161 additions and 2 deletions

View File

@@ -0,0 +1,59 @@
#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;
requirement_.flex.y = 0;
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?
if (y + requirement.min.y > box.y_max)
break; // No? Ignore the element.
Box children_box;
children_box.x_min = x;
children_box.x_max = x + requirement.min.x;
children_box.y_min = y;
children_box.y_max = y + requirement.min.y;
child->SetBox(children_box);
x = x + requirement.min.x + 1;
y_next = std::max(y_next, y + requirement.min.y + 1);
}
}
};
std::unique_ptr<Node> hflow(Elements children) {
return std::make_unique<HFlow>(std::move(children));
}
}; // namespace ftxui

View File

@@ -39,6 +39,8 @@ class Size : public Node {
void SetBox(Box box) override {
Node::SetBox(box);
if (constraint_ == LESS_THAN)
box.x_max = std::min(box.x_min + value_ + 1, box.x_max);
children[0]->SetBox(box);
}

View File

@@ -109,6 +109,17 @@ Screen Screen::TerminalOutput(std::unique_ptr<Node>& element) {
return Screen(size.dimx, element->requirement().min.y);
}
// static
Screen Screen::FitDocument(std::unique_ptr<Node>& element) {
element->ComputeRequirement();
Terminal::Dimensions size = Terminal::Size();
return
Screen(
std::min(size.dimx, element->requirement().min.x),
std::min(size.dimy, element->requirement().min.y)
);
}
std::string Screen::ResetPosition() {
std::stringstream ss;
ss << MOVE_LEFT << CLEAR_LINE;