FTXUI  4.1.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
component_options.cpp
Go to the documentation of this file.
2
3#include <ftxui/screen/color.hpp> // for Color, Color::Black, Color::White, Color::GrayDark, Color::GrayLight
4#include <memory> // for shared_ptr
5#include <utility> // for move
6
7#include "ftxui/component/animation.hpp" // for Function, Duration
8#include "ftxui/dom/elements.hpp" // for operator|=, text, Element, bold, inverted, operator|, dim, hbox, automerge, borderEmpty, borderLight
9
10namespace ftxui {
11
13 Color a_active,
14 animation::Duration a_duration,
15 animation::easing::Function a_function) {
16 enabled = true;
17 inactive = a_inactive;
18 active = a_active;
19 duration = a_duration;
20 function = std::move(a_function);
21}
22
28
33
38
41 animation::easing::Function f_follower) {
42 leader_function = std::move(f_leader);
43 follower_function = std::move(f_follower);
44}
45
46// static
48 MenuOption option;
50 option.entries.transform = [](const EntryState& state) {
51 Element e = text(state.label);
52 if (state.focused) {
53 e |= inverted;
54 }
55 if (state.active) {
56 e |= bold;
57 }
58 if (!state.focused && !state.active) {
59 e |= dim;
60 }
61 return e;
62 };
63 option.elements_infix = [] { return text(" "); };
64
65 return option;
66}
67
68// static
70 auto option = Horizontal();
71 option.underline.enabled = true;
72 return option;
73}
74
75// static
77 MenuOption option;
78 option.entries.transform = [](const EntryState& state) {
79 Element e = text((state.active ? "> " : " ") + state.label); // NOLINT
80 if (state.focused) {
81 e |= inverted;
82 }
83 if (state.active) {
84 e |= bold;
85 }
86 if (!state.focused && !state.active) {
87 e |= dim;
88 }
89 return e;
90 };
91 return option;
92}
93
94// static
96 auto option = MenuOption::Vertical();
97 option.entries.transform = [](const EntryState& state) {
98 Element e = text(state.label);
99 if (state.focused) {
100 e |= inverted;
101 }
102 if (state.active) {
103 e |= bold;
104 }
105 if (!state.focused && !state.active) {
106 e |= dim;
107 }
108 return e;
109 };
110 option.underline.enabled = true;
111 return option;
112}
113
114// static
116 auto option = MenuOption::Horizontal();
117 option.elements_infix = [] { return text("│") | automerge; };
118 return option;
119}
120
121/// @brief Create a ButtonOption, highlighted using [] characters.
122// static
124 ButtonOption option;
125 option.transform = [](const EntryState& s) {
126 const std::string label = s.focused ? "[" + s.label + "]" //
127 : " " + s.label + " ";
128 return text(label);
129 };
130 return option;
131}
132
133/// @brief Create a ButtonOption, inverted when focused.
134// static
136 ButtonOption option;
137 option.transform = [](const EntryState& s) {
138 auto element = text(s.label) | borderLight;
139 if (s.focused) {
140 element |= inverted;
141 }
142 return element;
143 };
144 return option;
145}
146
147/// @brief Create a ButtonOption. The button is shown using a border, inverted
148/// when focused. This is the current default.
150 ButtonOption option;
151 option.transform = [](const EntryState& s) {
152 auto element = text(s.label) | border;
153 if (s.active) {
154 element |= bold;
155 }
156 if (s.focused) {
157 element |= inverted;
158 }
159 return element;
160 };
161 return option;
162}
163
164/// @brief Create a ButtonOption, using animated colors.
165// static
170
171/// @brief Create a ButtonOption, using animated colors.
172// static
180
181/// @brief Create a ButtonOption, using animated colors.
182// static
184 // NOLINTBEGIN
186 /*bakground=*/background,
187 /*foreground=*/foreground,
188 /*background_active=*/foreground,
189 /*foreground_active=*/background);
190 // NOLINTEND
191}
192
193/// @brief Create a ButtonOption, using animated colors.
194// static
196 Color foreground,
197 Color background_active,
198 Color foreground_active) {
199 ButtonOption option;
200 option.transform = [](const EntryState& s) {
201 auto element = text(s.label) | borderEmpty;
202 if (s.focused) {
203 element |= bold;
204 }
205 return element;
206 };
207 option.animated_colors.foreground.Set(foreground, foreground_active);
208 option.animated_colors.background.Set(background, background_active);
209 return option;
210}
211
212/// @brief Option for standard Checkbox.
213// static
215 auto option = CheckboxOption();
216 option.transform = [](const EntryState& s) {
217#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
218 // Microsoft terminal do not use fonts able to render properly the default
219 // radiobox glyph.
220 auto prefix = text(s.state ? "[X] " : "[ ] "); // NOLINT
221#else
222 auto prefix = text(s.state ? "▣ " : "☐ "); // NOLINT
223#endif
224 auto t = text(s.label);
225 if (s.active) {
226 t |= bold;
227 }
228 if (s.focused) {
229 t |= inverted;
230 }
231 return hbox({prefix, t});
232 };
233 return option;
234}
235
236/// @brief Option for standard Radiobox
237// static
239 auto option = RadioboxOption();
240 option.transform = [](const EntryState& s) {
241#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
242 // Microsoft terminal do not use fonts able to render properly the default
243 // radiobox glyph.
244 auto prefix = text(s.state ? "(*) " : "( ) "); // NOLINT
245#else
246 auto prefix = text(s.state ? "◉ " : "○ "); // NOLINT
247#endif
248 auto t = text(s.label);
249 if (s.active) {
250 t |= bold;
251 }
252 if (s.focused) {
253 t |= inverted;
254 }
255 return hbox({prefix, t});
256 };
257 return option;
258}
259
260} // namespace ftxui
261
262// Copyright 2022 Arthur Sonzogni. All rights reserved.
263// Use of this source code is governed by the MIT license that can be found in
264// the LICENSE file.
A class representing terminal colors.
Definition color.hpp:18
static Color Interpolate(float t, const Color &a, const Color &b)
Definition color.cpp:172
std::function< float(float)> Function
Definition animation.hpp:36
std::chrono::duration< float > Duration
Definition animation.hpp:21
std::shared_ptr< Node > Element
Definition elements.hpp:20
Element bold(Element)
Use a bold font, for elements with more emphasis.
Definition bold.cpp:28
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definition hbox.cpp:77
std::function< Element(const EntryState &state)> transform
Element inverted(Element)
Add a filter that will invert the foreground and the background colors.
Definition inverted.cpp:29
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:111
Element borderLight(Element)
Draw a dashed border around the element.
Definition border.cpp:330
Element dim(Element)
Use a light font, for elements with less emphasis.
Definition dim.cpp:28
Element automerge(Element child)
Enable character to be automatically merged with others nearby.
Definition automerge.cpp:14
Element border(Element)
Draw a border around the element.
Definition border.cpp:222
Element borderEmpty(Element)
Draw an empty border around the element.
Definition border.cpp:470
Decorator color(Color)
Decorate using a foreground color.
Definition color.cpp:86
arguments for |ButtonOption::transform|, |CheckboxOption::transform|, |Radiobox::transform|,...
animation::easing::Function function
void Set(Color inactive, Color active, animation::Duration duration=std::chrono::milliseconds(250), animation::easing::Function function=animation::easing::QuadraticInOut)
Option for the AnimatedButton component.
static ButtonOption Animated()
Create a ButtonOption, using animated colors.
static ButtonOption Border()
Create a ButtonOption. The button is shown using a border, inverted when focused. This is the current...
static ButtonOption Simple()
Create a ButtonOption, inverted when focused.
static ButtonOption Ascii()
Create a ButtonOption, highlighted using [] characters.
AnimatedColorsOption animated_colors
std::function< Element(const EntryState &)> transform
Option for the Checkbox component.
static CheckboxOption Simple()
Option for standard Checkbox.
Option for the Menu component.
static MenuOption Toggle()
static MenuOption Horizontal()
static MenuOption VerticalAnimated()
static MenuOption Vertical()
std::function< Element()> elements_infix
MenuEntryOption entries
static MenuOption HorizontalAnimated()
Option for the Radiobox component.
static RadioboxOption Simple()
Option for standard Radiobox.
animation::Duration follower_duration
animation::easing::Function leader_function
void SetAnimationFunction(animation::easing::Function f)
animation::Duration leader_duration
void SetAnimation(animation::Duration d, animation::easing::Function f)
void SetAnimationDuration(animation::Duration d)
animation::easing::Function follower_function