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. 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
17 namespace ftxui {
18
19 namespace {
20 class 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 // If this Node box_ doesn't intersect with the selection, then no
69 // selection.
70 if (Box::Intersection(selection.GetBox(), box_).IsEmpty()) {
71 return;
72 }
73
74 Selection selection_saturated = selection.SaturateVertical(box_);
75
76 for (auto& child : children_) {
77 child->Select(selection_saturated);
78 }
79 }
80};
81} // namespace
82
83/// @brief A container displaying elements vertically one by one.
84/// @param children The elements in the container
85/// @return The container.
86/// @ingroup dom
87///
88/// #### Example
89///
90/// ```cpp
91/// vbox({
92/// text("Up"),
93/// text("Down"),
94/// });
95/// ```
96 Element vbox(Elements children) {
97 return std::make_shared<VBox>(std::move(children));
98}
99
100} // namespace ftxui
box.hpp
box_helper.hpp
elements.hpp
ftxui::Node::SetBox
virtual void SetBox(Box box)
Assign a position and a dimension to an element for drawing.
Definition node.cpp:41
ftxui::vbox
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition vbox.cpp:96
ftxui::Box::Intersection
static auto Intersection(Box a, Box b) -> Box
Definition box.cpp:11
ftxui::box_helper::Compute
void Compute(std::vector< Element > *elements, int target_size)
Definition box_helper.cpp:65
ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
ftxui::Element
std::shared_ptr< Node > Element
Definition elements.hpp:22
ftxui::Elements
std::vector< Element > Elements
Definition elements.hpp:23
node.hpp
requirement.hpp
selection.hpp