2018-10-10 01:06:03 +08:00
|
|
|
#include "ftxui/dom/node.hpp"
|
|
|
|
#include "ftxui/dom/elements.hpp"
|
2018-09-18 14:48:40 +08:00
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
namespace ftxui {
|
2018-09-18 14:48:40 +08:00
|
|
|
|
|
|
|
class HBox : public Node {
|
|
|
|
public:
|
2019-01-13 01:24:46 +08:00
|
|
|
HBox(Elements children) : Node(std::move(children)) {}
|
2018-09-18 14:48:40 +08:00
|
|
|
~HBox() {}
|
|
|
|
|
|
|
|
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();
|
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.x_min += requirement_.min.x;
|
|
|
|
requirement_.selected_box.x_max += requirement_.min.x;
|
|
|
|
}
|
2018-09-18 14:48:40 +08:00
|
|
|
requirement_.min.x += child->requirement().min.x;
|
|
|
|
requirement_.min.y =
|
|
|
|
std::max(requirement_.min.y, child->requirement().min.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetBox(Box box) override {
|
|
|
|
Node::SetBox(box);
|
|
|
|
|
|
|
|
int flex_sum = 0;
|
|
|
|
for (auto& child : children)
|
|
|
|
flex_sum += child->requirement().flex.x;
|
|
|
|
|
2019-01-20 05:06:05 +08:00
|
|
|
int space = box.x_max - box.x_min + 1;
|
2018-09-18 14:48:40 +08:00
|
|
|
int extra_space = space - requirement_.min.x;
|
|
|
|
|
|
|
|
int remaining_flex = flex_sum;
|
|
|
|
int remaining_extra_space = extra_space;
|
|
|
|
|
2019-01-20 05:06:05 +08:00
|
|
|
int x = box.x_min;
|
2018-09-18 14:48:40 +08:00
|
|
|
for (auto& child : children) {
|
2019-01-20 05:06:05 +08:00
|
|
|
if (x > box.x_max)
|
2018-09-18 14:48:40 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
Box child_box = box;
|
2019-01-20 05:06:05 +08:00
|
|
|
child_box.x_min = x;
|
2018-09-18 14:48:40 +08:00
|
|
|
|
2019-01-20 05:06:05 +08:00
|
|
|
child_box.x_max = x + child->requirement().min.x - 1;
|
2018-09-18 14:48:40 +08:00
|
|
|
|
|
|
|
if (child->requirement().flex.x && remaining_extra_space > 0) {
|
|
|
|
int added_space = remaining_extra_space * child->requirement().flex.x /
|
|
|
|
remaining_flex;
|
|
|
|
remaining_extra_space -= added_space;
|
|
|
|
remaining_flex -= child->requirement().flex.x;
|
2019-01-20 05:06:05 +08:00
|
|
|
child_box.x_max += added_space;
|
2018-09-18 14:48:40 +08:00
|
|
|
}
|
2019-01-20 05:06:05 +08:00
|
|
|
child_box.x_max = std::min(child_box.x_max, box.x_max);
|
2018-09-18 14:48:40 +08:00
|
|
|
|
|
|
|
child->SetBox(child_box);
|
2019-01-20 05:06:05 +08:00
|
|
|
x = child_box.x_max + 1;
|
2018-09-18 14:48:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-13 01:24:46 +08:00
|
|
|
std::unique_ptr<Node> hbox(Elements children) {
|
2018-09-18 14:48:40 +08:00
|
|
|
return std::make_unique<HBox>(std::move(children));
|
|
|
|
}
|
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
}; // namespace ftxui
|