FTXUI  4.1.1
C++ functional terminal UI.
Loading...
Searching...
No Matches
hbox.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, hbox
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 HBox : public Node {
16 public:
17 explicit HBox(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_x += child->requirement().min_x;
37 std::max(requirement_.min_y, child->requirement().min_y);
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_x;
49 element.flex_grow = requirement.flex_grow_x;
50 element.flex_shrink = requirement.flex_shrink_x;
51 }
52 const int target_size = box.x_max - box.x_min + 1;
53 box_helper::Compute(&elements, target_size);
54
55 int x = box.x_min;
56 for (size_t i = 0; i < children_.size(); ++i) {
57 box.x_min = x;
58 box.x_max = x + elements[i].size - 1;
59 children_[i]->SetBox(box);
60 x = box.x_max + 1;
61 }
62 }
63};
64
65/// @brief A container displaying elements horizontally one by one.
66/// @param children The elements in the container
67/// @return The container.
68///
69/// #### Example
70///
71/// ```cpp
72/// hbox({
73/// text("Left"),
74/// text("Right"),
75/// });
76/// ```
78 return std::make_shared<HBox>(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: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
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definition hbox.cpp:77
std::vector< Element > Elements
Definition elements.hpp:20
int x_max
Definition box.hpp:8
int x_min
Definition box.hpp:7