FTXUI/src/ftxui/dom/vbox.cpp

77 lines
2.1 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.
2020-03-23 05:32:44 +08:00
#include <algorithm>
2020-03-27 08:42:46 +08:00
#include <iostream>
2020-03-23 05:32:44 +08:00
#include "ftxui/dom/elements.hpp"
#include "ftxui/dom/node.hpp"
2018-09-18 14:48:40 +08:00
namespace ftxui {
2018-09-18 14:48:40 +08:00
class VBox : public Node {
public:
2019-01-13 01:24:46 +08:00
VBox(Elements children) : Node(std::move(children)) {}
2018-09-18 14:48:40 +08:00
~VBox() {}
void ComputeRequirement() {
2020-06-01 22:13:29 +08:00
requirement_.min_x = 0;
requirement_.min_y = 0;
requirement_.flex_x = 0;
requirement_.flex_y = 1;
2018-09-18 14:48:40 +08:00
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;
2020-06-01 22:13:29 +08:00
requirement_.selected_box.y_min += requirement_.min_y;
requirement_.selected_box.y_max += requirement_.min_y;
2019-01-20 05:06:05 +08:00
}
2020-06-01 22:13:29 +08:00
requirement_.min_y += child->requirement().min_y;
requirement_.min_x =
std::max(requirement_.min_x, child->requirement().min_x);
2018-09-18 14:48:40 +08:00
}
}
void SetBox(Box box) {
Node::SetBox(box);
int flex_sum = 0;
for (auto& child : children)
2020-06-01 22:13:29 +08:00
flex_sum += child->requirement().flex_y;
2018-09-18 14:48:40 +08:00
2019-01-20 05:06:05 +08:00
int space = box.y_max - box.y_min + 1;
2020-06-01 22:13:29 +08:00
int extra_space = space - requirement_.min_y;
2018-09-18 14:48:40 +08:00
int remaining_flex = flex_sum;
int remaining_extra_space = extra_space;
2019-01-20 05:06:05 +08:00
int y = box.y_min;
2018-09-18 14:48:40 +08:00
for (auto& child : children) {
Box child_box = box;
2019-01-20 05:06:05 +08:00
child_box.y_min = y;
2018-09-18 14:48:40 +08:00
2020-06-01 22:13:29 +08:00
child_box.y_max = y + child->requirement().min_y - 1;
2018-09-18 14:48:40 +08:00
2020-06-01 22:13:29 +08:00
if (child->requirement().flex_y) {
int added_space = remaining_extra_space * child->requirement().flex_y /
2018-09-18 14:48:40 +08:00
remaining_flex;
remaining_extra_space -= added_space;
2020-06-01 22:13:29 +08:00
remaining_flex -= child->requirement().flex_y;
2019-01-20 05:06:05 +08:00
child_box.y_max += added_space;
2018-09-18 14:48:40 +08:00
}
2019-01-20 05:06:05 +08:00
child_box.y_max = std::min(child_box.y_max, box.y_max);
2018-09-18 14:48:40 +08:00
child->SetBox(child_box);
2019-01-20 05:06:05 +08:00
y = child_box.y_max + 1;
2018-09-18 14:48:40 +08:00
}
}
};
Element vbox(Elements children) {
return std::make_shared<VBox>(std::move(children));
2018-09-18 14:48:40 +08:00
}
2020-02-12 04:44:55 +08:00
} // namespace ftxui