FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
vbox.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. Todos los derechos reservados.
2// El uso de este código fuente se rige por la licencia MIT que se puede encontrar en
3// el archivo 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, 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 // Propagate the focused requirement.
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 // Extend the min_x and min_y to contain all the children
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 // Si la 'box_' de este nodo no interseca con la selección, entonces no hay
69 // selección. 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 Un contenedor que muestra elementos verticalmente uno por uno.
83/// @param children Los elementos en el contenedor
84/// @return El contenedor.
85/// @ingroup dom
86///
87/// #### Ejemplo
88///
89/// ```cpp
90/// vbox({
91/// text("Up"),
92/// text("Down"),
93/// });
94/// ```
95Element vbox(Elements children) {
96 return std::make_shared<VBox>(std::move(children));
97}
98
99} // namespace ftxui
virtual void SetBox(Box box)
Asigna una posición y una dimensión a un elemento para dibujarlo.
Definition node.cpp:41
Element vbox(Elements children)
Un contenedor que muestra elementos verticalmente uno por uno.
Definition vbox.cpp:95
void Compute(std::vector< Element > *elements, int target_size)
El espacio de nombres ftxui:: de FTXUI.
Definition animation.hpp:10
std::vector< Element > Elements
Definition elements.hpp:23