FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
vbox.cpp
浏览该文件的文档.
1// 版权所有 2020 Arthur Sonzogni. 保留所有权利。
2// 此源代码的使用受 MIT 许可证的约束,该许可证可在以下文件中找到:
3// LICENSE 文件。
4#include <algorithm> // for max
5#include <cstddef> // for size_t
6#include <memory> // for __shared_ptr_access, shared_ptr, make_shared, allocator_traits<>::value_type
7#include <utility> // for move
8#include <vector> // for vector, __alloc_traits<>::value_type
9
10#include "ftxui/dom/box_helper.hpp" // for Element, Compute
11#include "ftxui/dom/elements.hpp" // for Element, Elements, vbox
12#include "ftxui/dom/node.hpp" // for Node, Elements
13#include "ftxui/dom/requirement.hpp" // for Requirement
14#include "ftxui/dom/selection.hpp" // for Selection
15#include "ftxui/screen/box.hpp" // for Box
16
17namespace ftxui {
18
19namespace {
20class VBox : public Node {
21 public:
22 explicit VBox(Elements children) : Node(std::move(children)) {}
23
24 private:
25 void ComputeRequirement() override {
26 requirement_ = Requirement{};
27
28 for (auto& child : children_) {
29 child->ComputeRequirement();
30
31 // Propagate the focused requirement.
32 if (requirement_.focused.Prefer(child->requirement().focused)) {
33 requirement_.focused = child->requirement().focused;
34 requirement_.focused.box.Shift(0, requirement_.min_y);
35 }
36
37 // Extend the min_x and min_y to contain all the children
38 requirement_.min_y += child->requirement().min_y;
39 requirement_.min_x =
40 std::max(requirement_.min_x, child->requirement().min_x);
41 }
42 }
43
44 void SetBox(Box box) override {
45 Node::SetBox(box);
46
47 std::vector<box_helper::Element> elements(children_.size());
48 for (size_t i = 0; i < children_.size(); ++i) {
49 auto& element = elements[i];
50 const auto& requirement = children_[i]->requirement();
51 element.min_size = requirement.min_y;
52 element.flex_grow = requirement.flex_grow_y;
53 element.flex_shrink = requirement.flex_shrink_y;
54 }
55 const int target_size = box.y_max - box.y_min + 1;
56 box_helper::Compute(&elements, target_size);
57
58 int y = box.y_min;
59 for (size_t i = 0; i < children_.size(); ++i) {
60 box.y_min = y;
61 box.y_max = y + elements[i].size - 1;
62 children_[i]->SetBox(box);
63 y = box.y_max + 1;
64 }
65 }
66
67 void Select(Selection& selection) override {
68 // If this Node box_ doesn't intersect with the selection, then no
69 // selection.
70 if (Box::Intersection(selection.GetBox(), box_).IsEmpty()) {
71 return;
72 }
73
74 Selection selection_saturated = selection.SaturateVertical(box_);
75
76 for (auto& child : children_) {
77 child->Select(selection_saturated);
78 }
79 }
80};
81} // namespace
82
83/// @brief 垂直一个接一个显示元素的容器。
84/// @param children 容器中的元素
85/// @return 容器。
86/// @ingroup dom
87///
88/// #### 示例
89///
90/// ```cpp
91/// vbox({
92/// text("Up"),
93/// text("Down"),
94/// });
95/// ```
97 return std::make_shared<VBox>(std::move(children));
98}
99
100} // namespace ftxui
virtual void SetBox(Box box)
为绘图元素分配位置和尺寸。
Element vbox(Elements)
垂直一个接一个显示元素的容器。
static auto Intersection(Box a, Box b) -> Box
定义 box.cpp:11
void Compute(std::vector< Element > *elements, int target_size)
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase
std::shared_ptr< Node > Element
std::vector< Element > Elements