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