FTXUI  0.11.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
hflow.cpp
Go to the documentation of this file.
1#include <algorithm> // for max
2#include <memory> // for __shared_ptr_access, make_shared, shared_ptr
3#include <utility> // for move
4#include <vector> // for vector
5
6#include "ftxui/dom/elements.hpp" // for Element, Elements, hflow
7#include "ftxui/dom/node.hpp" // for Node
8#include "ftxui/dom/requirement.hpp" // for Requirement
9#include "ftxui/screen/box.hpp" // for Box
10
11namespace ftxui {
12
13class HFlow : public Node {
14 public:
15 HFlow(Elements children) : Node(std::move(children)) {}
16
17 void ComputeRequirement() override {
24 for (auto& child : children_)
25 child->ComputeRequirement();
26 }
27
28 void SetBox(Box box) override {
29 Node::SetBox(box);
30
31 // The position of the first component.
32 int x = box.x_min;
33 int y = box.y_min;
34 int y_next = y; // The position of next row of elements.
35
36 for (auto& child : children_) {
37 Requirement requirement = child->requirement();
38
39 // Does it fit the end of the row?
40 if (x + requirement.min_x > box.x_max) {
41 // No? Use the next row.
42 x = box.x_min;
43 y = y_next;
44 }
45
46 // Does the current row big enough to contain the element?
47 if (y + requirement.min_y > box.y_max + 1)
48 break; // No? Ignore the element.
49
50 Box children_box;
51 children_box.x_min = x;
52 children_box.x_max = x + requirement.min_x - 1;
53 children_box.y_min = y;
54 children_box.y_max = y + requirement.min_y - 1;
55 child->SetBox(children_box);
56
57 x = x + requirement.min_x;
58 y_next = std::max(y_next, y + requirement.min_y);
59 }
60 }
61};
62
63/// @brief A container displaying elements horizontally one by one.
64/// @param children The elements in the container
65/// @return The container.
66///
67/// #### Example
68///
69/// ```cpp
70/// hbox({
71/// text("Left"),
72/// text("Right"),
73/// });
74/// ```
76 return std::make_shared<HFlow>(std::move(children));
77}
78
79} // namespace ftxui
80
81// Copyright 2020 Arthur Sonzogni. All rights reserved.
82// Use of this source code is governed by the MIT license that can be found in
83// 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
std::shared_ptr< Node > Element
Definition elements.hpp:15
Element hflow(Elements)
A container displaying elements horizontally one by one.
Definition hflow.cpp:75
std::vector< Element > Elements
Definition elements.hpp:16