FTXUI  3.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
component_options.cpp
Go to the documentation of this file.
2
3#include <memory> // for shared_ptr
4#include <utility> // for move
5
6#include "ftxui/component/animation.hpp" // for Function, Duration
7#include "ftxui/dom/elements.hpp" // for operator|=, text, Element, bold, inverted, operator|, dim, hbox, automerge, borderEmpty, borderLight
8
9namespace ftxui {
10
12 Color a_active,
13 animation::Duration a_duration,
14 animation::easing::Function a_function) {
15 enabled = true;
16 inactive = a_inactive;
17 active = a_active;
18 duration = a_duration;
19 function = std::move(a_function);
20}
21
27
32
37
40 animation::easing::Function f_follower) {
41 leader_function = std::move(f_leader);
42 follower_function = std::move(f_follower);
43}
44
45// static
47 MenuOption option;
49 option.entries.transform = [](const EntryState& state) {
50 Element e = text(state.label);
51 if (state.focused) {
52 e |= inverted;
53 }
54 if (state.active) {
55 e |= bold;
56 }
57 if (!state.focused && !state.active) {
58 e |= dim;
59 }
60 return e;
61 };
62 option.elements_infix = [] { return text(" "); };
63
64 return option;
65}
66
67// static
69 auto option = Horizontal();
70 option.underline.enabled = true;
71 return option;
72}
73
74// static
76 MenuOption option;
77 option.entries.transform = [](const EntryState& state) {
78 Element e = text((state.active ? "> " : " ") + state.label); // NOLINT
79 if (state.focused) {
80 e |= inverted;
81 }
82 if (state.active) {
83 e |= bold;
84 }
85 if (!state.focused && !state.active) {
86 e |= dim;
87 }
88 return e;
89 };
90 return option;
91}
92
93// static
95 auto option = MenuOption::Vertical();
96 option.entries.transform = [](const EntryState& state) {
97 Element e = text(state.label);
98 if (state.focused) {
99 e |= inverted;
100 }
101 if (state.active) {
102 e |= bold;
103 }
104 if (!state.focused && !state.active) {
105 e |= dim;
106 }
107 return e;
108 };
109 option.underline.enabled = true;
110 return option;
111}
112
113// static
115 auto option = MenuOption::Horizontal();
116 option.elements_infix = [] { return text("│") | automerge; };
117 return option;
118}
119
120/// @brief Create a ButtonOption, highlighted using [] characters.
121// static
123 ButtonOption option;
124 option.transform = [](const EntryState& s) {
125 std::string label = s.focused ? "[" + s.label + "]" //
126 : " " + s.label + " ";
127 return text(label);
128 };
129 return option;
130}
131
132/// @brief Create a ButtonOption, inverted when focused.
133// static
135 ButtonOption option;
136 option.transform = [](const EntryState& s) {
137 auto element = text(s.label) | borderLight;
138 if (s.focused) {
139 element |= inverted;
140 }
141 return element;
142 };
143 return option;
144}
145
146/// @brief Create a ButtonOption, using animated colors.
147// static
152
153/// @brief Create a ButtonOption, using animated colors.
154// static
162
163/// @brief Create a ButtonOption, using animated colors.
164// static
166 return ButtonOption::Animated(background, foreground, foreground, background);
167}
168
169/// @brief Create a ButtonOption, using animated colors.
170// static
172 Color foreground,
173 Color background_active,
174 Color foreground_active) {
175 ButtonOption option;
176 option.transform = [](const EntryState& s) {
177 auto element = text(s.label) | borderEmpty;
178 if (s.focused) {
179 element |= bold;
180 }
181 return element;
182 };
183 option.animated_colors.foreground.Set(foreground, foreground_active);
184 option.animated_colors.background.Set(background, background_active);
185 return option;
186}
187
188/// @brief Option for standard Checkbox.
189// static
191 auto option = CheckboxOption();
192 option.transform = [](const EntryState& s) {
193#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
194 // Microsoft terminal do not use fonts able to render properly the default
195 // radiobox glyph.
196 auto prefix = text(s.state ? "[X] " : "[ ] "); // NOLINT
197#else
198 auto prefix = text(s.state ? "▣ " : "☐ "); // NOLINT
199#endif
200 auto t = text(s.label);
201 if (s.active) {
202 t |= bold;
203 }
204 if (s.focused) {
205 t |= inverted;
206 }
207 return hbox({prefix, t});
208 };
209 return option;
210}
211
212/// @brief Option for standard Radiobox
213// static
215 auto option = RadioboxOption();
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 ? "(*) " : "( ) "); // 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} // namespace ftxui
237
238// Copyright 2022 Arthur Sonzogni. All rights reserved.
239// Use of this source code is governed by the MIT license that can be found in
240// the LICENSE file.
A class representing terminal colors.
Definition color.hpp:17
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< double > Duration
Definition animation.hpp:21
std::shared_ptr< Node > Element
Definition elements.hpp:18
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:76
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 light border around the element.
Definition border.cpp:252
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 borderEmpty(Element)
Draw an empty border around the element.
Definition border.cpp:388
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 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