FTXUI  5.0.0
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/screen/box.hpp" // for Box
15
16namespace ftxui {
17
18namespace {
19class VBox : public Node {
20 public:
21 explicit VBox(Elements children) : Node(std::move(children)) {}
22
23 void ComputeRequirement() override {
24 requirement_.min_x = 0;
25 requirement_.min_y = 0;
26 requirement_.flex_grow_x = 0;
27 requirement_.flex_grow_y = 0;
28 requirement_.flex_shrink_x = 0;
29 requirement_.flex_shrink_y = 0;
30 requirement_.selection = Requirement::NORMAL;
31 for (auto& child : children_) {
32 child->ComputeRequirement();
33 if (requirement_.selection < child->requirement().selection) {
34 requirement_.selection = child->requirement().selection;
35 requirement_.selected_box = child->requirement().selected_box;
36 requirement_.selected_box.y_min += requirement_.min_y;
37 requirement_.selected_box.y_max += requirement_.min_y;
38 }
39 requirement_.min_y += child->requirement().min_y;
40 requirement_.min_x =
41 std::max(requirement_.min_x, child->requirement().min_x);
42 }
43 }
44
45 void SetBox(Box box) override {
47
48 std::vector<box_helper::Element> elements(children_.size());
49 for (size_t i = 0; i < children_.size(); ++i) {
50 auto& element = elements[i];
51 const auto& requirement = children_[i]->requirement();
52 element.min_size = requirement.min_y;
53 element.flex_grow = requirement.flex_grow_y;
54 element.flex_shrink = requirement.flex_shrink_y;
55 }
56 const int target_size = box.y_max - box.y_min + 1;
58
59 int y = box.y_min;
60 for (size_t i = 0; i < children_.size(); ++i) {
61 box.y_min = y;
62 box.y_max = y + elements[i].size - 1;
63 children_[i]->SetBox(box);
64 y = box.y_max + 1;
65 }
66 }
67
68 void Select(Selection& selection) override {
69 // If this Node box_ doesn't intersect with the selection, then no
70 // selection.
71 if (Box::Intersection(selection.GetBox(), box_).IsEmpty()) {
72 return;
73 }
74
75 Selection selection_saturated = selection.SaturateVertical(box_);
76
77 for (auto& child : children_) {
79 }
80 }
81};
82} // namespace
83
84/// @brief A container displaying elements vertically one by one.
85/// @param children The elements in the container
86/// @return The container.
87/// @ingroup dom
88///
89/// #### Example
90///
91/// ```cpp
92/// vbox({
93/// text("Up"),
94/// text("Down"),
95/// });
96/// ```
98 return std::make_shared<VBox>(std::move(children));
99}
100
101} // namespace ftxui
virtual void SetBox(Box box)
Assign a position and a dimension to an element for drawing.
Definition node.cpp:27
void Compute(std::vector< Element > *elements, int target_size)
std::shared_ptr< Node > Element
Definition elements.hpp:22
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
std::vector< Element > Elements
Definition elements.hpp:23
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition vbox.cpp:97
static auto Intersection(Box a, Box b) -> Box
Definition box.cpp:12