FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
hbox.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. 全著作権所有。
2// このソースコードの使用は、LICENSEファイルにあるMITライセンスに準拠します。
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, hbox
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
15namespace ftxui {
16
17namespace {
18class HBox : public Node {
19 public:
20 explicit HBox(Elements children) : Node(std::move(children)) {}
21
22 private:
23 void ComputeRequirement() override {
24 requirement_ = Requirement{};
25
26 for (auto& child : children_) {
27 child->ComputeRequirement();
28
29 // Propagate the focused requirement.
30 if (requirement_.focused.Prefer(child->requirement().focused)) {
31 requirement_.focused = child->requirement().focused;
32 requirement_.focused.box.Shift(requirement_.min_x, 0);
33 }
34
35 // Extend the min_x and min_y to contain all the children
36 requirement_.min_x += child->requirement().min_x;
37 requirement_.min_y =
38 std::max(requirement_.min_y, child->requirement().min_y);
39 }
40 }
41
42 void SetBox(Box box) override {
43 Node::SetBox(box);
44
45 std::vector<box_helper::Element> elements(children_.size());
46 for (size_t i = 0; i < children_.size(); ++i) {
47 auto& element = elements[i];
48 const auto& requirement = children_[i]->requirement();
49 element.min_size = requirement.min_x;
50 element.flex_grow = requirement.flex_grow_x;
51 element.flex_shrink = requirement.flex_shrink_x;
52 }
53 const int target_size = box.x_max - box.x_min + 1;
54 box_helper::Compute(&elements, target_size);
55
56 int x = box.x_min;
57 for (size_t i = 0; i < children_.size(); ++i) {
58 box.x_min = x;
59 box.x_max = x + elements[i].size - 1;
60 children_[i]->SetBox(box);
61 x = box.x_max + 1;
62 }
63 }
64
65 void Select(Selection& selection) override {
66 // If this Node box_ doesn't intersect with the selection, then no
67 // selection.
68 if (Box::Intersection(selection.GetBox(), box_).IsEmpty()) {
69 return;
70 }
71
72 Selection selection_saturated = selection.SaturateHorizontal(box_);
73 for (auto& child : children_) {
74 child->Select(selection_saturated);
75 }
76 }
77};
78
79} // namespace
80
81/// @brief 要素を水平方向に1つずつ表示するコンテナ。
82/// @param children コンテナ内の要素
83/// @return コンテナ。
84///
85/// #### 例
86///
87/// ```cpp
88/// hbox({
89/// text("Left"),
90/// text("Right"),
91/// });
92/// ```
94 return std::make_shared<HBox>(std::move(children));
95}
96
97} // namespace ftxui
virtual void SetBox(Box box)
描画のために要素に位置と次元を割り当てます。
Definition node.cpp:41
static auto Intersection(Box a, Box b) -> Box
Definition box.cpp:10
void Compute(std::vector< Element > *elements, int target_size)
FTXUI ftxui:: 名前空間
Definition animation.hpp:9
std::shared_ptr< Node > Element
Definition elements.hpp:21
Element hbox(Elements)
要素を水平方向に1つずつ表示するコンテナ。
Definition hbox.cpp:93
std::vector< Element > Elements
Definition elements.hpp:22