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

@@ -19,6 +19,7 @@ add_library(dom
src/ftxui/dom/frame.cpp
src/ftxui/dom/gauge.cpp
src/ftxui/dom/hbox.cpp
src/ftxui/dom/hflow.cpp
src/ftxui/dom/inverted.cpp
src/ftxui/dom/node.cpp
src/ftxui/dom/node_decorator.cpp

View File

@@ -33,9 +33,10 @@ Element bgcolor(Color, Element);
// --- Layout ---
// Horizontal, Vertical or stacked set of elements.
Element vbox(Elements);
Element hbox(Elements);
Element vbox(Elements);
Element dbox(Elements);
Element hflow(Elements);
// -- Flexibility ---
// Define how to share the remaining space when not all of it is used inside a
@@ -73,6 +74,7 @@ Decorator operator|(Decorator, Decorator);
TAKE_ANY_ARGS(vbox)
TAKE_ANY_ARGS(hbox)
TAKE_ANY_ARGS(dbox)
TAKE_ANY_ARGS(hflow)
}; // namespace ftxui

View File

@@ -33,6 +33,7 @@ class Screen {
// Constructor using the terminal.
static Screen TerminalFullscreen();
static Screen TerminalOutput(std::unique_ptr<Node>& element);
static Screen FitDocument(std::unique_ptr<Node>& element);
// Node write into the screen using Screen::at.
wchar_t& at(size_t x, size_t y);

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;