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