FTXUI  0.11.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
hbox.cpp
Go to the documentation of this file.
1#include <stddef.h> // for size_t
2#include <algorithm> // for max
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 HBox(Elements children) : Node(std::move(children)) {}
18
19 void ComputeRequirement() override {
26 for (auto& child : children_) {
27 child->ComputeRequirement();
28 if (requirement_.selection < child->requirement().selection) {
29 requirement_.selection = child->requirement().selection;
30 requirement_.selected_box = child->requirement().selected_box;
33 }
34 requirement_.min_x += child->requirement().min_x;
36 std::max(requirement_.min_y, child->requirement().min_y);
37 }
38 }
39
40 void SetBox(Box box) override {
41 Node::SetBox(box);
42
43 std::vector<box_helper::Element> elements(children_.size());
44 for (size_t i = 0; i < children_.size(); ++i) {
45 auto& element = elements[i];
46 const auto& requirement = children_[i]->requirement();
47 element.min_size = requirement.min_x;
48 element.flex_grow = requirement.flex_grow_x;
49 element.flex_shrink = requirement.flex_shrink_x;
50 }
51 int target_size = box.x_max - box.x_min + 1;
52 box_helper::Compute(&elements, target_size);
53
54 int x = box.x_min;
55 for (size_t i = 0; i < children_.size(); ++i) {
56 box.x_min = x;
57 box.x_max = x + elements[i].size - 1;
58 children_[i]->SetBox(box);
59 x = box.x_max + 1;
60 }
61 }
62};
63
64/// @brief A container displaying elements horizontally one by one.
65/// @param children The elements in the container
66/// @return The container.
67///
68/// #### Example
69///
70/// ```cpp
71/// hbox({
72/// text("Left"),
73/// text("Right"),
74/// });
75/// ```
77 return std::make_shared<HBox>(std::move(children));
78}
79
80} // namespace ftxui
81
82// Copyright 2020 Arthur Sonzogni. All rights reserved.
83// Use of this source code is governed by the MIT license that can be found in
84// the LICENSE file.
Elements children_
Definition node.hpp:39
virtual void SetBox(Box box)
Assign a position and a dimension to an element for drawing.
Definition node.cpp:21
Requirement requirement_
Definition node.hpp:40
Requirement requirement()
Definition node.hpp:29
void Compute(std::vector< Element > *elements, int target_size)
std::shared_ptr< Node > Element
Definition elements.hpp:15
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definition hbox.cpp:76
std::vector< Element > Elements
Definition elements.hpp:16
int x_max
Definition box.hpp:8
int x_min
Definition box.hpp:7