FTXUI  0.8.1
C++ functional terminal UI.
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 
11 namespace ftxui {
12 
13 class HFlow : public Node {
14  public:
15  HFlow(Elements children) : Node(std::move(children)) {}
16 
17  void ComputeRequirement() override {
18  requirement_.min_x = 1;
19  requirement_.min_y = 1;
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 /// ```
75 Element hflow(Elements children) {
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.
ftxui::Node::SetBox
virtual void SetBox(Box box)
Assign a position and a dimension to an element for drawing.
Definition: node.cpp:21
ftxui::Requirement::min_x
int min_x
Definition: requirement.hpp:10
ftxui
Definition: captured_mouse.hpp:6
node.hpp
ftxui::Node::requirement
Requirement requirement()
Definition: node.hpp:29
box.hpp
requirement.hpp
ftxui::Elements
std::vector< Element > Elements
Definition: elements.hpp:16
elements.hpp
ftxui::Node::Node
Node()
Definition: node.cpp:8
ftxui::Requirement::flex_grow_y
int flex_grow_y
Definition: requirement.hpp:15
ftxui::Element
std::shared_ptr< Node > Element
Definition: elements.hpp:15
ftxui::Requirement::min_y
int min_y
Definition: requirement.hpp:11
ftxui::Node::requirement_
Requirement requirement_
Definition: node.hpp:40
ftxui::Requirement::flex_shrink_y
int flex_shrink_y
Definition: requirement.hpp:17
ftxui::hflow
Element hflow(Elements)
A container displaying elements horizontally one by one.
Definition: hflow.cpp:75
ftxui::Requirement::flex_grow_x
int flex_grow_x
Definition: requirement.hpp:14
ftxui::Requirement::flex_shrink_x
int flex_shrink_x
Definition: requirement.hpp:16