FTXUI 6.1.9 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
12 namespace ftxui {
13
14 namespace {
15
16 using FlexFunction = void (*)(Requirement&);
17
18 void function_flex_grow(Requirement& r) {
19 r.flex_grow_x = 1;
20 r.flex_grow_y = 1;
21}
22
23 void function_xflex_grow(Requirement& r) {
24 r.flex_grow_x = 1;
25}
26
27 void function_yflex_grow(Requirement& r) {
28 r.flex_grow_y = 1;
29}
30
31 void function_flex_shrink(Requirement& r) {
32 r.flex_shrink_x = 1;
33 r.flex_shrink_y = 1;
34}
35
36 void function_xflex_shrink(Requirement& r) {
37 r.flex_shrink_x = 1;
38}
39
40 void function_yflex_shrink(Requirement& r) {
41 r.flex_shrink_y = 1;
42}
43
44 void 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
51 void function_xflex(Requirement& r) {
52 r.flex_grow_x = 1;
53 r.flex_shrink_x = 1;
54}
55
56 void function_yflex(Requirement& r) {
57 r.flex_grow_y = 1;
58 r.flex_shrink_y = 1;
59}
60
61 void 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
68 class 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 {
83 Node::SetBox(box);
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
98 Element filler() {
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/// ~~~
123 Element flex(Element child) {
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
129 Element xflex(Element child) {
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
135 Element yflex(Element child) {
136 return std::make_shared<Flex>(function_yflex, std::move(child));
137}
138
139/// @brief Expand if possible.
140/// @ingroup dom
141 Element flex_grow(Element child) {
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
147 Element xflex_grow(Element child) {
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
153 Element yflex_grow(Element child) {
154 return std::make_shared<Flex>(function_yflex_grow, std::move(child));
155}
156
157/// @brief Minimize if needed.
158/// @ingroup dom
159 Element flex_shrink(Element child) {
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
165 Element xflex_shrink(Element child) {
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
171 Element yflex_shrink(Element child) {
172 return std::make_shared<Flex>(function_yflex_shrink, std::move(child));
173}
174
175/// @brief Make the element not flexible.
176/// @ingroup dom
177 Element notflex(Element child) {
178 return std::make_shared<Flex>(function_not_flex, std::move(child));
179}
180
181} // namespace ftxui
box.hpp
elements.hpp
f_
FlexFunction f_
Definition flex.cpp:90
ftxui::Node::SetBox
virtual void SetBox(Box box)
Assign a position and a dimension to an element for drawing.
Definition node.cpp:41
ftxui::xflex
Element xflex(Element)
Expand/Minimize if possible/needed on the X axis.
Definition flex.cpp:129
ftxui::xflex_grow
Element xflex_grow(Element)
Expand if possible on the X axis.
Definition flex.cpp:147
ftxui::flex
Element flex(Element)
Make a child element to expand proportionally to the space left in a container.
Definition flex.cpp:123
ftxui::yflex
Element yflex(Element)
Expand/Minimize if possible/needed on the Y axis.
Definition flex.cpp:135
ftxui::flex_shrink
Element flex_shrink(Element)
Minimize if needed.
Definition flex.cpp:159
ftxui::yflex_grow
Element yflex_grow(Element)
Expand if possible on the Y axis.
Definition flex.cpp:153
ftxui::flex_grow
Element flex_grow(Element)
Expand if possible.
Definition flex.cpp:141
ftxui::notflex
Element notflex(Element)
Make the element not flexible.
Definition flex.cpp:177
ftxui::xflex_shrink
Element xflex_shrink(Element)
Minimize if needed on the X axis.
Definition flex.cpp:165
ftxui::filler
Element filler()
An element that will take expand proportionally to the space left in a container.
Definition flex.cpp:98
ftxui::yflex_shrink
Element yflex_shrink(Element)
Minimize if needed on the Y axis.
Definition flex.cpp:171
ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
ftxui::Element
std::shared_ptr< Node > Element
Definition elements.hpp:22
node.hpp
requirement.hpp