FTXUI 7.0.3
C++ functional terminal UI.
Loading...
Searching...
No Matches
component_options.cpp
Go to the documentation of this file.
1// Copyright 2022 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.
5
6#include <ftxui/screen/color.hpp> // for Color, Color::White, Color::Black, Color::GrayDark, Color::Blue, Color::GrayLight, Color::Red
8#include <memory> // for shared_ptr
9#include <string>
10#include <utility> // for move
11#include "ftxui/component/animation.hpp" // for Function, Duration
13#include "ftxui/dom/elements.hpp" // for operator|=, Element, text, bgcolor, inverted, bold, dim, operator|, color, borderEmpty, hbox, automerge, border, borderLight
14
15namespace ftxui {
16
17/// @brief Una opción de color que puede animarse.
18/// @param _inactive El color cuando el componente está inactivo.
19/// @param _active El color cuando el componente está activo.
20/// @param _duration La duración de la animación.
21/// @param _function La función de easing de la animación.
22void AnimatedColorOption::Set(Color _inactive,
23 Color _active,
24 animation::Duration _duration,
26 enabled = true;
27 inactive = _inactive;
28 active = _active;
29 duration = _duration;
30 function = std::move(_function);
31}
32
33/// @brief Establece cómo debe animarse el subrayado.
34/// @param d La duración de la animación.
35/// @param f La función de aceleración de la animación.
36void UnderlineOption::SetAnimation(animation::Duration d,
38 SetAnimationDuration(d);
39 SetAnimationFunction(std::move(f));
40}
41
42/// @brief Establece cómo debe animarse el subrayado.
43/// @param d La duración de la animación.
44void UnderlineOption::SetAnimationDuration(animation::Duration d) {
45 leader_duration = d;
46 follower_duration = d;
47}
48
49/// @brief Establece cómo debe animarse el subrayado.
50/// @param f La función de aceleración de la animación.
51void UnderlineOption::SetAnimationFunction(animation::easing::Function f) {
52 leader_function = f;
53 follower_function = std::move(f);
54}
55
56/// @brief Establece cómo debe animarse el subrayado.
57/// Esto es útil para desincronizar la animación del líder y el seguidor.
58/// @param f_leader La duración de la animación para el líder.
59/// @param f_follower La duración de la animación para el seguidor.
60void UnderlineOption::SetAnimationFunction(
62 animation::easing::Function f_follower) {
63 leader_function = std::move(f_leader);
64 follower_function = std::move(f_follower);
65}
66
67/// @brief Opciones estándar para un menú horizontal.
68/// Esto puede ser útil para implementar una barra de pestañas.
69// static
70MenuOption MenuOption::Horizontal() {
71 MenuOption option;
72 option.direction = Direction::Right;
73 option.entries_option.transform = [](const EntryState& state) {
74 Element e = text(state.label);
75 if (state.focused) {
76 e |= inverted;
77 }
78 if (state.active) {
79 e |= bold;
80 }
81 if (!state.focused && !state.active) {
82 e |= dim;
83 }
84 return e;
85 };
86 option.elements_infix = [] { return text(" "); };
87
88 return option;
89}
90
91/// @brief Opciones estándar para un menú horizontal animado.
92/// Esto puede ser útil para implementar una barra de pestañas.
93// static
94MenuOption MenuOption::HorizontalAnimated() {
95 auto option = Horizontal();
96 option.underline.enabled = true;
97 return option;
98}
99
100/// @brief Opciones estándar para un menú vertical.
101/// Esto puede ser útil para implementar una lista de elementos seleccionables.
102// static
103MenuOption MenuOption::Vertical() {
104 MenuOption option;
105 option.entries_option.transform = [](const EntryState& state) {
106 Element e = text((state.active ? "> " : " ") + state.label); // NOLINT
107 if (state.focused) {
108 e |= inverted;
109 }
110 if (state.active) {
111 e |= bold;
112 }
113 if (!state.focused && !state.active) {
114 e |= dim;
115 }
116 return e;
117 };
118 return option;
119}
120
121/// @brief Opciones estándar para un menú vertical animado.
122/// Esto puede ser útil para implementar una lista de elementos seleccionables.
123// static
124MenuOption MenuOption::VerticalAnimated() {
125 auto option = MenuOption::Vertical();
126 option.entries_option.transform = [](const EntryState& state) {
127 Element e = text(state.label);
128 if (state.focused) {
129 e |= inverted;
130 }
131 if (state.active) {
132 e |= bold;
133 }
134 if (!state.focused && !state.active) {
135 e |= dim;
136 }
137 return e;
138 };
139 option.underline.enabled = true;
140 return option;
141}
142
143/// @brief Opciones estándar para un menú horizontal con un separador.
144/// Esto puede ser útil para implementar una barra de pestañas.
145// static
146MenuOption MenuOption::Toggle() {
147 auto option = MenuOption::Horizontal();
148 option.elements_infix = [] { return text("│") | automerge; };
149 return option;
150}
151
152/// @brief Crea una ButtonOption, resaltada usando los caracteres [].
153// static
154ButtonOption ButtonOption::Ascii() {
155 ButtonOption option;
156 option.transform = [](const EntryState& s) {
157 const std::string t = s.focused ? "[" + s.label + "]" //
158 : " " + s.label + " ";
159 return text(t);
160 };
161 return option;
162}
163
164/// @brief Crea una ButtonOption, invertida cuando está enfocada.
165// static
166ButtonOption ButtonOption::Simple() {
167 ButtonOption option;
168 option.transform = [](const EntryState& s) {
169 auto element = text(s.label) | borderLight;
170 if (s.focused) {
171 element |= inverted;
172 }
173 return element;
174 };
175 return option;
176}
177
178/// @brief Crea una ButtonOption. El botón se muestra usando un borde, invertido
179/// cuando está enfocado. Este es el valor predeterminado actual.
180ButtonOption ButtonOption::Border() {
181 ButtonOption option;
182 option.transform = [](const EntryState& s) {
183 auto element = text(s.label) | border;
184 if (s.active) {
185 element |= bold;
186 }
187 if (s.focused) {
188 element |= inverted;
189 }
190 return element;
191 };
192 return option;
193}
194
195/// @brief Crea una ButtonOption, usando colores animados.
196// static
197ButtonOption ButtonOption::Animated() {
198 return Animated(Color::Black, Color::GrayLight, //
199 Color::GrayDark, Color::White);
200}
201
202/// @brief Crea una ButtonOption, usando colores animados.
203// static
204ButtonOption ButtonOption::Animated(Color color) {
205 return ButtonOption::Animated(
206 Color::Interpolate(0.85F, color, Color::Black), // NOLINT
207 Color::Interpolate(0.10F, color, Color::White), // NOLINT
208 Color::Interpolate(0.10F, color, Color::Black), // NOLINT
209 Color::Interpolate(0.85F, color, Color::White)); // NOLINT
210}
211
212/// @brief Crea una ButtonOption, usando colores animados.
213// static
214ButtonOption ButtonOption::Animated(Color background, Color foreground) {
215 // NOLINTBEGIN
216 return ButtonOption::Animated(
217 /*background=*/background,
218 /*foreground=*/foreground,
219 /*background_active=*/foreground,
220 /*foreground_active=*/background);
221 // NOLINTEND
222}
223
224/// @brief Crea una ButtonOption, usando colores animados.
225// static
226ButtonOption ButtonOption::Animated(Color background,
227 Color foreground,
228 Color background_active,
229 Color foreground_active) {
230 ButtonOption option;
231 option.transform = [](const EntryState& s) {
232 auto element = text(s.label) | borderEmpty;
233 if (s.focused) {
234 element |= bold;
235 }
236 return element;
237 };
238 option.animated_colors.foreground.Set(foreground, foreground_active);
239 option.animated_colors.background.Set(background, background_active);
240 return option;
241}
242
243/// @brief Opción para Checkbox estándar.
244// static
245CheckboxOption CheckboxOption::Simple() {
246 auto option = CheckboxOption();
247 option.transform = [](const EntryState& s) {
248 auto prefix = (Terminal::GetQuirks().ComponentAscii())
249 ? text(s.state ? "[X] " : "[ ] ") // NOLINT
250 : text(s.state ? "▣ " : "☐ "); // NOLINT
251 auto t = text(s.label);
252 if (s.active) {
253 t |= bold;
254 }
255 if (s.focused) {
256 t |= inverted;
257 }
258 return hbox({prefix, t});
259 };
260 return option;
261}
262
263/// @brief Opción para Radiobox estándar
264// static
265RadioboxOption RadioboxOption::Simple() {
266 auto option = RadioboxOption();
267 option.transform = [](const EntryState& s) {
268 auto prefix = (Terminal::GetQuirks().ComponentAscii())
269 ? text(s.state ? "(*) " : "( ) ") // NOLINT
270 : text(s.state ? "◉ " : "○ "); // NOLINT
271 auto t = text(s.label);
272 if (s.active) {
273 t |= bold;
274 }
275 if (s.focused) {
276 t |= inverted;
277 }
278 return hbox({prefix, t});
279 };
280 return option;
281}
282
283/// @brief Opciones estándar para el componente de entrada.
284// static
285InputOption InputOption::Default() {
286 InputOption option;
287 option.transform = [](InputState state) {
288 if (state.is_placeholder) {
289 state.element |= dim;
290 }
291
292 if (state.focused) {
293 state.element |= inverted;
294 } else if (state.hovered) {
295 state.element |= underlined;
296 }
297
298 return state.element;
299 };
300 return option;
301}
302
303/// @brief Opciones estándar para un componente de entrada más bonito.
304// static
305InputOption InputOption::Spacious() {
306 InputOption option;
307 option.transform = [](InputState state) {
308 state.element |= borderEmpty;
309
310 if (state.is_placeholder) {
311 state.element |= dim;
312 }
313
314 if (state.focused) {
315 state.element |= inverted;
316 } else if (state.hovered) {
317 state.element |= bold;
318 }
319
320 return state.element;
321 };
322 return option;
323}
324
325} // namespace ftxui
std::function< float(float)> Function
Definition animation.hpp:45
std::chrono::duration< float > Duration
Definition animation.hpp:30
El espacio de nombres ftxui:: de FTXUI.
Definition animation.hpp:11
std::shared_ptr< Node > Element
Definition elements.hpp:25
const Element & element
Definition node.hpp:95