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 <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 transform from |ButtonOption|, |CheckboxOption|,
22/// |RadioboxOption|, |MenuEntryOption|, |MenuOption|.
23struct EntryState {
24 std::string label; ///< The label to display.
25 bool state; ///< The state of the button/checkbox/radiobox
26 bool active; ///< Whether the entry is the active one.
27 bool focused; ///< Whether the entry is one focused by the user.
28 int index; ///< Index of the entry when applicable or -1.
29};
30
31/// @brief Option for the underline effect.
32/// @ingroup component
55
56/// @brief Option about a potentially animated color.
57/// @ingroup component
59 void Set(
62 animation::Duration duration = std::chrono::milliseconds(250),
63 animation::easing::Function function = animation::easing::QuadraticInOut);
64
65 bool enabled = false;
68 animation::Duration duration = std::chrono::milliseconds(250);
70};
71
76
77/// @brief Option for the MenuEntry component.
78/// @ingroup component
84
85/// @brief Option for the Menu component.
86/// @ingroup component
87struct MenuOption {
88 // Standard constructors:
89 static MenuOption Horizontal();
91 static MenuOption Vertical();
93 static MenuOption Toggle();
94
95 ConstStringListRef entries; ///> The list of entries.
96 Ref<int> selected = 0; ///> The index of the selected entry.
97
98 // Style:
102 std::function<Element()> elements_prefix;
103 std::function<Element()> elements_infix;
104 std::function<Element()> elements_postfix;
105
106 // Observers:
107 std::function<void()> on_change; ///> Called when the selected entry changes.
108 std::function<void()> on_enter; ///> Called when the user presses enter.
110};
111
112/// @brief Option for the AnimatedButton component.
113/// @ingroup component
115 // Standard constructors:
116 static ButtonOption Ascii();
117 static ButtonOption Simple();
118 static ButtonOption Border();
119 static ButtonOption Animated();
120 static ButtonOption Animated(Color color);
121 static ButtonOption Animated(Color background, Color foreground);
122 static ButtonOption Animated(Color background,
123 Color foreground,
124 Color background_active,
125 Color foreground_active);
126
128 std::function<void()> on_click = [] {};
129
130 // Style:
131 std::function<Element(const EntryState&)> transform;
133};
134
135/// @brief Option for the Checkbox component.
136/// @ingroup component
138 // Standard constructors:
139 static CheckboxOption Simple();
140
141 ConstStringRef label = "Checkbox";
142
144
145 // Style:
146 std::function<Element(const EntryState&)> transform;
147
148 // Observer:
149 /// Called when the user change the state.
150 std::function<void()> on_change = [] {};
151};
152
153/// @brief Used to define style for the Input component.
156 bool hovered; ///< Whether the input is hovered by the mouse.
157 bool focused; ///< Whether the input is focused by the user.
158 bool is_placeholder; ///< Whether the input is empty and displaying the
159 ///< placeholder.
160};
161
162/// @brief Option for the Input component.
163/// @ingroup component
165 // A set of predefined styles:
166
167 /// @brief Create the default input style:
168 static InputOption Default();
169 /// @brief A white on black style with high margins:
170 static InputOption Spacious();
171
172 /// The content of the input.
174
175 /// The content of the input when it's empty.
177
178 // Style:
179 std::function<Element(InputState)> transform;
180 Ref<bool> password = false; ///< Obscure the input content using '*'.
181 Ref<bool> multiline = true; ///< Whether the input can be multiline.
182 Ref<bool> insert = true; ///< Insert or overtype character mode.
183
184 /// Called when the content changes.
185 std::function<void()> on_change = [] {};
186 /// Called when the user presses enter.
187 std::function<void()> on_enter = [] {};
188
189 // The char position of the cursor:
191};
192
193/// @brief Option for the Radiobox component.
194/// @ingroup component
196 // Standard constructors:
197 static RadioboxOption Simple();
198
199 // Content:
202
203 // Style:
204 std::function<Element(const EntryState&)> transform;
205
206 // Observers:
207 /// Called when the selected entry changes.
208 std::function<void()> on_change = [] {};
210};
211
221
222// @brief Option for the `Slider` component.
223// @ingroup component
224template <typename T>
235
236/// @brief State passed to the `Window` component's render function.
237/// @ingroup component
239 Element inner; ///< The element wrapped inside this window.
240 const std::string& title; ///< The title of the window.
241 bool active = false; ///< Whether the window is the active one.
242 bool drag = false; ///< Whether the window is being dragged.
243 bool resize = false; ///< Whether the window is being resized.
244 bool hover_left = false; ///< Whether the resizeable left side is hovered.
245 bool hover_right = false; ///< Whether the resizeable right side is hovered.
246 bool hover_top = false; ///< Whether the resizeable top side is hovered.
247 bool hover_down = false; ///< Whether the resizeable down side is hovered.
248};
249
250// @brief Option for the `Window` component.
251// @ingroup component
253 Component inner; ///< The component wrapped by this window.
254 ConstStringRef title = ""; ///< The title displayed by this window.
255
256 Ref<int> left = 0; ///< The left side position of the window.
257 Ref<int> top = 0; ///< The top side position of the window.
258 Ref<int> width = 20; ///< The width of the window.
259 Ref<int> height = 10; ///< The height of the window.
260
261 Ref<bool> resize_left = true; ///< Can the left side be resized?
262 Ref<bool> resize_right = true; ///< Can the right side be resized?
263 Ref<bool> resize_top = true; ///< Can the top side be resized?
264 Ref<bool> resize_down = true; ///< Can the down side be resized?
265
266 /// An optional function to customize how the window looks like:
267 std::function<Element(const WindowRenderState&)> render;
268};
269
270/// @brief Option for the Dropdown component.
271/// @ingroup component
272/// A dropdown menu is a checkbox opening/closing a radiobox.
274 /// Whether the dropdown is open or closed:
276 // The options for the checkbox:
278 // The options for the radiobox:
280 // The transformation function:
281 std::function<Element(bool open, Element checkbox, Element radiobox)>
283};
284
285} // namespace ftxui
286
287#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.