FTXUI  3.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
vbox.cpp
Go to the documentation of this file.
1#include <algorithm> // for max
2#include <cstddef> // for size_t
3#include <memory> // for __shared_ptr_access, shared_ptr, make_shared, allocator_traits<>::value_type
4#include <utility> // for move
5#include <vector> // for vector, __alloc_traits<>::value_type
6
7#include "ftxui/dom/box_helper.hpp" // for Element, Compute
8#include "ftxui/dom/elements.hpp" // for Element, Elements, vbox
9#include "ftxui/dom/node.hpp" // for Node, Elements
10#include "ftxui/dom/requirement.hpp" // for Requirement
11#include "ftxui/screen/box.hpp" // for Box
12
13namespace ftxui {
14
15class VBox : public Node {
16 public:
17 explicit VBox(Elements children) : Node(std::move(children)) {}
18
19 void ComputeRequirement() override {
26 for (auto& child : children_) {
27 child->ComputeRequirement();
28 if (requirement_.selection < child->requirement().selection) {
29 requirement_.selection = child->requirement().selection;
30 requirement_.selected_box = child->requirement().selected_box;
33 }
34 requirement_.min_y += child->requirement().min_y;
36 std::max(requirement_.min_x, child->requirement().min_x);
37 }
38 }
39
40 void SetBox(Box box) override {
41 Node::SetBox(box);
42
43 std::vector<box_helper::Element> elements(children_.size());
44 for (size_t i = 0; i < children_.size(); ++i) {
45 auto& element = elements[i];
46 const auto& requirement = children_[i]->requirement();
47 element.min_size = requirement.min_y;
48 element.flex_grow = requirement.flex_grow_y;
49 element.flex_shrink = requirement.flex_shrink_y;
50 }
51 int target_size = box.y_max - box.y_min + 1;
52 box_helper::Compute(&elements, target_size);
53
54 int y = box.y_min;
55 for (size_t i = 0; i < children_.size(); ++i) {
56 box.y_min = y;
57 box.y_max = y + elements[i].size - 1;
58 children_[i]->SetBox(box);
59 y = box.y_max + 1;
60 }
61 }
62};
63
64/// @brief A container displaying elements vertically one by one.
65/// @param children The elements in the container
66/// @return The container.
67/// @ingroup dom
68///
69/// #### Example
70///
71/// ```cpp
72/// vbox({
73/// text("Up"),
74/// text("Down"),
75/// });
76/// ```
78 return std::make_shared<VBox>(std::move(children));
79}
80
81} // namespace ftxui
82
83// Copyright 2020 Arthur Sonzogni. All rights reserved.
84// Use of this source code is governed by the MIT license that can be found in
85// the LICENSE file.
Elements children_
Definition node.hpp:53
virtual void SetBox(Box box)
Assign a position and a dimension to an element for drawing.
Definition node.cpp:22
Requirement requirement_
Definition node.hpp:54
Requirement requirement()
Definition node.hpp:34
void Compute(std::vector< Element > *elements, int target_size)
std::shared_ptr< Node > Element
Definition elements.hpp:18
std::vector< Element > Elements
Definition elements.hpp:19
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition vbox.cpp:77
int y_min
Definition box.hpp:9
int y_max
Definition box.hpp:10