FTXUI  0.8.1
C++ functional terminal UI.
flex.cpp
Go to the documentation of this file.
1 #include <memory> // for make_shared, __shared_ptr_access
2 #include <utility> // for move
3 #include <vector> // for vector, __alloc_traits<>::value_type
4 
5 #include "ftxui/dom/elements.hpp" // for Element, unpack, filler, flex, flex_grow, flex_shrink, notflex, xflex, xflex_grow, xflex_shrink, yflex, yflex_grow, yflex_shrink
6 #include "ftxui/dom/node.hpp" // for Node
7 #include "ftxui/dom/requirement.hpp" // for Requirement
8 #include "ftxui/screen/box.hpp" // for Box
9 
10 namespace ftxui {
11 
12 namespace {
13 
14 using FlexFunction = void (*)(Requirement&);
15 
16 void function_flex_grow(Requirement& r) {
17  r.flex_grow_x = 1;
18  r.flex_grow_y = 1;
19 }
20 
21 void function_xflex_grow(Requirement& r) {
22  r.flex_grow_x = 1;
23 }
24 
25 void function_yflex_grow(Requirement& r) {
26  r.flex_grow_y = 1;
27 }
28 
29 void function_flex_shrink(Requirement& r) {
30  r.flex_shrink_x = 1;
31  r.flex_shrink_y = 1;
32 }
33 
34 void function_xflex_shrink(Requirement& r) {
35  r.flex_shrink_x = 1;
36 }
37 
38 void function_yflex_shrink(Requirement& r) {
39  r.flex_shrink_y = 1;
40 }
41 
42 void function_flex(Requirement& r) {
43  r.flex_grow_x = 1;
44  r.flex_grow_y = 1;
45  r.flex_shrink_x = 1;
46  r.flex_shrink_y = 1;
47 }
48 
49 void function_xflex(Requirement& r) {
50  r.flex_grow_x = 1;
51  r.flex_shrink_x = 1;
52 }
53 
54 void function_yflex(Requirement& r) {
55  r.flex_grow_y = 1;
56  r.flex_shrink_y = 1;
57 }
58 
59 void function_not_flex(Requirement& r) {
60  r.flex_grow_x = 0;
61  r.flex_grow_y = 0;
62  r.flex_shrink_x = 0;
63  r.flex_shrink_y = 0;
64 }
65 
66 } // namespace
67 
68 class Flex : public Node {
69  public:
70  Flex(FlexFunction f) : f_(f) {}
71  Flex(FlexFunction f, Element child) : Node(unpack(std::move(child))), f_(f) {}
72  void ComputeRequirement() override {
73  requirement_.min_x = 0;
74  requirement_.min_y = 0;
75  if (!children_.empty()) {
76  children_[0]->ComputeRequirement();
77  requirement_ = children_[0]->requirement();
78  }
79  f_(requirement_);
80  }
81 
82  void SetBox(Box box) override {
83  if (children_.empty())
84  return;
85  children_[0]->SetBox(box);
86  }
87 
88  FlexFunction f_;
89 };
90 
91 /// @brief An element that will take expand proportionnally to the space left in
92 /// a container.
93 /// @ingroup dom
95  return std::make_shared<Flex>(function_flex);
96 }
97 
98 /// @brief Make a child element to expand proportionnally to the space left in a
99 /// container.
100 /// @ingroup dom
101 ///
102 /// #### Examples:
103 ///
104 /// ~~~cpp
105 /// hbox({
106 /// text("left") | border ,
107 /// text("middle") | border | flex,
108 /// text("right") | border,
109 /// });
110 /// ~~~
111 ///
112 /// #### Output:
113 ///
114 /// ~~~bash
115 /// ┌────┐┌─────────────────────────────────────────────────────────┐┌─────┐
116 /// │left││middle ││right│
117 /// └────┘└─────────────────────────────────────────────────────────┘└─────┘
118 /// ~~~
120  return std::make_shared<Flex>(function_flex, std::move(child));
121 }
122 
123 /// @brief Expand/Minimize if possible/needed on the X axis.
124 /// @ingroup dom
126  return std::make_shared<Flex>(function_xflex, std::move(child));
127 }
128 
129 /// @brief Expand/Minimize if possible/needed on the Y axis.
130 /// @ingroup dom
132  return std::make_shared<Flex>(function_yflex, std::move(child));
133 }
134 
135 /// @brief Expand if possible.
136 /// @ingroup dom
138  return std::make_shared<Flex>(function_flex_grow, std::move(child));
139 }
140 
141 /// @brief Expand if possible on the X axis.
142 /// @ingroup dom
144  return std::make_shared<Flex>(function_xflex_grow, std::move(child));
145 }
146 
147 /// @brief Expand if possible on the Y axis.
148 /// @ingroup dom
150  return std::make_shared<Flex>(function_yflex_grow, std::move(child));
151 }
152 
153 /// @brief Minimize if needed.
154 /// @ingroup dom
156  return std::make_shared<Flex>(function_flex_shrink, std::move(child));
157 }
158 
159 /// @brief Minimize if needed on the X axis.
160 /// @ingroup dom
162  return std::make_shared<Flex>(function_xflex_shrink, std::move(child));
163 }
164 
165 /// @brief Minimize if needed on the Y axis.
166 /// @ingroup dom
168  return std::make_shared<Flex>(function_yflex_shrink, std::move(child));
169 }
170 
171 /// @brief Make the element not flexible.
172 /// @ingroup dom
174  return std::make_shared<Flex>(function_not_flex, std::move(child));
175 }
176 
177 } // namespace ftxui
178 
179 // Copyright 2020 Arthur Sonzogni. All rights reserved.
180 // Use of this source code is governed by the MIT license that can be found in
181 // the LICENSE file.
ftxui::flex_grow
Element flex_grow(Element)
Expand if possible.
Definition: flex.cpp:137
ftxui::xflex
Element xflex(Element)
Expand/Minimize if possible/needed on the X axis.
Definition: flex.cpp:125
ftxui::Requirement::min_x
int min_x
Definition: requirement.hpp:10
ftxui::yflex_shrink
Element yflex_shrink(Element)
Minimize if needed on the Y axis.
Definition: flex.cpp:167
ftxui
Definition: captured_mouse.hpp:6
node.hpp
ftxui::yflex
Element yflex(Element)
Expand/Minimize if possible/needed on the Y axis.
Definition: flex.cpp:131
ftxui::Node::children_
Elements children_
Definition: node.hpp:39
box.hpp
requirement.hpp
ftxui::filler
Element filler()
An element that will take expand proportionnally to the space left in a container.
Definition: flex.cpp:94
ftxui::flex_shrink
Element flex_shrink(Element)
Minimize if needed.
Definition: flex.cpp:155
ftxui::flex
Element flex(Element)
Make a child element to expand proportionnally to the space left in a container.
Definition: flex.cpp:119
elements.hpp
ftxui::Node::Node
Node()
Definition: node.cpp:8
ftxui::xflex_shrink
Element xflex_shrink(Element)
Minimize if needed on the X axis.
Definition: flex.cpp:161
ftxui::Element
std::shared_ptr< Node > Element
Definition: elements.hpp:15
ftxui::xflex_grow
Element xflex_grow(Element)
Expand if possible on the X axis.
Definition: flex.cpp:143
ftxui::notflex
Element notflex(Element)
Make the element not flexible.
Definition: flex.cpp:173
ftxui::Requirement::min_y
int min_y
Definition: requirement.hpp:11
ftxui::Node::requirement_
Requirement requirement_
Definition: node.hpp:40
ftxui::yflex_grow
Element yflex_grow(Element)
Expand if possible on the Y axis.
Definition: flex.cpp:149