FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
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
13#include <functional> // for function
14#include <limits> // for numeric_limits
15#include <string> // for string
16
17#include "ftxui/component/component_base.hpp" // for Component
18#include "ftxui/screen/color.hpp" // for Color, Color::GrayDark, Color::White
19
20namespace ftxui {
21
22/// @brief arguments for transform from |ButtonOption|, |CheckboxOption|,
23/// |RadioboxOption|, |MenuEntryOption|, |MenuOption|.
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 int index; ///< Index of the entry when applicable or -1.
30};
31
32/// @brief Option for the underline effect.
33/// @ingroup component
56
57/// @brief Option about a potentially animated color.
58/// @ingroup component
60 void Set(
63 animation::Duration duration = std::chrono::milliseconds(250),
64 animation::easing::Function function = animation::easing::QuadraticInOut);
65
66 bool enabled = false;
69 animation::Duration duration = std::chrono::milliseconds(250);
71};
72
77
78/// @brief Option for the MenuEntry component.
79/// @ingroup component
85
86/// @brief Option for the Menu component.
87/// @ingroup component
88struct MenuOption {
89 // Standard constructors:
90 static MenuOption Horizontal();
92 static MenuOption Vertical();
94 static MenuOption Toggle();
95
96 ConstStringListRef entries; ///> The list of entries.
97 Ref<int> selected = 0; ///> The index of the selected entry.
98
99 // Style:
103 std::function<Element()> elements_prefix;
104 std::function<Element()> elements_infix;
105 std::function<Element()> elements_postfix;
106
107 // Observers:
108 std::function<void()> on_change; ///> Called when the selected entry changes.
109 std::function<void()> on_enter; ///> Called when the user presses enter.
111};
112
113/// @brief Option for the AnimatedButton component.
114/// @ingroup component
116 // Standard constructors:
117 static ButtonOption Ascii();
118 static ButtonOption Simple();
119 static ButtonOption Border();
120 static ButtonOption Animated();
121 static ButtonOption Animated(Color color);
122 static ButtonOption Animated(Color background, Color foreground);
123 static ButtonOption Animated(Color background,
124 Color foreground,
125 Color background_active,
126 Color foreground_active);
127
129 std::function<void()> on_click = [] {};
130
131 // Style:
132 std::function<Element(const EntryState&)> transform;
134};
135
136/// @brief Option for the Checkbox component.
137/// @ingroup component
139 // Standard constructors:
140 static CheckboxOption Simple();
141
142 ConstStringRef label = "Checkbox";
143
145
146 // Style:
147 std::function<Element(const EntryState&)> transform;
148
149 // Observer:
150 /// Called when the user change the state.
151 std::function<void()> on_change = [] {};
152};
153
154/// @brief Used to define style for the Input component.
157 bool hovered; ///< Whether the input is hovered by the mouse.
158 bool focused; ///< Whether the input is focused by the user.
159 bool is_placeholder; ///< Whether the input is empty and displaying the
160 ///< placeholder.
161};
162
163/// @brief Option for the Input component.
164/// @ingroup component
166 // A set of predefined styles:
167
168 /// @brief Create the default input style:
169 static InputOption Default();
170 /// @brief A white on black style with high margins:
171 static InputOption Spacious();
172
173 /// The content of the input.
175
176 /// The content of the input when it's empty.
178
179 // Style:
180 std::function<Element(InputState)> transform;
181 Ref<bool> password = false; ///< Obscure the input content using '*'.
182 Ref<bool> multiline = true; ///< Whether the input can be multiline.
183 Ref<bool> insert = true; ///< Insert or overtype character mode.
184
185 /// Called when the content changes.
186 std::function<void()> on_change = [] {};
187 /// Called when the user presses enter.
188 std::function<void()> on_enter = [] {};
189
190 // The char position of the cursor:
192};
193
194/// @brief Option for the Radiobox component.
195/// @ingroup component
197 // Standard constructors:
198 static RadioboxOption Simple();
199
200 // Content:
203
204 // Style:
205 std::function<Element(const EntryState&)> transform;
206
207 // Observers:
208 /// Called when the selected entry changes.
209 std::function<void()> on_change = [] {};
211};
212
219 : 10;
220 std::function<Element()> separator_func = [] { return ::ftxui::separator(); };
221
222 // Constraints on main_size:
224 Ref<int> max = std::numeric_limits<int>::max();
225};
226
227// @brief Option for the `Slider` component.
228// @ingroup component
229template <typename T>
240
241/// @brief State passed to the `Window` component's render function.
242/// @ingroup component
244 Element inner; ///< The element wrapped inside this window.
245 const std::string& title; ///< The title of the window.
246 bool active = false; ///< Whether the window is the active one.
247 bool drag = false; ///< Whether the window is being dragged.
248 bool resize = false; ///< Whether the window is being resized.
249 bool hover_left = false; ///< Whether the resizeable left side is hovered.
250 bool hover_right = false; ///< Whether the resizeable right side is hovered.
251 bool hover_top = false; ///< Whether the resizeable top side is hovered.
252 bool hover_down = false; ///< Whether the resizeable down side is hovered.
253};
254
255// @brief Option for the `Window` component.
256// @ingroup component
258 Component inner; ///< The component wrapped by this window.
259 ConstStringRef title = ""; ///< The title displayed by this window.
260
261 Ref<int> left = 0; ///< The left side position of the window.
262 Ref<int> top = 0; ///< The top side position of the window.
263 Ref<int> width = 20; ///< The width of the window.
264 Ref<int> height = 10; ///< The height of the window.
265
266 Ref<bool> resize_left = true; ///< Can the left side be resized?
267 Ref<bool> resize_right = true; ///< Can the right side be resized?
268 Ref<bool> resize_top = true; ///< Can the top side be resized?
269 Ref<bool> resize_down = true; ///< Can the down side be resized?
270
271 /// An optional function to customize how the window looks like:
272 std::function<Element(const WindowRenderState&)> render;
273};
274
275/// @brief Option for the Dropdown component.
276/// @ingroup component
277/// A dropdown menu is a checkbox opening/closing a radiobox.
279 /// Whether the dropdown is open or closed:
281 // The options for the checkbox:
283 // The options for the radiobox:
285 // The transformation function:
286 std::function<Element(bool open, Element checkbox, Element radiobox)>
288};
289
290} // namespace ftxui
291
292#endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP */
An adapter. Own or reference an immutable object.
Definition ref.hpp:17
An adapter. Reference a list of strings.
Definition ref.hpp:116
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
Definition ref.hpp:94
An adapter. Own or reference an mutable object.
Definition ref.hpp:46
An adapter. Own or reference a constant string. For convenience, this class convert multiple mutable ...
Definition ref.hpp:82
static ButtonOption Animated()
Create a ButtonOption, using animated colors.
bool active
Whether the window is the active one.
std::function< void()> on_click
std::function< Element()> elements_prefix
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
bool drag
Whether the window is being dragged.
static InputOption Default()
Create the default input style:
animation::easing::Function function
animation::Duration follower_delay
static ButtonOption Border()
Create a ButtonOption. The button is shown using a border, inverted when focused. This is the current...
bool hover_down
Whether the resizeable down side is hovered.
const std::string & title
The title of the window.
void SetAnimationFunction(animation::easing::Function f)
Set how the underline should animate.
static InputOption Spacious()
A white on black style with high margins:
Ref< bool > insert
Insert or overtype character mode.
static CheckboxOption Simple()
Option for standard Checkbox.
bool resize
Whether the window is being resized.
std::function< void()> on_enter
Element inner
The element wrapped inside this window.
static ButtonOption Simple()
Create a ButtonOption, inverted when focused.
UnderlineOption underline
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.
ConstStringListRef entries
animation::easing::Function follower_function
bool hover_right
Whether the resizeable right side is hovered.
Ref< bool > password
Obscure the input content using '*'.
std::function< Element(InputState)> transform
std::function< Element()> elements_infix
Ref< bool > open
Whether the dropdown is open or closed:
StringRef placeholder
The content of the input when it's empty.
std::function< Element()> elements_postfix
AnimatedColorsOption animated_colors
bool hover_left
Whether the resizeable left side is hovered.
std::function< void()> on_change
StringRef content
The content of the input.
bool hover_top
Whether the resizeable top side is hovered.
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....
animation::Duration leader_delay
std::function< Element(bool open, Element checkbox, Element radiobox)> transform
static MenuOption HorizontalAnimated()
Standard options for an animated horizontal menu. This can be useful to implement a tab bar.
Ref< bool > multiline
Whether the input can be multiline.
static RadioboxOption Simple()
Option for standard Radiobox.
std::function< Element(const EntryState &)> transform
Option about a potentially animated color.
Option for the AnimatedButton component.
Option for the Checkbox component.
Option for the Dropdown component.A dropdown menu is a checkbox opening/closing a radiobox.
Option for the Input component.
Option for the MenuEntry component.
Option for the Menu component.
Option for the Radiobox component.
Option for the underline effect.
State passed to the Window component's render function.
Direction
Direction is an enumeration that represents the four cardinal directions.
Definition direction.hpp:13
Color is a class that represents a color in the terminal user interface.
Definition color.hpp:22
float QuadraticInOut(float p)
Definition animation.cpp:46
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
std::shared_ptr< ComponentBase > Component
arguments for transform from |ButtonOption|, |CheckboxOption|, |RadioboxOption|, |MenuEntryOption|,...
bool active
Whether the entry is the active one.
std::string label
The label to display.
bool focused
Whether the entry is one focused by the user.
int index
Index of the entry when applicable or -1.
bool state
The state of the button/checkbox/radiobox.
Used to define style for the Input component.
bool focused
Whether the input is focused by the user.
bool hovered
Whether the input is hovered by the mouse.
std::function< Element()> separator_func
std::function< void()> on_change
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?
Ref< int > height
The height of the window.
Ref< bool > resize_top
Can the top side be resized?
Ref< int > width
The width of the window.
std::function< Element(const WindowRenderState &)> render
An optional function to customize how the window looks like:
ConstStringRef title
The title displayed by this window.
Ref< bool > resize_right
Can the right side be resized?
Ref< int > left
The left side position of the window.
Ref< int > top
The top side position of the window.