FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
hbox.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, hbox
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
16namespace ftxui {
17
18namespace {
19class HBox : public Node {
20 public:
21 explicit HBox(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(requirement_.min_x, 0);
34 }
35
36 // Extend the min_x and min_y to contain all the children
37 requirement_.min_x += child->requirement().min_x;
38 requirement_.min_y =
39 std::max(requirement_.min_y, child->requirement().min_y);
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_x;
51 element.flex_grow = requirement.flex_grow_x;
52 element.flex_shrink = requirement.flex_shrink_x;
53 }
54 const int target_size = box.x_max - box.x_min + 1;
55 box_helper::Compute(&elements, target_size);
56
57 int x = box.x_min;
58 for (size_t i = 0; i < children_.size(); ++i) {
59 box.x_min = x;
60 box.x_max = x + elements[i].size - 1;
61 children_[i]->SetBox(box);
62 x = box.x_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.SaturateHorizontal(box_);
74 for (auto& child : children_) {
75 child->Select(selection_saturated);
76 }
77 }
78};
79
80} // namespace
81
82/// @brief 一个按水平顺序逐一显示元素的容器。
83/// @param children 容器中的元素
84/// @return 容器。
85///
86/// #### 示例
87///
88/// ```cpp
89/// hbox({
90/// text("Left"),
91/// text("Right"),
92/// });
93/// ```
95 return std::make_shared<HBox>(std::move(children));
96}
97
98} // namespace ftxui
virtual void SetBox(Box box)
为绘图元素分配位置和尺寸。
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
Element hbox(Elements)
一个按水平顺序逐一显示元素的容器。
std::vector< Element > Elements