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.
4#include "ftxui/component/component_options.hpp"
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
10#include "ftxui/dom/direction.hpp"
11#include "ftxui/dom/elements.hpp" // for operator|=, Element, text, bgcolor, inverted, bold, dim, operator|, color, borderEmpty, hbox, automerge, border, borderLight
12
13 namespace 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.
20 void AnimatedColorOption::Set(Color _inactive,
21 Color _active,
22 animation::Duration _duration,
23 animation::easing::Function _function) {
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.
34 void UnderlineOption::SetAnimation(animation::Duration d,
35 animation::easing::Function f) {
36 SetAnimationDuration(d);
37 SetAnimationFunction(std::move(f));
38}
39
40/// @brief Set how the underline should animate.
41/// @param d The duration of the animation.
42 void UnderlineOption::SetAnimationDuration(animation::Duration d) {
43 leader_duration = d;
44 follower_duration = d;
45}
46
47/// @brief Set how the underline should animate.
48/// @param f The easing function of the animation.
49 void UnderlineOption::SetAnimationFunction(animation::easing::Function f) {
50 leader_function = f;
51 follower_function = std::move(f);
52}
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.
59 void UnderlineOption::SetAnimationFunction(
60 animation::easing::Function f_leader,
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
69 MenuOption MenuOption::Horizontal() {
70 MenuOption option;
71 option.direction = Direction::Right;
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
93 MenuOption MenuOption::HorizontalAnimated() {
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
102 MenuOption MenuOption::Vertical() {
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
123 MenuOption MenuOption::VerticalAnimated() {
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
145 MenuOption MenuOption::Toggle() {
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
153 ButtonOption ButtonOption::Ascii() {
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
165 ButtonOption ButtonOption::Simple() {
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.
179 ButtonOption ButtonOption::Border() {
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
196 ButtonOption ButtonOption::Animated() {
197 return Animated(Color::Black, Color::GrayLight, //
198 Color::GrayDark, Color::White);
199}
200
201/// @brief Create a ButtonOption, using animated colors.
202// static
203 ButtonOption ButtonOption::Animated(Color color) {
204 return ButtonOption::Animated(
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
213 ButtonOption ButtonOption::Animated(Color background, Color foreground) {
214 // NOLINTBEGIN
215 return ButtonOption::Animated(
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
225 ButtonOption ButtonOption::Animated(Color background,
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
244 CheckboxOption CheckboxOption::Simple() {
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
268 RadioboxOption RadioboxOption::Simple() {
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
292 InputOption InputOption::Default() {
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
314 InputOption InputOption::Spacious() {
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
animation.hpp
color.hpp
component_options.hpp
direction.hpp
elements.hpp
ftxui::ButtonOption::Animated
static ButtonOption Animated()
Create a ButtonOption, using animated colors.
Definition component_options.cpp:196
ftxui::MenuOption::Toggle
static MenuOption Toggle()
Standard options for a horizontal menu with some separator. This can be useful to implement a tab bar...
Definition component_options.cpp:145
ftxui::UnderlineOption::follower_duration
animation::Duration follower_duration
Definition component_options.hpp:45
ftxui::UnderlineOption::leader_function
animation::easing::Function leader_function
Definition component_options.hpp:38
ftxui::MenuOption::entries_option
MenuEntryOption entries_option
Definition component_options.hpp:99
ftxui::InputOption::Default
static InputOption Default()
Create the default input style:
Definition component_options.cpp:292
ftxui::AnimatedColorOption::function
animation::easing::Function function
Definition component_options.hpp:68
ftxui::ButtonOption::Border
static ButtonOption Border()
Create a ButtonOption. The button is shown using a border, inverted when focused. This is the current...
Definition component_options.cpp:179
ftxui::UnderlineOption::SetAnimationFunction
void SetAnimationFunction(animation::easing::Function f)
Set how the underline should animate.
Definition component_options.cpp:49
ftxui::InputOption::Spacious
static InputOption Spacious()
A white on black style with high margins:
Definition component_options.cpp:314
ftxui::CheckboxOption::Simple
static CheckboxOption Simple()
Option for standard Checkbox.
Definition component_options.cpp:244
ftxui::MenuOption::direction
Direction direction
Definition component_options.hpp:100
ftxui::ButtonOption::Simple
static ButtonOption Simple()
Create a ButtonOption, inverted when focused.
Definition component_options.cpp:165
ftxui::MenuEntryOption::transform
std::function< Element(const EntryState &state)> transform
Definition component_options.hpp:80
ftxui::AnimatedColorOption::active
Color active
Definition component_options.hpp:66
ftxui::MenuOption::Horizontal
static MenuOption Horizontal()
Standard options for a horizontal menu. This can be useful to implement a tab bar.
Definition component_options.cpp:69
ftxui::MenuOption::VerticalAnimated
static MenuOption VerticalAnimated()
Standard options for an animated vertical menu. This can be useful to implement a list of selectable ...
Definition component_options.cpp:123
ftxui::UnderlineOption::leader_duration
animation::Duration leader_duration
Definition component_options.hpp:43
ftxui::MenuOption::Vertical
static MenuOption Vertical()
Standard options for a vertical menu. This can be useful to implement a list of selectable items.
Definition component_options.cpp:102
ftxui::ButtonOption::Ascii
static ButtonOption Ascii()
Create a ButtonOption, highlighted using [] characters.
Definition component_options.cpp:153
ftxui::AnimatedColorOption::duration
animation::Duration duration
Definition component_options.hpp:67
ftxui::UnderlineOption::SetAnimation
void SetAnimation(animation::Duration d, animation::easing::Function f)
Set how the underline should animate.
Definition component_options.cpp:34
ftxui::UnderlineOption::SetAnimationDuration
void SetAnimationDuration(animation::Duration d)
Set how the underline should animate.
Definition component_options.cpp:42
ftxui::UnderlineOption::follower_function
animation::easing::Function follower_function
Definition component_options.hpp:40
ftxui::AnimatedColorOption::enabled
bool enabled
Definition component_options.hpp:64
ftxui::AnimatedColorOption::inactive
Color inactive
Definition component_options.hpp:65
ftxui::InputOption::transform
std::function< Element(InputState)> transform
Definition component_options.hpp:178
ftxui::MenuOption::elements_infix
std::function< Element()> elements_infix
Definition component_options.hpp:102
ftxui::ButtonOption::animated_colors
AnimatedColorsOption animated_colors
Definition component_options.hpp:131
ftxui::AnimatedColorOption::Set
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....
Definition component_options.cpp:20
ftxui::MenuOption::HorizontalAnimated
static MenuOption HorizontalAnimated()
Standard options for an animated horizontal menu. This can be useful to implement a tab bar.
Definition component_options.cpp:93
ftxui::RadioboxOption::Simple
static RadioboxOption Simple()
Option for standard Radiobox.
Definition component_options.cpp:268
ftxui::ButtonOption::transform
std::function< Element(const EntryState &)> transform
Definition component_options.hpp:130
ftxui::ButtonOption
Option for the AnimatedButton component.
Definition component_options.hpp:113
ftxui::CheckboxOption
Option for the Checkbox component.
Definition component_options.hpp:136
ftxui::InputOption
Option for the Input component.
Definition component_options.hpp:163
ftxui::MenuOption
Option for the Menu component.
Definition component_options.hpp:86
ftxui::RadioboxOption
Option for the Radiobox component.
Definition component_options.hpp:194
ftxui::bgcolor
Decorator bgcolor(Color)
Decorate using a background color.
Definition dom/color.cpp:124
ftxui::bold
Element bold(Element)
Use a bold font, for elements with more emphasis.
Definition bold.cpp:33
ftxui::inverted
Element inverted(Element)
Add a filter that will invert the foreground and the background colors.
Definition inverted.cpp:34
ftxui::text
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:160
ftxui::borderLight
Element borderLight(Element)
Draw a light border around the element.
Definition src/ftxui/dom/border.cpp:335
ftxui::dim
Element dim(Element)
Use a light font, for elements with less emphasis.
Definition dim.cpp:33
ftxui::automerge
Element automerge(Element child)
Enable character to be automatically merged with others nearby.
Definition automerge.cpp:17
ftxui::border
Element border(Element)
Draw a border around the element.
Definition src/ftxui/dom/border.cpp:227
ftxui::borderEmpty
Element borderEmpty(Element)
Draw an empty border around the element.
Definition src/ftxui/dom/border.cpp:475
ftxui::color
Decorator color(Color)
Decorate using a foreground color.
Definition dom/color.cpp:110
ftxui::Direction::Right
@ Right
ftxui::Color::White
@ White
Definition color.hpp:69
ftxui::Color::Black
@ Black
Definition color.hpp:54
ftxui::Color::GrayDark
@ GrayDark
Definition color.hpp:62
ftxui::Color::GrayLight
@ GrayLight
Definition color.hpp:61
ftxui::Color::Interpolate
static Color Interpolate(float t, const Color &a, const Color &b)
Definition screen/color.cpp:203
ftxui::Color
Color is a class that represents a color in the terminal user interface.
Definition color.hpp:22
ftxui::animation::easing::Function
std::function< float(float)> Function
Definition animation.hpp:45
ftxui::animation::Duration
std::chrono::duration< float > Duration
Definition animation.hpp:30
ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
ftxui::Element
std::shared_ptr< Node > Element
Definition elements.hpp:22
ftxui::hbox
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definition hbox.cpp:94
ftxui::AnimatedColorsOption::foreground
AnimatedColorOption foreground
Definition component_options.hpp:73
ftxui::AnimatedColorsOption::background
AnimatedColorOption background
Definition component_options.hpp:72
ftxui::EntryState
arguments for transform from |ButtonOption|, |CheckboxOption|, |RadioboxOption|, |MenuEntryOption|,...
Definition component_options.hpp:22
ftxui::InputState
Used to define style for the Input component.
Definition component_options.hpp:153