FTXUI 7.0.3
C++ functional terminal UI.
Loading...
Searching...
No Matches
vbox.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
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 // フォーカス要求を伝播する。
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 // 全ての子要素を内包するようにmin_xとmin_yを拡張する。
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 // このNode box_が選択範囲と交差しない場合、選択は行われない。
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
Element vbox(Elements children)
要素を縦に一つずつ表示するコンテナ。
Definition vbox.cpp:95
void Compute(std::vector< Element > *elements, int target_size)
FTXUI ftxui::名前空間
Definition animation.hpp:11
std::shared_ptr< Node > Element
Definition elements.hpp:25
std::vector< Element > Elements
Definition elements.hpp:26
int y
Definition elements.hpp:131
const Element & element
Definition node.hpp:94