FTXUI  4.1.1
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 {
27 for (auto& child : children_) {
28 child->ComputeRequirement();
29 if (requirement_.selection < child->requirement().selection) {
30 requirement_.selection = child->requirement().selection;
31 requirement_.selected_box = child->requirement().selected_box;
34 }
35 requirement_.min_y += child->requirement().min_y;
37 std::max(requirement_.min_x, child->requirement().min_x);
38 }
39 }
40
41 void SetBox(Box box) override {
42 Node::SetBox(box);
43
44 std::vector<box_helper::Element> elements(children_.size());
45 for (size_t i = 0; i < children_.size(); ++i) {
46 auto& element = elements[i];
47 const auto& requirement = children_[i]->requirement();
48 element.min_size = requirement.min_y;
49 element.flex_grow = requirement.flex_grow_y;
50 element.flex_shrink = requirement.flex_shrink_y;
51 }
52 const int target_size = box.y_max - box.y_min + 1;
53 box_helper::Compute(&elements, target_size);
54
55 int y = box.y_min;
56 for (size_t i = 0; i < children_.size(); ++i) {
57 box.y_min = y;
58 box.y_max = y + elements[i].size - 1;
59 children_[i]->SetBox(box);
60 y = box.y_max + 1;
61 }
62 }
63};
64
65/// @brief A container displaying elements vertically one by one.
66/// @param children The elements in the container
67/// @return The container.
68/// @ingroup dom
69///
70/// #### Example
71///
72/// ```cpp
73/// vbox({
74/// text("Up"),
75/// text("Down"),
76/// });
77/// ```
79 return std::make_shared<VBox>(std::move(children));
80}
81
82} // namespace ftxui
83
84// Copyright 2020 Arthur Sonzogni. All rights reserved.
85// Use of this source code is governed by the MIT license that can be found in
86// 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:23
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:19
std::vector< Element > Elements
Definition elements.hpp:20
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition vbox.cpp:78
int y_min
Definition box.hpp:9
int y_max
Definition box.hpp:10