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