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