FTXUI 6.1.9
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
7#include <memory> // for shared_ptr
8#include <utility> // for move
9#include "ftxui/component/animation.hpp" // for Function, Duration
11#include "ftxui/dom/elements.hpp" // for operator|=, Element, text, bgcolor, inverted, bold, dim, operator|, color, borderEmpty, hbox, automerge, border, borderLight
12
13namespace ftxui {
14
15/// @brief A color option that can be animated.
16/// @params _inactive The color when the component is inactive.
17/// @params _active The color when the component is active.
18/// @params _duration The duration of the animation.
19/// @params _function The easing function of the animation.
21 Color _active,
22 animation::Duration _duration,
24 enabled = true;
25 inactive = _inactive;
26 active = _active;
27 duration = _duration;
28 function = std::move(_function);
29}
30
31/// @brief Set how the underline should animate.
32/// @param d The duration of the animation.
33/// @param f The easing function of the animation.
39
40/// @brief Set how the underline should animate.
41/// @param d The duration of the animation.
46
47/// @brief Set how the underline should animate.
48/// @param f The easing function of the animation.
53
54/// @brief Set how the underline should animate.
55/// This is useful to desynchronize the animation of the leader and the
56/// follower.
57/// @param f_leader The duration of the animation for the leader.
58/// @param f_follower The duration of the animation for the follower.
61 animation::easing::Function f_follower) {
62 leader_function = std::move(f_leader);
63 follower_function = std::move(f_follower);
64}
65
66/// @brief Standard options for a horizontal menu.
67/// This can be useful to implement a tab bar.
68// static
70 MenuOption option;
72 option.entries_option.transform = [](const EntryState& state) {
73 Element e = text(state.label);
74 if (state.focused) {
75 e |= inverted;
76 }
77 if (state.active) {
78 e |= bold;
79 }
80 if (!state.focused && !state.active) {
81 e |= dim;
82 }
83 return e;
84 };
85 option.elements_infix = [] { return text(" "); };
86
87 return option;
88}
89
90/// @brief Standard options for an animated horizontal menu.
91/// This can be useful to implement a tab bar.
92// static
94 auto option = Horizontal();
95 option.underline.enabled = true;
96 return option;
97}
98
99/// @brief Standard options for a vertical menu.
100/// This can be useful to implement a list of selectable items.
101// static
103 MenuOption option;
104 option.entries_option.transform = [](const EntryState& state) {
105 Element e = text((state.active ? "> " : " ") + state.label); // NOLINT
106 if (state.focused) {
107 e |= inverted;
108 }
109 if (state.active) {
110 e |= bold;
111 }
112 if (!state.focused && !state.active) {
113 e |= dim;
114 }
115 return e;
116 };
117 return option;
118}
119
120/// @brief Standard options for an animated vertical menu.
121/// This can be useful to implement a list of selectable items.
122// static
124 auto option = MenuOption::Vertical();
125 option.entries_option.transform = [](const EntryState& state) {
126 Element e = text(state.label);
127 if (state.focused) {
128 e |= inverted;
129 }
130 if (state.active) {
131 e |= bold;
132 }
133 if (!state.focused && !state.active) {
134 e |= dim;
135 }
136 return e;
137 };
138 option.underline.enabled = true;
139 return option;
140}
141
142/// @brief Standard options for a horizontal menu with some separator.
143/// This can be useful to implement a tab bar.
144// static
146 auto option = MenuOption::Horizontal();
147 option.elements_infix = [] { return text("│") | automerge; };
148 return option;
149}
150
151/// @brief Create a ButtonOption, highlighted using [] characters.
152// static
154 ButtonOption option;
155 option.transform = [](const EntryState& s) {
156 const std::string t = s.focused ? "[" + s.label + "]" //
157 : " " + s.label + " ";
158 return text(t);
159 };
160 return option;
161}
162
163/// @brief Create a ButtonOption, inverted when focused.
164// static
166 ButtonOption option;
167 option.transform = [](const EntryState& s) {
168 auto element = text(s.label) | borderLight;
169 if (s.focused) {
170 element |= inverted;
171 }
172 return element;
173 };
174 return option;
175}
176
177/// @brief Create a ButtonOption. The button is shown using a border, inverted
178/// when focused. This is the current default.
180 ButtonOption option;
181 option.transform = [](const EntryState& s) {
182 auto element = text(s.label) | border;
183 if (s.active) {
184 element |= bold;
185 }
186 if (s.focused) {
187 element |= inverted;
188 }
189 return element;
190 };
191 return option;
192}
193
194/// @brief Create a ButtonOption, using animated colors.
195// static
200
201/// @brief Create a ButtonOption, using animated colors.
202// static
205 Color::Interpolate(0.85F, color, Color::Black), // NOLINT
206 Color::Interpolate(0.10F, color, Color::White), // NOLINT
207 Color::Interpolate(0.10F, color, Color::Black), // NOLINT
208 Color::Interpolate(0.85F, color, Color::White)); // NOLINT
209}
210
211/// @brief Create a ButtonOption, using animated colors.
212// static
214 // NOLINTBEGIN
216 /*bakground=*/background,
217 /*foreground=*/foreground,
218 /*background_active=*/foreground,
219 /*foreground_active=*/background);
220 // NOLINTEND
221}
222
223/// @brief Create a ButtonOption, using animated colors.
224// static
226 Color foreground,
227 Color background_active,
228 Color foreground_active) {
229 ButtonOption option;
230 option.transform = [](const EntryState& s) {
231 auto element = text(s.label) | borderEmpty;
232 if (s.focused) {
233 element |= bold;
234 }
235 return element;
236 };
237 option.animated_colors.foreground.Set(foreground, foreground_active);
238 option.animated_colors.background.Set(background, background_active);
239 return option;
240}
241
242/// @brief Option for standard Checkbox.
243// static
245 auto option = CheckboxOption();
246 option.transform = [](const EntryState& s) {
247#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
248 // Microsoft terminal do not use fonts able to render properly the default
249 // radiobox glyph.
250 auto prefix = text(s.state ? "[X] " : "[ ] "); // NOLINT
251#else
252 auto prefix = text(s.state ? "▣ " : "☐ "); // NOLINT
253#endif
254 auto t = text(s.label);
255 if (s.active) {
256 t |= bold;
257 }
258 if (s.focused) {
259 t |= inverted;
260 }
261 return hbox({prefix, t});
262 };
263 return option;
264}
265
266/// @brief Option for standard Radiobox
267// static
269 auto option = RadioboxOption();
270 option.transform = [](const EntryState& s) {
271#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
272 // Microsoft terminal do not use fonts able to render properly the default
273 // radiobox glyph.
274 auto prefix = text(s.state ? "(*) " : "( ) "); // NOLINT
275#else
276 auto prefix = text(s.state ? "◉ " : "○ "); // NOLINT
277#endif
278 auto t = text(s.label);
279 if (s.active) {
280 t |= bold;
281 }
282 if (s.focused) {
283 t |= inverted;
284 }
285 return hbox({prefix, t});
286 };
287 return option;
288}
289
290/// @brief Standard options for the input component.
291// static
293 InputOption option;
294 option.transform = [](InputState state) {
295 state.element |= color(Color::White);
296
297 if (state.is_placeholder) {
298 state.element |= dim;
299 }
300
301 if (state.focused) {
302 state.element |= inverted;
303 } else if (state.hovered) {
304 state.element |= bgcolor(Color::GrayDark);
305 }
306
307 return state.element;
308 };
309 return option;
310}
311
312/// @brief Standard options for a more beautiful input component.
313// static
315 InputOption option;
316 option.transform = [](InputState state) {
317 state.element |= borderEmpty;
318 state.element |= color(Color::White);
319
320 if (state.is_placeholder) {
321 state.element |= dim;
322 }
323
324 if (state.focused) {
325 state.element |= bgcolor(Color::Black);
326 }
327
328 if (state.hovered) {
329 state.element |= bgcolor(Color::GrayDark);
330 }
331
332 return state.element;
333 };
334 return option;
335}
336
337} // namespace ftxui
static ButtonOption Animated()
Create a ButtonOption, using animated colors.
static MenuOption Toggle()
Standard options for a horizontal menu with some separator. This can be useful to implement a tab bar...
animation::Duration follower_duration
animation::easing::Function leader_function
MenuEntryOption entries_option
static InputOption Default()
Create the default input style:
animation::easing::Function function
static ButtonOption Border()
Create a ButtonOption. The button is shown using a border, inverted when focused. This is the current...
void SetAnimationFunction(animation::easing::Function f)
Set how the underline should animate.
static InputOption Spacious()
A white on black style with high margins:
static CheckboxOption Simple()
Option for standard Checkbox.
static ButtonOption Simple()
Create a ButtonOption, inverted when focused.
std::function< Element(const EntryState &state)> transform
static MenuOption Horizontal()
Standard options for a horizontal menu. This can be useful to implement a tab bar.
static MenuOption VerticalAnimated()
Standard options for an animated vertical menu. This can be useful to implement a list of selectable ...
animation::Duration leader_duration
static MenuOption Vertical()
Standard options for a vertical menu. This can be useful to implement a list of selectable items.
static ButtonOption Ascii()
Create a ButtonOption, highlighted using [] characters.
void SetAnimation(animation::Duration d, animation::easing::Function f)
Set how the underline should animate.
void SetAnimationDuration(animation::Duration d)
Set how the underline should animate.
animation::easing::Function follower_function
std::function< Element(InputState)> transform
std::function< Element()> elements_infix
AnimatedColorsOption animated_colors
void Set(Color inactive, Color active, animation::Duration duration=std::chrono::milliseconds(250), animation::easing::Function function=animation::easing::QuadraticInOut)
A color option that can be animated. @params _inactive The color when the component is inactive....
static MenuOption HorizontalAnimated()
Standard options for an animated horizontal menu. This can be useful to implement a tab bar.
static RadioboxOption Simple()
Option for standard Radiobox.
std::function< Element(const EntryState &)> transform
Option for the AnimatedButton component.
Option for the Checkbox component.
Option for the Input component.
Option for the Menu component.
Option for the Radiobox component.
Decorator bgcolor(Color)
Decorate using a background color.
Element bold(Element)
Use a bold font, for elements with more emphasis.
Definition bold.cpp:33
Element inverted(Element)
Add a filter that will invert the foreground and the background colors.
Definition inverted.cpp:34
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:160
Element borderLight(Element)
Draw a light border around the element.
Element dim(Element)
Use a light font, for elements with less emphasis.
Definition dim.cpp:33
Element automerge(Element child)
Enable character to be automatically merged with others nearby.
Definition automerge.cpp:17
Element border(Element)
Draw a border around the element.
Element borderEmpty(Element)
Draw an empty border around the element.
Decorator color(Color)
Decorate using a foreground color.
static Color Interpolate(float t, const Color &a, const Color &b)
Color is a class that represents a color in the terminal user interface.
Definition color.hpp:22
std::function< float(float)> Function
Definition animation.hpp:45
std::chrono::duration< float > Duration
Definition animation.hpp:30
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
std::shared_ptr< Node > Element
Definition elements.hpp:22
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definition hbox.cpp:94
arguments for transform from |ButtonOption|, |CheckboxOption|, |RadioboxOption|, |MenuEntryOption|,...
Used to define style for the Input component.