FTXUI/src/ftxui/dom/vbox.cpp

85 lines
2.6 KiB
C++
Raw Normal View History

2021-05-02 02:40:35 +08:00
#include <algorithm> // for max
#include <memory> // for __shared_ptr_access, shared_ptr, make_shared
#include <utility> // for move
#include <vector> // for vector
2020-03-23 05:32:44 +08:00
#include "ftxui/dom/box_helper.hpp" // for Box
2021-05-02 02:40:35 +08:00
#include "ftxui/dom/elements.hpp" // for Element, Elements, vbox
#include "ftxui/dom/node.hpp" // for Node
#include "ftxui/dom/requirement.hpp" // for Requirement
#include "ftxui/screen/box.hpp" // for Box
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
void ComputeRequirement() override {
2020-06-01 22:13:29 +08:00
requirement_.min_x = 0;
requirement_.min_y = 0;
requirement_.flex_grow_x = 0;
requirement_.flex_grow_y = 0;
requirement_.flex_shrink_x = 0;
requirement_.flex_shrink_y = 0;
for (auto& child : children_) {
2018-09-18 14:48:40 +08:00
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) override {
2018-09-18 14:48:40 +08:00
Node::SetBox(box);
std::vector<box_helper::Element> elements(children_.size());
for (size_t i = 0; i < children_.size(); ++i) {
auto& element = elements[i];
const auto& requirement = children_[i]->requirement();
element.min_size = requirement.min_y;
element.flex_grow = requirement.flex_grow_y;
element.flex_shrink = requirement.flex_shrink_y;
}
int target_size = box.y_max - box.y_min + 1;
box_helper::Compute(&elements, target_size);
int y = box.y_min;
for (size_t i = 0; i < children_.size(); ++i) {
box.y_min = y;
box.y_max = y + elements[i].size - 1;
children_[i]->SetBox(box);
y = box.y_max + 1;
2018-09-18 14:48:40 +08:00
}
}
};
2020-05-25 07:34:13 +08:00
/// @brief A container displaying elements vertically one by one.
/// @param children The elements in the container
/// @return The container.
2020-08-16 08:24:50 +08:00
/// @ingroup dom
2020-05-25 07:34:13 +08:00
///
/// #### Example
///
/// ```cpp
/// vbox({
/// text("Up"),
/// text("Down"),
2020-05-25 07:34:13 +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
// 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.