FTXUI  5.0.0
C++ functional terminal UI.
component_options.hpp
Go to the documentation of this file.
1// Copyright 2021 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#ifndef FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP
5#define FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP
6
7#include <chrono> // for milliseconds
8#include <ftxui/component/animation.hpp> // for Duration, QuadraticInOut, Function
9#include <ftxui/dom/direction.hpp> // for Direction, Direction::Left, Direction::Right, Direction::Down
10#include <ftxui/dom/elements.hpp> // for Element, separator
11#include <ftxui/util/ref.hpp> // for Ref, ConstRef, StringRef
12#include <functional> // for function
13#include <optional> // for optional
14#include <string> // for string
15
16#include "ftxui/component/component_base.hpp" // for Component
17#include "ftxui/screen/color.hpp" // for Color, Color::GrayDark, Color::White
18
19namespace ftxui {
20
21/// @brief arguments for |ButtonOption::transform|, |CheckboxOption::transform|,
22/// |Radiobox::transform|, |MenuEntryOption::transform|,
23/// |MenuOption::transform|.
24struct EntryState {
25 std::string label; ///< The label to display.
26 bool state; ///< The state of the button/checkbox/radiobox
27 bool active; ///< Whether the entry is the active one.
28 bool focused; ///< Whether the entry is one focused by the user.
29};
30
32 bool enabled = false;
33
36
41
42 animation::Duration leader_duration = std::chrono::milliseconds(250);
43 animation::Duration leader_delay = std::chrono::milliseconds(0);
44 animation::Duration follower_duration = std::chrono::milliseconds(250);
45 animation::Duration follower_delay = std::chrono::milliseconds(0);
46
52};
53
54/// @brief Option about a potentially animated color.
55/// @ingroup component
57 void Set(
60 animation::Duration duration = std::chrono::milliseconds(250),
62
63 bool enabled = false;
66 animation::Duration duration = std::chrono::milliseconds(250);
68};
69
73};
74
75/// @brief Option for the MenuEntry component.
76/// @ingroup component
78 ConstStringRef label = "MenuEntry";
79 std::function<Element(const EntryState& state)> transform;
81};
82
83/// @brief Option for the Menu component.
84/// @ingroup component
85struct MenuOption {
86 // Standard constructors:
87 static MenuOption Horizontal();
89 static MenuOption Vertical();
91 static MenuOption Toggle();
92
93 ConstStringListRef entries; ///> The list of entries.
94 Ref<int> selected = 0; ///> The index of the selected entry.
95
96 // Style:
100 std::function<Element()> elements_prefix;
101 std::function<Element()> elements_infix;
102 std::function<Element()> elements_postfix;
103
104 // Observers:
105 std::function<void()> on_change; ///> Called when the selected entry changes.
106 std::function<void()> on_enter; ///> Called when the user presses enter.
108};
109
110/// @brief Option for the AnimatedButton component.
111/// @ingroup component
113 // Standard constructors:
114 static ButtonOption Ascii();
115 static ButtonOption Simple();
116 static ButtonOption Border();
117 static ButtonOption Animated();
119 static ButtonOption Animated(Color background, Color foreground);
120 static ButtonOption Animated(Color background,
121 Color foreground,
122 Color background_active,
123 Color foreground_active);
124
126 std::function<void()> on_click = [] {};
127
128 // Style:
129 std::function<Element(const EntryState&)> transform;
131};
132
133/// @brief Option for the Checkbox component.
134/// @ingroup component
136 // Standard constructors:
137 static CheckboxOption Simple();
138
139 ConstStringRef label = "Checkbox";
140
142
143 // Style:
144 std::function<Element(const EntryState&)> transform;
145
146 // Observer:
147 /// Called when the user change the state.
148 std::function<void()> on_change = [] {};
149};
150
151/// @brief Used to define style for the Input component.
154 bool hovered; ///< Whether the input is hovered by the mouse.
155 bool focused; ///< Whether the input is focused by the user.
156 bool is_placeholder; ///< Whether the input is empty and displaying the
157 ///< placeholder.
158};
159
160/// @brief Option for the Input component.
161/// @ingroup component
163 // A set of predefined styles:
164
165 /// @brief Create the default input style:
166 static InputOption Default();
167 /// @brief A white on black style with high margins:
168 static InputOption Spacious();
169
170 /// The content of the input.
172
173 /// The content of the input when it's empty.
175
176 // Style:
177 std::function<Element(InputState)> transform;
178 Ref<bool> password = false; ///< Obscure the input content using '*'.
179 Ref<bool> multiline = true; ///< Whether the input can be multiline.
180 Ref<bool> insert = true; ///< Insert or overtype character mode.
181
182 /// Called when the content changes.
183 std::function<void()> on_change = [] {};
184 /// Called when the user presses enter.
185 std::function<void()> on_enter = [] {};
186
187 // The char position of the cursor:
189};
190
191/// @brief Option for the Radiobox component.
192/// @ingroup component
194 // Standard constructors:
195 static RadioboxOption Simple();
196
197 // Content:
200
201 // Style:
202 std::function<Element(const EntryState&)> transform;
203
204 // Observers:
205 /// Called when the selected entry changes.
206 std::function<void()> on_change = [] {};
208};
209
216 : 10;
218};
219
220// @brief Option for the `Slider` component.
221// @ingroup component
222template <typename T>
226 ConstRef<T> max = T(100);
227 ConstRef<T> increment = (max() - min()) / 20;
231};
232
233// Parameter pack used by `WindowOptions::render`.
235 Element inner; ///< The element wrapped inside this window.
236 const std::string& title; ///< The title of the window.
237 bool active = false; ///< Whether the window is the active one.
238 bool drag = false; ///< Whether the window is being dragged.
239 bool resize = false; ///< Whether the window is being resized.
240 bool hover_left = false; ///< Whether the resizeable left side is hovered.
241 bool hover_right = false; ///< Whether the resizeable right side is hovered.
242 bool hover_top = false; ///< Whether the resizeable top side is hovered.
243 bool hover_down = false; ///< Whether the resizeable down side is hovered.
244};
245
246// @brief Option for the `Window` component.
247// @ingroup component
249 Component inner; ///< The component wrapped by this window.
250 ConstStringRef title = ""; ///< The title displayed by this window.
251
252 Ref<int> left = 0; ///< The left side position of the window.
253 Ref<int> top = 0; ///< The top side position of the window.
254 Ref<int> width = 20; ///< The width of the window.
255 Ref<int> height = 10; ///< The height of the window.
256
257 Ref<bool> resize_left = true; ///< Can the left side be resized?
258 Ref<bool> resize_right = true; ///< Can the right side be resized?
259 Ref<bool> resize_top = true; ///< Can the top side be resized?
260 Ref<bool> resize_down = true; ///< Can the down side be resized?
261
262 /// An optional function to customize how the window looks like:
263 std::function<Element(const WindowRenderState&)> render;
264};
265
266} // namespace ftxui
267
268#endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP */
A class representing terminal colors.
Definition: color.hpp:21
An adapter. Own or reference an immutable object.
Definition: ref.hpp:15
An adapter. Reference a list of strings.
Definition: ref.hpp:98
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
Definition: ref.hpp:86
An adapter. Own or reference a constant string. For convenience, this class convert multiple mutable ...
Definition: ref.hpp:76
float QuadraticInOut(float p)
Definition: animation.cpp:47
std::function< float(float)> Function
Definition: animation.hpp:39
std::chrono::duration< float > Duration
Definition: animation.hpp:24
bool active
Whether the entry is the active one.
Ref< bool > resize_down
Can the down side be resized?
Component inner
The component wrapped by this window.
Ref< bool > resize_left
Can the left side be resized?
AnimatedColorOption foreground
std::string label
The label to display.
bool drag
Whether the window is being dragged.
Direction
Definition: direction.hpp:8
std::function< Element()> separator_func
std::shared_ptr< Node > Element
Definition: elements.hpp:23
bool focused
Whether the entry is one focused by the user.
bool hover_down
Whether the resizeable down side is hovered.
std::shared_ptr< ComponentBase > Component
const std::string & title
The title of the window.
Ref< int > height
The height of the window.
bool resize
Whether the window is being resized.
Element inner
The element wrapped inside this window.
AnimatedColorOption background
std::function< Element(const EntryState &state)> transform
Ref< bool > resize_top
Can the top side be resized?
bool hover_right
Whether the resizeable right side is hovered.
bool hovered
Whether the input is hovered by the mouse.
Ref< int > width
The width of the window.
std::function< Element(const WindowRenderState &)> render
An optional function to customize how the window looks like:
bool state
The state of the button/checkbox/radiobox.
AnimatedColorsOption animated_colors
ConstStringRef title
The title displayed by this window.
Ref< bool > resize_right
Can the right side be resized?
bool hover_left
Whether the resizeable left side is hovered.
Element separator()
Draw a vertical or horizontal separation in between two other elements.
Definition: separator.cpp:132
bool hover_top
Whether the resizeable top side is hovered.
Ref< int > left
The left side position of the window.
Ref< int > top
The top side position of the window.
Decorator color(Color)
Decorate using a foreground color.
Definition: color.cpp:110
arguments for |ButtonOption::transform|, |CheckboxOption::transform|, |Radiobox::transform|,...
Used to define style for the Input component.
Option for the MenuEntry component.
Option about a potentially animated color.
animation::easing::Function function
animation::Duration duration
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....
Option for the AnimatedButton component.
static ButtonOption Animated()
Create a ButtonOption, using animated colors.
std::function< void()> on_click
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.
std::function< void()> on_change
Called when the user change the state.
std::function< Element(const EntryState &)> transform
Option for the Input component.
static InputOption Default()
Create the default input style:
static InputOption Spacious()
A white on black style with high margins:
Ref< bool > insert
Insert or overtype character mode.
std::function< void()> on_enter
Called when the user presses enter.
Ref< bool > password
Obscure the input content using '*'.
std::function< Element(InputState)> transform
StringRef placeholder
The content of the input when it's empty.
std::function< void()> on_change
Called when the content changes.
StringRef content
The content of the input.
Ref< bool > multiline
Whether the input can be multiline.
Option for the Menu component.
std::function< Element()> elements_prefix
static MenuOption Toggle()
Standard options for a horitontal menu with some separator. This can be useful to implement a tab bar...
MenuEntryOption entries_option
std::function< void()> on_enter
‍Called when the selected entry changes.
UnderlineOption underline
‍The index of the selected entry.
static MenuOption Horizontal()
Standard options for an 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 ...
static MenuOption Vertical()
Standard options for a vertical menu. This can be useful to implement a list of selectable items.
ConstStringListRef entries
Ref< int > focused_entry
‍Called when the user presses enter.
std::function< Element()> elements_infix
std::function< Element()> elements_postfix
std::function< void()> on_change
Ref< int > selected
‍The list of entries.
static MenuOption HorizontalAnimated()
Standard options for an animated horizontal menu. This can be useful to implement a tab bar.
Option for the Radiobox component.
ConstStringListRef entries
std::function< void()> on_change
Called when the selected entry changes.
static RadioboxOption Simple()
Option for standard Radiobox.
std::function< Element(const EntryState &)> transform
animation::Duration follower_duration
animation::easing::Function leader_function
animation::Duration follower_delay
void SetAnimationFunction(animation::easing::Function f)
Set how the underline should animate.
animation::Duration leader_duration
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
animation::Duration leader_delay