FTXUI/src/ftxui/dom/hflow.cpp

68 lines
1.7 KiB
C++
Raw Normal View History

2020-04-20 03:00:37 +08:00
// Copyright 2020 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
#include <algorithm>
2019-01-23 06:42:57 +08:00
#include "ftxui/dom/elements.hpp"
2020-03-23 05:32:44 +08:00
#include "ftxui/dom/node.hpp"
2019-01-23 06:42:57 +08:00
namespace ftxui {
class HFlow : public Node {
public:
HFlow(Elements children) : Node(std::move(children)) {}
~HFlow() {}
void ComputeRequirement() override {
requirement_.min_x = 1;
requirement_.min_y = 1;
requirement_.flex_grow_x = 1;
requirement_.flex_grow_y = 1;
requirement_.flex_shrink_x = 0;
requirement_.flex_shrink_y = 0;
2020-03-23 05:32:44 +08:00
for (auto& child : children)
2019-01-23 06:42:57 +08:00
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;
2020-03-23 05:32:44 +08:00
int y_next = y; // The position of next row of elements.
2019-01-23 06:42:57 +08:00
for (auto& child : children) {
Requirement requirement = child->requirement();
// Does it fit the end of the row?
2020-06-01 22:13:29 +08:00
if (x + requirement.min_x > box.x_max) {
2019-01-23 06:42:57 +08:00
// No? Use the next row.
x = box.x_min;
y = y_next;
}
// Does the current row big enough to contain the element?
2020-06-01 22:13:29 +08:00
if (y + requirement.min_y > box.y_max + 1)
2020-03-23 05:32:44 +08:00
break; // No? Ignore the element.
2019-01-23 06:42:57 +08:00
Box children_box;
children_box.x_min = x;
2020-06-01 22:13:29 +08:00
children_box.x_max = x + requirement.min_x - 1;
2019-01-23 06:42:57 +08:00
children_box.y_min = y;
2020-06-01 22:13:29 +08:00
children_box.y_max = y + requirement.min_y - 1;
2019-01-23 06:42:57 +08:00
child->SetBox(children_box);
2020-06-01 22:13:29 +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
}
}
};
Element hflow(Elements children) {
return std::make_shared<HFlow>(std::move(children));
2019-01-23 06:42:57 +08:00
}
2020-02-12 04:44:55 +08:00
} // namespace ftxui