FTXUI 7.0.3
C++ functional terminal UI.
Loading...
Searching...
No Matches
flexbox_gallery.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 <stddef.h> // for size_t
5#include <memory> // for shared_ptr, __shared_ptr_access, allocator
6#include <string> // for string, basic_string, to_string, operator+, char_traits
7#include <vector> // for vector
8
9#include "ftxui/component/app.hpp" // for App
10#include "ftxui/component/captured_mouse.hpp" // for ftxui
11#include "ftxui/component/component.hpp" // for Radiobox, Vertical, Checkbox, Horizontal, Renderer, ResizableSplitBottom, ResizableSplitRight
12#include "ftxui/component/component_base.hpp" // for ComponentBase
13#include "ftxui/dom/elements.hpp" // for text, window, operator|, vbox, hbox, Element, flexbox, bgcolor, filler, flex, size, border, hcenter, color, EQUAL, bold, dim, notflex, xflex_grow, yflex_grow, HEIGHT, WIDTH
14#include "ftxui/dom/flexbox_config.hpp" // for FlexboxConfig, FlexboxConfig::AlignContent, FlexboxConfig::JustifyContent, FlexboxConfig::AlignContent::Center, FlexboxConfig::AlignItems, FlexboxConfig::Direction, FlexboxConfig::JustifyContent::Center, FlexboxConfig::Wrap
15#include "ftxui/screen/color.hpp" // for Color, Color::Black
16
17using namespace ftxui;
18
19int main() {
20 auto screen = App::Fullscreen();
21
22 int direction_index = 0;
23 int wrap_index = 0;
24 int justify_content_index = 0;
25 int align_items_index = 0;
26 int align_content_index = 0;
27
28 std::vector<std::string> directions = {
29 "Row",
30 "RowInversed",
31 "Column",
32 "ColumnInversed",
33 };
34
35 std::vector<std::string> wraps = {
36 "NoWrap",
37 "Wrap",
38 "WrapInversed",
39 };
40
41 std::vector<std::string> justify_content = {
42 "FlexStart", "FlexEnd", "Center", "Stretch",
43 "SpaceBetween", "SpaceAround", "SpaceEvenly",
44 };
45
46 std::vector<std::string> align_items = {
47 "FlexStart",
48 "FlexEnd",
49 "Center",
50 "Stretch",
51 };
52
53 std::vector<std::string> align_content = {
54 "FlexStart", "FlexEnd", "Center", "Stretch",
55 "SpaceBetween", "SpaceAround", "SpaceEvenly",
56 };
57
58 auto radiobox_direction = Radiobox(&directions, &direction_index);
59 auto radiobox_wrap = Radiobox(&wraps, &wrap_index);
60 auto radiobox_justify_content =
61 Radiobox(&justify_content, &justify_content_index);
62 auto radiobox_align_items = Radiobox(&align_items, &align_items_index);
63 auto radiobox_align_content = Radiobox(&align_content, &align_content_index);
64
65 bool element_xflex_grow = false;
66 bool element_yflex_grow = false;
67 bool group_xflex_grow = true;
68 bool group_yflex_grow = true;
69 auto checkbox_element_xflex_grow =
70 Checkbox("element |= xflex_grow", &element_xflex_grow);
71 auto checkbox_element_yflex_grow =
72 Checkbox("element |= yflex_grow", &element_yflex_grow);
73 auto checkbox_group_xflex_grow =
74 Checkbox("group |= xflex_grow", &group_xflex_grow);
75 auto checkbox_group_yflex_grow =
76 Checkbox("group |= yflex_grow", &group_yflex_grow);
77
78 auto make_box = [&](size_t dimx, size_t dimy, size_t index) {
79 std::string title = std::to_string(dimx) + "x" + std::to_string(dimy);
80 auto element = window(text(title) | hcenter | bold,
81 text(std::to_string(index)) | hcenter | dim) |
82 size(WIDTH, EQUAL, dimx) | size(HEIGHT, EQUAL, dimy) |
83 bgcolor(Color::HSV(index * 25, 255, 255)) |
84 color(Color::Black);
85 if (element_xflex_grow) {
87 }
88 if (element_yflex_grow) {
90 }
91 return element;
92 };
93
94 auto content_renderer = Renderer([&] {
95 FlexboxConfig config;
96 config.direction = static_cast<FlexboxConfig::Direction>(direction_index);
97 config.wrap = static_cast<FlexboxConfig::Wrap>(wrap_index);
98 config.justify_content =
99 static_cast<FlexboxConfig::JustifyContent>(justify_content_index);
100 config.align_items =
101 static_cast<FlexboxConfig::AlignItems>(align_items_index);
102 config.align_content =
103 static_cast<FlexboxConfig::AlignContent>(align_content_index);
104
105 auto group = flexbox(
106 {
107 make_box(8, 4, 0),
108 make_box(9, 6, 1),
109 make_box(11, 6, 2),
110 make_box(10, 4, 3),
111 make_box(13, 7, 4),
112 make_box(12, 4, 5),
113 make_box(12, 5, 6),
114 make_box(10, 4, 7),
115 make_box(12, 4, 8),
116 make_box(10, 5, 9),
117 },
118 config);
119
120 group = group | bgcolor(Color::Black);
121
122 group = group | notflex;
123
124 if (!group_xflex_grow) {
125 group = hbox(group, filler());
126 }
127 if (!group_yflex_grow) {
128 group = vbox(group, filler());
129 }
130
131 group = group | flex;
132 return group;
133 });
134
135 auto center = FlexboxConfig()
136 .Set(FlexboxConfig::JustifyContent::Center)
137 .Set(FlexboxConfig::AlignContent::Center);
138 int space_right = 10;
139 int space_bottom = 1;
140 content_renderer = ResizableSplitRight(
141 Renderer([&] { return flexbox({text("resizable")}, center); }),
142 content_renderer, &space_right);
143 content_renderer = ResizableSplitBottom(
144 Renderer([&] { return flexbox({text("resizable")}, center); }),
145 content_renderer, &space_bottom);
146
147 auto main_container = Container::Vertical({
148 Container::Horizontal({
149 radiobox_direction,
150 radiobox_wrap,
151 Container::Vertical({
152 checkbox_element_xflex_grow,
153 checkbox_element_yflex_grow,
154 checkbox_group_xflex_grow,
155 checkbox_group_yflex_grow,
156 }),
157 }),
158 Container::Horizontal({
159 radiobox_justify_content,
160 radiobox_align_items,
161 radiobox_align_content,
162 }),
163 content_renderer,
164 });
165
166 auto main_renderer = Renderer(main_container, [&] {
167 return vbox({
168 vbox({hbox({
169 window(text("FlexboxConfig::Direction"),
170 radiobox_direction->Render()),
171 window(text("FlexboxConfig::Wrap"), radiobox_wrap->Render()),
172 window(text("Misc:"),
173 vbox({
174 checkbox_element_xflex_grow->Render(),
175 checkbox_element_yflex_grow->Render(),
176 checkbox_group_xflex_grow->Render(),
177 checkbox_group_yflex_grow->Render(),
178 })),
179 }),
180 hbox({
181 window(text("FlexboxConfig::JustifyContent"),
182 radiobox_justify_content->Render()),
183 window(text("FlexboxConfig::AlignItems"),
184 radiobox_align_items->Render()),
185 window(text("FlexboxConfig::AlignContent"),
186 radiobox_align_content->Render()),
187 })}),
188 content_renderer->Render() | flex | border,
189 });
190 });
191
192 screen.Loop(main_renderer);
193
194 return 0;
195}
Element make_box(int x, int y)
Component Renderer(Component child, std::function< Element()>)
Devuelve un nuevo Component, similar a |child|, pero usando |render| como el evento Component::Render...
Component Checkbox(ConstStringRef label, bool *checked, CheckboxOption options=CheckboxOption::Simple())
Dibuja un elemento marcable.
Component ResizableSplitRight(Component main, Component back, int *main_size)
Una división horizontal entre dos componentes, configurable usando el ratón.
Component Radiobox(ConstStringListRef entries, int *selected_, RadioboxOption options={})
Una lista de elementos, donde solo uno puede ser seleccionado.
Component ResizableSplitBottom(Component main, Component back, int *main_size)
Una división vertical entre dos componentes, configurable usando el ratón.
Element window(Element title, Element content, BorderStyle border=ROUNDED)
Dibuja una ventana con un título y un borde alrededor del elemento.
Element text(std::string_view text)
Muestra un fragmento de texto Unicode codificado en UTF8.
Element xflex_grow(Element)
Expandir si es posible en el eje X.
Definition flex.cpp:119
Decorator size(WidthOrHeight direction, Constraint constraint, int value)
Aplica una restricción al tamaño de un elemento.
Element flex(Element child)
Hace que un elemento hijo se expanda proporcionalmente al espacio restante en un contenedor.
Definition flex.cpp:95
Element center(Element child)
Centra un elemento horizontal y verticalmente.
Element yflex_grow(Element)
Expandir si es posible en el eje Y.
Definition flex.cpp:125
Element notflex(Element child)
Hace que el elemento no sea flexible.
Definition flex.cpp:149
Element bgcolor(Color color, Element child)
Establece el color de fondo de un elemento.
Definition dom/color.cpp:96
Element filler()
Un elemento que se expandirá proporcionalmente al espacio restante en un contenedor.
Definition flex.cpp:70
Element color(Color color, Element child)
Establece el color de primer plano de un elemento.
Definition dom/color.cpp:81
Element border(Element child)
Dibuja un borde alrededor del elemento.
Element vbox(Elements children)
Un contenedor que muestra elementos verticalmente uno por uno.
Definition vbox.cpp:96
El espacio de nombres ftxui:: de FTXUI.
Definition animation.hpp:11
Element flexbox(Elements, FlexboxConfig config=FlexboxConfig())
Un contenedor que muestra elementos en filas/columnas y es capaz de ajustarse a la siguiente columna/...
Definition flexbox.cpp:252
Element hbox(Elements children)
Un contenedor que muestra elementos horizontalmente uno por uno.
Definition hbox.cpp:94
const Element & element
Definition node.hpp:95